Class: ProvisionCmd
Instance Attribute Summary collapse
-
#attributes_map ⇒ Object
Returns the value of attribute attributes_map.
-
#hostlist ⇒ Object
Returns the value of attribute hostlist.
-
#runlist_map ⇒ Object
Returns the value of attribute runlist_map.
Attributes inherited from MofaCmd
Instance Method Summary collapse
- #cleanup ⇒ Object
- #create_data_bags(sftp, hostname, solo_dir) ⇒ Object
- #create_node_json(sftp, hostname, solo_dir, attributes_map) ⇒ Object
- #create_solo_rb(sftp, hostname, solo_dir) ⇒ Object
- #execute ⇒ Object
-
#initialize(token, cookbook) ⇒ ProvisionCmd
constructor
A new instance of ProvisionCmd.
- #prepare ⇒ Object
-
#prepare_host(hostname, host_index, solo_dir) ⇒ Object
FIXME This Code is Copy’n’Pasted from the old mofa tooling.
- #run_chef_solo_on_hosts ⇒ Object
Methods inherited from MofaCmd
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_map ⇒ Object
Returns the value of attribute attributes_map.
7 8 9 |
# File 'lib/mofa/provision_cmd.rb', line 7 def attributes_map @attributes_map end |
#hostlist ⇒ Object
Returns the value of attribute hostlist.
5 6 7 |
# File 'lib/mofa/provision_cmd.rb', line 5 def hostlist @hostlist end |
#runlist_map ⇒ Object
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
#cleanup ⇒ Object
37 38 39 |
# File 'lib/mofa/provision_cmd.rb', line 37 def cleanup cookbook.cleanup end |
#create_data_bags(sftp, hostname, solo_dir) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mofa/provision_cmd.rb', line 116 def create_data_bags(sftp, hostname, solo_dir) if cookbook.instance_of?(SourceCookbook) and File.directory?("#{cookbook.source_dir}/data_bags") Dir.entries("#{cookbook.source_dir}/data_bags").select { |f| File.directory?(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 end |
#create_node_json(sftp, hostname, solo_dir, attributes_map) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mofa/provision_cmd.rb', line 103 def create_node_json(sftp, hostname, solo_dir, attributes_map) puts "Remotely creating \"#{solo_dir}/node.json\"" 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mofa/provision_cmd.rb', line 80 def create_solo_rb(sftp, hostname, solo_dir) puts "Remotely creating \"#{solo_dir}/solo.rb\"" sftp.file.open("#{solo_dir}/solo.rb", "w") do |file| if cookbook.instance_of?(SourceCookbook) solo_rb = " cookbook_path [ \"\#{solo_dir}/cookbooks\" ]\n EOF\n else\n solo_rb = <<-\"EOF\"\n recipe_url \"\#{solo_dir}/\#{cookbook.pkg_name}\"\n EOF\n end\n solo_rb += <<-\"EOF\"\n data_bag_path \"\#{solo_dir}/data_bags\"\n log_level :info\n log_location \"\#{solo_dir}/log\"\n verify_api_cert true\n EOF\n\n file.write(solo_rb)\n end\nend\n" |
#execute ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mofa/provision_cmd.rb', line 17 def execute 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}" 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 |
#prepare ⇒ Object
13 14 15 |
# File 'lib/mofa/provision_cmd.rb', line 13 def prepare cookbook.prepare end |
#prepare_host(hostname, host_index, solo_dir) ⇒ Object
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.
45 46 47 48 49 50 51 52 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 |
# File 'lib/mofa/provision_cmd.rb', line 45 def prepare_host(hostname, host_index, solo_dir) prerequesits_met = false puts puts "----------------------------------------------------------------------" puts "Chef-Solo on Host #{hostname} (#{host_index}/#{hostlist.list.length.to_s})" puts "----------------------------------------------------------------------" 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 Net::SSH.start(hostname, Mofa::Config.config['ssh_user'], :keys => [Mofa::Config.config['ssh_keyfile']], :port => Mofa::Config.config['ssh_port'], :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 cookbook.instance_of?(SourceCookbook) and 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 prerequesits_met end |
#run_chef_solo_on_hosts ⇒ Object
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 |
# File 'lib/mofa/provision_cmd.rb', line 128 def run_chef_solo_on_hosts 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 #{Mofa::Config.config['ssh_user']}, ssh_port #{Mofa::Config.config['ssh_port']} 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 next unless prepare_host(hostname, host_index, solo_dir) chef_solo_runs.store(hostname, {}) Net::SFTP.start(hostname, Mofa::Config.config['ssh_user'], :keys => [Mofa::Config.config['ssh_keyfile']], :port => Mofa::Config.config['ssh_port'], :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." # Do it -> Execute the chef-solo run! Net::SSH.start(hostname, Mofa::Config::config['ssh_user'], :keys => [Mofa::Config::config['ssh_keyfile']], :port => Mofa::Config.config['ssh_port'], :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 out = ssh_exec!(ssh, "sudo mkdir -p /var/lib/mofa && ln -s #{solo_dir} /var/lib/mofa/last_run && echo #{cookbook.pkg_name} > /var/lib/mofa/last_cookbook && echo #{chef_solo_runs[hostname]['status']} > /var/lib/mofa/last_status") 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}." if exit_code != 0 raise Thor::Error, "Chef client exited with non zero exit code: #{exit_code}" end exit_code end |