Class: KnifeHitori::RunRemoteCook

Inherits:
Object
  • Object
show all
Defined in:
lib/knife-hitori/lib/run_remote_cook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_config, run_config, server, opts) ⇒ RunRemoteCook

Returns a new instance of RunRemoteCook.



10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def config
  @config
end

#knifeObject (readonly)

Returns the value of attribute knife.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def knife
  @knife
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def opts
  @opts
end

#run_configObject (readonly)

Returns the value of attribute run_config.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def run_config
  @run_config
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def server
  @server
end

#uiObject (readonly)

Returns the value of attribute ui.



8
9
10
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 8

def ui
  @ui
end

Instance Method Details

#bg_color(text) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



132
133
134
# File 'lib/knife-hitori/lib/run_remote_cook.rb', line 132

def console_log(msg)
  print bg_color(msg)
end

#create_remote_dirs(info) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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_chefObject



51
52
53
54
55
56
57
# 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_cmdObject



59
60
61
62
63
64
65
66
67
68
# 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

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# 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