bolt-modules/boltlib/lib/puppet/functions/apply_prep.rb in bolt-2.17.0 vs bolt-modules/boltlib/lib/puppet/functions/apply_prep.rb in bolt-2.18.0

- old
+ new

@@ -11,14 +11,17 @@ # property of its transport (PCP) or by explicitly setting it as a feature in Bolt's inventory. # # > **Note:** Not available in apply block Puppet::Functions.create_function(:apply_prep) do # @param targets A pattern or array of patterns identifying a set of targets. + # @param options Options hash. + # @option options [Array] _required_modules An array of modules to sync to the target. # @example Prepare targets by name. # apply_prep('target1,target2') dispatch :apply_prep do param 'Boltlib::TargetSpec', :targets + optional_param 'Hash[String, Data]', :options end def script_compiler @script_compiler ||= Puppet::Pal::ScriptCompiler.new(closure_scope.compiler) end @@ -58,22 +61,38 @@ def executor @executor ||= Puppet.lookup(:bolt_executor) end - def apply_prep(target_spec) + def apply_prep(target_spec, options = {}) unless Puppet[:tasks] raise Puppet::ParseErrorWithIssue .from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING, action: 'apply_prep') end + options = options.transform_keys { |k| k.sub(/^_/, '').to_sym } + applicator = Puppet.lookup(:apply_executor) executor.report_function_call(self.class.name) targets = inventory.get_targets(target_spec) + required_modules = options[:required_modules].nil? ? nil : Array(options[:required_modules]) + if required_modules&.any? + Puppet.debug("Syncing only required modules: #{required_modules.join(',')}.") + end + + # Gather facts, including custom facts + plugins = applicator.build_plugin_tarball do |mod| + next unless required_modules.nil? || required_modules.include?(mod.name) + search_dirs = [] + search_dirs << mod.plugins if mod.plugins? + search_dirs << mod.pluginfacts if mod.pluginfacts? + search_dirs + end + executor.log_action('install puppet and gather facts', targets) do executor.without_default_logging do # Skip targets that include the puppet-agent feature, as we know an agent will be available. agent_targets, need_install_targets = targets.partition { |target| agent?(target, executor, inventory) } agent_targets.each { |target| Puppet.debug "Puppet Agent feature declared for #{target.name}" } @@ -105,17 +124,9 @@ end set = Bolt::ResultSet.new(results + hook_errors) raise Bolt::RunFailure.new(set.error_set, 'apply_prep') unless set.ok need_install_targets.each { |target| set_agent_feature(target) } - end - - # Gather facts, including custom facts - plugins = applicator.build_plugin_tarball do |mod| - search_dirs = [] - search_dirs << mod.plugins if mod.plugins? - search_dirs << mod.pluginfacts if mod.pluginfacts? - search_dirs end task = applicator.custom_facts_task arguments = { 'plugins' => Puppet::Pops::Types::PSensitiveType::Sensitive.new(plugins) } results = executor.run_task(targets, task, arguments)