Class: ProvisionCmd

Inherits:
MofaCmd show all
Defined in:
lib/mofa/provision_cmd.rb

Instance Attribute Summary collapse

Attributes inherited from MofaCmd

#cookbook, #options, #token

Instance Method Summary collapse

Methods inherited from MofaCmd

generate_token, #ssh_exec!

Constructor Details

#initialize(token, cookbook) ⇒ ProvisionCmd

Returns a new instance of ProvisionCmd.



9
10
11
# File 'lib/mofa/provision_cmd.rb', line 9

def initialize(token, cookbook)
  super(token, cookbook)
end

Instance Attribute Details

#attributes_mapObject

Returns the value of attribute attributes_map.



7
8
9
# File 'lib/mofa/provision_cmd.rb', line 7

def attributes_map
  @attributes_map
end

#hostlistObject

Returns the value of attribute hostlist.



5
6
7
# File 'lib/mofa/provision_cmd.rb', line 5

def hostlist
  @hostlist
end

#runlist_mapObject

Returns the value of attribute runlist_map.



6
7
8
# File 'lib/mofa/provision_cmd.rb', line 6

def runlist_map
  @runlist_map
end

Instance Method Details

#cleanupObject



38
39
40
# File 'lib/mofa/provision_cmd.rb', line 38

def cleanup
  cookbook.cleanup
end

#create_data_bags(sftp, hostname, solo_dir) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mofa/provision_cmd.rb', line 107

def create_data_bags(sftp, hostname, solo_dir)
  puts "Remotely creating data_bags items on #{hostname}..."
  if File.directory?("#{cookbook.source_dir}/data_bags")
    Dir.entries("#{cookbook.source_dir}/data_bags/").each do |data_bag|
      next if data_bag =~ /^\.\.?$/
      puts "Found data_bag #{data_bag}... "
      Dir.entries("#{cookbook.source_dir}/data_bags/#{data_bag}").select { |f| f.match(/\.json$/) }.each do |data_bag_item|
        puts "Uploading data_bag_item #{data_bag_item}... "
        sftp.upload!("#{cookbook.source_dir}/data_bags/#{data_bag}/#{data_bag_item}", "#{solo_dir}/data_bags/#{data_bag}/#{data_bag_item}")
        puts 'OK.'
      end
    end
  end
end

#create_node_json(sftp, hostname, solo_dir, attributes_map) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mofa/provision_cmd.rb', line 94

def create_node_json(sftp, hostname, solo_dir, attributes_map)
  puts "Remotely creating \"#{solo_dir}/node.json\" on #{hostname}..."
  node_json = {}
  node_json.store('run_list', runlist_map.mp[hostname])
  attributes_map.mp[hostname].each do |key, value|
    node_json.store(key, value)
  end

  sftp.file.open("#{solo_dir}/node.json", 'w') do |file|
    file.write(JSON.pretty_generate(node_json))
  end
end

#create_solo_rb(sftp, hostname, solo_dir) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mofa/provision_cmd.rb', line 81

def create_solo_rb(sftp, hostname, solo_dir)
  puts "Remotely creating \"#{solo_dir}/solo.rb\" on #{hostname}..."
  sftp.file.open("#{solo_dir}/solo.rb", 'w') do |file|
    solo_rb = <<-"EOF"
  cookbook_path [ "#{solo_dir}/cookbooks" ]
  data_bag_path "#{solo_dir}/data_bags"
  verify_api_cert true
    EOF

    file.write(solo_rb)
  end
end

#execute(ssh_port, ssh_user, ssh_keyfile) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mofa/provision_cmd.rb', line 17

def execute(ssh_port, ssh_user, ssh_keyfile)
  cookbook.execute

  hostlist.retrieve
  runlist_map.generate
  attributes_map.generate

  puts "Runlist Map: #{runlist_map.mp.inspect}"
  puts "Attributes Map: #{attributes_map.mp.inspect}"
  puts "Hostlist before runlist filtering: #{hostlist.list.inspect}"
  puts "Options: #{options.inspect}"

  hostlist.filter_by_runlist_map(runlist_map)

  puts "Hostlist after runlist filtering: #{hostlist.list.inspect}"

  exit_code = run_chef_solo_on_hosts(ssh_port, ssh_user, ssh_keyfile)

  exit_code
end

#host_avail?(hostname) ⇒ Boolean

FIXME This Code is Copy’n’Pasted from the old mofa tooling. Only to make the MVP work in time!! This needs to be refactored ASAP.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mofa/provision_cmd.rb', line 46

