Class: Bolt::PAL
- Inherits:
-
Object
- Object
- Bolt::PAL
- Defined in:
- lib/bolt/pal.rb,
lib/bolt/pal/issues.rb,
lib/bolt/pal/yaml_plan.rb,
lib/bolt/pal/yaml_plan/step.rb,
lib/bolt/pal/yaml_plan/loader.rb,
lib/bolt/pal/yaml_plan/evaluator.rb,
lib/bolt/pal/yaml_plan/parameter.rb,
lib/bolt/pal/yaml_plan/step/eval.rb,
lib/bolt/pal/yaml_plan/step/plan.rb,
lib/bolt/pal/yaml_plan/step/task.rb,
lib/bolt/pal/yaml_plan/transpiler.rb,
lib/bolt/pal/yaml_plan/step/script.rb,
lib/bolt/pal/yaml_plan/step/upload.rb,
lib/bolt/pal/yaml_plan/step/command.rb,
lib/bolt/pal/yaml_plan/step/resources.rb
Defined Under Namespace
Modules: Issues Classes: PALError, YamlPlan
Constant Summary collapse
- BOLTLIB_PATH =
File.('../../bolt-modules', __dir__)
- MODULES_PATH =
File.('../../modules', __dir__)
Instance Attribute Summary collapse
-
#modulepath ⇒ Object
readonly
Returns the value of attribute modulepath.
Class Method Summary collapse
-
.configure_logging ⇒ Object
Puppet logging is global so this is class method to avoid confusion.
- .load_puppet ⇒ Object
Instance Method Summary collapse
-
#alias_types(compiler) ⇒ Object
Create a top-level alias for TargetSpec and PlanResult so that users don’t have to namespace it with Boltlib, which is just an implementation detail.
- #convert_plan(plan_path) ⇒ Object
- #get_plan_info(plan_name) ⇒ Object
- #get_task_info(task_name) ⇒ Object
-
#in_bolt_compiler ⇒ Object
Runs a block in a PAL script compiler configured for Bolt.
- #in_plan_compiler(executor, inventory, pdb_client, applicator = nil) ⇒ Object
- #in_task_compiler(executor, inventory) ⇒ Object
-
#initialize(modulepath, hiera_config, max_compiles = Etc.nprocessors) ⇒ PAL
constructor
A new instance of PAL.
- #list_modulepath ⇒ Object
-
#list_modules ⇒ Hash{String => Array<Hash{Symbol => String,nil}>}
Returns a mapping of all modules available to the Bolt compiler.
- #list_plans ⇒ Object
- #list_tasks ⇒ Object
-
#parse_manifest(code, filename) ⇒ Object
Parses a snippet of Puppet manifest code and returns the AST represented in JSON.
- #parse_params(type, object_name, params) ⇒ Object
- #run_plan(plan_name, params, executor = nil, inventory = nil, pdb_client = nil, applicator = nil) ⇒ Object
- #run_task(task_name, targets, params, executor, inventory, description = nil) ⇒ Object
- #task_signature(task_name) ⇒ Object
- #with_bolt_executor(executor, inventory, pdb_client = nil, applicator = nil, &block) ⇒ Object
-
#with_puppet_settings ⇒ Object
TODO: PUP-8553 should replace this.
Constructor Details
#initialize(modulepath, hiera_config, max_compiles = Etc.nprocessors) ⇒ PAL
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bolt/pal.rb', line 42 def initialize(modulepath, hiera_config, max_compiles = Etc.nprocessors) # Nothing works without initialized this global state. Reinitializing # is safe and in practice only happen in tests self.class.load_puppet # This makes sure we don't accidentally create puppet dirs with_puppet_settings { |_| nil } @original_modulepath = modulepath @modulepath = [BOLTLIB_PATH, *modulepath, MODULES_PATH] @hiera_config = hiera_config @max_compiles = max_compiles @logger = Logging.logger[self] if modulepath && !modulepath.empty? @logger.info("Loading modules from #{@modulepath.join(File::PATH_SEPARATOR)}") end end |
Instance Attribute Details
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
40 41 42 |
# File 'lib/bolt/pal.rb', line 40 def modulepath @modulepath end |
Class Method Details
.configure_logging ⇒ Object
Puppet logging is global so this is class method to avoid confusion
62 63 64 65 66 67 68 |
# File 'lib/bolt/pal.rb', line 62 def self.configure_logging Puppet::Util::Log.destinations.clear Puppet::Util::Log.newdestination(Logging.logger['Puppet']) # Defer all log level decisions to the Logging library by telling Puppet # to log everything Puppet.settings[:log_level] = 'debug' end |
.load_puppet ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bolt/pal.rb', line 70 def self.load_puppet if Bolt::Util.windows? # Windows 'fix' for openssl behaving strangely. Prevents very slow operation # of random_bytes later when establishing winrm connections from a Windows host. # See https://github.com/rails/rails/issues/25805 for background. require 'openssl' OpenSSL::Random.random_bytes(1) end begin require 'puppet_pal' rescue LoadError raise Bolt::Error.new("Puppet must be installed to execute tasks", "bolt/puppet-missing") end require 'bolt/pal/logging' require 'bolt/pal/issues' require 'bolt/pal/yaml_plan/loader' require 'bolt/pal/yaml_plan/transpiler' # Now that puppet is loaded we can include puppet mixins in data types Bolt::ResultSet.include_iterable end |
Instance Method Details
#alias_types(compiler) ⇒ Object
Create a top-level alias for TargetSpec and PlanResult so that users don’t have to namespace it with Boltlib, which is just an implementation detail. This allows them to feel like a built-in type in bolt, rather than something has been, no pun intended, “bolted on”.
98 99 100 101 |
# File 'lib/bolt/pal.rb', line 98 def alias_types(compiler) compiler.evaluate_string('type TargetSpec = Boltlib::TargetSpec') compiler.evaluate_string('type PlanResult = Boltlib::PlanResult') end |
#convert_plan(plan_path) ⇒ Object
304 305 306 307 308 |
# File 'lib/bolt/pal.rb', line 304 def convert_plan(plan_path) Puppet[:tasks] = true transpiler = YamlPlan::Transpiler.new transpiler.transpile(plan_path) end |
#get_plan_info(plan_name) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/bolt/pal.rb', line 290 def get_plan_info(plan_name) plan_info = in_bolt_compiler do |compiler| plan = compiler.plan_signature(plan_name) hash = plan_hash(plan_name, plan) if plan hash['module'] = plan.instance_variable_get(:@plan_func).loader.parent.path if plan hash end if plan_info.nil? raise Bolt::Error.new(Bolt::Error.unknown_plan(plan_name), 'bolt/unknown-plan') end plan_info end |
#get_task_info(task_name) ⇒ Object
249 250 251 252 253 254 255 256 257 |
# File 'lib/bolt/pal.rb', line 249 def get_task_info(task_name) task = task_signature(task_name) if task.nil? raise Bolt::Error.new(Bolt::Error.unknown_task(task_name), 'bolt/unknown-task') end task.task_hash.reject { |k, _| k == 'parameters' } end |
#in_bolt_compiler ⇒ Object
Runs a block in a PAL script compiler configured for Bolt. Catches exceptions thrown by the block and re-raises them ensuring they are Bolt::Errors since the script compiler block will squash all exceptions.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/bolt/pal.rb', line 106 def in_bolt_compiler r = Puppet::Pal.in_tmp_environment('bolt', modulepath: @modulepath, facts: {}) do |pal| pal.with_script_compiler do |compiler| alias_types(compiler) begin Puppet.override(yaml_plan_instantiator: Bolt::PAL::YamlPlan::Loader) do yield compiler end rescue Bolt::Error => e e rescue Puppet::PreformattedError => e PALError.from_preformatted_error(e) rescue StandardError => e PALError.from_preformatted_error(e) end end end # Plans may return PuppetError but nothing should be throwing them if r.is_a?(StandardError) && !r.is_a?(Bolt::PuppetError) raise r end r end |
#in_plan_compiler(executor, inventory, pdb_client, applicator = nil) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bolt/pal.rb', line 152 def in_plan_compiler(executor, inventory, pdb_client, applicator = nil) with_bolt_executor(executor, inventory, pdb_client, applicator) do # TODO: remove this call and see if anything breaks when # settings dirs don't actually exist. Plans shouldn't # actually be using them. with_puppet_settings do in_bolt_compiler do |compiler| yield compiler end end end end |
#in_task_compiler(executor, inventory) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/bolt/pal.rb', line 165 def in_task_compiler(executor, inventory) with_bolt_executor(executor, inventory) do in_bolt_compiler do |compiler| yield compiler end end end |
#list_modulepath ⇒ Object
208 209 210 |
# File 'lib/bolt/pal.rb', line 208 def list_modulepath @modulepath - [BOLTLIB_PATH, MODULES_PATH] end |
#list_modules ⇒ Hash{String => Array<Hash{Symbol => String,nil}>}
Returns a mapping of all modules available to the Bolt compiler
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/bolt/pal.rb', line 317 def list_modules internal_module_groups = { BOLTLIB_PATH => 'Plan Language Modules', MODULES_PATH => 'Packaged Modules' } in_bolt_compiler do # NOTE: Can replace map+to_h with transform_values when Ruby 2.4 # is the minimum supported version. Puppet.lookup(:current_environment).modules_by_path.map do |path, modules| module_group = internal_module_groups[path] values = modules.map do |mod| mod_info = { name: (mod.forge_name || mod.name), version: mod.version } mod_info[:internal_module_group] = module_group unless module_group.nil? mod_info end [path, values] end.to_h end end |
#list_plans ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/bolt/pal.rb', line 259 def list_plans in_bolt_compiler do |compiler| errors = [] plans = compiler.list_plans(nil, errors).map { |plan| [plan.name] }.sort errors.each do |error| @logger.warn(error.details['original_error']) end plans end end |
#list_tasks ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/bolt/pal.rb', line 196 def list_tasks in_bolt_compiler do |compiler| tasks = compiler.list_tasks tasks.map(&:name).sort.each_with_object([]) do |task_name, data| task_sig = compiler.task_signature(task_name) unless task_sig.task_hash['metadata']['private'] data << [task_name, task_sig.task_hash['metadata']['description']] end end end end |
#parse_manifest(code, filename) ⇒ Object
Parses a snippet of Puppet manifest code and returns the AST represented in JSON.
190 191 192 193 194 |
# File 'lib/bolt/pal.rb', line 190 def parse_manifest(code, filename) Puppet::Pops::Parser::EvaluatingParser.new.parse_string(code, filename) rescue Puppet::Error => e raise Bolt::PAL::PALError, "Failed to parse manifest: #{e}" end |
#parse_params(type, object_name, params) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bolt/pal.rb', line 212 def parse_params(type, object_name, params) in_bolt_compiler do |compiler| if type == 'task' param_spec = compiler.task_signature(object_name)&.task_hash&.dig('parameters') elsif type == 'plan' plan = compiler.plan_signature(object_name) param_spec = plan.params_type.elements&.each_with_object({}) { |t, h| h[t.name] = t.value_type } if plan end param_spec ||= {} params.each_with_object({}) do |(name, str), acc| type = param_spec[name] begin parsed = JSON.parse(str, quirks_mode: true) # The type may not exist if the module is remote on orch or if a task # defines no parameters. Since we treat no parameters as Any we # should parse everything in this case acc[name] = if type && !type.instance?(parsed) str else parsed end rescue JSON::ParserError # This value may not be assignable in which case run_* will error acc[name] = str end acc end end end |
#run_plan(plan_name, params, executor = nil, inventory = nil, pdb_client = nil, applicator = nil) ⇒ Object
347 348 349 350 351 352 353 354 |
# File 'lib/bolt/pal.rb', line 347 def run_plan(plan_name, params, executor = nil, inventory = nil, pdb_client = nil, applicator = nil) in_plan_compiler(executor, inventory, pdb_client, applicator) do |compiler| r = compiler.call_function('run_plan', plan_name, params.merge('_bolt_api_call' => true)) Bolt::PlanResult.from_pcore(r, 'success') end rescue Bolt::Error => e Bolt::PlanResult.new(e, 'failure') end |
#run_task(task_name, targets, params, executor, inventory, description = nil) ⇒ Object
340 341 342 343 344 345 |
# File 'lib/bolt/pal.rb', line 340 def run_task(task_name, targets, params, executor, inventory, description = nil) in_task_compiler(executor, inventory) do |compiler| params = params.merge('_bolt_api_call' => true, '_catch_errors' => true) compiler.call_function('run_task', task_name, targets, description, params) end end |
#task_signature(task_name) ⇒ Object
243 244 245 246 247 |
# File 'lib/bolt/pal.rb', line 243 def task_signature(task_name) in_bolt_compiler do |compiler| compiler.task_signature(task_name) end end |
#with_bolt_executor(executor, inventory, pdb_client = nil, applicator = nil, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/bolt/pal.rb', line 131 def with_bolt_executor(executor, inventory, pdb_client = nil, applicator = nil, &block) opts = { bolt_executor: executor, bolt_inventory: inventory, bolt_pdb_client: pdb_client, apply_executor: applicator || Applicator.new( inventory, executor, @modulepath, # Skip syncing built-in plugins, since we vendor some Puppet 6 # versions of "core" types, which are already present on the agent, # but may cause issues on Puppet 5 agents. @original_modulepath, pdb_client, @hiera_config, @max_compiles ) } Puppet.override(opts, &block) end |
#with_puppet_settings ⇒ Object
TODO: PUP-8553 should replace this
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/bolt/pal.rb', line 174 def with_puppet_settings Dir.mktmpdir('bolt') do |dir| cli = [] Puppet::Settings::REQUIRED_APP_SETTINGS.each do |setting| cli << "--#{setting}" << dir end Puppet.settings.send(:clear_everything_for_tests) Puppet.initialize_settings(cli) Puppet::GettextConfig.create_default_text_domain self.class.configure_logging yield end end |