| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class KnifeCommands | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   attr_accessor :config | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def initialize config | 
					
						
							|  |  |  |     self.config = config | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def chef_node_list | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     knife("node list")[0].split.map{|c| c.strip} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def chef_client_list | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     knife("client list")[0].split.map{|c| c.strip} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def chef_node_delete name | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     o = knife("node delete #{name} -y")[0] | 
					
						
							|  |  |  |     (o.nil? ? o : o.strip) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def chef_client_delete name | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     o = knife("client delete #{name} -y")[0] | 
					
						
							|  |  |  |     (o.nil? ? o : o.strip) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def tags_list name | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     knife("tag list #{name}")[0].split.map{|c| c.strip} | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def tags_create name, tagsStr | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     knife("tag create #{name} #{tagsStr}") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def tags_delete name, tagsStr | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     knife("tag delete #{name} #{tagsStr}") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 13:43:42 +03:00
										 |  |  |   # extracted from server_executor.rb | 
					
						
							|  |  |  |   def swap_tags(name, from, to) | 
					
						
							|  |  |  |     tags_delete(name, from) | 
					
						
							|  |  |  |     result = tags_create(name, to) | 
					
						
							|  |  |  |     !!result[1] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def create_role role_name, project, env | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     file = "/tmp/new_role.json" | 
					
						
							|  |  |  |     File.open(file, "w") do |f| | 
					
						
							|  |  |  |       f.puts <<-EOH
 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-26 13:40:25 +04:00
										 |  |  |   "name" : "#{role_name}", | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   "description": "", | 
					
						
							|  |  |  |   "json_class": "Chef::Role", | 
					
						
							|  |  |  |   "default_attributes": { | 
					
						
							|  |  |  |     "project": "#{project}", | 
					
						
							| 
									
										
										
										
											2015-09-30 17:51:15 +03:00
										 |  |  |     "env": "#{env}", | 
					
						
							|  |  |  |     "deploy_env": "#{env}" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   }, | 
					
						
							|  |  |  |   "override_attributes": {}, | 
					
						
							|  |  |  |   "chef_type": "role", | 
					
						
							|  |  |  |   "run_list": [], | 
					
						
							|  |  |  |   "env_run_lists": {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EOH | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |     out, res = knife("role from file #{file}") | 
					
						
							|  |  |  |     raise "Cannot create role '#{role_name}': #{out}" unless res | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     true | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def roles(chef_env=nil) | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     o, s = knife("role list --format json") | 
					
						
							|  |  |  |     return (s ? JSON.parse(o) : nil) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def role_name project_name, deploy_env | 
					
						
							| 
									
										
										
										
											2015-02-19 14:16:07 +03:00
										 |  |  |     project_name + (DevopsConfig.config[:role_separator] || "_") + deploy_env | 
					
						
							| 
									
										
										
										
											2014-05-26 13:40:25 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def knife cmd | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |     cmd = "bundle exec knife #{cmd} -c #{self.config} 2>&1" | 
					
						
							|  |  |  |     DevopsLogger.logger.info("Going to invoke command: #{cmd}") | 
					
						
							|  |  |  |     o = `#{cmd}` | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |     return o, $?.success? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def ssh_options cmd, host, user, cert | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |     ["-m", "-x", user, "-i", cert, "--no-host-key-verify", host, "'#{(user == "root" ? cmd : "sudo #{cmd}")}'"] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def ssh_stream out, cmd, host, user, cert | 
					
						
							| 
									
										
										
										
											2015-10-01 13:13:12 +03:00
										 |  |  |     knife_cmd = "bundle exec knife ssh -c #{self.config} #{ssh_options(cmd, host, user, cert).join(" ")}" | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |     out << "\nExecuting '#{knife_cmd}' \n\n" | 
					
						
							|  |  |  |     out.flush if out.respond_to?(:flush) | 
					
						
							|  |  |  |     status = 2
 | 
					
						
							| 
									
										
										
										
											2014-11-07 16:16:27 +03:00
										 |  |  |     lline = nil | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  |     IO.popen(knife_cmd + " 2>&1") do |o| | 
					
						
							|  |  |  |       while line = o.gets do | 
					
						
							|  |  |  |         out << line | 
					
						
							|  |  |  |         lline = line | 
					
						
							|  |  |  |         out.flush if out.respond_to?(:flush) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       o.close | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     return lline | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def knife_bootstrap out, ip, options | 
					
						
							| 
									
										
										
										
											2014-11-20 15:08:42 +03:00
										 |  |  |     knife_stream(out, "bootstrap", options + [ ip ]) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def knife_stream out, cmd, options=[] | 
					
						
							| 
									
										
										
										
											2015-10-01 13:13:12 +03:00
										 |  |  |     knife_cmd = "bundle exec knife #{cmd} #{options.join(" ")} -c #{self.config}" | 
					
						
							| 
									
										
										
										
											2014-11-20 15:08:42 +03:00
										 |  |  |     out << "\nExecuting '#{knife_cmd}' \n\n" | 
					
						
							|  |  |  |     out.flush if out.respond_to?(:flush) | 
					
						
							|  |  |  |     status = nil | 
					
						
							|  |  |  |     IO.popen(knife_cmd + " 2>&1") do |o| | 
					
						
							|  |  |  |       while line = o.gets do | 
					
						
							|  |  |  |         out << line | 
					
						
							|  |  |  |         out.flush if out.respond_to?(:flush) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       o.close | 
					
						
							|  |  |  |       status = $?.to_i | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     return status | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:47:54 +03:00
										 |  |  |   def set_run_list node, list | 
					
						
							| 
									
										
										
										
											2014-11-20 15:08:42 +03:00
										 |  |  |     knife("node run_list set #{node} '#{list.join("','")}'") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | private | 
					
						
							|  |  |  |   def self.get_config | 
					
						
							|  |  |  |     File.join(ENV["HOME"], ".chef", "knife.rb") | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | end |