def host_avail?(hostname)
  host_available = false
  puts "Pinging host #{hostname}..."
  exit_status = system("ping -q -c 1 #{hostname} >/dev/null 2>&1")
  if exit_status
    puts "  --> Host #{hostname} is available."
    host_available = true
  else
    puts "  --> Host #{hostname} is unavailable!"
  end
  host_available
end

#prepareObject



13
14
15
# File 'lib/mofa/provision_cmd.rb', line 13

def prepare
  cookbook.prepare
end

#prepare_host(hostname, host_index, solo_dir, ssh_port, ssh_user, ssh_keyfile) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mofa/provision_cmd.rb', line 59

def prepare_host(hostname, host_index, solo_dir, ssh_port, ssh_user, ssh_keyfile)
  puts
  puts '----------------------------------------------------------------------'
  puts "Chef-Solo on Host #{hostname} (#{host_index}/#{hostlist.list.length})"
  puts '----------------------------------------------------------------------'
  Net::SSH.start(hostname, ssh_user, keys: [ssh_keyfile], port: ssh_port, use_agent: false, verbose: :error) do |ssh|
    puts "Remotely creating solo_dir \"#{solo_dir}\" on host #{hostname}"
    # remotely create the temp folder
    out = ssh_exec!(ssh, "[ -d #{solo_dir} ] || mkdir #{solo_dir}")
    puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0

    # remotely create a data_bags folder structure on the target host
    if File.directory?("#{cookbook.source_dir}/data_bags")
      Dir.entries("#{cookbook.source_dir}/data_bags").select { |f| !f.match(/^\.\.?$/) }.each do |data_bag|
        puts "Remotely creating data_bags dir \"#{solo_dir}/data_bags/#{data_bag}\""
        out = ssh_exec!(ssh, "[ -d #{solo_dir}/data_bags/#{data_bag} ] || mkdir -p #{solo_dir}/data_bags/#{data_bag}")
        puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
      end
    end
  end
end

#run_chef_solo_on_hosts(ssh_port, ssh_user, ssh_keyfile) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mofa/provision_cmd.rb', line 122

