Module: Oye
- Defined in:
- lib/oye.rb,
lib/oye/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT_WATCH_INTERVAL =
5- DEFAULT_ENVIRONMENTS =
%w(development production test)- LOG_FORMAT =
"[%Y-%m-%d %H:%M:%S]"- VERSION =
"0.1.10"
Class Method Summary collapse
- .build_app(dir) ⇒ Object
-
.config_oye ⇒ Object
print oye config files.
- .help ⇒ Object
-
.info ⇒ Object
print watched repos.
- .list ⇒ Object
- .log(repo, options = {}) ⇒ Object
-
.monitor ⇒ Object
main method.
- .read_config ⇒ Object
- .restart_app(clone) ⇒ Object
-
.restart_oye ⇒ Object
TODO implement.
- .start(args) ⇒ Object
- .start_app(dir) ⇒ Object
- .stop_app(dir) ⇒ Object
- .stop_oye ⇒ Object
- .update_app(dir) ⇒ Object
- .version ⇒ Object
Class Method Details
.build_app(dir) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/oye.rb', line 175 def build_app(dir) Dir.chdir(dir) do %x(bundle) if rails_app?(dir) %x(RAILS_ENV=#{@environment} rails db:migrate) if @environment == 'production' %x(RAILS_ENV=production rails assets:{clean,precompile}) end elsif jekyll_app?(dir) %x{jekyll build} end end log(dir, status: :info, message: "Built app") rescue => e log(dir, status: :warn, message: "#{__method__.to_s} (#{e.message})") end |
.config_oye ⇒ Object
print oye config files
246 247 248 249 250 251 |
# File 'lib/oye.rb', line 246 def config_oye puts "Config file: #{oye_config}" puts "Log file: #{oye_logfile}" puts "PID file: #{oye_pidfile}" exit end |
.help ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/oye.rb', line 82 def help puts " -c, --config print config, log and pid files\n -e, --environment ENV environment for the app server [development|production|test] (default production)\n -h, --help print this message\n -i, --info [PATTERN] print info of repos matching PATTERN (default .*)\n -l, --list print monitored repos\n -p, --port PORT port for app server\n -r, --restart restart oye\n -s, --stop stop oye\n -t, --time SECS time interval for repo monitoring (default 5)\n -v, --version print oye version\n eos\n exit\nend\n" |
.info ⇒ Object
print watched repos
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/oye.rb', line 254 def info not_found = [] @repos.keys.each do |repo| next unless repo.match?(@pattern) unless File.exists?(repo) not_found << repo next end ftime=File.stat(repo).ctime # use same format than "ls -l" time_format = if ftime.year == Time.now.year "%b %d %H:%M" else "%b %d %Y" end puts "#{ftime.strftime(time_format)} #{repo}" end unless not_found.empty? puts "\nCould not find repos:", not_found end exit end |
.list ⇒ Object
237 238 239 240 241 242 243 |
# File 'lib/oye.rb', line 237 def list @repos.each do |origin, clones| puts origin clones.values.flatten.map {|clone| puts clone.prepend " - "} end exit end |
.log(repo, options = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/oye.rb', line 162 def log(repo, = {}) = [ Time.now.strftime(LOG_FORMAT), repo, "[#{options[:status].to_s.upcase}]", "\"#{options[:message]}\"" ].join(' ') File.open(oye_logfile, 'a') do |f| f.puts end end |
.monitor ⇒ Object
main method
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/oye.rb', line 99 def monitor %w(TERM INT).each do |signal| trap(signal) do stop_oye exit end end # get change-times of origin repos @repos.keys.filter_map do |origin| next unless File.exists?(origin) @repos[origin]['stat'] = File.stat(origin).ctime end pid = fork do begin # initial build and start of apps @repos.values.flatten.each do |app| app['clones'].each do |clone| if !File.exists?(clone) log(clone, status: :warn, message: "Could not find repo") next end build_app(clone) start_app(clone) end end # loop that watches for changes in origin repos loop do repos_dup = @repos repos_dup.keys.each do |origin| unless File.exists?(origin) log(origin, {status: :warn, message: "Could not find repo"}) next end repos_dup[origin]['clones'].each do |clone| unless File.exists?(clone) log(clone, {status: :warn, message: "Could not find repo"}) next end unless @repos[origin]['stat'] == File.stat(origin).ctime @repos[origin]['stat'] = File.stat(origin).ctime update_app(clone) build_app(clone) restart_app(clone) end end end sleep @interval end end end File.open(oye_pidfile, 'w') { |f| f.puts pid } ::Process.detach pid end |
.read_config ⇒ Object
76 77 78 79 80 |
# File 'lib/oye.rb', line 76 def read_config YAML.load(File.open(oye_config)).each do |origin, clones| @repos[origin] = {'clones' => clones} end end |
.restart_app(clone) ⇒ Object
300 301 302 303 |
# File 'lib/oye.rb', line 300 def restart_app(clone) stop_app(clone) start_app(clone) end |
.restart_oye ⇒ Object
TODO implement
297 298 |
# File 'lib/oye.rb', line 297 def restart_oye end |
.start(args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/oye.rb', line 17 def start(args) help if(args.include?('-h') or args.include?('--help')) version if(args.include?('-v') or args.include?('--version')) stop_oye if(args.include?('-s') or args.include?('--stop')) restart_oye if(args.include?('-r') or args.include?('--restart')) config_oye if(args.include?('-c') or args.include?('--config')) FileUtils.mkdir_p(oyedir) @repos = {} read_config list if(args.include?('-l') or args.include?('--list')) if(args.include?('-i') or args.include?('--info')) args.map! {|a| a == '--info' ? '-i' : a} _, @pattern = args.slice(args.index('-i'),2) @pattern = @pattern.nil? ? /.*/ : Regexp.new(@pattern) info end @interval = DEFAULT_WATCH_INTERVAL if(args.include?('-t') or args.include?('--time')) args.map! {|a| a == '--time' ? '-t' : a} _, @interval = args.slice(args.index('-t'),2) @interval = @interval.to_i unless @interval > 0 puts "Interval must a positive integer" exit end end if(args.include?('-p') or args.include?('--port')) args.map! {|a| a == '--port' ? '-p' : a} _, @port = args.slice(args.index('-p'),2) @port = @port.to_i unless @port > 0 puts "Port must a positive integer" exit end end @environment = 'production' if(args.include?('-e') or args.include?('--environment')) args.map! {|a| a == '--environment' ? '-e' : a} _, @environment = args.slice(args.index('-e'),2) unless DEFAULT_ENVIRONMENTS.include?(@environment) puts "Specify a supported environment for -e option" exit end end @default_unicorn_options = "-E #{@environment} -D" @default_unicorn_options << " -l #{@port}" if @port @default_jekyll_options = "-B" monitor end |
.start_app(dir) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/oye.rb', line 209 def start_app(dir) Dir.chdir(dir) do if rails_app?(dir) = << " -c #{unicorn_file(dir)}" system("unicorn_rails #{unicorn_options}", [:out, :err] => File::NULL) elsif jekyll_app?(dir) if @environment == 'development' = << " -s #{dir} -d #{dir}/_site" system("jekyll serve #{jekyll_options}", [:out, :err] => File::NULL) end end end log(dir, status: :info, message: "Started app") rescue => e log(dir, status: :warn, message: "#{__method__.to_s} (#{e.message})") end |
.stop_app(dir) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/oye.rb', line 195 def stop_app(dir) if rails_app?(dir) if File.exists?(app_pid_file(dir)) Process.kill 'TERM', app_pid(dir) end elsif jekyll_app?(dir) if @environment == 'development' %x(pkill -f jekyll) end end rescue => e log(dir, status: :warn, message: "#{__method__.to_s} (#{e.message})") end |
.stop_oye ⇒ Object
289 290 291 292 293 294 |
# File 'lib/oye.rb', line 289 def stop_oye Process.kill 'TERM', oye_pid FileUtils.rm_f(oye_pidfile) log(oye_pidfile, status: :info, message: "Stopped oye") rescue Errno::ENOENT end |
.update_app(dir) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/oye.rb', line 229 def update_app(dir) system("git -C #{dir} pull", [:out, :err] => File::NULL) log(dir, status: :info, message: "Pulled from origin") rescue => e log(dir, status: :warn, message: "#{__method__.to_s} (#{e.message})") end |
.version ⇒ Object
284 285 286 287 |
# File 'lib/oye.rb', line 284 def version puts "oye #{VERSION}" exit end |