| 
									
										
										
										
											2015-08-12 09:41:19 +03:00
										 |  |  | require "lib/knife/knife_factory" | 
					
						
							| 
									
										
										
										
											2016-01-20 01:43:34 +03:00
										 |  |  | require "lib/executors/expiration_scheduler" | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  | require "lib/puts_and_flush" | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  | require "hooks" | 
					
						
							| 
									
										
										
										
											2015-12-21 16:07:52 +03:00
										 |  |  | require 'net/ssh' | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Devops | 
					
						
							|  |  |  |   module Executor | 
					
						
							|  |  |  |     class ServerExecutor | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |       include Hooks | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |       include PutsAndFlush | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |       ERROR_CODES = { | 
					
						
							| 
									
										
										
										
											2015-09-22 15:07:56 +03:00
										 |  |  |         server_bootstrap_fail: 2, | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         server_cannot_update_tags: 3, | 
					
						
							|  |  |  |         server_bootstrap_private_ip_unset: 4, | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         server_not_in_chef_nodes: 5, | 
					
						
							|  |  |  |         server_bootstrap_unknown_error: 7, | 
					
						
							|  |  |  |         deploy_unknown_error: 6, | 
					
						
							| 
									
										
										
										
											2015-12-24 18:51:52 +03:00
										 |  |  |         deploy_failed: 8, | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         creating_server_unknown_error: 9, | 
					
						
							|  |  |  |         creating_server_in_cloud_failed: 10
 | 
					
						
							| 
									
										
										
										
											2015-09-22 15:07:56 +03:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-29 12:01:35 +03:00
										 |  |  |       # waiting for 5*60 seconds (5 min) | 
					
						
							|  |  |  |       MAX_SSH_RETRIES_AMOUNT = 60
 | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |       #params: | 
					
						
							|  |  |  |       #  out - container for output data | 
					
						
							|  |  |  |       #  deploy_info - hash with deploy data | 
					
						
							|  |  |  |       define_hook :before_deploy | 
					
						
							|  |  |  |       define_hook :after_deploy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       define_hook :before_create | 
					
						
							|  |  |  |       define_hook :after_create | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       #params: | 
					
						
							|  |  |  |       #  out - container for output data | 
					
						
							|  |  |  |       define_hook :before_bootstrap | 
					
						
							|  |  |  |       define_hook :after_bootstrap | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 17:00:21 +03:00
										 |  |  |       before_deploy :add_run_list_to_deploy_info | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 18:51:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 17:44:10 +03:00
										 |  |  |       attr_accessor :server, :deploy_env, :report, :project | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |       attr_reader :out | 
					
						
							| 
									
										
										
										
											2015-12-24 18:51:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |       def initialize server, out, options={} | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |         if server | 
					
						
							|  |  |  |           @project = Devops::Db.connector.project(server.project) | 
					
						
							|  |  |  |           @deploy_env = @project.deploy_env(server.deploy_env) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         @server = server | 
					
						
							|  |  |  |         @out = out | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |         @out.class.send(:define_method, :flush) { } unless @out.respond_to?(:flush) | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         @current_user = options[:current_user] | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |       def self.error_code(reason) | 
					
						
							|  |  |  |         ERROR_CODES.fetch(reason) | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |       def self.reason_from_error_code(integer_code) | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |         ERROR_CODES.key(integer_code) || :unknown_error | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |       def self.bootstrap_errors_reasons | 
					
						
							|  |  |  |         [:server_bootstrap_fail, :server_not_in_chef_nodes, :server_bootstrap_unknown_error] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def error_code(reason) | 
					
						
							|  |  |  |         self.class.error_code(reason) | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-23 14:55:53 +03:00
										 |  |  |       def create_server_object options | 
					
						
							|  |  |  |         Devops::Model::Server.new({ | 
					
						
							|  |  |  |           "project" => @project.id, | 
					
						
							|  |  |  |           "deploy_env" => @deploy_env.identifier, | 
					
						
							|  |  |  |           "created_by" => options["created_by"], | 
					
						
							|  |  |  |           "provider" => @deploy_env.provider, | 
					
						
							| 
									
										
										
										
											2016-01-27 15:44:59 +03:00
										 |  |  |           "provider_account" => @deploy_env.provider_account, | 
					
						
							|  |  |  |           "private_ip" => options["private_ip"] | 
					
						
							| 
									
										
										
										
											2015-11-23 14:55:53 +03:00
										 |  |  |         }) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       def create_server options | 
					
						
							| 
									
										
										
										
											2015-11-23 14:55:53 +03:00
										 |  |  |         @server = create_server_object(options) | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         @server.set_last_operation(Devops::Model::Server::OperationType::CREATION, options["created_by"]) | 
					
						
							| 
									
										
										
										
											2015-10-27 12:01:21 +03:00
										 |  |  |         provider = @deploy_env.provider_instance | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         mongo = ::Devops::Db.connector | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |           @out.puts "Using '#{@deploy_env.provider}' account '#{@deploy_env.provider_account}'\n" if @deploy_env.provider_account | 
					
						
							| 
									
										
										
										
											2015-10-27 12:01:21 +03:00
										 |  |  |           @out.puts "Create server..." | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |           @server.run_list = options["run_list"] || [] | 
					
						
							|  |  |  |           @server.chef_node_name = options["name"] | 
					
						
							|  |  |  |           @server.key = options["key"] || provider.ssh_key | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |           i = mongo.image(@deploy_env.image) | 
					
						
							|  |  |  |           @server.remote_user = i.remote_user | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           res = {} | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |           @out << "\nBefore create hooks...\n" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           res[:before] = self.run_hook :before_create | 
					
						
							| 
									
										
										
										
											2015-08-12 16:39:41 +03:00
										 |  |  |           @out << "Done\n" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           unless provider.create_server(@server, @deploy_env.image, @deploy_env.flavor, @deploy_env.subnets, @deploy_env.groups, @out) | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |             return error_code(:creating_server_in_cloud_failed) | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           mongo.server_insert @server | 
					
						
							| 
									
										
										
										
											2015-08-12 16:39:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |           @out << "\nAfter create hooks...\n" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           res[:after] = self.run_hook :after_create | 
					
						
							| 
									
										
										
										
											2015-08-12 16:39:41 +03:00
										 |  |  |           @out << "Done\n" | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           DevopsLogger.logger.info "Server with parameters: #{@server.to_hash.inspect} is running" | 
					
						
							| 
									
										
										
										
											2015-09-15 19:54:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 01:43:34 +03:00
										 |  |  |           schedule_expiration() | 
					
						
							| 
									
										
										
										
											2015-09-15 19:54:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           unless options["without_bootstrap"] | 
					
						
							|  |  |  |             bootstrap_options = { | 
					
						
							|  |  |  |               bootstrap_template: i.bootstrap_template | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |             return two_phase_bootstrap(bootstrap_options) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           else | 
					
						
							|  |  |  |             return 0
 | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         rescue => e | 
					
						
							| 
									
										
										
										
											2015-11-13 17:22:55 +03:00
										 |  |  |           @out.puts e.message | 
					
						
							|  |  |  |           @out.puts e.backtrace.join("\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           DevopsLogger.logger.error e.message | 
					
						
							|  |  |  |           roll_back | 
					
						
							|  |  |  |           mongo.server_delete @server.id | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           error_code(:creating_server_unknown_error) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |       # options: | 
					
						
							|  |  |  |       #   :run_list (optional) | 
					
						
							|  |  |  |       #   :bootstrap_template (optional) | 
					
						
							|  |  |  |       #   :chef_environment (optional) | 
					
						
							|  |  |  |       #   :config (optional) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       def bootstrap options | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |         @out << "\n\nBootstrap...\n" | 
					
						
							|  |  |  |         @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         k = Devops::Db.connector.key(@server.key) | 
					
						
							|  |  |  |         cert_path = k.path | 
					
						
							| 
									
										
										
										
											2015-08-12 13:55:22 +03:00
										 |  |  |         options[:cert_path] = k.path | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:17 +03:00
										 |  |  |         @out << "\nBefore bootstrap hooks...\n" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         res = self.run_hook(:before_bootstrap, @out) | 
					
						
							|  |  |  |         @out << "Done\n" | 
					
						
							|  |  |  |         if @server.private_ip.nil? | 
					
						
							|  |  |  |           @out << "Error: Private IP is null" | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           return error_code(:server_bootstrap_private_ip_unset) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |         ja = { | 
					
						
							|  |  |  |           :provider => @server.provider, | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |           :provider_account => @server.provider_account, | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           :devops_host => `hostname`.strip | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ip = @server.private_ip | 
					
						
							|  |  |  |         unless @server.public_ip.nil? | 
					
						
							|  |  |  |           ip = @server.public_ip | 
					
						
							|  |  |  |           @out << "\nPublic IP is present\n" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         address = "#{@server.remote_user}@#{ip}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         cmd = check_ssh_command(cert_path, address) | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         @out << "\nWaiting for SSH..." | 
					
						
							| 
									
										
										
										
											2015-09-21 17:41:40 +03:00
										 |  |  |         @out << "\nTest command: '#{cmd}'\n" | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  |         @out.flush | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         retries_amount = 0
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         begin | 
					
						
							|  |  |  |           sleep(5) | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           res = execute_system_command(cmd) | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  |           retries_amount += 1
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           if retries_amount >= MAX_SSH_RETRIES_AMOUNT | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |             @out.puts "Can not connect to #{address}" | 
					
						
							|  |  |  |             @out.puts res | 
					
						
							|  |  |  |             @out.flush | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  |             DevopsLogger.logger.error "Can not connect with command '#{cmd}':\n#{res}" | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |             return error_code(:server_bootstrap_fail) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-01-20 10:23:34 +03:00
										 |  |  |           raise ArgumentError.new("Can not connect with command '#{cmd}' ") unless last_command_successful? | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         rescue ArgumentError => e | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |           @out.puts "SSH command failed, retry (#{retries_amount}/#{MAX_SSH_RETRIES_AMOUNT})" | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           retry | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 16:07:29 +03:00
										 |  |  |         provider = @server.provider_instance | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |         @server.chef_node_name = provider.create_default_chef_node_name(@server) if @server.chef_node_name.nil? | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         r = knife_instance.knife_bootstrap(@out, ip, self.bootstrap_options(ja, options)) | 
					
						
							| 
									
										
										
										
											2015-09-21 20:57:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         if r == 0
 | 
					
						
							|  |  |  |           @out << "Chef node name: #{@server.chef_node_name}\n" | 
					
						
							|  |  |  |           ::Devops::Db.connector.server_set_chef_node_name @server | 
					
						
							|  |  |  |           @out << "Chef node name has been updated\n" | 
					
						
							|  |  |  |           @out << "After bootstrap hooks...\n" | 
					
						
							|  |  |  |           res = self.run_hook(:after_bootstrap, @out) | 
					
						
							|  |  |  |           @out << "Done\n" | 
					
						
							| 
									
										
										
										
											2015-11-06 13:53:52 +03:00
										 |  |  |           msg = "Server with id '#{@server.id}' is bootstraped" | 
					
						
							|  |  |  |           @out.puts msg | 
					
						
							| 
									
										
										
										
											2015-09-21 20:57:07 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-11-06 13:53:52 +03:00
										 |  |  |           DevopsLogger.logger.info msg | 
					
						
							| 
									
										
										
										
											2015-09-21 20:57:07 +03:00
										 |  |  |           r | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |           @out << "Can not bootstrap node '#{@server.id}', error code: #{r}" | 
					
						
							| 
									
										
										
										
											2015-09-21 20:57:07 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           error_code(:server_bootstrap_fail) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |       # options: | 
					
						
							|  |  |  |       #   :cert_path (required) | 
					
						
							|  |  |  |       #   :run_list (optional) | 
					
						
							|  |  |  |       #   :bootstrap_template (optional) | 
					
						
							|  |  |  |       #   :chef_environment (optional) | 
					
						
							|  |  |  |       #   :config (optional) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       def bootstrap_options attributes, options | 
					
						
							|  |  |  |         bootstrap_options = [ | 
					
						
							|  |  |  |           "-x #{@server.remote_user}", | 
					
						
							|  |  |  |           "-i #{options[:cert_path]}", | 
					
						
							| 
									
										
										
										
											2015-08-20 21:55:44 +03:00
										 |  |  |           "--json-attributes '#{attributes.to_json}'" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2015-08-20 21:55:44 +03:00
										 |  |  |         bootstrap_options.push "-N #{@server.chef_node_name}" if @server.chef_node_name | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         bootstrap_options.push "--sudo" unless @server.remote_user == "root" | 
					
						
							|  |  |  |         bootstrap_options.push "-t #{options[:bootstrap_template]}" if options[:bootstrap_template] | 
					
						
							| 
									
										
										
										
											2015-11-20 12:04:59 +03:00
										 |  |  |         bootstrap_options.push "-E #{options[:chef_environment]}" if options[:chef_environment] | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         rl = options[:run_list] | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |         bootstrap_options.push "-r #{rl.join(",")}" unless rl.nil? or rl.empty? | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         bootstrap_options.push "-c #{options[:config]}" if options[:config] | 
					
						
							|  |  |  |         bootstrap_options | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 14:54:56 +03:00
										 |  |  |       def prepare_two_phase_bootstrap options | 
					
						
							| 
									
										
										
										
											2015-10-26 12:39:54 +03:00
										 |  |  |         @out << "Prepare bootstrap...\n" | 
					
						
							|  |  |  |         @out << "Done\n" | 
					
						
							| 
									
										
										
										
											2015-10-23 14:54:56 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 16:10:05 +03:00
										 |  |  |       # essentially, it just bootstrap and then deploy | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |       def two_phase_bootstrap options | 
					
						
							| 
									
										
										
										
											2015-10-23 14:54:56 +03:00
										 |  |  |         prepare_two_phase_bootstrap(options) | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         # bootstrap phase | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           provider = @server.provider_instance | 
					
						
							|  |  |  |           mongo = ::Devops::Db.connector | 
					
						
							|  |  |  |           options[:run_list] = provider.run_list | 
					
						
							|  |  |  |           bootstrap_status = bootstrap(options) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if bootstrap_status == 0
 | 
					
						
							| 
									
										
										
										
											2015-12-29 16:10:05 +03:00
										 |  |  |             if check_server_on_chef_server | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |               @out << "Server #{@server.chef_node_name} is created" | 
					
						
							|  |  |  |             else | 
					
						
							| 
									
										
										
										
											2015-11-06 14:00:24 +03:00
										 |  |  |               @out.puts "Can not find client or node on chef-server" | 
					
						
							|  |  |  |               roll_back | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |               @out.flush | 
					
						
							|  |  |  |               mongo.server_delete @server.id | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |               return error_code(:server_not_in_chef_nodes) | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2016-03-04 17:50:01 +03:00
										 |  |  |             roll_back | 
					
						
							|  |  |  |             mongo.server_delete @server.id | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |             msg = "Failed while bootstraping server with id '#{@server.id}'\n" | 
					
						
							|  |  |  |             msg << "Bootstraping operation result was #{bootstrap_status}" | 
					
						
							|  |  |  |             DevopsLogger.logger.error msg | 
					
						
							|  |  |  |             @out.puts msg | 
					
						
							|  |  |  |             @out.flush | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |             return error_code(:server_bootstrap_fail) | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         rescue => e | 
					
						
							|  |  |  |           @out << "\nError: #{e.message}\n" | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           return error_code(:server_bootstrap_unknown_error) | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         # deploy phase. Assume that all servers are bootstraped successfully here. | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |           run_list = compute_run_list | 
					
						
							| 
									
										
										
										
											2015-12-29 16:10:05 +03:00
										 |  |  |           @out << "\n\nComputed run list: #{run_list.join(", ")}" | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           knife_instance.set_run_list(@server.chef_node_name, run_list) | 
					
						
							| 
									
										
										
										
											2015-11-06 12:52:31 +03:00
										 |  |  |           deploy_info = options[:deploy_info] || @project.deploy_info(@deploy_env) | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |           deploy_status = deploy_server(deploy_info) | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |           if deploy_status == 0
 | 
					
						
							|  |  |  |             0
 | 
					
						
							|  |  |  |           else | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |             msg = "Failed on chef-client with run list, server with id '#{@server.id}'" | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |             msg << "\nDeploing server operation status was #{deploy_status}" | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |             DevopsLogger.logger.error msg | 
					
						
							|  |  |  |             @out << "\n" + msg + "\n" | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |             error_code(:deploy_failed) | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-09-25 17:31:10 +03:00
										 |  |  |         rescue => e | 
					
						
							|  |  |  |           @out << "\nError: #{e.message}\n" | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |           DevopsLogger.logger.error(e.message + "\n" + e.backtrace.join("\n")) | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           error_code(:deploy_unknown_error) | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 16:10:05 +03:00
										 |  |  |       def check_server_on_chef_server | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         knife_instance.chef_node_list.include?(@server.chef_node_name) and knife_instance.chef_client_list.include?(@server.chef_node_name) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 20:46:15 +03:00
										 |  |  |       # returns a hash with :chef_node, :chef_client and :server keys | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       def unbootstrap | 
					
						
							|  |  |  |         k = Devops::Db.connector.key(@server.key) | 
					
						
							|  |  |  |         cert_path = k.path | 
					
						
							|  |  |  |         i = 0
 | 
					
						
							| 
									
										
										
										
											2015-10-22 15:39:27 +03:00
										 |  |  |         res = delete_from_chef_server(@server.chef_node_name) | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2015-10-22 13:56:16 +03:00
										 |  |  |           new_name = "/etc/chef.backup_#{Time.now.strftime("%d-%m-%Y_%H.%M.%S")}" | 
					
						
							| 
									
										
										
										
											2015-12-21 16:07:52 +03:00
										 |  |  |           cmd = (@server.remote_user == 'root' ? "" : "sudo ") | 
					
						
							|  |  |  |           cmd = cmd + "/bin/sh -c 'if [[ -d /etc/chef ]]; then mv /etc/chef #{new_name} && echo ok; else echo not found; fi'" | 
					
						
							|  |  |  |           DevopsLogger.logger.info("SSH: trying to run command '#{cmd}'") | 
					
						
							|  |  |  |           Net::SSH.start(@server.private_ip, @server.remote_user, :keys => [cert_path]) do |session| | 
					
						
							|  |  |  |             session.open_channel do |channel| | 
					
						
							|  |  |  |               channel.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }) do |c, success| | 
					
						
							|  |  |  |                 raise "could not request pty" unless success | 
					
						
							|  |  |  |                 channel.exec cmd | 
					
						
							|  |  |  |                 channel.on_data do |c_, data| | 
					
						
							|  |  |  |                   if data == 'not found' | 
					
						
							|  |  |  |                     res[:server] = "Directory '/etc/chef' does not exists" | 
					
						
							|  |  |  |                   elsif data == 'ok' | 
					
						
							|  |  |  |                     res[:server] = "'/etc/chef' renamed to '#{new_name}'" | 
					
						
							|  |  |  |                   else | 
					
						
							|  |  |  |                     DevopsLogger.logger.error "Unexpected error: " + data | 
					
						
							|  |  |  |                     raise(data) | 
					
						
							|  |  |  |                   end | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-10-22 15:39:27 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         rescue => e | 
					
						
							|  |  |  |           DevopsLogger.logger.error "Unbootstrap error: " + e.message | 
					
						
							|  |  |  |           i += 1
 | 
					
						
							|  |  |  |           sleep(1) | 
					
						
							|  |  |  |           retry unless i == 5
 | 
					
						
							| 
									
										
										
										
											2015-10-22 13:56:16 +03:00
										 |  |  |           return {error: e.message} | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-10-22 13:56:16 +03:00
										 |  |  |         res | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def deploy_server_with_tags tags, deploy_info | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         return deploy_server(deploy_info) if tags.empty? | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         old_tags_str = knife_instance.tags_list(@server.chef_node_name).join(" ") | 
					
						
							|  |  |  |         new_tags_str = tags.join(" ") | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         @out.puts "Temporarily changing tags (#{old_tags_str}) to (#{new_tags_str})" | 
					
						
							|  |  |  |         unless knife_instance.swap_tags(@server.chef_node_name, old_tags_str, new_tags_str) | 
					
						
							|  |  |  |           m = "Error: Cannot add tags '#{new_tags_str}' to server '#{@server.chef_node_name}'" | 
					
						
							|  |  |  |           DevopsLogger.logger.error(m) | 
					
						
							|  |  |  |           @out.puts m | 
					
						
							| 
									
										
										
										
											2016-01-19 14:29:34 +03:00
										 |  |  |           return error_code(:server_cannot_update_tags) | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |         DevopsLogger.logger.info("Set tags for '#{@server.chef_node_name}': #{new_tags_str}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           deploy_result = deploy_server deploy_info | 
					
						
							|  |  |  |         ensure | 
					
						
							|  |  |  |           @out.puts "Restoring tags" | 
					
						
							|  |  |  |           knife_instance.swap_tags(@server.chef_node_name, new_tags_str, old_tags_str) | 
					
						
							|  |  |  |           DevopsLogger.logger.info("Restoring tags for #{@server.chef_node_name}: from #{new_tags_str} back to (#{old_tags_str})") | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |         deploy_result | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def deploy_server deploy_info | 
					
						
							| 
									
										
										
										
											2015-08-12 14:19:52 +03:00
										 |  |  |         @out << "\nBefore deploy hooks...\n" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         res = self.run_hook(:before_deploy, @out, deploy_info) | 
					
						
							|  |  |  |         @out << "Done\n" | 
					
						
							|  |  |  |         @out << "\nRun chef-client on '#{@server.chef_node_name}'\n" | 
					
						
							|  |  |  |         cmd = "chef-client --no-color" | 
					
						
							|  |  |  |         if deploy_info["use_json_file"] | 
					
						
							| 
									
										
										
										
											2015-08-20 17:21:41 +03:00
										 |  |  |           deploy_info.delete("use_json_file") | 
					
						
							| 
									
										
										
										
											2015-11-06 16:11:28 +03:00
										 |  |  |           json = nil | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           dir = DevopsConfig.config[:project_info_dir] | 
					
						
							| 
									
										
										
										
											2015-11-11 14:35:27 +03:00
										 |  |  |           file = deploy_info.delete("json_file") || "#{@server.project}_#{@server.deploy_env}_#{Time.new.to_i}" | 
					
						
							| 
									
										
										
										
											2015-11-06 16:11:28 +03:00
										 |  |  |           path = File.join(dir, file) | 
					
						
							|  |  |  |           if File.exists?(path) | 
					
						
							|  |  |  |             json = File.read(path) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             json = JSON.pretty_generate(deploy_info) | 
					
						
							|  |  |  |             File.open(File.join(dir, file), "w") do |f| | 
					
						
							|  |  |  |               f.write json | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-01-19 13:36:29 +03:00
										 |  |  |           @out.puts "Deploy Input Parameters:" | 
					
						
							| 
									
										
										
										
											2015-11-06 16:11:28 +03:00
										 |  |  |           @out.puts json | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |           @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           cmd << " -j http://#{DevopsConfig.config[:address]}:#{DevopsConfig.config[:port]}/#{DevopsConfig.config[:url_prefix]}/v2.0/deploy/data/#{file}" | 
					
						
							| 
									
										
										
										
											2015-09-30 17:05:52 +03:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2016-03-04 13:00:17 +03:00
										 |  |  |           if deploy_info['named_task'].present? | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |             named_task = @project.named_tasks.detect {|task| task['name'] == deploy_info['named_task']} | 
					
						
							|  |  |  |             raise "Named task #{deploy_info['named_task']} doesn't exist." unless named_task | 
					
						
							|  |  |  |             puts_and_flush "Using named task #{deploy_info['named_task']}." | 
					
						
							|  |  |  |             cmd << " -o #{named_task['run_list'].join(',')}" | 
					
						
							| 
									
										
										
										
											2016-02-12 21:13:09 +03:00
										 |  |  |           else | 
					
						
							|  |  |  |             cmd << " -r #{deploy_info["run_list"].join(",")}" unless @server.stack.nil? | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |         ip = if @server.public_ip.nil? | 
					
						
							|  |  |  |           @server.private_ip | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           @out << "Public IP detected\n" | 
					
						
							|  |  |  |           @server.public_ip | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-08-12 12:24:48 +03:00
										 |  |  |         @out.flush | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         k = Devops::Db.connector.key(@server.key) | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         lline = knife_instance.ssh_stream(@out, cmd, ip, @server.remote_user, k.path) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         r = /Chef\sClient\sfinished/i | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if lline && lline[r] | 
					
						
							| 
									
										
										
										
											2015-08-12 14:19:52 +03:00
										 |  |  |           @out << "\nAfter deploy hooks...\n" | 
					
						
							|  |  |  |           res = self.run_hook(:after_deploy, @out, deploy_info) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           @out << "Done\n" | 
					
						
							| 
									
										
										
										
											2015-11-09 18:57:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |           @server.set_last_operation(Devops::Model::Server::OperationType::DEPLOY, @current_user) | 
					
						
							| 
									
										
										
										
											2015-11-09 18:57:22 +03:00
										 |  |  |           Devops::Db.connector.server_update(@server) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           0
 | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         else | 
					
						
							|  |  |  |           @out << "An error occured during knife command executing" | 
					
						
							|  |  |  |           1
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def delete_from_chef_server node_name | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |           :chef_node => knife_instance.chef_node_delete(node_name), | 
					
						
							|  |  |  |           :chef_client => knife_instance.chef_client_delete(node_name) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def delete_server | 
					
						
							|  |  |  |         mongo = ::Devops::Db.connector | 
					
						
							|  |  |  |         if @server.static? | 
					
						
							|  |  |  |           if !@server.chef_node_name.nil? | 
					
						
							|  |  |  |             unbootstrap | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           mongo.server_delete @server.id | 
					
						
							| 
									
										
										
										
											2016-02-02 11:43:13 +03:00
										 |  |  |           puts_and_flush "Static server '#{@server.id}' is removed" | 
					
						
							|  |  |  |           return 0
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         puts_and_flush "Deleting from chef server:" | 
					
						
							|  |  |  |         delete_from_chef_server(@server.chef_node_name).each do |key, result| | 
					
						
							|  |  |  |           @out.puts "#{key} - #{result}" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-02-02 11:43:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         puts_and_flush "Deleting from cloud:" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         provider = @server.provider_instance | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2016-02-02 11:43:13 +03:00
										 |  |  |           puts_and_flush provider.delete_server @server | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         rescue Fog::Compute::OpenStack::NotFound, Fog::Compute::AWS::NotFound | 
					
						
							| 
									
										
										
										
											2016-02-02 11:43:13 +03:00
										 |  |  |           puts_and_flush "Server with id '#{@server.id}' not found among '#{provider.name}' servers" | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |         mongo.server_delete @server.id | 
					
						
							| 
									
										
										
										
											2016-02-02 11:43:13 +03:00
										 |  |  |         puts_and_flush "Server '#{@server.id}' with name '#{@server.chef_node_name}' for project '#{@server.project}-#{@server.deploy_env}' is removed." | 
					
						
							|  |  |  |         0
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def roll_back | 
					
						
							| 
									
										
										
										
											2015-11-06 14:00:24 +03:00
										 |  |  |         @out.puts "Trying to roll back..." | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |         unless @server.id.nil? | 
					
						
							| 
									
										
										
										
											2015-11-06 14:00:24 +03:00
										 |  |  |           @out.puts "Server '#{@server.chef_node_name}' with id '#{@server.id}' is not created" | 
					
						
							|  |  |  |           @out.puts delete_from_chef_server(@server.chef_node_name).values.join("\n") | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           begin | 
					
						
							| 
									
										
										
										
											2015-11-06 14:00:24 +03:00
										 |  |  |             @out.puts @server.provider_instance.delete_server(@server) | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           rescue => e | 
					
						
							| 
									
										
										
										
											2015-11-06 14:00:24 +03:00
										 |  |  |             @out.puts e.message | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |           end | 
					
						
							|  |  |  |           @out << "\nRolled back\n" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 17:00:21 +03:00
										 |  |  |       def add_run_list_to_deploy_info out, deploy_info | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |         out << "\nGenerate run list hook...\n" | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |         if deploy_info["run_list"] | 
					
						
							| 
									
										
										
										
											2015-09-30 17:05:52 +03:00
										 |  |  |           out << "Deploy info already contains 'run_list': #{deploy_info["run_list"].join(", ")}\n" | 
					
						
							| 
									
										
										
										
											2015-09-11 12:22:58 +03:00
										 |  |  |           return | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |         out << "Project run list: #{@project.run_list.join(", ")}\n" | 
					
						
							|  |  |  |         out << "Deploy environment run list: #{@deploy_env.run_list.join(", ")}\n" | 
					
						
							|  |  |  |         out << "Server run list: #{@server.run_list.join(", ")}\n" | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |         deploy_info["run_list"] = compute_run_list | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |         out << "New deploy run list: #{deploy_info["run_list"].join(", ")}\nRun list has been generated\n\n" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |       def compute_run_list | 
					
						
							| 
									
										
										
										
											2015-10-01 22:41:35 +03:00
										 |  |  |         rlist = [] | 
					
						
							|  |  |  |         [@deploy_env.provider_instance.run_list, @project.run_list, @deploy_env.run_list, @server.run_list].each do |sub_run_list| | 
					
						
							| 
									
										
										
										
											2016-01-19 17:00:21 +03:00
										 |  |  |           rlist += sub_run_list if sub_run_list | 
					
						
							| 
									
										
										
										
											2015-10-01 22:41:35 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |         if @server.stack | 
					
						
							|  |  |  |           stack = Devops::Db.connector.stack(@server.stack) | 
					
						
							| 
									
										
										
										
											2015-10-01 22:41:35 +03:00
										 |  |  |           srl = stack.run_list | 
					
						
							| 
									
										
										
										
											2016-01-19 17:00:21 +03:00
										 |  |  |           rlist += srl if srl | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-01-19 17:00:21 +03:00
										 |  |  |         rlist.uniq | 
					
						
							| 
									
										
										
										
											2015-10-01 17:27:04 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-03 17:03:47 +03:00
										 |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 01:43:34 +03:00
										 |  |  |       def schedule_expiration | 
					
						
							|  |  |  |         if @deploy_env.expires | 
					
						
							| 
									
										
										
										
											2016-03-09 15:44:57 +03:00
										 |  |  |           job_id = ExpirationScheduler.new(@deploy_env.expires, @server).schedule_expiration! | 
					
						
							|  |  |  |           puts_and_flush "Planning expiration in #{@deploy_env.expires}, job_id: #{job_id}" | 
					
						
							| 
									
										
										
										
											2015-09-15 19:54:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |       def check_ssh_command(cert_path, address) | 
					
						
							|  |  |  |         cmd = 'ssh ' | 
					
						
							|  |  |  |         cmd << "-i #{cert_path} " | 
					
						
							|  |  |  |         cmd << '-q ' | 
					
						
							|  |  |  |         cmd << '-o StrictHostKeyChecking=no ' | 
					
						
							|  |  |  |         cmd << '-o ConnectTimeout=2 -o ConnectionAttempts=1 ' | 
					
						
							|  |  |  |         cmd << "#{address} 'exit'" | 
					
						
							|  |  |  |         cmd << " 2>&1" | 
					
						
							|  |  |  |         cmd | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def execute_system_command(cmd) | 
					
						
							|  |  |  |         `#{cmd}` | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 10:23:34 +03:00
										 |  |  |       def last_command_successful? | 
					
						
							| 
									
										
										
										
											2015-12-28 13:07:13 +03:00
										 |  |  |         $?.success? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def knife_instance | 
					
						
							|  |  |  |         @knife_instance ||= KnifeFactory.instance | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |