| 
									
										
										
										
											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-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-07-30 15:37:43 +03:00
										 |  |  | require 'workers/workers_storage' | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  | require "lib/knife/knife_factory" | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Worker | 
					
						
							|  |  |  |   include Sidekiq::Worker | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |   def convert_config conf | 
					
						
							|  |  |  |     config = {} | 
					
						
							|  |  |  |     conf.each {|k,v| config[k.is_a?(String) ? k.to_sym : k] = v} | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |     DevopsLogger.logger.debug "Config: #{config.inspect}" | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |     config | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def set_status id, status | 
					
						
							|  |  |  |     Sidekiq.redis {|con| con.hset "devops", id, status} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 12:49:39 +03:00
										 |  |  |   # it is called from creating server handler. But maybe we can somehow refactore code | 
					
						
							|  |  |  |   # to get rid of duplication? | 
					
						
							|  |  |  |   # TODO: check it | 
					
						
							|  |  |  |   def self.set_status id, status | 
					
						
							|  |  |  |     Sidekiq.redis {|con| con.hset "devops", id, status} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |   def call conf, e_provider, dir | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |     DevopsLogger.logger = logger | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     FileUtils.mkdir_p(dir) unless File.exists?(dir) | 
					
						
							|  |  |  |     set_status jid, "init" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |     DevopsConfig.config = convert_config(conf) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     file = File.join(dir, jid) | 
					
						
							|  |  |  |     error = nil | 
					
						
							|  |  |  |     provider = nil | 
					
						
							|  |  |  |     begin | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |       KnifeFactory.init | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       Devops::Db.init | 
					
						
							| 
									
										
										
										
											2015-08-05 14:05:14 +03:00
										 |  |  |       ::Provider::ProviderFactory.init(DevopsConfig.config) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |       unless e_provider.nil? | 
					
						
							|  |  |  |         provider = ::Provider::ProviderFactory.get(e_provider) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     rescue Exception => e | 
					
						
							|  |  |  |       error = e | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       DevopsLogger.logger.error e.message | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |     mongo = ::Devops::Db.connector | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |     File.open(file, "w") do |out| | 
					
						
							|  |  |  |       begin | 
					
						
							|  |  |  |         set_status jid, STATUS::RUNNING | 
					
						
							|  |  |  |         raise error unless error.nil? | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         status = yield(provider, out, file) | 
					
						
							| 
									
										
										
										
											2014-12-04 13:07:25 +03:00
										 |  |  |         status = (status == 0 ? STATUS::COMPLETED : STATUS::FAILED) | 
					
						
							|  |  |  |         set_status jid, status | 
					
						
							|  |  |  |         mongo.set_report_status(jid, status) | 
					
						
							|  |  |  |         status | 
					
						
							|  |  |  |       rescue Exception => e | 
					
						
							|  |  |  |         out << "\n" | 
					
						
							|  |  |  |         out << e.message | 
					
						
							|  |  |  |         out << "\n" | 
					
						
							|  |  |  |         out << e.backtrace.join("\n") | 
					
						
							|  |  |  |         set_status jid, STATUS::FAILED | 
					
						
							|  |  |  |         mongo.set_report_status(jid, STATUS::FAILED) unless mongo.nil? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | end |