| 
									
										
										
										
											2015-08-12 09:41:19 +03:00
										 |  |  | require "lib/executors/server_executor" | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  | require "commands/status" | 
					
						
							|  |  |  | require "workers/deploy_worker" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | require "exceptions/deploy_info_error" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "app/api3/parsers/deploy" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | require_relative "request_handler" | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   module API3 | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |     module Handler | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       class Deploy < RequestHandler | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | #        extend DeployCommands | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |         extend StatusCommands | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         set_parser Devops::API3::Parser::DeployParser | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         def deploy | 
					
						
							|  |  |  |           body = parser.deploy | 
					
						
							|  |  |  |           names = body["names"] | 
					
						
							|  |  |  |           tags = body["tags"] || [] | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |           run_list = body["run_list"] | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           files = [] | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           jid = nil | 
					
						
							|  |  |  |           owner = parser.current_user | 
					
						
							|  |  |  |           @deploy_info_buf = {} | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           servers(names).each do |s| | 
					
						
							|  |  |  |             project = begin | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |               Devops::Model::Project.check_user_authorization(s.project, s.environment, owner) | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |             rescue InvalidPrivileges, RecordNotFound  => e | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |               DevopsLogger.logger.warn e.message | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |               next | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             begin | 
					
						
							|  |  |  |               deploy_info = create_deploy_info(s, project, body["build_number"]) | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |               deploy_info["run_list"] = run_list if run_list | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |               deploy_info["named_task"] = body["named_task"] | 
					
						
							| 
									
										
										
										
											2015-09-02 11:38:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |               jid = Worker.start_async(DeployWorker, | 
					
						
							| 
									
										
										
										
											2015-09-02 11:38:58 +03:00
										 |  |  |                 server_attrs: s.to_hash, | 
					
						
							|  |  |  |                 owner: owner, | 
					
						
							|  |  |  |                 tags: tags, | 
					
						
							|  |  |  |                 deploy_info: deploy_info | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             rescue DeployInfoError => e | 
					
						
							|  |  |  |               msg = "Can not get deploy info: " + e.message | 
					
						
							|  |  |  |               DevopsLogger.logger.error msg | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |               jid = "error_#{s.name}_#{Time.new.to_i}" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |               file = File.jon(dir, jid) | 
					
						
							|  |  |  |               File.open(file, "w") do |out| | 
					
						
							|  |  |  |                 out.write msg | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               o = { | 
					
						
							|  |  |  |                 "file" => file, | 
					
						
							|  |  |  |                 "_id" => jid, | 
					
						
							|  |  |  |                 "created_by" => owner, | 
					
						
							|  |  |  |                 "project" => s.project, | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |                 "environment" => s.environment, | 
					
						
							|  |  |  |                 "type" => JobTask::DEPLOY_TYPE, | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |                 "status" => Worker::STATUS::FAILED | 
					
						
							|  |  |  | #                "attributes" => attributes | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |               JobTask.create(o) | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-11-03 12:05:07 +03:00
										 |  |  |             files.push(jid) | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           end | 
					
						
							|  |  |  |           files | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         def deploy_stream out | 
					
						
							|  |  |  |           body = parser.deploy | 
					
						
							|  |  |  |           names = body["names"] | 
					
						
							|  |  |  |           tags = body["tags"] || [] | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |           run_list = body["run_list"] | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           status = [] | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           owner = parser.current_user | 
					
						
							|  |  |  |           @deploy_info_buf = {} | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           servers(names).each do |s| | 
					
						
							|  |  |  |             project = begin | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |               Devops::Model::Project.check_user_authorization(s.project, s.environment, owner) | 
					
						
							|  |  |  |             rescue InvalidPrivileges, Devops::Exception::RecordNotFound  => e | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |               out << e.message + "\n" | 
					
						
							|  |  |  |               status.push 2
 | 
					
						
							|  |  |  |               next | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             begin | 
					
						
							|  |  |  |               deploy_info = create_deploy_info(s, project, body["build_number"]) | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |               deploy_info["run_list"] = run_list if run_list | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |               res = Devops::Executor::ServerExecutor.new(s, out, current_user: owner).deploy_server_with_tags(tags, deploy_info) | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |               status.push(res) | 
					
						
							|  |  |  |             rescue DeployInfoError => e | 
					
						
							|  |  |  |               msg = "Can not get deploy info: " + e.message | 
					
						
							|  |  |  |               DevopsLogger.logger.error msg | 
					
						
							|  |  |  |               out << msg + "\n" | 
					
						
							|  |  |  |               status.push 2
 | 
					
						
							|  |  |  |               next | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           status | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         rescue Devops::Exception::RecordNotFound  => e | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           out << e.message | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           [-10] | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def servers names | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           servers = Devops::Model::Server.find(name: {'$in' => names}, reserved: true) | 
					
						
							|  |  |  |           raise Devops::Exception::RecordNotFound.new("No reserved servers found for names '#{names.join("', '")}'") if servers.empty? | 
					
						
							|  |  |  |           servers.sort_by!{|s| names.index(s.name)} | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           servers | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def create_deploy_info server, project, build_number | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           deploy_env_model = project.environment(server.environment) | 
					
						
							|  |  |  |           buf_key = "#{server.project}_#{server.environment}" | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           deploy_info = if @deploy_info_buf.key?(buf_key) | 
					
						
							|  |  |  |             @deploy_info_buf[buf_key] | 
					
						
							|  |  |  |           else | 
					
						
							| 
									
										
										
										
											2015-09-02 11:38:58 +03:00
										 |  |  |             @deploy_info_buf[buf_key] = project.deploy_info(deploy_env_model, build_number) | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-03-01 17:41:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 |