def run_chef_solo_on_hosts(ssh_port, ssh_user, ssh_keyfile)
  time = Time.new
  # Create a temp working dir on the target host
  solo_dir = '/var/tmp/' + time.strftime('%Y-%m-%d_%H%M%S')
  puts
  puts 'Chef-Solo Run started at ' + time.strftime('%Y-%m-%d %H:%M:%S')
  puts "Will use ssh_user '#{ssh_user}', ssh_port '#{ssh_port}' and ssh_keyfile '#{ssh_keyfile}'"
  at_least_one_chef_solo_run_failed = false
  chef_solo_runs = {}
  host_index = 0
  hostlist.list.each do |hostname|
    host_index += 1
    chef_solo_runs.store(hostname, {})

    unless options.key?('ignore_ping') && options[:ignore_ping] == true
      unless host_avail?(hostname)
        chef_solo_runs[hostname].store('status', 'UNAVAIL')
        chef_solo_runs[hostname].store('status_msg', "Host #{hostname} unreachable.")
        at_least_one_chef_solo_run_failed = true
        next
      end
    end

    prepare_host(hostname, host_index, solo_dir, ssh_port, ssh_user, ssh_keyfile)

    Net::SFTP.start(hostname, ssh_user, keys: [ssh_keyfile], port: ssh_port, use_agent: false, verbose: :error) do |sftp|
      # remotely creating solo.rb
      create_solo_rb(sftp, hostname, solo_dir)

      # remotely creating node.json
      create_node_json(sftp, hostname, solo_dir, attributes_map)

      # remotely create data_bag items
      create_data_bags(sftp, hostname, solo_dir)

      puts "Uploading Package #{cookbook.pkg_name}... "
      sftp.upload!("#{cookbook.pkg_dir}/#{cookbook.pkg_name}", "#{solo_dir}/#{cookbook.pkg_name}")
      puts 'OK.'

      File.open("#{cookbook.pkg_dir}/log", 'w') do |log_file|
        # Do it -> Execute the chef-solo run!
        begin
          begin
            Net::SSH.start(hostname, ssh_user, keys: [ssh_keyfile], port: ssh_port, use_agent: false, verbose: :error) do |ssh|
              puts "Remotely unpacking Cookbook Package #{cookbook.pkg_name}... "
              ssh.exec!("cd #{solo_dir}; tar xvfz #{cookbook.pkg_name}") do |_ch, _stream, line|
                puts line if Mofa::CLI.option_debug
                log_file.write(line) if Mofa::CLI.option_debug
              end
              puts 'OK.'
            end
          rescue StandardError => e
            status_msg = "ERROR: Unpacking cookbook archive on remote host #{hostname} failed (#{e.message})!"
            chef_solo_runs[hostname].store('status', 'FAIL')
            chef_solo_runs[hostname].store('status_msg', status_msg)
            puts status_msg
            log_file.write(status_msg)
            raise e
          end
          begin
            Net::SSH.start(hostname, ssh_user, keys: [ssh_keyfile], port: ssh_port, use_agent: false, verbose: :error) do |ssh|
              puts "Remotely running chef-solo -c #{solo_dir}/solo.rb -j #{solo_dir}/node.json"
              chef_run_exit_code = 0
              ssh.exec!("sudo chef-solo -c #{solo_dir}/solo.rb -j #{solo_dir}/node.json") do |_ch, _stream, line|
                puts line if Mofa::CLI.option_verbose || Mofa::CLI.option_debug
                log_file.write(line)
                chef_run_exit_code = 1 if line =~ /Chef run process exited unsuccessfully/ || line =~ /chef-solo: command not found/
              end
              raise 'Chef run process exited unsuccessfully' if chef_run_exit_code != 0
              chef_solo_runs[hostname].store('status', 'SUCCESS')
              chef_solo_runs[hostname].store('status_msg', '')
              puts 'Chef-solo run was a SUCCESS!'
              log_file.write('chef-solo run: SUCCESS')
            end
          rescue StandardError => e
            status_msg = "ERROR: Chef-solo run on #{hostname} FAILED! (#{e.message})"
            chef_solo_runs[hostname].store('status', 'FAIL')
            chef_solo_runs[hostname].store('status_msg', status_msg)
            puts status_msg
            log_file.write(status_msg)
            raise e
          end
        rescue StandardError => e
          log_file.write('chef-solo run: FAIL')
          puts "ERRORS detected while provisioning #{hostname} (#{e.message})."
        end
        Net::SSH.start(hostname, ssh_user, keys: [ssh_keyfile], port: ssh_port, use_agent: false, verbose: :error) do |ssh|
          snapshot_or_release = cookbook.is_a?(SourceCookbook) ? 'snapshot' : 'release'
          out = ssh_exec!(ssh, "sudo chown -R #{ssh_user}.#{ssh_user} #{solo_dir}")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, '[ -d /var/lib/mofa ] || sudo mkdir /var/lib/mofa')
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo #{cookbook.name} | sudo tee /var/lib/mofa/last_cookbook_name")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo '#{snapshot_or_release}' | sudo tee /var/lib/mofa/last_cookbook_snapshot_or_release")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo #{cookbook.version} | sudo tee /var/lib/mofa/last_cookbook_version")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo #{chef_solo_runs[hostname]['status']} | sudo tee /var/lib/mofa/last_status")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "date '+%Y-%m-%d %H:%M:%S' | sudo tee /var/lib/mofa/last_timestamp")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo '#{solo_dir}/log' | sudo tee /var/lib/mofa/last_log")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "echo $(date '+%Y-%m-%d %H:%M:%S')': #{cookbook.name}@#{cookbook.version} (#{chef_solo_runs[hostname]['status']})' | sudo tee -a /var/lib/mofa/history")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "sudo find #{solo_dir} -type d | xargs chmod 700")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          out = ssh_exec!(ssh, "sudo find #{solo_dir} -type f | xargs chmod 600")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
        end
      end
      at_least_one_chef_solo_run_failed = true unless chef_solo_runs[hostname]['status'] == 'SUCCESS'
      sftp.upload!("#{cookbook.pkg_dir}/log", "#{solo_dir}/log")
    end
  end
  # ------- print out report
  puts
  puts '----------------------------------------------------------------------'
  puts 'Chef-Solo Run REPORT'
  puts '----------------------------------------------------------------------'
  puts "Chef-Solo has been run on #{chef_solo_runs.keys.length} hosts."

  chef_solo_runs.each do |hostname, content|
    status_msg = ''
    status_msg = "(#{content['status_msg']})" if content['status'] == 'FAIL'
    puts "#{content['status']}: #{hostname} #{status_msg}"
  end

  exit_code = at_least_one_chef_solo_run_failed ? 1 : 0
  puts "Exiting with exit code #{exit_code}."

  if exit_code != 0
    raise Thor::Error, "Chef client exited with non zero exit code: #{exit_code}"
  end
  exit_code
end