Class: MofaCmd

Inherits:
Object
  • Object
show all
Defined in:
lib/mofa/mofa_cmd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



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

def cookbook
  @cookbook
end

#hostlistObject

Returns the value of attribute hostlist.



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

def hostlist
  @hostlist
end

#runlist_mapObject

Returns the value of attribute runlist_map.



8
9
10
# File 'lib/mofa/mofa_cmd.rb', line 8

def runlist_map
  @runlist_map
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.create(cookbook, hostlist, runlist_map, token) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/mofa/mofa_cmd.rb', line 14

def self.create(cookbook, hostlist, runlist_map, token)
  mofa_cmd = MofaCmd.new
  mofa_cmd.token = token
  mofa_cmd.cookbook = cookbook
  mofa_cmd.hostlist = hostlist
  mofa_cmd.runlist_map = runlist_map
  mofa_cmd
end

.generate_tokenObject



10
11
12
# File 'lib/mofa/mofa_cmd.rb', line 10

def self.generate_token
  Digest::SHA1.hexdigest([Time.now, rand].join)[0..10]
end

Instance Method Details

#cleanupObject



45
46
47
# File 'lib/mofa/mofa_cmd.rb', line 45

def cleanup
  cookbook.cleanup
end

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mofa/mofa_cmd.rb', line 28

def execute
  cookbook.execute
  hostlist.retrieve
  runlist_map.generate

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

  hostlist.filter_by_runlist_map(runlist_map)

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

  exit_code = run_chef_solo_on_hosts

  exit_code
end

#prepareObject



23
24
25
26
# File 'lib/mofa/mofa_cmd.rb', line 23

def prepare
  cookbook.prepare
  fail "Hostlist Service not reachable! (cannot ping #{hostlist.service_url})" unless hostlist.up?
end

#run_chef_solo_on_hostsObject

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
# File 'lib/mofa/mofa_cmd.rb', line 53

