| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | root = File.join(File.dirname(__FILE__), "..") | 
					
						
							|  |  |  | $LOAD_PATH.push root unless $LOAD_PATH.include? root | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "sidekiq" | 
					
						
							|  |  |  | require "sidekiq/api" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  | require "fileutils" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 19:37:58 +03:00
										 |  |  | require "core/devops-service" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | require "core/devops-config" | 
					
						
							|  |  |  | require "core/devops-logger" | 
					
						
							|  |  |  | require "core/devops-db" | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  | require "providers/provider_factory" | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  | require "lib/knife/knife_factory" | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 12:50:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | # All options keys MUST be a symbol!!! | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | class Worker | 
					
						
							|  |  |  |   include Sidekiq::Worker | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 12:05:13 +03:00
										 |  |  |   attr_accessor :out | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |   module STATUS | 
					
						
							|  |  |  |     INIT = "init" | 
					
						
							|  |  |  |     RUNNING = "running" | 
					
						
							|  |  |  |     COMPLETED = "completed" | 
					
						
							|  |  |  |     FAILED = "failed" | 
					
						
							| 
									
										
										
										
											2015-06-02 12:49:39 +03:00
										 |  |  |     IN_QUEUE = "queued" | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def self.start_async(worker_class, job_options) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     jid = worker_class.perform_async(job_options.dup) | 
					
						
							|  |  |  |     Worker.set_status jid, Worker::STATUS::IN_QUEUE | 
					
						
							| 
									
										
										
										
											2015-09-01 16:54:21 +03:00
										 |  |  |     DevopsLogger.logger.info "Job '#{jid}' has been queued" | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |     jid | 
					
						
							|  |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     uri = URI.parse(request.url) | 
					
						
							|  |  |  |     uri.path = "#{DevopsConfig[:url_prefix]}/v2.0/report/#{jid}" | 
					
						
							|  |  |  |     uri.to_s | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 12:05:13 +03:00
										 |  |  |   def self.start_sync(worker_class, request, job_options, out) | 
					
						
							| 
									
										
										
										
											2015-10-26 13:27:14 +03:00
										 |  |  |     stringified_options = job_options | 
					
						
							| 
									
										
										
										
											2015-10-26 12:50:31 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-09-02 12:50:24 +03:00
										 |  |  |     job_options.each do |key, value| | 
					
						
							|  |  |  |       stringified_options[key.to_s] = value | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-10-26 12:50:31 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2015-09-11 12:05:13 +03:00
										 |  |  |     w = worker_class.new | 
					
						
							|  |  |  |     w.out = out | 
					
						
							|  |  |  |     w.perform(stringified_options) | 
					
						
							| 
									
										
										
										
											2015-09-02 12:50:24 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 12:49:39 +03:00
										 |  |  |   def self.set_status id, status | 
					
						
							|  |  |  |     Sidekiq.redis {|con| con.hset "devops", id, status} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def call &block | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     begin | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |       initialize_devops() | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |       if jid | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         call_async(&block) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         call_sync(&block) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     rescue StandardError => e | 
					
						
							| 
									
										
										
										
											2015-11-02 13:09:53 +03:00
										 |  |  |       DevopsLogger.logger.error "#{e.message}:\n#{e.backtrace.join("\n")}" | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def initialize_devops() | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     DevopsLogger.logger = logger | 
					
						
							|  |  |  |     DevopsConfig.read | 
					
						
							|  |  |  |     DevopsService.init | 
					
						
							|  |  |  |     ::Provider::ProviderFactory.init(DevopsConfig.config) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # outputs to file | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def call_async() | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     dir = DevopsConfig[:report_dir_v2] | 
					
						
							|  |  |  |     # directory is created on server start in config.ru | 
					
						
							|  |  |  |     file = File.join(dir, jid) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |     update_job_status(STATUS::INIT, nil) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     File.open(file, "w") do |out| | 
					
						
							|  |  |  |       begin | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         update_job_status(STATUS::RUNNING, nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 12:05:13 +03:00
										 |  |  |         self.out = out | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         job_result = yield(out, file) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |         canonical_status = (job_result == 0 ? STATUS::COMPLETED : STATUS::FAILED) | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         update_job_status(canonical_status, job_result) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |       rescue StandardError, RecordNotFound => e | 
					
						
							|  |  |  |         out << "\n #{e.class}\n #{e.message}\n" | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |         out << e.backtrace.join("\n") | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |         update_job_status(STATUS::FAILED, -100) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |   # outputs to STDOUT | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def call_sync() | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     out = STDOUT | 
					
						
							|  |  |  |     begin | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |       yield(out, '') | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     rescue StandardError, RecordNotFound => e | 
					
						
							|  |  |  |       out << "\n" | 
					
						
							|  |  |  |       out << e.message | 
					
						
							|  |  |  |       out << "\n" | 
					
						
							|  |  |  |       out << e.backtrace.join("\n") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def mongo | 
					
						
							|  |  |  |     ::Devops::Db.connector | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |   def update_job_status(status, job_result_code) | 
					
						
							| 
									
										
										
										
											2015-11-17 16:21:28 +03:00
										 |  |  |     Worker.set_status(jid, status) | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |     mongo.set_report_status(jid, status, job_result_code) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     status | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 13:27:14 +03:00
										 |  |  |   def convert_config conf | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |     config = {} | 
					
						
							|  |  |  |     conf.each {|k,v| config[k.is_a?(String) ? k.to_sym : k] = v} | 
					
						
							|  |  |  |     config | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | end |