Class: ChefCLI::PolicyfileCompiler
- Inherits:
-
Object
- Object
- ChefCLI::PolicyfileCompiler
- Extended by:
- Forwardable
- Defined in:
- lib/chef-cli/policyfile_compiler.rb
Constant Summary collapse
- DEFAULT_DEMAND_CONSTRAINT =
">= 0.0.0".freeze
- SOURCE_TYPES_WITH_FIXED_VERSIONS =
Cookbooks from these sources lock that cookbook to exactly one version
i{git path}.freeze
Instance Attribute Summary collapse
-
#dsl ⇒ Object
readonly
Returns the value of attribute dsl.
-
#install_report ⇒ Object
readonly
Returns the value of attribute install_report.
-
#storage_config ⇒ Object
readonly
Returns the value of attribute storage_config.
Class Method Summary collapse
Instance Method Summary collapse
- #all_cookbook_location_specs ⇒ Object
- #artifacts_graph ⇒ Object
- #build {|@dsl| ... } ⇒ Object
- #combined_run_lists ⇒ Object
- #combined_run_lists_by_cb_name ⇒ Object
- #cookbook_location_spec_for(cookbook_name) ⇒ Object
- #cookbook_version_fixed?(cookbook_name) ⇒ Boolean
- #cookbooks_in_run_list ⇒ Object
- #create_spec_for_cookbook(cookbook_name, version) ⇒ Object
- #default_attributes ⇒ Object
- #default_source(source_type = nil, source_argument = nil, &block) ⇒ Object
- #error! ⇒ Object
- #evaluate_policyfile(policyfile_string, policyfile_filename) ⇒ Object
- #expanded_named_run_lists ⇒ Object
- #expanded_run_list ⇒ Object
- #fixed_version_cookbooks_specs ⇒ Object
- #graph ⇒ Object
- #graph_demands ⇒ Object
-
#graph_solution ⇒ Object
Compilation Methods.
-
#initialize(ui: nil, chef_config: nil) ⇒ PolicyfileCompiler
constructor
A new instance of PolicyfileCompiler.
- #install ⇒ Object
-
#local_artifacts_graph ⇒ Object
Gives a dependency graph for cookbooks that are source from an alternate location.
- #lock ⇒ Object
- #normalized_named_run_lists ⇒ Object
-
#normalized_run_list ⇒ Object
copy of the expanded_run_list, properly formatted for use in a lockfile.
- #override_attributes ⇒ Object
- #remote_artifacts_graph ⇒ Object
- #solution_dependencies ⇒ Object
- #version_constraint_for(cookbook_name) ⇒ Object
Constructor Details
#initialize(ui: nil, chef_config: nil) ⇒ PolicyfileCompiler
Returns a new instance of PolicyfileCompiler.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 62 def initialize(ui: nil, chef_config: nil) @storage_config = Policyfile::StorageConfig.new @dsl = Policyfile::DSL.new(storage_config, chef_config:) @artifact_server_cookbook_location_specs = {} @merged_graph = nil @ui = ui || UI.null @install_report = Policyfile::Reports::Install.new(ui: @ui, policyfile_compiler: self) end |
Instance Attribute Details
#dsl ⇒ Object (readonly)
Returns the value of attribute dsl.
58 59 60 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 58 def dsl @dsl end |
#install_report ⇒ Object (readonly)
Returns the value of attribute install_report.
60 61 62 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 60 def install_report @install_report end |
#storage_config ⇒ Object (readonly)
Returns the value of attribute storage_config.
59 60 61 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 59 def storage_config @storage_config end |
Class Method Details
.evaluate(policyfile_string, policyfile_filename, ui: nil, chef_config: nil) ⇒ Object
44 45 46 47 48 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 44 def self.evaluate(policyfile_string, policyfile_filename, ui: nil, chef_config: nil) compiler = new(ui:, chef_config:) compiler.evaluate_policyfile(policyfile_string, policyfile_filename) compiler end |
Instance Method Details
#all_cookbook_location_specs ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 218 def all_cookbook_location_specs # in the installation process, we create "artifact_server_cookbook_location_specs" # for any cookbook that isn't sourced from a single-version source (e.g., # path and git only support one version at a time), but we might have # specs for them to track additional version constraint demands. Merging # in this order ensures the artifact_server_cookbook_location_specs "win". cookbook_location_specs.merge(@artifact_server_cookbook_location_specs) end |
#artifacts_graph ⇒ Object
271 272 273 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 271 def artifacts_graph remote_artifacts_graph.merge(local_artifacts_graph) end |
#build {|@dsl| ... } ⇒ Object
347 348 349 350 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 347 def build yield @dsl self end |
#combined_run_lists ⇒ Object
335 336 337 338 339 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 335 def combined_run_lists .values.inject(.to_a) do |accum_run_lists, run_list| accum_run_lists | run_list.to_a end end |
#combined_run_lists_by_cb_name ⇒ Object
341 342 343 344 345 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 341 def combined_run_lists_by_cb_name combined_run_lists.inject({}) do |by_name_accum, run_list_item| by_name_accum end end |
#cookbook_location_spec_for(cookbook_name) ⇒ Object
92 93 94 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 92 def cookbook_location_spec_for(cookbook_name) cookbook_location_specs[cookbook_name] end |
#cookbook_version_fixed?(cookbook_name) ⇒ Boolean
322 323 324 325 326 327 328 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 322 def cookbook_version_fixed?(cookbook_name) if ( cookbook_location_spec = cookbook_location_spec_for(cookbook_name) ) cookbook_location_spec.version_fixed? else false end end |
#cookbooks_in_run_list ⇒ Object
330 331 332 333 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 330 def cookbooks_in_run_list recipes = combined_run_lists.map(&:name) recipes.map { |r| r[/^([^:]+)/, 1] } end |
#create_spec_for_cookbook(cookbook_name, version) ⇒ Object
211 212 213 214 215 216 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 211 def create_spec_for_cookbook(cookbook_name, version) matching_source = best_source_for(cookbook_name) = matching_source.(cookbook_name, version) spec = Policyfile::CookbookLocationSpecification.new(cookbook_name, "= #{version}", , storage_config) @artifact_server_cookbook_location_specs[cookbook_name] = spec end |
#default_attributes ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 145 def default_attributes check_for_default_attribute_conflicts! included_policies.map(&:policyfile_lock).inject( dsl.node_attributes.combined_default.to_hash ) do |acc, lock| Chef::Mixin::DeepMerge.merge(acc, lock.default_attributes) end end |
#default_source(source_type = nil, source_argument = nil, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 73 def default_source(source_type = nil, source_argument = nil, &block) if source_type.nil? prepend_array = if included_policies.length > 0 [included_policies_cookbook_source] else [] end prepend_array + dsl.default_source else dsl.default_source(source_type, source_argument, &block) end end |
#error! ⇒ Object
86 87 88 89 90 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 86 def error! unless errors.empty? raise PolicyfileError, errors.join("\n") end end |
#evaluate_policyfile(policyfile_string, policyfile_filename) ⇒ Object
352 353 354 355 356 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 352 def evaluate_policyfile(policyfile_string, policyfile_filename) storage_config.use_policyfile(policyfile_filename) @dsl.eval_policyfile(policyfile_string) self end |
#expanded_named_run_lists ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 116 def included_policies_named_runlists = included_policies.inject({}) do |acc, policy_spec| lock = policy_spec.policyfile_lock lock.named_run_lists.inject(acc) do |, (name, run_list_items)| [name] ||= Chef::RunList.new run_list_items.each do |run_list_item| [name] << run_list_item end end acc end named_run_lists.inject(included_policies_named_runlists) do |, (name, run_list_items)| [name] ||= Chef::RunList.new run_list_items.each do |run_list_item| [name] << run_list_item end end end |
#expanded_run_list ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 96 def # doesn't support roles yet... concated_runlist = Chef::RunList.new included_policies.each do |policy_spec| lock = policy_spec.policyfile_lock lock.run_list.each do |run_list_item| concated_runlist << run_list_item end end run_list.each do |run_list_item| concated_runlist << run_list_item end concated_runlist end |
#fixed_version_cookbooks_specs ⇒ Object
358 359 360 361 362 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 358 def fixed_version_cookbooks_specs @fixed_version_cookbooks_specs ||= cookbook_location_specs.select do |_cookbook_name, cookbook_location_spec| cookbook_location_spec.version_fixed? end end |
#graph ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 238 def graph @graph ||= Solve::Graph.new.tap do |g| artifacts_graph.each do |name, dependencies_by_version| dependencies_by_version.each do |version, dependencies| artifact = g.artifact(name, version) dependencies.each do |dep_name, constraint| artifact.dependency(dep_name, constraint) end end end end end |
#graph_demands ⇒ Object
265 266 267 268 269 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 265 def graph_demands ## TODO: By merging cookbooks from the current policyfile and included policies, # we lose the ability to know where a conflict came from (cookbook_demands_from_current + cookbook_demands_from_policies) end |
#graph_solution ⇒ Object
Compilation Methods
231 232 233 234 235 236 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 231 def graph_solution return @solution if @solution cache_fixed_version_cookbooks @solution = Solve.it!(graph, graph_demands) end |
#install ⇒ Object
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 203 204 205 206 207 208 209 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 167 def install ensure_cache_dir_exists cookbook_and_recipe_list = combined_run_lists.map(&:name).map do |recipe_spec| cookbook, _separator, recipe = recipe_spec.partition("::") recipe = "default" if recipe.empty? [cookbook, recipe] end missing_recipes_by_cb_spec = {} graph_solution.each do |cookbook_name, version| spec = cookbook_location_spec_for(cookbook_name) if spec.nil? || !spec.version_fixed? spec = create_spec_for_cookbook(cookbook_name, version) install_report.installing_cookbook(spec) spec.ensure_cached end required_recipes = cookbook_and_recipe_list.select { |cb_name, _recipe| cb_name == spec.name } missing_recipes = required_recipes.select { |_cb_name, recipe| !spec.cookbook_has_recipe?(recipe) } unless missing_recipes.empty? missing_recipes_by_cb_spec[spec] = missing_recipes end end unless missing_recipes_by_cb_spec.empty? = "The installed cookbooks do not contain all the recipes required by your run list(s):\n" missing_recipes_by_cb_spec.each do |spec, missing_items| formatted_opts = "{" + spec..map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + "}" << "Cookbook '#{spec.name}' = #{spec.version} #{formatted_opts}\n" << "is missing the following required recipes:\n" missing_items.each { |_cb, recipe| << "* #{recipe}\n" } end << "\n" << "You may have specified an incorrect recipe in your run list,\nor this recipe may not be available in that version of the cookbook\n" raise CookbookDoesNotContainRequiredRecipe, end end |
#local_artifacts_graph ⇒ Object
Gives a dependency graph for cookbooks that are source from an alternate location. These cookbooks could have a different set of dependencies compared to an unmodified copy upstream. For example, the community site may have a cookbook “apache2” at version “1.10.4”, which the user has forked on github and modified the dependencies without changing the version number. To accommodate this, the local_artifacts_graph should be merged over the upstream’s artifacts graph.
282 283 284 285 286 287 288 289 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 282 def local_artifacts_graph cookbook_location_specs.inject({}) do |local_artifacts, (cookbook_name, cookbook_location_spec)| if cookbook_location_spec.version_fixed? local_artifacts[cookbook_name] = { cookbook_location_spec.version => cookbook_location_spec.dependencies } end local_artifacts end end |
#lock ⇒ Object
163 164 165 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 163 def lock @policyfile_lock ||= PolicyfileLock.build_from_compiler(self, storage_config) end |
#normalized_named_run_lists ⇒ Object
138 139 140 141 142 143 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 138 def normalized_named_run_lists .inject({}) do |normalized, (name, run_list)| normalized[name] = run_list.map { |i| normalize_recipe(i) } normalized end end |
#normalized_run_list ⇒ Object
copy of the expanded_run_list, properly formatted for use in a lockfile
112 113 114 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 112 def normalized_run_list .map { |i| normalize_recipe(i) } end |
#override_attributes ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 154 def override_attributes check_for_override_attribute_conflicts! included_policies.map(&:policyfile_lock).inject( dsl.node_attributes.combined_override.to_hash ) do |acc, lock| Chef::Mixin::DeepMerge.merge(acc, lock.override_attributes) end end |
#remote_artifacts_graph ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 291 def remote_artifacts_graph @merged_graph ||= begin conflicting_cb_names = [] merged = {} default_source.each do |source| merged.merge!(source.universe_graph) do |conflicting_cb_name, _old, _new| if (preference = preferred_source_for_cookbook(conflicting_cb_name)) preference.universe_graph[conflicting_cb_name] elsif cookbook_could_appear_in_solution?(conflicting_cb_name) conflicting_cb_names << conflicting_cb_name {} # return empty set of versions else {} # return empty set of versions end end end handle_conflicting_cookbooks(conflicting_cb_names) merged end end |
#solution_dependencies ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 251 def solution_dependencies solution_deps = Policyfile::SolutionDependencies.new all_cookbook_location_specs.each do |name, spec| solution_deps.add_policyfile_dep(name, spec.version_constraint) end graph_solution.each do |name, version| transitive_deps = artifacts_graph[name][version] solution_deps.add_cookbook_dep(name, version, transitive_deps) end solution_deps end |
#version_constraint_for(cookbook_name) ⇒ Object
313 314 315 316 317 318 319 320 |
# File 'lib/chef-cli/policyfile_compiler.rb', line 313 def version_constraint_for(cookbook_name) if (cookbook_location_spec = cookbook_location_spec_for(cookbook_name)) && cookbook_location_spec.version_fixed? version = cookbook_location_spec.version "= #{version}" else DEFAULT_DEMAND_CONSTRAINT end end |