Class: ChefZero::Endpoints::EnvironmentCookbookVersionsEndpoint
- Inherits:
-
RestBase
- Object
- RestBase
- ChefZero::Endpoints::EnvironmentCookbookVersionsEndpoint
- Defined in:
- lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb
Overview
/environments/NAME/cookbook_versions
Constant Summary
Constants inherited from RestBase
RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION
Instance Attribute Summary
Attributes inherited from RestBase
Instance Method Summary collapse
- #depsolve(request, unsolved, desired_versions, environment_constraints) ⇒ Object
- #filter_by_constraint(versions, cookbook_name, constraint) ⇒ Object
- #post(request) ⇒ Object
- #sort_versions(versions) ⇒ Object
Methods inherited from RestBase
#accepts?, #already_json_response, #build_uri, build_uri, #call, #check_api_version, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #get_data_or_else, #hashify_list, #head_request, #initialize, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, #populate_defaults, rfc2396_parser, #set_data, #text_response, #to_json
Constructor Details
This class inherits a constructor from ChefZero::RestBase
Instance Method Details
#depsolve(request, unsolved, desired_versions, environment_constraints) ⇒ Object
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 |
# File 'lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb', line 59 def depsolve(request, unsolved, desired_versions, environment_constraints) desired_versions.each do |cb, ver| if ver.empty? @last_constraint_failure = cb return nil end end # If everything is already solve_for = unsolved[0] return desired_versions unless solve_for # Go through each desired version of this cookbook, starting with the latest, # until we find one we can solve successfully with sort_versions(desired_versions[solve_for]).each do |desired_version| new_desired_versions = desired_versions.clone new_desired_versions[solve_for] = [ desired_version ] new_unsolved = unsolved[1..-1] # Pick this cookbook, and add dependencies cookbook_obj = FFI_Yajl::Parser.parse(get_data(request, request.rest_path[0..1] + ["cookbooks", solve_for, desired_version])) = cookbook_obj["metadata"] || {} cookbook_dependencies = ["dependencies"] || {} dep_not_found = false cookbook_dependencies.each_pair do |dep_name, dep_constraint| # If the dep is not already in the list, add it to the list to solve # and bring in all environment-allowed cookbook versions to desired_versions unless new_desired_versions.key?(dep_name) new_unsolved += [dep_name] # If the dep is missing, we will try other versions of the cookbook that might not have the bad dep. unless exists_data_dir?(request, request.rest_path[0..1] + ["cookbooks", dep_name]) @last_missing_dep = dep_name.to_s dep_not_found = true break end new_desired_versions[dep_name] = list_data(request, request.rest_path[0..1] + ["cookbooks", dep_name]) new_desired_versions = filter_by_constraint(new_desired_versions, dep_name, environment_constraints[dep_name]) end new_desired_versions = filter_by_constraint(new_desired_versions, dep_name, dep_constraint) end next if dep_not_found # Depsolve children with this desired version! First solution wins. result = depsolve(request, new_unsolved, new_desired_versions, environment_constraints) return result if result end nil end |
#filter_by_constraint(versions, cookbook_name, constraint) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb', line 114 def filter_by_constraint(versions, cookbook_name, constraint) return versions unless constraint constraint = Gem::Requirement.new(constraint) new_versions = versions[cookbook_name] new_versions = new_versions.select { |version| constraint.satisfied_by?(Gem::Version.new(version.dup)) } result = versions.clone result[cookbook_name] = new_versions result end |
#post(request) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb', line 10 def post(request) cookbook_names = list_data(request, request.rest_path[0..1] + ["cookbooks"]) # Get the list of cookbooks and versions desired by the runlist desired_versions = {} run_list = FFI_Yajl::Parser.parse(request.body)["run_list"] run_list.each do |run_list_entry| if run_list_entry =~ /(.+)::.+\@(.+)/ || run_list_entry =~ /(.+)\@(.+)/ raise RestErrorResponse.new(412, "No such cookbook: #{$1}") unless cookbook_names.include?($1) raise RestErrorResponse.new(412, "No such cookbook version for cookbook #{$1}: #{$2}") unless list_data(request, request.rest_path[0..1] + ["cookbooks", $1]).include?($2) desired_versions[$1] = [ $2 ] else desired_cookbook = run_list_entry.split("::")[0] raise RestErrorResponse.new(412, "No such cookbook: #{desired_cookbook}") unless cookbook_names.include?(desired_cookbook) desired_versions[desired_cookbook] = list_data(request, request.rest_path[0..1] + ["cookbooks", desired_cookbook]) end end # Filter by environment constraints environment = FFI_Yajl::Parser.parse(get_data(request, request.rest_path[0..3])) environment_constraints = environment["cookbook_versions"] || {} desired_versions.each_key do |name| desired_versions = filter_by_constraint(desired_versions, name, environment_constraints[name]) end # Depsolve! solved = depsolve(request, desired_versions.keys, desired_versions, environment_constraints) unless solved if @last_missing_dep && !cookbook_names.include?(@last_missing_dep) return raise RestErrorResponse.new(412, "No such cookbook: #{@last_missing_dep}") elsif @last_constraint_failure return raise RestErrorResponse.new(412, "Could not satisfy version constraints for: #{@last_constraint_failure}") else return raise RestErrorResponse.new(412, "Unsolvable versions!") end end result = {} solved.each_pair do |name, versions| cookbook = FFI_Yajl::Parser.parse(get_data(request, request.rest_path[0..1] + ["cookbooks", name, versions[0]])) result[name] = ChefData::DataNormalizer.normalize_cookbook(self, request.rest_path[0..1], cookbook, name, versions[0], request.base_uri, "MIN", false, api_version: request.api_version) end json_response(200, result) end |
#sort_versions(versions) ⇒ Object
109 110 111 112 |
# File 'lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb', line 109 def sort_versions(versions) result = versions.sort_by { |version| Gem::Version.new(version.dup) } result.reverse end |