| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  | require "commands/deploy" | 
					
						
							|  |  |  | require "commands/status" | 
					
						
							|  |  |  | require "workers/deploy_worker" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |   module API2_0 | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |     module Handler | 
					
						
							|  |  |  |       class Deploy | 
					
						
							|  |  |  |         extend DeployCommands | 
					
						
							|  |  |  |         extend StatusCommands | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |         def initialize req, params | 
					
						
							|  |  |  |           @request = req | 
					
						
							|  |  |  |           @params = params | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def deploy names, tags | 
					
						
							|  |  |  |           dir = DevopsConfig.config[:report_dir_v2] | 
					
						
							|  |  |  |           files = [] | 
					
						
							|  |  |  |           uri = URI.parse(@request.url) | 
					
						
							|  |  |  |           servers(names).each do |s| | 
					
						
							|  |  |  |             project = begin | 
					
						
							|  |  |  |               Devops::Db.connector.check_project_auth s.project, s.deploy_env, @request.env['REMOTE_USER'] | 
					
						
							|  |  |  |             rescue InvalidPrivileges, RecordNotFound  => e | 
					
						
							|  |  |  |               next | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             jid = DeployWorker.perform_async(dir, s.to_hash, tags, @request.env['REMOTE_USER'], DevopsConfig.config) | 
					
						
							|  |  |  |             files.push jid | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           files | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |         def deploy_stream out, names, tags | 
					
						
							|  |  |  |           status = [] | 
					
						
							|  |  |  |           servers(names).each do |s| | 
					
						
							|  |  |  |             project = begin | 
					
						
							|  |  |  |               Devops::Db.connector.check_project_auth s.project, s.deploy_env, @request.env['REMOTE_USER'] | 
					
						
							|  |  |  |             rescue InvalidPrivileges, RecordNotFound  => e | 
					
						
							|  |  |  |               out << e.message + "\n" | 
					
						
							|  |  |  |               status.push 2
 | 
					
						
							|  |  |  |               next | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |             res = deploy_server_proc.call(out, s, tags) | 
					
						
							|  |  |  |             status.push(res) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           out << create_status(status) | 
					
						
							|  |  |  |         rescue RecordNotFound  => e | 
					
						
							|  |  |  |           out << e.message | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def servers names | 
					
						
							|  |  |  |           servers = Devops::Db.connector.servers(nil, nil, names, true) | 
					
						
							|  |  |  |           raise RecordNotFound.new("No reserved servers found for names '#{names.join("', '")}'") if servers.empty? | 
					
						
							|  |  |  |           servers.sort_by!{|s| names.index(s.chef_node_name)} | 
					
						
							|  |  |  |           servers | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 |