Class: Toaster::ChefUtil
- Inherits:
-
Object
- Object
- Toaster::ChefUtil
- Defined in:
- lib/toaster/chef/chef_util.rb
Constant Summary collapse
- OPSCODE_API_URL =
OPSCODE_API_URL = “cookbooks.opscode.com/api/v1/”
"https://supermarket.getchef.com/api/v1/"
- OPSCODE_SEARCH_URL =
"http://community.opscode.com/search"
- @@chef_classes =
{}
- @@create_backups =
false
- @@DEFAULT_CHEF_DIR =
"/tmp/toaster_cookbooks/"
- @@DEFAULT_COOKBOOKS_DIR =
"#{@@DEFAULT_CHEF_DIR}/cookbooks/"
- @@DEFAULT_OPSCODE_TMP_DIR =
TODO merge with above?
"/tmp/opscode_cookbooks/"
Class Method Summary collapse
- .available_cookbook_versions(cookbook_name) ⇒ Object
- .available_cookbooks_from_opscode(query_details = false, overwrite_downloads = false) ⇒ Object
- .available_recipes_for_cookbook(cookbook_name, cookbooks_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .available_recipes_from_opscode(cookbook_name, version = "latest", overwrite_downloads = false, target_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
- .create_chef_config(solo_file) ⇒ Object
- .DEFAULT_CHEF_DIR ⇒ Object
- .DEFAULT_COOKBOOKS_DIR ⇒ Object
- .DEFAULT_OPSCODE_TMP_DIR ⇒ Object
- .diff_cookbook_versions(cookb, v1, v2) ⇒ Object
- .download_all_from_opscode(target_dir = @@DEFAULT_OPSCODE_TMP_DIR, overwrite_downloads = false) ⇒ Object
-
.download_cookbook_url(link, target_dir = @@DEFAULT_COOKBOOKS_DIR, quiet = false, num_attempts = 2) ⇒ Object
def self.download_cookbook_version1(name, version=“latest”, target_dir=@@DEFAULT_COOKBOOKS_DIR, quiet=false, num_attempts=2).
- .download_cookbook_url_in_lxc(lxc, link, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .download_cookbook_version(name, version = "latest", target_dir = @@DEFAULT_COOKBOOKS_DIR, quiet = false, num_attempts = 2) ⇒ Object
- .download_cookbook_version_in_lxc(lxc, name, version = "latest", target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .download_dependencies(cookbook, recipe, cookbook_dir = @@DEFAULT_COOKBOOKS_DIR, overwrite_all = false, downloaded_so_far = [], parsed_so_far = []) ⇒ Object
- .download_latest_cookbook(name, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .download_latest_cookbook_in_lxc(lxc, name, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .extract_node_name(name) ⇒ Object
- .fetch_cookbook_details(cookbook_name, cbook = {}, resources = {}, overwrite_downloads = false) ⇒ Object
- .fix_encodings(target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .fix_known_bugs_in_recipes(cookbook_dir = @@DEFAULT_COOKBOOKS_DIR, phase = "after") ⇒ Object
- .get_absolute_file_path(cookbook_paths, relative_file_path) ⇒ Object
- .get_attribute_file(recipe_file) ⇒ Object
- .get_chef_log_level ⇒ Object
- .get_cookbook_download_link(name, version = "latest") ⇒ Object
- .get_reduced_run_list(run_list) ⇒ Object
- .get_sourcefile(start_source_line) ⇒ Object
- .get_sourceline(start_source_line) ⇒ Object
- .guess_cookbook_from_runlist(runlist) ⇒ Object
- .guess_cookbook_or_recipe_from_runlist(runlist, type) ⇒ Object
- .guess_recipe_from_runlist(runlist) ⇒ Object
- .lxc_cookbook_dir(lxc, relative_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
- .parse_all_resources(recipe_pattern = "default.rb", cookbook_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
- .parse_resources(cookbook, recipe_name, version = "latest", result = {}, cookbook_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
- .prepare_run_list(chef_node, run_list_hash_OR_chef_node_file) ⇒ Object
- .read_sourcecode(start_source_line) ⇒ Object
- .read_sourcecode_from_line(file, line) ⇒ Object
- .run_chef(chef_solo_file, node_file, print_output = true) ⇒ Object
- .runtime_resource_sourcecode(resource) ⇒ Object
- .set_chef_log_level(level) ⇒ Object
- .wrap_node_name(node_name) ⇒ Object
Class Method Details
.available_cookbook_versions(cookbook_name) ⇒ Object
763 764 765 |
# File 'lib/toaster/chef/chef_util.rb', line 763 def self.available_cookbook_versions(cookbook_name) fetch_cookbook_details(cookbook_name)["_versions"].uniq end |
.available_cookbooks_from_opscode(query_details = false, overwrite_downloads = false) ⇒ Object
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/toaster/chef/chef_util.rb', line 629 def self.available_cookbooks_from_opscode(query_details=false, overwrite_downloads=false) result = [] tmpmap = {} max = 1000 start = 0 while start < max url = "#{OPSCODE_API_URL}cookbooks?start=#{start}&items=100" json = `curl '#{url}' 2> /dev/null` books = MarkupUtil.parse_json(json.strip) if books["items"].kind_of?(Array) result.concat(books["items"]) books["items"].each do |item| tmpmap[item["cookbook_name"]] = item end end max = books["total"].to_i if books["total"] && books["total"].to_i > 0 && books["total"].to_i < 2000 start += 100 end if query_details # download all (!) cookbooks from opscode download_all_from_opscode(nil, overwrite_downloads) # get information about all resources resources = parse_all_resources("*.rb") begin html = `curl '#{OPSCODE_SEARCH_URL}?page=1' 2> /dev/null` doc = Toaster::MarkupUtil.parse_xml(html, true) num_pages = REXML::XPath.match(doc, "//a[text()='Next']/preceding-sibling::a[1]/text()") num_pages = "#{num_pages[0]}".to_i (2..num_pages).each do |page_num| REXML::XPath.match(doc, "//ul[@class='cookbook-list']/li").each do |cbxml| cbxml = REXML::Document.new cbxml.to_s title = REXML::XPath.match(cbxml, "//a[@class='title']/text()")[0] title = "#{title}".strip if tmpmap[title] = REXML::XPath.match(cbxml, "//span[@class='count']/text()")[0] downloads = REXML::XPath.match(cbxml, "//div[@class='downloads']/text()")[0] followers = REXML::XPath.match(cbxml, "//div[@class='followers']/text()")[0] platforms = REXML::XPath.match(cbxml, "//div[@class='platforms']/p/text()")[0] category = REXML::XPath.match(cbxml, "//div[@class='category']/a/text()")[0] = "#{}".gsub(/(\\n)|\s*/, "").gsub(/rating(s)*/, "").strip.to_i downloads = "#{downloads}".gsub(/(\\n)|\s*/, "").gsub(/download(s)*/, "").strip.to_i followers = "#{followers}".gsub(/(\\n)|\s*/, "").gsub(/follower(s)*/, "").strip.to_i platforms = "#{platforms}".gsub(/\\n/, "").gsub(/Platforms:/, "").gsub(">", ">").strip category = "#{category}".gsub(/\\n/, "").gsub("&", "&").strip #puts "ratings #{title}: #{ratings} - #{downloads} - #{followers} - #{platforms} - #{category}" tmpmap[title]["_ratings"] = tmpmap[title]["_downloads"] = downloads tmpmap[title]["_followers"] = followers tmpmap[title]["_platforms"] = platforms tmpmap[title]["_category"] = category end end html = `curl '#{OPSCODE_SEARCH_URL}?page=#{page_num}' 2> /dev/null` doc = Toaster::MarkupUtil.parse_xml(html, true) end result.each do |cbook| fetch_cookbook_details(cbook["cookbook_name"], cbook, resources) end rescue Object => exc puts "WARN: Exception when querying cookbook details from #{OPSCODE_SEARCH_URL}?page=1: #{exc} - #{exc.backtrace}" end # sort results result.sort! { |a,b| a["_downloads"] = 0 if !a["_downloads"] b["_downloads"] = 0 if !b["_downloads"] a["_average_rating"] = 0 if !a["_average_rating"] b["_average_rating"] = 0 if !b["_average_rating"] (b["_downloads"] <=> a["_downloads"]) != 0 ? b["_downloads"] <=> a["_downloads"] : b["_average_rating"] <=> a["_average_rating"] } result.each_with_index do |cb,idx| cb["_position"] = idx + 1 end end return result end |
.available_recipes_for_cookbook(cookbook_name, cookbooks_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
593 594 595 596 597 598 599 600 601 602 |
# File 'lib/toaster/chef/chef_util.rb', line 593 def self.available_recipes_for_cookbook(cookbook_name, cookbooks_dir=@@DEFAULT_COOKBOOKS_DIR) result = [] out = `ls #{cookbooks_dir}/#{cookbook_name}/recipes` out = out.strip.split(/\s+/) out.each do |file| recipe = file.sub(/(.*)\.rb/, '\1') result << { "recipe_name" => recipe } end return result end |
.available_recipes_from_opscode(cookbook_name, version = "latest", overwrite_downloads = false, target_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/toaster/chef/chef_util.rb', line 604 def self.available_recipes_from_opscode(cookbook_name, version="latest", 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 FileUtils.rm_rf(cookbook_folder) end if !File.directory?(cookbook_folder) download_cookbook_version(cookbook_name, version, target_dir, true) end return available_recipes_for_cookbook(cookbook_name, target_dir) else Util.mktmpdir() { |file| download_cookbook_version(cookbook_name, version, file, true) return available_recipes_for_cookbook(cookbook_name, file) } end end |
.create_chef_config(solo_file) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/toaster/chef/chef_util.rb', line 169 def self.create_chef_config(solo_file) root_dir = Util.project_root_dir() puts "DEBUG: Writing Chef configuration (cookbook_path) to '#{solo_file}'" Util.write(solo_file, (<<-EOF cookbook_path [ "#{File.(File.join(Dir.pwd, "cookbooks"))}", "#{@@DEFAULT_COOKBOOKS_DIR}", "#{File.join(root_dir, "chef", "cookbooks")}" ] EOF ), true) end |
.DEFAULT_CHEF_DIR ⇒ Object
48 49 50 |
# File 'lib/toaster/chef/chef_util.rb', line 48 def self.DEFAULT_CHEF_DIR @@DEFAULT_CHEF_DIR end |
.DEFAULT_COOKBOOKS_DIR ⇒ Object
51 52 53 |
# File 'lib/toaster/chef/chef_util.rb', line 51 def self.DEFAULT_COOKBOOKS_DIR @@DEFAULT_COOKBOOKS_DIR end |
.DEFAULT_OPSCODE_TMP_DIR ⇒ Object
54 55 56 |
# File 'lib/toaster/chef/chef_util.rb', line 54 def self.DEFAULT_OPSCODE_TMP_DIR @@DEFAULT_OPSCODE_TMP_DIR end |
.diff_cookbook_versions(cookb, v1, v2) ⇒ Object
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/toaster/chef/chef_util.rb', line 861 def self.diff_cookbook_versions(cookb, v1, v2) v1.gsub!(".", "_") v2.gsub!(".", "_") target_dir = "/tmp/toaster/tmp_chef_cb/#{cookb}" dir1 = "#{target_dir}/#{v1}" dir2 = "#{target_dir}/#{v2}" `mkdir -p '#{target_dir}'` if !File.directory?(dir1) `mkdir -p '#{dir1}'` download_cookbook_version(cookb, v1, dir1) end if !File.directory?(dir2) `mkdir -p '#{dir2}'` download_cookbook_version(cookb, v2, dir2) end return Util.diff_dirs(dir1, dir2) end |
.download_all_from_opscode(target_dir = @@DEFAULT_OPSCODE_TMP_DIR, overwrite_downloads = false) ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 |
# File 'lib/toaster/chef/chef_util.rb', line 767 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| cookbook_name = cb["cookbook_name"] #puts "downloading cookbook #{cookbook_name} to #{target_dir}" download_cookbook_version(cookbook_name, "latest", target_dir, true) end end end |
.download_cookbook_url(link, target_dir = @@DEFAULT_COOKBOOKS_DIR, quiet = false, num_attempts = 2) ⇒ Object
def self.download_cookbook_version1(name, version=“latest”,
target_dir=@@DEFAULT_COOKBOOKS_DIR, quiet=false, num_attempts=2)
link = get_cookbook_download_link(name, version)
`mkdir -p '#{target_dir}'` if !File.exist?(target_dir)
tgz_file = File.join(target_dir, "#{name}.tgz")
tar_file = File.join(target_dir, "#{name}.tar")
File.delete(tar_file) if File.exist?(tar_file)
cookbook_dir = File.join(target_dir, name)
if @@create_backups
if File.exist?(cookbook_dir) && !File.exist?("#{cookbook_dir}.bak")
`mv #{cookbook_dir} #{cookbook_dir}.bak`
end
end
while num_attempts > 0
`rm -rf #{cookbook_dir}`
if !quiet
puts "DEBUG: Downloading '#{link}' to #{target_dir}"
end
error = false
`wget #{link} -O #{tgz_file} > /dev/null 2>&1`
error ||= $?.exitstatus != 0
out = `cd #{target_dir} && tar zxf #{name}.tgz`
# tar reports status code 2 in case of error...
if $?.exitstatus > 1
# sometimes, the files are in tar format,
# not in tgz format - let's give it a try!
puts "DEBUG: 'cd #{target_dir} && tar zxf #{name}.tgz' returned exit code #{$?.exitstatus}, trying to extract as tar file..."
full_file = "#{target_dir}/#{name}.tgz"
puts "DEBUG: File #{full_file} exists: #{File.exist?(full_file)}}"
out += `cd #{target_dir} && cp #{name}.tgz #{name}.tar`
out += `cd #{target_dir} && tar xf #{name}.tar`
end
error ||= $?.exitstatus > 1
break if !error
puts "WARN: Could not download/extract #{link} to #{target_dir} . Remaining attempts: #{num_attempts}"
num_attempts -= 1
sleep 2
end
return out
end
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/toaster/chef/chef_util.rb', line 299 def self.download_cookbook_url(link, target_dir=@@DEFAULT_COOKBOOKS_DIR, quiet=false, num_attempts=2) out = "" if !File.exist?(target_dir) FileUtils.mkpath(target_dir) end cookbooks_before = Dir.entries(target_dir) Toaster::Util.mktmpfile() { |tgz_file| while num_attempts > 0 if !quiet puts "DEBUG: Downloading '#{link}' to #{target_dir}" end error = false `wget #{link} -O #{tgz_file} > /dev/null 2>&1` error ||= $?.exitstatus != 0 cmd = "cd #{target_dir} && tar -z -x -f #{tgz_file}" out = `#{cmd}` # tar reports status code 2 in case of error... if $?.exitstatus > 1 # sometimes, the files are in tar format, # not in tgz format - let's give it a try! puts "DEBUG: '#{cmd}' returned exit code #{$?.exitstatus}, trying to extract as tar file..." #full_file = "#{target_dir}/#{name}.tgz" #puts "DEBUG: File #{full_file} exists: #{File.exist?(full_file)}}" #out += `cd #{target_dir} && cp #{name}.tgz #{name}.tar` out += `cd #{target_dir} && tar -x -f #{tgz_file}` end error ||= $?.exitstatus > 1 break if !error puts "WARN: Could not download/extract #{link} to #{target_dir} . Remaining attempts: #{num_attempts}" num_attempts -= 1 sleep 2 end } cookbooks_after = Dir.entries(target_dir) new_cb = cookbooks_after - cookbooks_before 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 return new_cb end |
.download_cookbook_url_in_lxc(lxc, link, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
352 353 354 355 |
# File 'lib/toaster/chef/chef_util.rb', line 352 def self.download_cookbook_url_in_lxc(lxc, link, target_dir=@@DEFAULT_COOKBOOKS_DIR) cookbook_dir = "#{lxc['rootdir']}/#{target_dir}" download_cookbook_url(link, cookbook_dir) end |
.download_cookbook_version(name, version = "latest", target_dir = @@DEFAULT_COOKBOOKS_DIR, quiet = false, num_attempts = 2) ⇒ Object
248 249 250 251 252 |
# File 'lib/toaster/chef/chef_util.rb', line 248 def self.download_cookbook_version(name, version="latest", target_dir=@@DEFAULT_COOKBOOKS_DIR, quiet=false, num_attempts=2) link = get_cookbook_download_link(name, version) download_cookbook_url(link, target_dir, quiet, num_attempts) end |
.download_cookbook_version_in_lxc(lxc, name, version = "latest", target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
357 358 359 360 |
# File 'lib/toaster/chef/chef_util.rb', line 357 def self.download_cookbook_version_in_lxc(lxc, name, version="latest", target_dir=@@DEFAULT_COOKBOOKS_DIR) cookbook_dir = "#{lxc['rootdir']}/#{target_dir}" download_cookbook_version(name, version, cookbook_dir) end |
.download_dependencies(cookbook, recipe, cookbook_dir = @@DEFAULT_COOKBOOKS_DIR, overwrite_all = false, downloaded_so_far = [], parsed_so_far = []) ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/toaster/chef/chef_util.rb', line 503 def self.download_dependencies(cookbook, recipe, cookbook_dir=@@DEFAULT_COOKBOOKS_DIR, overwrite_all=false, downloaded_so_far=[], parsed_so_far=[]) includes = [] if recipe include_commands = "((include_recipe)|(include_attribute)|(include_library))" recipe_files = [] recipe = recipe.split("::")[1] if recipe.include?("::") recipe_files << File.join(cookbook_dir, cookbook, "recipes", "#{recipe}.rb") recipe_files.each do |recipe_file| if !File.exist?(recipe_file) puts "WARN: Expected recipe file does not exist: #{recipe_file}" return end file = File.read(recipe_file) parsed_so_far << "#{cookbook}::#{recipe}" file.scan(/(#{include_commands}[\s'"]+.*)$/) { |rec,total_match| tmp_includes = [] # use eval(..) to handle cases like the following: # include_recipe %w{apache2 apache2::mod_php5} # include_recipe "apache2" # include_recipe ["apache2", "apache2::mod_php5"] rec.sub!(/#{include_commands}/, "tmp_includes=") begin eval(rec) rescue => ex puts "WARN: Unable to evaluate expression while parsing Chef recipe dependencies in file #{recipe_file}: '#{rec}': #{ex}" # this could be a case like the following: # include_recipe "nodejs::install_from_#{node['nodejs']['install_method']}" rec.scan(/["'](.*)::([^"']*)["']/) { |cb,rc| if rc.match(/^[a-z_\-A-Z0-9\.]+$/) tmp_includes << "#{cb}::#{rc}" else available_recipes_for_cookbook(cb, cookbook_dir).each do |rec_hash| tmp_includes << "#{cb}::#{rec_hash['recipe_name']}" end end } end tmp_includes = [tmp_includes] if !tmp_includes.kind_of?(Array) includes.concat(tmp_includes) } end end # additionally, load dependencies from metadata files = File.join(cookbook_dir, cookbook, "metadata.json") if File.exist?() #puts "DEBUG: Reading dependencies from '#{metadata_json}'" json = MarkupUtil.parse_json(File.read()) if json["dependencies"] json["dependencies"].each do |dep_cb,dep_version| includes << dep_cb if !includes.include?(dep_cb) end end end = File.join(cookbook_dir, cookbook, "metadata.rb") #puts "DEBUG: Reading dependencies from '#{metadata_rb}'" if File.exist?() file = File.read() file.scan(/depends\s+["']([^"']+)["']/) { |dep_cb,total_string| includes << dep_cb if !includes.include?(dep_cb) } end puts "INFO: Recipe #{cookbook}::#{recipe} depends on: #{includes.inspect}" if !includes.empty? includes.each do |i| dep_recipe = i.include?("::") ? i.split("::")[1] : "default" dep_book = i.include?("::") ? i.split("::")[0] : i dep_file = File.join(cookbook_dir, dep_book, "recipes", "#{dep_recipe}.rb") if parsed_so_far.include?("#{dep_book}::#{dep_recipe}") || downloaded_so_far.include?(dep_book) #puts "INFO: possible loop detected in cookbook dependencies for #{cookbook}::#{dep_recipe}: #{dep_book.inspect}. Stopping recursive lookup at this point." else downloaded_so_far << dep_book if !File.exist?(dep_file) || overwrite_all download_latest_cookbook(dep_book, cookbook_dir) end # recursively download cookbook dependencies #puts "DEBUG: Recursively downloading dependencies for '#{dep_book}::#{dep_recipe}'" download_dependencies(dep_book, dep_recipe, cookbook_dir, overwrite_all, downloaded_so_far, parsed_so_far) end end end |
.download_latest_cookbook(name, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
362 363 364 |
# File 'lib/toaster/chef/chef_util.rb', line 362 def self.download_latest_cookbook(name, target_dir=@@DEFAULT_COOKBOOKS_DIR) download_cookbook_version(name, "latest", target_dir) end |
.download_latest_cookbook_in_lxc(lxc, name, target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
366 367 368 |
# File 'lib/toaster/chef/chef_util.rb', line 366 def self.download_latest_cookbook_in_lxc(lxc, name, target_dir=@@DEFAULT_COOKBOOKS_DIR) download_cookbook_version_in_lxc(lxc, name, "latest", target_dir) end |
.extract_node_name(name) ⇒ Object
83 84 85 86 87 |
# File 'lib/toaster/chef/chef_util.rb', line 83 def self.extract_node_name(name) short_name = name short_name = short_name.gsub(/.*node\[(.+)\]/, '\1') if short_name.include?("node[") return short_name end |
.fetch_cookbook_details(cookbook_name, cbook = {}, resources = {}, overwrite_downloads = false) ⇒ Object
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/toaster/chef/chef_util.rb', line 709 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') end cbook["_versions"] = versions if !details["average_rating"].nil? cbook["_average_rating"] = details["average_rating"] end # load all recipe names for this cookbook if !cbook["_recipes"] cb_name = cookbook_name recipes = Toaster::ChefUtil.available_recipes_from_opscode( cb_name, cbook["_latest_version"], overwrite_downloads) cbook["_recipes"] = {} if !cbook["_recipes"] recipe_names = [] recipes.each do |r| rec_name = r["recipe_name"] recipe_names << rec_name cbook["_recipes"][rec_name] = {} cbook["_recipes"][rec_name]["_resource_names"] = [] if resources[cb_name] && resources[cb_name][rec_name] if resources[cb_name][rec_name]["_possibly_non_idempotent"] cbook["_possibly_non_idempotent"] = true end resources[cb_name][rec_name]["resources"].each do |line,code| if code code = code.split("\n")[0] resrc_name = code.gsub(/^\s*([a-zA-Z0-9_\-]*)\s+(.*)\s+((do)|\{).*$/, '\1[\2]') resrc_name = resrc_name.strip #puts "TRACE: resource name: #{resrc_name}" cbook["_recipes"][rec_name]["_resource_names"] << resrc_name end end else puts "WARN: Did not find detailed Chef resource information for '#{cb_name}'::'#{rec_name}'" end end cbook["_recipe_names"] = recipe_names end return cbook end |
.fix_encodings(target_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
346 347 348 349 350 |
# File 'lib/toaster/chef/chef_util.rb', line 346 def self.fix_encodings(target_dir=@@DEFAULT_COOKBOOKS_DIR) # add 'encoding' headers to avoid "invalid multibyte char" errors out1 = `cd #{target_dir} && magic_encoding` puts "DEBUG: magic_encoding: #{out1}" end |
.fix_known_bugs_in_recipes(cookbook_dir = @@DEFAULT_COOKBOOKS_DIR, phase = "after") ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/toaster/chef/chef_util.rb', line 374 def self.fix_known_bugs_in_recipes(cookbook_dir=@@DEFAULT_COOKBOOKS_DIR, phase="after") if phase == "before" puts "DEBUG: Cleaning up cookbooks directory before downloading.." # it seems that there is an issue with hadoop_cluster/attributes/default.rb if File.exist?("#{cookbook_dir}/hadoop_cluster") puts "DEBUG: Removing cookbook directory #{cookbook_dir}/hadoop_cluster (bug in attributes)" `rm -rf "#{cookbook_dir}/hadoop_cluster"` end elsif phase == "after" puts "DEBUG: Fixing some known bugs in existing recipes.." # get rid of the col on at the end of this line: # "if (not new_resource.dependency.empty?):" if File.exist?("#{cookbook_dir}/node/providers/nodejs.rb") `sed -i "s/empty?):/empty?)/" #{cookbook_dir}/node/providers/nodejs.rb` end # recipe php::php5 doesn't exist anymore (bug in recipe cakephp::default) if File.exist?("#{cookbook_dir}/cakephp/recipes/default.rb") `sed -i 's/include_recipe .*php5.*$/include_recipe %w{php::default php::module_mysql}/' #{cookbook_dir}/cakephp/recipes/default.rb` end # cookbook "recognizer" uses old Chef syntax, incompatible with new Chef versions if File.exist?("#{cookbook_dir}/recognizer/libraries/json_file.rb") `sed -i 's/attribute :content, :kind_of => Hash/state_attrs :content/' #{cookbook_dir}/recognizer/libraries/json_file.rb` end # cookbook "netkernel" uses invalid syntax file = "#{cookbook_dir}/netkernel/recipes/default.rb" if File.exist?(file) `sed -i 's/source defaults.erb/source "defaults.erb"/' #{file}` end # cookbook "recognizer" uses invalid syntax file = "#{cookbook_dir}/recognizer/recipes/default.rb" if File.exist?(file) `sed -i 's/content recognizer_config/content recognizer_config.to_s/' #{file}` end # cookbook "bacula" uses invalid syntax file = "#{cookbook_dir}/bacula/recipes/bat.rb" if File.exist?(file) `sed -i 's/=> n\\[/=> node[/' #{file}` end # cookbook "openvpn" uses invalid syntax file = "#{cookbook_dir}/openvpn/recipes/default.rb" if File.exist?(file) `sed -i 's/routes.flatten\!/routes.dup.flatten\!/' #{file}` end # cookbook "bacula" uses invalid syntax file = "#{cookbook_dir}/sanitize/recipes/default.rb" if File.exist?(file) `sed -i 's/node\\[\\'build_essential\\'][\\'compiletime\\'] = /node.set\\[\\'build_essential\\'][\\'compiletime\\'] = /' #{file}` end # cookbook "riak" uses invalid attribute syntax files = ["#{cookbook_dir}/riak/attributes/kv.rb", "#{cookbook_dir}/riak/attributes/sasl.rb", "#{cookbook_dir}/riak/attributes/bitcask.rb"] files.each do |file| if File.exist?(file) `sed -i 's/node.riak.kv.storage_backend = /set["riak"]["kv"]["storage_backend"] = /' #{file}` `sed -i 's/node.riak.sasl.errlog_type = /set["riak"]["sasl"]["errlog_type"] = /' #{file}` `sed -i 's/node.riak.bitcask.sync_strategy = /set["riak"]["bitcask"]["sync_strategy"] = /' #{file}` end end # openldap/attributes/default.rb attempts to access node['domain'] which is nil file = "#{cookbook_dir}/openldap/attributes/default.rb" if File.exist?(file) if !File.read(file).match("file_patched_for_toaster_testing") `sed -i "1idefault['domain']='' if !node['domain']" "#{file}"` `sed -i "2i#file_patched_for_toaster_testing" "#{file}"` end end # fix new syntax of resource notifications files = ["apache2/definitions/apache_site.rb", "tomcat6/recipes/default.rb"] files.each do |file| file = "#{cookbook_dir}/#{file}" if File.exist?(file) `sed -i 's/resources(:\\([a-z_]*\\) => "\\([^"]*\\)")/"\\1[\\2]"/g' "#{file}"` end end # new attribute access in Chef 11 file = "#{cookbook_dir}/kafka/recipes/default.rb" if File.exist?(file) `sed -i "s/node\\\\[:kafka\\\\]\\\\[:broker_host_name\\\\] =/node.set[:kafka][:broker_host_name] =/" "#{file}"` `sed -i "s/node\\\\[:kafka\\\\]\\\\[:broker_id\\\\] =/node.set[:kafka][:broker_id] =/" "#{file}"` end file = "#{cookbook_dir}/network_interfaces/recipes/default.rb" if File.exist?(file) `sed -i 's/node\\["network_interfaces"\\]\\["order"\\]=/node.set["network_interfaces"]["order"]=/' "#{file}"` end # add cookbook dependencies dependencies = { "vmware" => ["apt"], "maven" => ["ark"], "sonar" => ["ark", "maven"], "eaccelerator" => ["apache2"], "netkernel" => ["apache"], "chef-client-cron" => ["chef-client"] } dependencies.each do |cb,deps| deps.each do |dep| # add dependency to cookbook metadata file1 = "#{cookbook_dir}/#{dep}/" file2 = "#{cookbook_dir}/#{cb}/metadata.rb" if File.exist?(file1) && File.exist?(file2) if !File.read(file2).match(/depends\s+["']#{dep}["']/) `echo '' >> "#{file2}"` `echo 'depends "#{dep}"' >> "#{file2}"` end end end end # fix encodings in cookbook files fix_encodings(cookbook_dir) end end |
.get_absolute_file_path(cookbook_paths, relative_file_path) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/toaster/chef/chef_util.rb', line 96 def self.get_absolute_file_path(cookbook_paths, relative_file_path) cookbook_paths.each do |path| f_path = File.join(path, relative_file_path) return f_path if File.exist?(f_path) end return nil end |
.get_attribute_file(recipe_file) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/toaster/chef/chef_util.rb', line 104 def self.get_attribute_file(recipe_file) attr_dir = File.join(File.dirname(recipe_file), "..", "attributes") recipe_name = recipe_file.gsub(/.*\/([^\/]+)\.rb$/, '\1') dflt_attr_file = File.join(attr_dir, "#{recipe_name}.rb") return dflt_attr_file if File.exist?(dflt_attr_file) return nil if !File.exist?(attr_dir) content = Dir.entries(attr_dir) content.each do |f| if f.to_s.match(/.*\.rb$/) return File.join(attr_dir, f) end end return nil end |
.get_chef_log_level ⇒ Object
89 90 91 |
# File 'lib/toaster/chef/chef_util.rb', line 89 def self.get_chef_log_level() return Chef::Log.level end |
.get_cookbook_download_link(name, version = "latest") ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/toaster/chef/chef_util.rb', line 236 def self.get_cookbook_download_link(name, version="latest") url = "#{OPSCODE_API_URL}cookbooks/#{name}/versions/#{version}" puts "DEBUG: Getting Chef cookbook metadata from URL: '#{url}'" json = `curl #{url} 2> /dev/null` begin json = MarkupUtil.parse_json(json) rescue => ex raise "Unable to parse string as JSON (received from URL #{url}): #{json}" end return json["file"] end |
.get_reduced_run_list(run_list) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/toaster/chef/chef_util.rb', line 123 def self.get_reduced_run_list(run_list) reduced_run_list = run_list.select{ |i| !"#{i}".match(/toaster::testing/) && !"#{i}".match(/chef-solo-search::default/) } return reduced_run_list end |
.get_sourcefile(start_source_line) ⇒ Object
226 227 228 229 |
# File 'lib/toaster/chef/chef_util.rb', line 226 def self.get_sourcefile(start_source_line) l = start_source_line.split(":") return l[0] end |
.get_sourceline(start_source_line) ⇒ Object
231 232 233 234 |
# File 'lib/toaster/chef/chef_util.rb', line 231 def self.get_sourceline(start_source_line) l = start_source_line.split(":") return l[1].to_i end |
.guess_cookbook_from_runlist(runlist) ⇒ Object
58 59 60 |
# File 'lib/toaster/chef/chef_util.rb', line 58 def self.guess_cookbook_from_runlist(runlist) guess_cookbook_or_recipe_from_runlist(runlist, "cookbook") end |
.guess_cookbook_or_recipe_from_runlist(runlist, type) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/toaster/chef/chef_util.rb', line 65 def self.guess_cookbook_or_recipe_from_runlist(runlist, type) candidates = [] runlist.each do |i| if !i.match(/toaster::testing/) if i.include?("recipe[") if type == "cookbook" candidates << i.gsub(/recipe\[(.*::)*([^\]]+)\]/, '\1').gsub(/::/, '') elsif type == "recipe" candidates << i.gsub(/recipe\[(.*::)*([^\]]+)\]/, '\2') end end end end return "default" if candidates.empty? puts "WARN: Multiple #{type}s found in runlist #{runlist}: #{candidates}" if candidates.size > 1 return candidates[0] end |
.guess_recipe_from_runlist(runlist) ⇒ Object
61 62 63 |
# File 'lib/toaster/chef/chef_util.rb', line 61 def self.guess_recipe_from_runlist(runlist) guess_cookbook_or_recipe_from_runlist(runlist, "recipe") end |
.lxc_cookbook_dir(lxc, relative_dir = @@DEFAULT_COOKBOOKS_DIR) ⇒ Object
370 371 372 |
# File 'lib/toaster/chef/chef_util.rb', line 370 def self.lxc_cookbook_dir(lxc, relative_dir=@@DEFAULT_COOKBOOKS_DIR) return "#{lxc['rootdir']}/#{relative_dir}" end |
.parse_all_resources(recipe_pattern = "default.rb", cookbook_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
# File 'lib/toaster/chef/chef_util.rb', line 843 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_\-]+$/) recipe_dir = "#{cookbook_dir}/#{cookbook}/recipes" Dir["#{recipe_dir}/#{recipe_pattern}"].each do |recipe_file| recipe_name = recipe_file.gsub(/.*\/([^\/]+)\.rb$/, '\1') parse_resources(cookbook, recipe_name, "latest", result, cookbook_dir) scanned_files += 1 end end end puts "INFO: Scanned #{scanned_files} files for resource information." return result end |
.parse_resources(cookbook, recipe_name, version = "latest", result = {}, cookbook_dir = @@DEFAULT_OPSCODE_TMP_DIR) ⇒ Object
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'lib/toaster/chef/chef_util.rb', line 780 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) : "" if !File.exist?(recipe_file) download_cookbook_version(cookbook, version, cookbook_dir) end puts "TRACE: Scanning recipe file: #{recipe_file}" resource_lines = [] script_resource_lines = [] result[cookbook] = {} if !result[cookbook] result[cookbook][recipe_name] = {} if !result[cookbook][recipe_name] result[cookbook][recipe_name]["file"] = recipe_file_relative result[cookbook][recipe_name]["resources"] = {} if !result[cookbook][recipe_name]["resources"] result[cookbook][recipe_name]["resource_objs"] = {} if !result[cookbook][recipe_name]["resource_objs"] # resource names taken from # http://docs.opscode.com/resource.html # http://wiki.opscode.com/display/chef/Resources resource_names = ["apt_package", "bash", "chef_gem", "csh", "cron", "deploy", "directory", "dpkg_package", "easy_install_package", "env", "erlang_call", "erl_call", "execute", "file", "freebsd_package", "gem_package", "git", "group", "http_request", "ifconfig", "link", "log", "macports_package", "mdadm", "mount", "ohai", "package", "perl", "portage_package", "powershell_script", "python", "remote_directory", "remote_file", "route", "rpm_package", "ruby_block", "ruby", "scm", "script", "service", "solaris_package", "template", "user", "yum_package", "zypper_package", "mysql_service" # additional resource types ] script_resource_names = ["execute", "bash", "script", "ruby_block", "csh"] File.open(recipe_file) do |io| io.each_with_index { |line,idx| 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) if !code 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*/) resource_type = code.split("\n")[0].gsub(/^\s*([a-zA-Z0-9_\-]*)\s+(.*)\s+((do)|\{).*$/, '\1') if script_resource_names.include?(resource_type) result[cookbook][recipe_name]["_possibly_non_idempotent"] = true end end end end end return result end |
.prepare_run_list(chef_node, run_list_hash_OR_chef_node_file) ⇒ Object
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 |
# File 'lib/toaster/chef/chef_util.rb', line 131 def self.prepare_run_list(chef_node, run_list_hash_OR_chef_node_file) run_list = [] #puts "run_list_hash_OR_chef_node_file - #{run_list_hash_OR_chef_node_file} - #{run_list_hash_OR_chef_node_file.class}" if !run_list_hash_OR_chef_node_file || (run_list_hash_OR_chef_node_file.kind_of?(Array) && run_list_hash_OR_chef_node_file.empty?) run_list = ["recipe[#{chef_node}::default]"] elsif run_list_hash_OR_chef_node_file.kind_of?(Array) run_list = run_list_hash_OR_chef_node_file else node_file = run_list_hash_OR_chef_node_file if File.exist?(node_file) run_list = JSON.parse(File.read(node_file))["run_list"] else run_list = [node_file] end end run_list = [run_list] if !run_list.kind_of?(Array) # append toaster::testing recipe to the beginning of the # run list to enable the testing mechanism! if run_list[0] != "recipe[toaster::testing]" # due to recent changes in Chef 11, we also need to add # chef-solo-search::default into the run_list, in order # to load the libraries defined there (we depend on # a library function that overwrites the search(...) # function). More info concerning this Chef 11 change: # http://docs.opscode.com/breaking_changes_chef_11.html#non-recipe-file-evaluation-includes-dependencies run_list.unshift("recipe[chef-solo-search::default]") # ... and now add toaster::testing to the beginning run_list.unshift("recipe[toaster::testing]") end # ... and append toaster::testing_post to the end of the run list if run_list[-1] != "recipe[toaster::testing_post]" run_list << "recipe[toaster::testing_post]" end return run_list end |
.read_sourcecode(start_source_line) ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/toaster/chef/chef_util.rb', line 191 def self.read_sourcecode(start_source_line) # start_source_line looks like the following example: # /home/user/dir/file.rb:82:in `from_file' file = get_sourcefile(start_source_line) line = get_sourceline(start_source_line) read_sourcecode_from_line(file, line) end |
.read_sourcecode_from_line(file, line) ⇒ Object
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 |
# File 'lib/toaster/chef/chef_util.rb', line 199 def self.read_sourcecode_from_line(file, line) lines = File.readlines(file) parsed_source = nil last_line = -1 # read the source code file line by line and attempt to parse. for endline in line..lines.size code = lines[(line-1)..(endline-1)].join previous_stderr = $stderr $stderr.sync = true $stderr = StringIO.open('','w') begin parser = RubyParser.new parser.process(code.dup) parsed_source = code last_line = endline break rescue Exception => ex ## swallow ensure $stderr = previous_stderr end end #puts "source code, lines #{line} to #{last_line} of file #{file}:\n #{parsed_source}" return parsed_source end |
.run_chef(chef_solo_file, node_file, print_output = true) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/toaster/chef/chef_util.rb', line 182 def self.run_chef(chef_solo_file, node_file, print_output=true) cmd = "chef-solo -c #{chef_solo_file} -j #{node_file} 2>&1 | grep -v 'FATAL: No cookbook found in'" if print_output system(cmd) else `#{cmd}` end end |
.runtime_resource_sourcecode(resource) ⇒ Object
625 626 627 |
# File 'lib/toaster/chef/chef_util.rb', line 625 def self.runtime_resource_sourcecode(resource) return resource.to_text if resource.respond_to?("to_text") end |
.set_chef_log_level(level) ⇒ Object
92 93 94 |
# File 'lib/toaster/chef/chef_util.rb', line 92 def self.set_chef_log_level(level) Chef::Log.level = level end |
.wrap_node_name(node_name) ⇒ Object
119 120 121 |
# File 'lib/toaster/chef/chef_util.rb', line 119 def self.wrap_node_name(node_name) node_name = "node[#{node_name}]" if !node_name.include?("node[") end |