lib/toaster/chef/chef_util.rb in cloud-toaster-1.1.5 vs lib/toaster/chef/chef_util.rb in cloud-toaster-1.1.6

- old
+ new

@@ -41,11 +41,22 @@ OPSCODE_API_URL = "https://supermarket.getchef.com/api/v1/" OPSCODE_SEARCH_URL = "http://community.opscode.com/search" @@DEFAULT_CHEF_DIR = "/tmp/toaster_cookbooks/" @@DEFAULT_COOKBOOKS_DIR = "#{@@DEFAULT_CHEF_DIR}/cookbooks/" + @@DEFAULT_OPSCODE_TMP_DIR = "/tmp/opscode_cookbooks/" # TODO merge with above? + def self.DEFAULT_CHEF_DIR + @@DEFAULT_CHEF_DIR + end + def self.DEFAULT_COOKBOOKS_DIR + @@DEFAULT_COOKBOOKS_DIR + end + def self.DEFAULT_OPSCODE_TMP_DIR + @@DEFAULT_OPSCODE_TMP_DIR + end + def self.guess_cookbook_from_runlist(runlist) guess_cookbook_or_recipe_from_runlist(runlist, "cookbook") end def self.guess_recipe_from_runlist(runlist) guess_cookbook_or_recipe_from_runlist(runlist, "recipe") @@ -320,11 +331,11 @@ end } cookbooks_after = Dir.entries(target_dir) new_cb = cookbooks_after - cookbooks_before - puts "INFO: Downloaded and installed new cookbooks: #{new_cb}" + puts "INFO: Downloaded and installed new cookbooks: #{new_cb}" if !new_cb.empty? # download dependencies new_cb.each do |cb| download_dependencies(cb, nil, target_dir, true) end @@ -589,11 +600,11 @@ end return result end def self.available_recipes_from_opscode(cookbook_name, version="latest", - overwrite_downloads=false, target_dir="/tmp/opscode_cookbooks/") + overwrite_downloads=false, target_dir=@@DEFAULT_OPSCODE_TMP_DIR) if target_dir FileUtils.mkpath(target_dir) if !File.directory?(target_dir) # check if cookbook folder already exists cookbook_folder = File.join(target_dir, cookbook_name) if File.directory?(cookbook_folder) && overwrite_downloads @@ -697,10 +708,16 @@ def self.fetch_cookbook_details(cookbook_name, cbook={}, resources={}, overwrite_downloads=false) url = "#{OPSCODE_API_URL}cookbooks/#{cookbook_name}/" json = `curl '#{url}' 2> /dev/null` details = MarkupUtil.parse_json(json.strip) + if !details['latest_version'] + if details['error_code'] + raise "WARN: Unable to fetch cookbook metadata from #{url}: " + + "#{details['error_code']} - #{details['error_messages']}" + end + end latest_version = details['latest_version'].gsub(/.*\/([^\/]+)/, '\1') cbook['_latest_version'] = latest_version versions = [] details['versions'].each do |ver| versions << ver.gsub(/.*\/([^\/]+)/, '\1') @@ -745,11 +762,11 @@ def self.available_cookbook_versions(cookbook_name) fetch_cookbook_details(cookbook_name)["_versions"].uniq end - def self.download_all_from_opscode(target_dir="/tmp/opscode_cookbooks/", overwrite_downloads=false) + def self.download_all_from_opscode(target_dir=@@DEFAULT_OPSCODE_TMP_DIR, overwrite_downloads=false) target_dir = "/tmp/opscode_cookbooks/" if !target_dir if overwrite_downloads || !File.exist?(target_dir) `mkdir -p "#{target_dir}"` cookbooks = available_cookbooks_from_opscode() cookbooks.each do |cb| @@ -758,11 +775,12 @@ download_cookbook_version(cookbook_name, "latest", target_dir, true) end end end - def self.parse_resources(cookbook, recipe_name, version="latest", result = {}, cookbook_dir="/tmp/opscode_cookbooks/") + def self.parse_resources(cookbook, recipe_name, version="latest", + result = {}, cookbook_dir=@@DEFAULT_OPSCODE_TMP_DIR) recipe_file = "#{cookbook_dir}/#{cookbook}/recipes/#{recipe_name}.rb" attributes_file = "#{cookbook_dir}/#{cookbook}/attributes/#{recipe_name}.rb" recipe_file_relative = "#{cookbook}/recipes/#{recipe_name}.rb" attributes_source = File.exist?(attributes_file) ? File.read(attributes_file) : "" @@ -793,21 +811,21 @@ ] script_resource_names = ["execute", "bash", "script", "ruby_block", "csh"] File.open(recipe_file) do |io| io.each_with_index { |line,idx| - #if line.match(/^\s*((#{resource_names.join(")|(")}))\s+.*((do)|(\{)).*$/) + idx += 1 # index is 0-based, line should be 1-based if line.match(/^(\s*[0-9a-zA-Z_]+\s*=\s*)?\s*((#{resource_names.join(")|(")}))((\s+)|($)|(\s*\())/) resource_lines << idx elsif line.match(/execute.*do/) && !line.match(/^\s*#/) puts "WARN: NO resource line: #{line}" end } resource_lines.each do |line| - code = read_sourcecode_from_line(recipe_file, line + 1) + code = read_sourcecode_from_line(recipe_file, line) if !code - puts "WARN: Could not parse code file #{recipe_file} : #{line + 1}" + puts "WARN: Could not parse code file #{recipe_file} : #{line}" else resource_obj = ResourceInspector.get_resource_from_source(code, attributes_source) result[cookbook][recipe_name]["resources"][line] = code result[cookbook][recipe_name]["resource_objs"][line] = resource_obj if !code.match(/not_if\s*/) && !code.match(/only_if\s*/) @@ -820,10 +838,10 @@ end end return result end - def self.parse_all_resources(recipe_pattern="default.rb", cookbook_dir="/tmp/opscode_cookbooks/") + def self.parse_all_resources(recipe_pattern="default.rb", cookbook_dir=@@DEFAULT_OPSCODE_TMP_DIR) # cookbook_name -> recipe_file -> line_no -> resource_code result = {} scanned_files = 0 Dir.entries(cookbook_dir).each do |cookbook| if cookbook.match(/^[a-zA-Z0-9_\-]+$/)