Class: ChefDK::PolicyfileCompiler
- Inherits:
-
Object
- Object
- ChefDK::PolicyfileCompiler
- Extended by:
- Forwardable
- Defined in:
- lib/chef-dk/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
[:git, :path].freeze
Instance Attribute Summary collapse
-
#dsl ⇒ Object
readonly
Returns the value of attribute dsl.
-
#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
- #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
- #error! ⇒ Object
- #evaluate_policyfile(policyfile_string, policyfile_filename) ⇒ Object
- #expanded_run_list ⇒ Object
- #graph ⇒ Object
- #graph_demands ⇒ Object
-
#graph_solution ⇒ Object
Compilation Methods.
-
#initialize ⇒ 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_run_list ⇒ Object
copy of the expanded_run_list, properly formatted for use in a lockfile.
- #remote_artifacts_graph ⇒ Object
- #solution_dependencies ⇒ Object
- #version_constraint_for(cookbook_name) ⇒ Object
Constructor Details
#initialize ⇒ PolicyfileCompiler
Returns a new instance of PolicyfileCompiler.
52 53 54 55 56 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 52 def initialize @storage_config = Policyfile::StorageConfig.new @dsl = Policyfile::DSL.new(storage_config) @artifact_server_cookbook_location_specs = {} end |
Instance Attribute Details
#dsl ⇒ Object (readonly)
Returns the value of attribute dsl.
49 50 51 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 49 def dsl @dsl end |
#storage_config ⇒ Object (readonly)
Returns the value of attribute storage_config.
50 51 52 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 50 def storage_config @storage_config end |
Class Method Details
.evaluate(policyfile_string, policyfile_filename) ⇒ Object
37 38 39 40 41 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 37 def self.evaluate(policyfile_string, policyfile_filename) compiler = new compiler.evaluate_policyfile(policyfile_string, policyfile_filename) compiler end |
Instance Method Details
#all_cookbook_location_specs ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 100 def all_cookbook_location_specs # in the installation proces, 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
159 160 161 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 159 def artifacts_graph remote_artifacts_graph.merge(local_artifacts_graph) end |
#build {|@dsl| ... } ⇒ Object
205 206 207 208 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 205 def build yield @dsl self end |
#cookbook_location_spec_for(cookbook_name) ⇒ Object
64 65 66 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 64 def cookbook_location_spec_for(cookbook_name) cookbook_location_specs[cookbook_name] end |
#cookbook_version_fixed?(cookbook_name) ⇒ Boolean
192 193 194 195 196 197 198 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 192 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
200 201 202 203 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 200 def cookbooks_in_run_list recipes = .map {|recipe| recipe.name } recipes.map { |r| r[/^([^:]+)/, 1] } end |
#create_spec_for_cookbook(cookbook_name, version) ⇒ Object
94 95 96 97 98 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 94 def create_spec_for_cookbook(cookbook_name, version) = default_source.(cookbook_name, version) spec = Policyfile::CookbookLocationSpecification.new(cookbook_name, "= #{version}", , storage_config) @artifact_server_cookbook_location_specs[cookbook_name] = spec end |
#error! ⇒ Object
58 59 60 61 62 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 58 def error! unless errors.empty? raise PolicyfileError, errors.join("\n") end end |
#evaluate_policyfile(policyfile_string, policyfile_filename) ⇒ Object
210 211 212 213 214 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 210 def evaluate_policyfile(policyfile_string, policyfile_filename) storage_config.use_policyfile(policyfile_filename) @dsl.eval_policyfile(policyfile_string) self end |
#expanded_run_list ⇒ Object
68 69 70 71 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 68 def # doesn't support roles yet... Chef::RunList.new(*run_list) end |
#graph ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 119 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
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 146 def graph_demands cookbooks_for_demands.map do |cookbook_name| spec = cookbook_location_spec_for(cookbook_name) if spec.nil? [ cookbook_name, DEFAULT_DEMAND_CONSTRAINT ] elsif spec.version_fixed? [ cookbook_name, "= #{spec.version}" ] else [ cookbook_name, spec.version_constraint.to_s ] end end end |
#graph_solution ⇒ Object
Compilation Methods
113 114 115 116 117 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 113 def graph_solution return @solution if @solution cache_fixed_version_cookbooks @solution = Solve.it!(graph, graph_demands) end |
#install ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 82 def install ensure_cache_dir_exists graph_solution.each do |cookbook_name, version| spec = cookbook_location_spec_for(cookbook_name) if spec.nil? or !spec.version_fixed? spec = create_spec_for_cookbook(cookbook_name, version) spec.ensure_cached end 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 accomodate this, the local_artifacts_graph should be merged over the upstream’s artifacts graph.
170 171 172 173 174 175 176 177 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 170 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
78 79 80 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 78 def lock @policyfile_lock ||= PolicyfileLock.build_from_compiler(self, storage_config) end |
#normalized_run_list ⇒ Object
copy of the expanded_run_list, properly formatted for use in a lockfile
74 75 76 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 74 def normalized_run_list .map { |i| normalize_recipe(i) } end |
#remote_artifacts_graph ⇒ Object
179 180 181 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 179 def remote_artifacts_graph default_source.universe_graph end |
#solution_dependencies ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 132 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
183 184 185 186 187 188 189 190 |
# File 'lib/chef-dk/policyfile_compiler.rb', line 183 def version_constraint_for(cookbook_name) if (cookbook_location_spec = cookbook_location_spec_for(cookbook_name)) and cookbook_location_spec.version_fixed? version = cookbook_location_spec.version "= #{version}" else DEFAULT_DEMAND_CONSTRAINT end end |