summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLamont Granquist <454857+lamont-granquist@users.noreply.github.com>2021-07-06 10:57:07 -0700
committerGitHub <noreply@github.com>2021-07-06 10:57:07 -0700
commit35ccb4048aba082193acf404027b090934f9b544 (patch)
tree4c436ee033e8d01a595205534ce075839c4488a6 /lib
parentaed0f264dc104e3976b34788209c61767765e781 (diff)
parent18be6b87551174018628ad8f190c3e20add29ec7 (diff)
downloadchef-35ccb4048aba082193acf404027b090934f9b544.tar.gz
Merge pull request #11741 from chef/lcg/policyfile-override
Support override run_lists in policyfiles
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/policy_builder/policyfile.rb82
1 files changed, 51 insertions, 31 deletions
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb
index 35282bf915..ec2082d159 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 #{ChefUtils::Dist::Infra::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`
@@ -194,6 +181,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)
@@ -231,21 +223,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)
@@ -287,7 +271,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,7 +285,10 @@ class Chef
end
# @api private
+ # @return [Array<String>]
def run_list
+ return override_runlist.map(&:to_s) 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}'" +
@@ -458,7 +445,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)
@@ -470,7 +457,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
@@ -509,6 +496,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.
@@ -567,6 +561,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(&: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