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
- #execute ⇒ Object
-
#initialize(token, cookbook) ⇒ ProvisionCmd
constructor
A new instance of ProvisionCmd.
- #prepare ⇒ Object
-
#run_chef_solo_on_hosts ⇒ Object
FIXME This Code is Copy’n’Pasted from the old mofa tooling.
Methods inherited from MofaCmd
Constructor Details
#initialize(token, cookbook) ⇒ 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 |
#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 |
#run_chef_solo_on_hosts ⇒ 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 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 |
# File 'lib/mofa/provision_cmd.rb', line 45 def run_chef_solo_on_hosts time = Time.new 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 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 # 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']], :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 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']], :port => Mofa::Config.config['ssh_port'], :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 = " cookbook_path [ \"\#{solo_dir}/cookbooks\" ]\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\n\n # remotely creating node.json\n puts \"Remotely creating \\\"\#{solo_dir}/node.json\\\"\"\n node_json = {}\n node_json.store('run_list', runlist_map.mp[hostname])\n attributes_map.mp[hostname].each do |key, value|\n node_json.store(key, value)\n end\n\n sftp.file.open(\"\#{solo_dir}/node.json\", \"w\") do |file|\n file.write(JSON.pretty_generate(node_json))\n end\n\n # remotely create data_bag items\n if File.directory?(\"\#{cookbook.source_dir}/data_bags\")\n Dir.entries(\"\#{cookbook.source_dir}/data_bags\").select { |f| !f.match(/^\\.\\.?$/) }.each do |data_bag|\n Dir.entries(\"\#{cookbook.source_dir}/data_bags/\#{data_bag}\").select { |f| f.match(/\\.json$/) }.each do |data_bag_item|\n puts \"Uploading data_bag_item \#{data_bag_item}... \"\n sftp.upload!(\"\#{cookbook.source_dir}/data_bags/\#{data_bag}/\#{data_bag_item}\", \"\#{solo_dir}/data_bags/\#{data_bag}/\#{data_bag_item}\")\n puts \"OK.\"\n end\n end\n end\n\n if cookbook.instance_of?(SourceCookbook)\n puts \"Cookbook is a SourceCookbook! Uploading Snapshot Package \#{cookbook.pkg_name}... \"\n sftp.upload!(\"\#{cookbook.pkg_dir}/\#{cookbook.pkg_name}\", \"\#{solo_dir}/\#{cookbook.pkg_name}\")\n puts \"OK.\"\n end\n\n # Do it -> Execute the chef-solo run!\n 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|\n\n if cookbook.instance_of?(SourceCookbook)\n puts \"Remotely unpacking Snapshot Package \#{cookbook.pkg_name}... \"\n out = ssh_exec!(ssh, \"cd \#{solo_dir}; tar xvfz \#{cookbook.pkg_name}\")\n if out[0] != 0\n puts \"ERROR (\#{out[0]}): \#{out[2]}\"\n puts out[1]\n else\n puts \"OK.\"\n end\n end\n\n puts \"Remotely running chef-solo -c \#{solo_dir}/solo.rb -j \#{solo_dir}/node.json\"\n out = ssh_exec!(ssh, \"sudo chef-solo -c \#{solo_dir}/solo.rb -j \#{solo_dir}/node.json\")\n if out[0] != 0\n puts \"ERROR (\#{out[0]}): \#{out[2]}\"\n out = ssh_exec!(ssh, \"sudo cat \#{solo_dir}/log\")\n puts \"ERROR (\#{out[0]}): \#{out[2]}\" if out[0] != 0\n puts out[1]\n chef_solo_runs[hostname].store('status', 'FAIL')\n chef_solo_runs[hostname].store('status_msg', out[1])\n else\n unless Mofa::CLI::option_debug\n out = ssh_exec!(ssh, \"sudo grep 'Chef Run' \#{solo_dir}/log\")\n puts \"ERROR (\#{out[0]}): \#{out[2]}\" if out[0] != 0\n puts \"Done.\"\n else\n out = ssh_exec!(ssh, \"sudo cat \#{solo_dir}/log\")\n puts \"ERROR (\#{out[0]}): \#{out[2]}\" if out[0] != 0\n puts out[1]\n end\n chef_solo_runs[hostname].store('status', 'SUCCESS')\n chef_solo_runs[hostname].store('status_msg', '')\n end\n out = ssh_exec!(ssh, \"sudo chown -R \#{Mofa::Config.config['ssh_user']}.\#{Mofa::Config.config['ssh_user']} \#{solo_dir}\")\n puts \"ERROR (\#{out[0]}): \#{out[2]}\" if out[0] != 0\n end\n end\n at_least_one_chef_solo_run_failed = true if chef_solo_runs[hostname]['status'] == 'FAIL'\n end\n\n # ------- print out report\n puts\n puts \"----------------------------------------------------------------------\"\n puts \"Chef-Solo Run REPORT\"\n puts \"----------------------------------------------------------------------\"\n puts \"Chef-Solo has been run on \#{chef_solo_runs.keys.length.to_s} hosts.\"\n\n chef_solo_runs.each do |hostname, content|\n status_msg = ''\n status_msg = \"(\#{content['status_msg']})\" if content['status'] == 'FAIL'\n puts \"\#{content['status']}: \#{hostname} \#{status_msg}\"\n end\n\n exit_code = 0\n if at_least_one_chef_solo_run_failed\n exit_code = 1\n end\n\n puts \"Exiting with exit code \#{exit_code}.\"\n exit_code\n\nend\n" |