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, {})
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
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}"
out = ssh_exec!(ssh, "[ -d #{solo_dir} ] || mkdir #{solo_dir}")
puts "ERROR (#{out[0]}): #{out[2]}" if out[0] != 0
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
next unless prerequesits_met
Net::SFTP.start(hostname, Mofa::Config.config['ssh_user'], :keys => [Mofa::Config.config['ssh_keyfile']], :verbose => :error) do |sftp|
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
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
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
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
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
|