def run_chef_solo_on_hosts
  time = Time.new
  puts 'Chef-Solo Run started at ' + time.strftime('%Y-%m-%d %H:%M:%S')
  puts "Will use ssh_user #{Mofa::Config.config['ssh_user']} and ssh_key_file #{Mofa::Config.config['ssh_keyfile']}"
  at_least_one_chef_solo_run_failed = false
  chef_solo_runs = {}
  host_index = 0
  hostlist.list.each do |hostname|
    host_index = host_index + 1
    puts
    puts "----------------------------------------------------------------------"
    puts "Chef-Solo on Host #{hostname} (#{host_index}/#{hostlist.list.length.to_s})"
    puts "----------------------------------------------------------------------"
    chef_solo_runs.store(hostname, {})

    # do only one for faster dev-cycle...
    #next unless hostname.match(/^dash/)

    puts "Pinging host #{hostname}..."
    exit_status = system("ping -q -c 1 #{hostname} >/dev/null 2>&1")
    unless exit_status then
      puts "  --> Host #{hostname} is unavailable!"
      chef_solo_runs[hostname].store('status', 'UNAVAIL')
      chef_solo_runs[hostname].store('status_msg', "Host #{hostname} unreachable.")
    else
      puts "  --> Host #{hostname} is available."
      prerequesits_met = true
      # Create a temp working dir on the target host
      solo_dir = '/var/tmp/' + time.strftime('%Y-%m-%d_%H%M%S')
      Net::SSH.start(hostname, Mofa::Config.config['ssh_user'], :keys => [Mofa::Config.config['ssh_keyfile']], :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

    # skip the rest if prerequesits are not met
    next unless prerequesits_met


    Net::SFTP.start(hostname, Mofa::Config.config['ssh_user'], :keys => [Mofa::Config.config['ssh_keyfile']], :verbose => :error) do |sftp|

      # remotely creating solo.rb
      puts "Remotely creating \"#{solo_dir}/solo.rb\""
      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"
      log_level :info
      log_location "#{solo_dir}/log"
      verify_api_cert true
        EOF

        file.write(solo_rb)
      end

      # remotely creating node.json
      puts "Remotely creating \"#{solo_dir}/node.json\""
      node_json = {}
      node_json.store('run_list', runlist_map.mp[hostname])

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

      # remotely create data_bag items
      if File.directory?("#{cookbook.source_dir}/data_bags")
        Dir.entries("#{cookbook.source_dir}/data_bags").select{|f| !f.match(/^\.\.?$/)}.each do |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

      if cookbook.instance_of?(SourceCookbook)
        puts "Cookbook is a SourceCookbook! Uploading Snapshot Package #{cookbook.pkg_name}... "
        sftp.upload!("#{cookbook.pkg_dir}/#{cookbook.pkg_name}", "#{solo_dir}/#{cookbook.pkg_name}")
        puts "OK."
      end

      # Do it -> Execute the chef-solo run!
      Net::SSH.start(hostname, Mofa::Config::config['ssh_user'], :keys => [Mofa::Config::config['ssh_keyfile']], :verbose => :error) do |ssh|

        if cookbook.instance_of?(SourceCookbook)
          puts "Remotely unpacking Snapshot Package #{cookbook.pkg_name}... "
          out = ssh_exec!(ssh, "cd #{solo_dir}; tar xvfz #{cookbook.pkg_name}")
          if out[0] != 0
            puts "ERROR (#{out[0]}): #{out[2]}"
            puts out[1]
          else
            puts "OK."
          end
        end

        puts "Remotely running chef-solo -c #{solo_dir}/solo.rb -j #{solo_dir}/node.json"
        out = ssh_exec!(ssh, "sudo chef-solo -c #{solo_dir}/solo.rb -j #{solo_dir}/node.json")
        if out[0] != 0
          puts "ERROR (#{out[0]}): #{out[2]}"
          out = ssh_exec!(ssh, "sudo cat #{solo_dir}/log")
          puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
          puts out[1]
          chef_solo_runs[hostname].store('status', 'FAIL')
          chef_solo_runs[hostname].store('status_msg', out[1])
        else
          unless Mofa::CLI::option_debug
            out = ssh_exec!(ssh, "sudo grep 'Chef Run' #{solo_dir}/log")
            puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
            puts "Done."
          else
            out = ssh_exec!(ssh, "sudo cat #{solo_dir}/log")
            puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
            puts out[1]
          end
          chef_solo_runs[hostname].store('status', 'SUCCESS')
          chef_solo_runs[hostname].store('status_msg', '')
        end
        out = ssh_exec!(ssh, "sudo chown -R #{Mofa::Config.config['ssh_user']}.#{Mofa::Config.config['ssh_user']} #{solo_dir}")
        puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
      end
    end
    at_least_one_chef_solo_run_failed = true if chef_solo_runs[hostname]['status'] == 'FAIL'
  end

  # ------- print out report
  puts
  puts "----------------------------------------------------------------------"
  puts "Chef-Solo Run REPORT"
  puts "----------------------------------------------------------------------"
  puts "Chef-Solo has been run on #{chef_solo_runs.keys.length.to_s} 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 = 0
  if at_least_one_chef_solo_run_failed
    exit_code = 1
  end

  puts "Exiting with exit code #{exit_code}."
  exit_code

end

#ssh_exec!(ssh, command) ⇒ Object



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
# File 'lib/mofa/mofa_cmd.rb', line 211

def ssh_exec!(ssh, command)
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  exit_signal = nil
  ssh.open_channel do |channel|
    channel.exec(command) do |ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |ch, data|
        stdout_data+=data
      end

      channel.on_extended_data do |ch, type, data|
        stderr_data+=data
      end

      channel.on_request("exit-status") do |ch, data|
        exit_code = data.read_long
      end

      channel.on_request("exit-signal") do |ch, data|
        exit_signal = data.read_long
      end
    end
  end
  ssh.loop
  [exit_code, stdout_data, stderr_data, exit_signal]
end