#!/usr/bin/env ruby # This utility downloads cookbooks, json files and runs chef-solo # # Copyright 2012, Mirantis require 'rubygems' require 'ohai' require 'json' APIURL = "http://<%= node.cobbler.repoaddr %>:8000" URL = "http://<%= node.cobbler.repoaddr %>" COOKS_DIR = "/tmp/cookbooks" ohai = Ohai::System.new ohai.require_plugin("network") ohai.require_plugin("linux/network") ohai_data = ohai.data if ARGV[0] component = ARGV[0] else puts "Component name is not set." exit 1 end host_id = ohai.data["macaddress"].gsub(':', '') # Downloading host-specific json solo_json_file = "#{host_id}.json" system("wget -N -T 7 --tries=7 -a wget.log -q -O #{solo_json_file} #{APIURL}/api/endpoints/#{host_id}/#{component}") solo_json = JSON.parse(File.read(solo_json_file)) cooks = solo_json["run_list"].map do |recipe| cook_ver = recipe.match(/recipe\[(\S+)::\S+@(\S+)\]/) cook, version = cook_ver[1], cook_ver[2] {cook => version} end.uniq Dir.mkdir(COOKS_DIR) unless File.exists?(COOKS_DIR) if not solo_json["cooks"] cooks = solo_json["run_list"].map do |recipe| cook_ver = recipe.match(/recipe\[(\S+)::\S+@(\S+)\]/) cook, version = cook_ver[1], cook_ver[2] {cook => version} end.uniq cooks.each do |cb| filename = "#{cb.keys[0]}_#{cb.values[0]}.tar.gz" system("wget -N -T 7 --tries=7 -a wget.log #{URL}/#{filename}") system("tar xzf \"#{filename}\" -C #{COOKS_DIR}") end else solo_json["cooks"].each do |cook| cook_name = cook["name"] cook_version = cook["version"] filename = "#{cook_name}_#{cook_version}.tar.gz" system("wget -N -T 7 --tries=7 -a wget.log #{URL}/#{filename}") system("tar xzf \"#{filename}\" -C #{COOKS_DIR}") end end Dir.mkdir("/var/log/chef") unless File.exists?("/var/log/chef") Dir.mkdir("/tmp/chef") unless File.exists?("/tmp/chef") solo_rb = %{ log_location "/var/log/chef/solo.log" file_cache_path "/tmp/chef" cookbook_path "#{COOKS_DIR}" log_level :debug verbose_logging true } solo_rb_file = File.join(COOKS_DIR, 'solo.rb') File.open(solo_rb_file, 'w') {|f| f.write(solo_rb) } system("/usr/bin/chef-solo -c #{File.expand_path(solo_rb_file)} -j #{solo_json_file}")