class KnifeHitori::RunRemoteCook
Attributes
config[R]
knife[R]
opts[R]
run_config[R]
server[R]
ui[R]
Public Class Methods
new(chef_config, run_config, server, opts)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 10 def initialize(chef_config, run_config, server, opts) @config = chef_config @run_config = run_config @server = server @opts = opts @ui = opts[:ui] @knife = config.knife end
Public Instance Methods
bg_color(text)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 71 def bg_color(text) #Background colors #40 Black #41 Red #42 Green #43 Yellow #44 Blue #45 Magenta #46 Cyan #47 White c = 47 - ((opts[:idx].to_i + 3) % 7) "\e[#{c}m#{text}\e[0m" end
console_log(msg)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 132 def console_log(msg) print bg_color(msg) end
create_remote_dirs(info)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 98 def create_remote_dirs(info) if info[:opts] && info[:opts][:recursive] #dir_list = [] #Dir.glob("#{info[:src]}/**/").each do |d| # relative_dir = d.gsub("#{info[:src]}/", '') # dir_list << "#{info[:dest]}/#{relative_dir}" #end #cmd = "mkdir -p #{dir_list.join(' ')}" else cmd = "mkdir -p #{File.dirname(info[:dest])}" exec_remote(cmd) end end
exec_remote(command)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 112 def exec_remote(command) Net::SSH.start(server.public_ip_address, knife[:ssh_user], :keys => [knife[:identity_file]]) do |ssh| ssh.open_channel do |ch| ch.request_pty do |pty, success| raise 'Could not obtain pty' unless success end ch.exec(command) do |c, success| abort 'could not execute command' unless success ch.on_data do |c, data| console_log data end ch.on_extended_data do |c, type, data| console_log data end end end end end
install_chef()
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 51 def install_chef Chef::Log.debug 'install_chef' console_log "Install Chef in #{server.public_ip_address}\n" remote_filename = '/tmp/.install_chef_script' upload_files([{src: knife[:template_file], dest: remote_filename, opts: {:verbose => true}}]) exec_remote("sudo sh #{remote_filename}; rm #{remote_filename}") end
make_run_list_cmd()
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 59 def make_run_list_cmd script = '' if opts[:run_list] && !opts[:run_list].empty? script = <<-EOS [ -d run_list/#{config[:solo_environment]} ] || mkdir -p run_list/#{config[:solo_environment]} echo '#{opts[:run_list].join(',')}' > run_list/#{config[:solo_environment]}/`hostname` EOS end script end
run()
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 19 def run # Install Chef if needed install_chef if run_config[:install_chef] # Remove Old Cookbooks console_log "Remove Old Chef Cookbooks in #{server.public_ip_address}\n" mkdir_cmd = <<-EOS [ -e #{knife[:remote_chef_dir]} ] && sudo rm -rf #{knife[:remote_chef_dir]} sudo mkdir -p #{knife[:remote_chef_dir]} sudo chmod 777 #{knife[:remote_chef_dir]} EOS exec_remote(mkdir_cmd) # Upload Cookbooks console_log "Uploading Chef Cookbooks to #{server.public_ip_address}\n" remote_dir = "#{knife[:remote_chef_dir]}/#{::File.basename(config[:chef_solo_base_dir])}" chef_solo_filename = 'do_cook.sh' files_info = [] files_info << {src: config[:chef_solo_base_dir], dest: knife[:remote_chef_dir], opts: {:recursive => true, :verbose => true}} files_info << {src: KnifeHitori::resource(chef_solo_filename), dest: "#{remote_dir}/#{chef_solo_filename}", opts: {:verbose => true}} upload_files(files_info) console_log "Upload is done #{server.public_ip_address}\n" # run chef-solo script = <<-EOS cd #{remote_dir} tr -d '\\r' < #{chef_solo_filename} > .a mv .a #{chef_solo_filename} #{make_run_list_cmd} sudo CHEF_ENV=#{config[:solo_environment]} sh #{chef_solo_filename} EOS exec_remote(script) end
upload_files(files_info)
click to toggle source
# File lib/knife-hitori/lib/run_remote_cook.rb, line 85 def upload_files(files_info) Net::SCP.start(server.public_ip_address, knife[:ssh_user], :keys => [knife[:identity_file]], :compression => true) do |scp| channels = [] files_info.each do |info| create_remote_dirs(info) channels << scp.upload(info[:src], info[:dest], info[:opts]) end channels.each do |ch| ch.wait end end end