diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2019-05-03 17:18:50 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2019-05-03 17:18:50 -0700 |
commit | 02d2aa310bd9189b03d878f5f8e9e4c730848a84 (patch) | |
tree | 56c7cc8ea443dddb57c8d54b412048316113dd5f | |
parent | 61f69e18e69e0dca6e8cffdcd7451181d874cdb3 (diff) | |
download | chef-lcg/policyfile-override.tar.gz |
allow override_runlists with policyfileslcg/policyfile-override
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/policy_builder/policyfile.rb | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb index 421d2ca8ec..4ad6de0db8 100644 --- a/lib/chef/policy_builder/policyfile.rb +++ b/lib/chef/policy_builder/policyfile.rb @@ -81,10 +81,12 @@ class Chef attr_reader :ohai_data attr_reader :json_attribs attr_reader :run_context + attr_reader :override_runlist def initialize(node_name, ohai_data, json_attribs, override_runlist, events) @node_name = node_name @ohai_data = ohai_data + @override_runlist = override_runlist @json_attribs = json_attribs @events = events @@ -94,10 +96,6 @@ class Chef raise UnsupportedFeature, "Policyfile does not support chef-solo. Use #{Chef::Dist::CLIENT} local mode instead." end - if override_runlist - raise UnsupportedFeature, "Policyfile does not support override run lists. Use named run_lists instead." - end - if json_attribs && json_attribs.key?("run_list") raise UnsupportedFeature, "Policyfile does not support setting the run_list in json data." end @@ -107,19 +105,6 @@ class Chef end end - ## API Compat ## - # Methods related to unsupported features - - # Override run_list is not supported. - def original_runlist - nil - end - - # Override run_list is not supported. - def override_runlist - nil - end - # Policyfile gives you the run_list already expanded, but users of this # class may expect to get a run_list expansion compatible object by # calling this method. @@ -152,13 +137,15 @@ class Chef node.consume_external_attrs(ohai_data, json_attribs) + setup_run_list_override + expand_run_list apply_policyfile_attributes Chef::Log.info("Run List is [#{run_list}]") - Chef::Log.info("Run List expands to [#{run_list_with_versions_for_display.join(', ')}]") + Chef::Log.info("Run List expands to [#{run_list_with_versions_for_display(run_list).join(', ')}]") - events.node_load_completed(node, run_list_with_versions_for_display, Chef::Config) + events.node_load_completed(node, run_list_with_versions_for_display(run_list), Chef::Config) events.run_list_expanded(run_list_expansion_ish) # we must do this after `node.consume_external_attrs` @@ -193,6 +180,11 @@ class Chef events.cookbook_compilation_start(run_context) run_context.load(run_list_expansion_ish) + if specific_recipes + specific_recipes.each do |recipe_file| + run_context.load_recipe_file(recipe_file) + end + end events.cookbook_compilation_complete(run_context) @@ -230,21 +222,13 @@ class Chef cookbooks_to_sync end - # Whether or not this is a temporary policy. Since PolicyBuilder doesn't - # support override_runlist, this is always false. - # - # @return [false] - def temporary_policy? - false - end - ## Internal Public API ## # @api private # # Generates an array of strings with recipe names including version and # identifier info. - def run_list_with_versions_for_display + def run_list_with_versions_for_display(run_list) run_list.map do |recipe_spec| cookbook, recipe = parse_recipe_spec(recipe_spec) lock_data = cookbook_lock_for(cookbook) @@ -286,7 +270,7 @@ class Chef # @api private def parse_recipe_spec(recipe_spec) - rmatch = recipe_spec.match(/recipe\[([^:]+)::([^:]+)\]/) + rmatch = recipe_spec.to_s.match(/recipe\[([^:]+)::([^:]+)\]/) if rmatch.nil? raise PolicyfileError, "invalid recipe specification #{recipe_spec} in Policyfile from #{policyfile_location}" else @@ -301,6 +285,7 @@ class Chef # @api private def run_list + return override_runlist if override_runlist if named_run_list_requested? named_run_list || raise(ConfigurationError, "Policy '#{retrieved_policy_name}' revision '#{revision_id}' does not have named_run_list '#{named_run_list_name}'" + @@ -457,7 +442,7 @@ class Chef # should be reduced to a single call. def cookbooks_to_sync @cookbook_to_sync ||= begin - events.cookbook_resolution_start(run_list_with_versions_for_display) + events.cookbook_resolution_start(run_list_with_versions_for_display(policy["run_list"])) cookbook_versions_by_name = cookbook_locks.inject({}) do |cb_map, (name, lock_data)| cb_map[name] = manifest_for(name, lock_data) @@ -469,7 +454,7 @@ class Chef end rescue Exception => e # TODO: wrap/munge exception to provide helpful error output - events.cookbook_resolution_failed(run_list_with_versions_for_display, e) + events.cookbook_resolution_failed(run_list_with_versions_for_display(policy["run_list"]), e) raise end @@ -508,6 +493,13 @@ class Chef Chef::Config end + # Indicates whether the policy is temporary, which means an + # override_runlist was provided. Chef::Client uses this to decide whether + # to do the final node save at the end of the run or not. + def temporary_policy? + node.override_runlist_set? + end + private # This method injects the run_context and into the Chef class. @@ -566,6 +558,32 @@ class Chef Chef::CookbookVersion.from_cb_artifact_data(raw_manifest) end + def setup_run_list_override + unless override_runlist.nil? + runlist_override_sanity_check! + node.override_runlist = override_runlist + Chef::Log.warn "Run List override has been provided." + Chef::Log.warn "Original Run List: [#{node.primary_runlist}]" + Chef::Log.warn "Overridden Run List: [#{node.run_list}]" + end + end + + # Ensures runlist override contains RunListItem instances + def runlist_override_sanity_check! + # Convert to array and remove whitespace + if override_runlist.is_a?(String) + @override_runlist = override_runlist.split(",").map { |e| e.strip } + end + @override_runlist = [override_runlist].flatten.compact + override_runlist.map! do |item| + if item.is_a?(Chef::RunList::RunListItem) + item + else + Chef::RunList::RunListItem.new(item) + end + end + end + end end end |