vendored/puppet/lib/puppet_pal.rb in bolt-0.5.1 vs vendored/puppet/lib/puppet_pal.rb in bolt-0.6.0

- old
+ new

@@ -80,11 +80,12 @@ # @yieldparam [Puppet::Pal] context, a context that responds to Puppet::Pal methods # def self.in_tmp_environment(env_name, modulepath: [], settings_hash: {}, - facts: nil, + facts: nil, + variables: {}, &block ) assert_non_empty_string(env_name, _("temporary environment name")) # TRANSLATORS: do not translate variable name string in these assertions assert_optionally_empty_array(modulepath, 'modulepath') @@ -92,13 +93,14 @@ unless block_given? raise ArgumentError, _("A block must be given to 'in_tmp_environment'") # TRANSLATORS 'in_tmp_environment' is a name, do not translate end env = Puppet::Node::Environment.create(env_name, modulepath) + with_loaded_environment( Puppet::Environments::Static.new(env), # The tmp env is the only known env - env, facts, &block) + env, facts, variables, &block) end # Defines the context in which to perform puppet operations (evaluation, etc) # The code to evaluate in this context is given in a block. # @@ -122,12 +124,13 @@ # def self.in_environment(env_name, modulepath: nil, settings_hash: {}, env_dir: nil, - envpath: nil, - facts: nil, + envpath: nil, + facts: nil, + variables: {}, &block ) # TRANSLATORS terms in the assertions below are names of terms in code assert_non_empty_string(env_name, 'env_name') assert_optionally_empty_array(modulepath, 'modulepath', true) @@ -166,16 +169,16 @@ raise ArgumentError, _("No directory found for the environment '%{env_name}' on the path '%{envpath}'") % { env_name: env_name, envpath: envpath } end # A given modulepath should override the default env = env.override_with(:modulepath => modulepath) if !modulepath.nil? end - with_loaded_environment(environments, env, facts, &block) + with_loaded_environment(environments, env, facts, variables, &block) end private - def self.with_loaded_environment(environments, env, facts, &block) + def self.with_loaded_environment(environments, env, facts, variables, &block) env.each_plugin_directory do |dir| $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) end # Puppet requires Facter, which initializes its lookup paths. Reset Facter to @@ -184,11 +187,12 @@ node = Puppet::Node.new(Puppet[:node_name_value], :environment => env) Puppet.override( environments: environments, # The env being used is the only one... - current_node: node # to allow it to be picked up instead of created + current_node: node, # to allow it to be picked up instead of created + variables: variables ) do prepare_node_facts(node, facts) return block.call(self) end end @@ -207,10 +211,30 @@ # node.add_server_facts({}) end end + def self.add_variables(scope, variables) + return if variables.nil? + unless variables.is_a?(Hash) + raise ArgumentError, _("Given variables must be a hash, got %{type}") % { type: variables.class } + end + + rich_data_t = Puppet::Pops::Types::TypeFactory.rich_data + variables.each_pair do |k,v| + unless k =~ Puppet::Pops::Patterns::VAR_NAME + raise ArgumentError, _("Given variable '%{varname}' has illegal name") % { varname: k } + end + + unless rich_data_t.instance?(v) + raise ArgumentError, _("Given value for '%{varname}' has illegal type - got: %{type}") % { varname: k, type: v.class } + end + + scope.setvar(k, v) + end + end + def self.main(manifest = nil, facts = nil, &block) node = Puppet.lookup(:current_node) configured_environment = node.environment || Puppet.lookup(:current_environment) apply_environment = manifest ? @@ -250,9 +274,11 @@ facts_hash = node.facts.nil? ? {} : node.facts.values topscope.set_facts(facts_hash) # create the $settings:: variables topscope.merge_settings(node.environment.name, false) + + add_variables(topscope, Puppet.lookup(:variables)) compiler.compile(&block) rescue Puppet::ParseErrorWithIssue, Puppet::Error => detail # already logged and handled by the compiler for these two cases