Class: Bolt::PAL

Inherits:
Object
  • Object
show all
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/message.rb,
lib/bolt/pal/yaml_plan/step/verbose.rb,
lib/bolt/pal/yaml_plan/step/download.rb,
lib/bolt/pal/yaml_plan/step/resources.rb

Defined Under Namespace

Modules: Issues Classes: PALError, YamlPlan

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modulepath, hiera_config, resource_types, max_compiles = Etc.nprocessors, trusted_external = nil, apply_settings = {}, project = nil) ⇒ PAL

Returns a new instance of PAL.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bolt/pal.rb', line 47

def initialize(modulepath, hiera_config, resource_types, max_compiles = Etc.nprocessors,
               trusted_external = nil, apply_settings = {}, project = nil)
  unless modulepath.is_a?(Bolt::Config::Modulepath)
    msg = "Type error in PAL: modulepath must be a Bolt::Config::Modulepath"
    raise Bolt::Error.new(msg, "bolt/execution-error")
  end
  # Nothing works without initialized this global state. Reinitializing
  # is safe and in practice only happens in tests
  self.class.load_puppet
  @modulepath = modulepath
  @hiera_config = hiera_config
  @trusted_external = trusted_external
  @apply_settings = apply_settings
  @max_compiles = max_compiles
  @resource_types = resource_types
  @project = project

  @logger = Bolt::Logger.logger(self)
  unless user_modulepath.empty?
    @logger.debug("Loading modules from #{full_modulepath.join(File::PATH_SEPARATOR)}")
  end

  @loaded = false
end

Class Method Details

.configure_loggingObject

Puppet logging is global so this is class method to avoid confusion


81
82
83
84
85
86
87
# File 'lib/bolt/pal.rb', line 81

def self.configure_logging
  Puppet::Util::Log.destinations.clear
  Puppet::Util::Log.newdestination(Bolt::Logger.logger('Puppet'))
  # Defer all log level decisions to the Logging library by telling Puppet
  # to log everything
  Puppet.settings[:log_level] = 'debug'
end

.load_puppetObject


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bolt/pal.rb', line 89

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_relative 'pal/logging'
  require_relative 'pal/issues'
  require_relative 'pal/yaml_plan/loader'
  require_relative '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”.


128
129
130
131
# File 'lib/bolt/pal.rb', line 128

def alias_types(compiler)
  compiler.evaluate_string('type TargetSpec = Boltlib::TargetSpec')
  compiler.evaluate_string('type PlanResult = Boltlib::PlanResult')
end

#cache_plan_infoObject


714
715
716
717
718
719
720
721
722
723
# File 'lib/bolt/pal.rb', line 714

def cache_plan_info
  # plan_name is an array here
  plans_info = list_plans(filter_content: false).map do |plan_name,|
    data = get_plan_info(plan_name, with_mtime: true)
    { plan_name => data }
  end.reduce({}, :merge)

  FileUtils.touch(@project.plan_cache_file)
  File.write(@project.plan_cache_file, plans_info.to_json)
end

#cache_task_infoObject


725
726
727
728
729
730
731
732
733
734
# File 'lib/bolt/pal.rb', line 725

def cache_task_info
  # task_name is an array here
  tasks_info = list_tasks(filter_content: false).map do |task_name,|
    data = get_task_info(task_name, with_mtime: true)
    { task_name => data }
  end.reduce({}, :merge)

  FileUtils.touch(@project.task_cache_file)
  File.write(@project.task_cache_file, tasks_info.to_json)
end

#convert_plan(plan) ⇒ Object


615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/bolt/pal.rb', line 615

def convert_plan(plan)
  path = File.expand_path(plan)

  # If the path doesn't exist, check if it's a plan name
  unless File.exist?(path)
    in_bolt_compiler do |compiler|
      sig = compiler.plan_signature(plan)

      # If the plan was loaded, look for it on the module loader
      # There has to be an easier way to do this...
      if sig
        type = compiler.list_plans.find { |p| p.name == plan }
        path = sig.instance_variable_get(:@plan_func)
                  .loader
                  .find(type)
                  .origin
                  .first
      end
    end
  end

  Puppet[:tasks] = true
  transpiler = YamlPlan::Transpiler.new
  transpiler.transpile(path)
end

#detect_project_conflict(project, environment) ⇒ Object


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bolt/pal.rb', line 148

def detect_project_conflict(project, environment)
  return unless project && project.load_as_module?
  # The environment modulepath has stripped out non-existent directories,
  # so we don't need to check for them
  modules = environment.modulepath.flat_map do |path|
    Dir.children(path).select { |name| Puppet::Module.is_module_directory?(name, path) }
  end
  if modules.include?(project.name)
    Bolt::Logger.warn_once(
      "project_shadows_module",
      "The project '#{project.name}' shadows an existing module of the same name"
    )
  end
end

#filter_content(content, patterns) ⇒ Object

Filters content by a list of names and glob patterns specified in project configuration.


309
310
311
312
313
314
315
# File 'lib/bolt/pal.rb', line 309

def filter_content(content, patterns)
  return content unless content && patterns

  content.select do |name,|
    patterns.any? { |pattern| File.fnmatch?(pattern, name, File::FNM_EXTGLOB) }
  end
end

#full_modulepathObject


72
73
74
# File 'lib/bolt/pal.rb', line 72

def full_modulepath
  @modulepath.full_modulepath
end

#generate_types(cache: false) ⇒ Object


702
703
704
705
706
707
708
709
710
711
712
# File 'lib/bolt/pal.rb', line 702

def generate_types(cache: false)
  require 'puppet/face/generate'
  in_bolt_compiler do
    generator = Puppet::Generate::Type
    inputs = generator.find_inputs(:pcore)
    FileUtils.mkdir_p(@resource_types)
    cache_plan_info if @project && cache
    cache_task_info if @project && cache
    generator.generate(inputs, @resource_types, true)
  end
end

#get_plan_info(plan_name, with_mtime: false) ⇒ Object


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/bolt/pal.rb', line 479

def get_plan_info(plan_name, with_mtime: false)
  plan_sig = in_bolt_compiler do |compiler|
    compiler.plan_signature(plan_name)
  end

  if plan_sig.nil?
    raise Bolt::Error.unknown_plan(plan_name)
  end

  # path may be a Pathname object, so make sure to stringify it
  mod = plan_sig.instance_variable_get(:@plan_func).loader.parent.path.to_s

  # If it's a Puppet language plan, use strings to extract data. The only
  # way to tell is to check which filename exists in the module.
  plan_subpath = File.join(plan_name.split('::').drop(1))
  plan_subpath = 'init' if plan_subpath.empty?

  pp_path = File.join(mod, 'plans', "#{plan_subpath}.pp")
  if File.exist?(pp_path)
    require 'puppet-strings'
    require 'puppet-strings/yard'
    PuppetStrings::Yard.setup!
    YARD::Logger.instance.level = :error
    YARD.parse(pp_path)

    plan = YARD::Registry.at("puppet_plans::#{plan_name}")

    description = if plan.tag(:summary)
                    plan.tag(:summary).text
                  elsif !plan.docstring.empty?
                    plan.docstring
                  end

    defaults = plan.parameters.to_h.compact
    signature_params = Set.new(plan.parameters.map(&:first))
    parameters = plan.tags(:param).each_with_object({}) do |param, params|
      name = param.name
      if signature_params.include?(name)
        params[name] = { 'type' => param.types.first }
        params[name]['sensitive'] = param.types.first =~ /\ASensitive(\[.*\])?\z/ ? true : false
        params[name]['default_value'] = defaults[name] if defaults.key?(name)
        params[name]['description'] = param.text if param.text && !param.text.empty?
      else
        Bolt::Logger.warn(
          "missing_plan_parameter",
          "The documented parameter '#{name}' does not exist in signature for plan '#{plan.name}'"
        )
      end
    end

    pp_info = {
      'name'        => plan_name,
      'description' => description,
      'parameters'  => parameters,
      'module'      => mod,
      'private'     => private_plan?(plan)
    }

    pp_info.merge!(get_plan_mtime(plan.file)) if with_mtime
    pp_info

  # If it's a YAML plan, fall back to limited data
  else
    yaml_path = File.join(mod, 'plans', "#{plan_subpath}.yaml")
    plan_content = File.read(yaml_path)
    plan = Bolt::PAL::YamlPlan::Loader.from_string(plan_name, plan_content, yaml_path)

    parameters = plan.parameters.each_with_object({}) do |param, params|
      name = param.name
      type_str = case param.type_expr
                 when Puppet::Pops::Types::PTypeReferenceType
                   param.type_expr.type_string
                 when nil
                   'Any'
                 else
                   param.type_expr
                 end
      params[name] = { 'type' => type_str }
      params[name]['sensitive'] = param.type_expr.instance_of?(Puppet::Pops::Types::PSensitiveType)
      params[name]['default_value'] = param.value unless param.value.nil?
      params[name]['description'] = param.description if param.description
    end

    yaml_info = {
      'name'        => plan_name,
      'description' => plan.description,
      'parameters'  => parameters,
      'module'      => mod,
      'private'     => !!plan.private
    }

    yaml_info.merge!(get_plan_mtime(yaml_path)) if with_mtime
    yaml_info
  end
end

#get_plan_mtime(path) ⇒ Object


599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/bolt/pal.rb', line 599

def get_plan_mtime(path)
  # If the plan is from the project modules/ directory, or is in the
  # project itself, include the last mtime of the file so we can compare
  # if the plan has been updated since it was cached.
  if @project &&
     File.exist?(path) &&
     (path.include?(File.join(@project.path, 'modules')) ||
      path.include?(@project.plans_path.to_s))

    { 'file' => { 'mtime' => File.mtime(path),
                  'path' => path } }
  else
    {}
  end
end

#get_task(task_name, with_mtime: false) ⇒ Object


411
412
413
414
415
416
417
418
419
420
421
# File 'lib/bolt/pal.rb', line 411

def get_task(task_name, with_mtime: false)
  task = task_signature(task_name)

  if task.nil?
    raise Bolt::Error.unknown_task(task_name)
  end

  task = Bolt::Task.from_task_signature(task)
  task.add_mtimes if with_mtime
  task
end

#get_task_info(task_name, with_mtime: false) ⇒ Object


423
424
425
# File 'lib/bolt/pal.rb', line 423

def get_task_info(task_name, with_mtime: false)
  get_task(task_name, with_mtime: with_mtime).to_h
end

#in_bolt_compiler(compiler_params: {}) ⇒ 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.


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/bolt/pal.rb', line 166

def in_bolt_compiler(compiler_params: {})
  # TODO: If we always call this inside a bolt_executor we can remove this here
  setup
  compiler_params = compiler_params.merge(set_local_facts: false)
  r = Puppet::Pal.in_tmp_environment('bolt', modulepath: full_modulepath, facts: {}) do |pal|
    # Only load the project if it a) exists, b) has a name it can be loaded with
    Puppet.override(bolt_project: @project,
                    yaml_plan_instantiator: Bolt::PAL::YamlPlan::Loader) do
      # Because this has the side effect of loading and caching the list
      # of modules, it must happen *after* we have overridden
      # bolt_project or the project will be ignored
      detect_project_conflict(@project, Puppet.lookup(:environments).get('bolt'))
      pal.with_script_compiler(**compiler_params) do |compiler|
        alias_types(compiler)
        register_resource_types(Puppet.lookup(:loaders)) if @resource_types
        begin
          yield compiler
        rescue Bolt::Error => e
          e
        rescue Puppet::DataBinding::LookupError => e
          if e.issue_code == :HIERA_UNDEFINED_VARIABLE
            message = "Interpolations are not supported in lookups outside of an apply block: #{e.message}"
            PALError.new(message)
          else
            PALError.from_preformatted_error(e)
          end
        rescue Puppet::PreformattedError => e
          if e.issue_code == :UNKNOWN_VARIABLE &&
             %w[facts trusted server_facts settings].include?(e.arguments[:name])
            message = "Evaluation Error: Variable '#{e.arguments[:name]}' is not available in the current scope "\
                      "unless explicitly defined."
            details = { file: e.file, line: e.line, column: e.pos }
            PALError.new(message, details)
          else
            PALError.from_preformatted_error(e)
          end
        rescue StandardError => e
          PALError.from_preformatted_error(e)
        end
      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_catalog_compilerObject


241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/bolt/pal.rb', line 241

def in_catalog_compiler
  with_puppet_settings do
    Puppet.override(bolt_project: @project) do
      Puppet::Pal.in_tmp_environment('bolt', modulepath: full_modulepath) do |pal|
        pal.with_catalog_compiler do |compiler|
          yield compiler
        end
      end
    end
  rescue Puppet::Error => e
    raise PALError.from_error(e)
  end
end

#in_plan_compiler(executor, inventory, pdb_client, applicator = nil) ⇒ Object


255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/bolt/pal.rb', line 255

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


268
269
270
271
272
273
274
# File 'lib/bolt/pal.rb', line 268

def in_task_compiler(executor, inventory)
  with_bolt_executor(executor, inventory) do
    in_bolt_compiler do |compiler|
      yield compiler
    end
  end
end

#list_modulesHash{String => Array<Hash{Symbol => String,nil}>}

Returns a mapping of all modules available to the Bolt compiler

Returns:

  • (Hash{String => Array<Hash{Symbol => String,nil}>})

    A hash that associates each directory on the modulepath with an array containing a hash of information for each module in that directory. The information hash provides the name, version, and a string indicating whether the module belongs to an internal module group.


648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/bolt/pal.rb', line 648

def list_modules
  internal_module_groups = { Bolt::Config::Modulepath::BOLTLIB_PATH => 'Plan Language Modules',
                             Bolt::Config::Modulepath::MODULES_PATH => 'Packaged Modules',
                             @project.managed_moduledir.to_s => 'Project Dependencies' }

  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(filter_content: false) ⇒ Object


467
468
469
470
471
472
473
474
475
476
477
# File 'lib/bolt/pal.rb', line 467

def list_plans(filter_content: false)
  in_bolt_compiler do |compiler|
    errors = []
    plans = compiler.list_plans(nil, errors).map { |plan| [plan.name] }.sort
    errors.each do |error|
      Bolt::Logger.warn("plan_load_error", error.details['original_error'])
    end

    filter_content ? filter_content(plans, @project&.plans) : plans
  end
end

#list_plans_with_cache(filter_content: false) ⇒ Object


427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/bolt/pal.rb', line 427

def list_plans_with_cache(filter_content: false)
  # Don't filter content yet, so that if users update their plan filters
  # we don't need to refresh the cache
  plan_names = list_plans(filter_content: false).map(&:first)
  plan_cache = if @project
                 Bolt::Util.read_optional_json_file(@project.plan_cache_file, 'Plan cache file')
               else
                 {}
               end
  updated = false

  plan_list = plan_names.each_with_object([]) do |plan_name, list|
    data = plan_cache[plan_name] || get_plan_info(plan_name, with_mtime: true)
    # If the plan is a 'local' plan (in the project itself, or the modules/
    # directory) then verify it hasn't been updated since we cached it. If
    # it has been updated, refresh the cache and use the new data.
    if file_modified?(data.dig('file', 'path'), data.dig('file', 'mtime'))
      data = get_plan_info(plan_name, with_mtime: true)
      updated = true
      plan_cache[plan_name] = data
    end

    list << [plan_name, data['description']] unless data['private']
  end

  File.write(@project.plan_cache_file, plan_cache.to_json) if updated && @project

  filter_content ? filter_content(plan_list, @project&.plans) : plan_list
end

#list_tasks(filter_content: false) ⇒ Object


360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/bolt/pal.rb', line 360

def list_tasks(filter_content: false)
  in_bolt_compiler do |compiler|
    tasks = compiler.list_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

    filter_content ? filter_content(tasks, @project&.tasks) : tasks
  end
end

#list_tasks_with_cache(filter_content: false) ⇒ Object


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/bolt/pal.rb', line 317

def list_tasks_with_cache(filter_content: false)
  # Don't filter content yet, so that if users update their task filters
  # we don't need to refresh the cache
  task_names = list_tasks(filter_content: false).map(&:first)
  task_cache = if @project
                 Bolt::Util.read_optional_json_file(@project.task_cache_file, 'Task cache file')
               else
                 {}
               end
  updated = false

  task_list = task_names.each_with_object([]) do |task_name, list|
    data = task_cache[task_name] || get_task_info(task_name, with_mtime: true)

    # Make sure all the keys are strings - if we get data from
    # get_task_info they will be symbols
    data = Bolt::Util.walk_keys(data, &:to_s)

    # If any files in the task were updated, refresh the cache
    if data['files']&.any?
      # For all the files that are part of the task
      data['files'].each do |f|
        # If any file has been updated since we last cached, update the
        # cache
        next unless file_modified?(f['path'], f['mtime'])
        data = get_task_info(task_name, with_mtime: true)
        data = Bolt::Util.walk_keys(data, &:to_s)
        # Tell Bolt to write to the cache file once we're done
        updated = true
        # Update the cache data
        task_cache[task_name] = data
      end
    end
     = data['metadata'] || {}
    # Don't add tasks to the list to return if they are private
    list << [task_name, ['description']] unless ['private']
  end

  # Write the cache if any entries were updated
  File.write(@project.task_cache_file, task_cache.to_json) if updated && @project
  filter_content ? filter_content(task_list, @project&.tasks) : task_list
end

#lookup(key, targets, inventory, executor, plan_vars: {}) ⇒ Object


785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/bolt/pal.rb', line 785

def lookup(key, targets, inventory, executor, plan_vars: {})
  # Install the puppet-agent package and collect facts. Facts are
  # automatically added to the targets.
  in_plan_compiler(executor, inventory, nil) do |compiler|
    compiler.call_function('apply_prep', targets)
  end

  overrides = {
    bolt_inventory: inventory,
    bolt_project:   @project
  }

  # Do a lookup with a catalog compiler, which uses the 'hierarchy' key in
  # Hiera config.
  results = targets.map do |target|
    node = Puppet::Node.from_data_hash(
      'name'       => target.name,
      'parameters' => { 'clientcert' => target.name }
    )

    trusted = Puppet::Context::TrustedInformation.local(node).to_h

    # Separate environment configuration from interpolation data the same
    # way we do when compiling Puppet catalogs.
    env_conf = {
      modulepath: @modulepath.full_modulepath,
      facts:      target.facts
    }

    interpolations = {
      variables:        plan_vars,
      target_variables: target.vars
    }

    with_puppet_settings do
      Puppet::Pal.in_tmp_environment(target.name, **env_conf) do |pal|
        Puppet.override(overrides) do
          Puppet.lookup(:pal_current_node).trusted_data = trusted
          pal.with_catalog_compiler(**interpolations) do |compiler|
            Bolt::Result.for_lookup(target, key, compiler.call_function('lookup', key))
          rescue StandardError => e
            Bolt::Result.from_exception(target, e)
          end
        rescue Puppet::Error => e
          raise PALError.from_error(e)
        end
      end
    end
  end

  Bolt::ResultSet.new(results)
end

#parse_manifest(code, filename) ⇒ Object

Parses a snippet of Puppet manifest code and returns the AST represented in JSON.


300
301
302
303
304
305
# File 'lib/bolt/pal.rb', line 300

def parse_manifest(code, filename)
  setup
  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


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/bolt/pal.rb', line 373

def parse_params(type, object_name, params)
  in_bolt_compiler do |compiler|
    case type
    when 'task'
      param_spec = compiler.task_signature(object_name)&.task_hash&.dig('parameters')
    when '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

#plan_hierarchy_lookup(key, plan_vars: {}) ⇒ Object


771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/bolt/pal.rb', line 771

def plan_hierarchy_lookup(key, plan_vars: {})
  # Do a lookup with a script compiler, which uses the 'plan_hierarchy' key in
  # Hiera config.
  with_puppet_settings do
    # We want all of the setup and teardown that `in_bolt_compiler` does,
    # but also want to pass keys to the script compiler.
    in_bolt_compiler(compiler_params: { variables: plan_vars }) do |compiler|
      compiler.call_function('lookup', key)
    end
  rescue Puppet::Error => e
    raise PALError.from_error(e)
  end
end

#register_resource_types(loaders) ⇒ Object

Register all resource types defined in $Project/.resource_types as well as the built in types registered with the runtime_3_init method.


135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bolt/pal.rb', line 135

def register_resource_types(loaders)
  static_loader = loaders.static_loader
  static_loader.runtime_3_init
  if File.directory?(@resource_types)
    Dir.children(@resource_types).each do |resource_pp|
      type_name_from_file = File.basename(resource_pp, '.pp').capitalize
      typed_name = Puppet::Pops::Loader::TypedName.new(:type, type_name_from_file)
      resource_type = Puppet::Pops::Types::TypeFactory.resource(type_name_from_file)
      loaders.static_loader.set_entry(typed_name, resource_type)
    end
  end
end

#run_plan(plan_name, params, executor, inventory = nil, pdb_client = nil, applicator = nil) ⇒ Object


743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/bolt/pal.rb', line 743

def run_plan(plan_name, params, executor, inventory = nil, pdb_client = nil, applicator = nil)
  # Start the round robin inside the plan compiler, so that
  # backgrounded tasks can finish once the main plan exits
  in_plan_compiler(executor, inventory, pdb_client, applicator) do |compiler|
    # Create a Fiber for the main plan. This will be run along with any
    # other Fibers created during the plan run in the round_robin, with the
    # main plan always taking precedence in being resumed.
    #
    # Every future except for the main plan needs to have a plan id in
    # order to be tracked for the `wait()` function with no arguments.
    future = executor.create_future(name: plan_name, plan_id: 0) do |_scope|
      r = compiler.call_function('run_plan', plan_name, params.merge('_bolt_api_call' => true))
      Bolt::PlanResult.from_pcore(r, 'success')
    rescue Bolt::Error => e
      Bolt::PlanResult.new(e, 'failure')
    end

    # Round robin until all Fibers, including the main plan, have finished.
    # This will stay alive until backgrounded tasks have finished.
    executor.round_robin until executor.plan_complete?

    # Return the result from the main plan
    future.value
  end
rescue Bolt::Error => e
  Bolt::PlanResult.new(e, 'failure')
end

#run_task(task_name, targets, params, executor, inventory, description = nil) ⇒ Object


736
737
738
739
740
741
# File 'lib/bolt/pal.rb', line 736

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

#setupObject


113
114
115
116
117
118
119
120
121
122
# File 'lib/bolt/pal.rb', line 113

def setup
  unless @loaded
    # This is slow so don't do it until we have to
    Bolt::PAL.load_puppet

    # Make sure we don't create the puppet directories
    with_puppet_settings { |_| nil }
    @loaded = true
  end
end

#show_module(name) ⇒ Hash

Return information about a module.

Parameters:

  • name (String)

    The name of the module.

Returns:

  • (Hash)

677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/bolt/pal.rb', line 677

def show_module(name)
  name = name.tr('-', '/')

  data = in_bolt_compiler do |_compiler|
    mod = Puppet.lookup(:current_environment).module(name.split(%r{[/-]}, 2).last)

    unless mod && (mod.forge_name == name || mod.name == name)
      raise Bolt::Error.new("Could not find module '#{name}' on the modulepath.", 'bolt/unknown-module')
    end

    {
      name:     mod.forge_name || mod.name,
      metadata: mod.,
      path:     mod.path,
      plans:    mod.plans.map(&:name).sort,
      tasks:    mod.tasks.map(&:name).sort
    }
  end

  data[:plans] = list_plans_with_cache.to_h.slice(*data[:plans]).to_a
  data[:tasks] = list_tasks_with_cache.to_h.slice(*data[:tasks]).to_a

  data
end

#task_signature(task_name) ⇒ Object


405
406
407
408
409
# File 'lib/bolt/pal.rb', line 405

def task_signature(task_name)
  in_bolt_compiler do |compiler|
    compiler.task_signature(task_name)
  end
end

#user_modulepathObject


76
77
78
# File 'lib/bolt/pal.rb', line 76

def user_modulepath
  @modulepath.user_modulepath
end

#with_bolt_executor(executor, inventory, pdb_client = nil, applicator = nil, &block) ⇒ Object


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/bolt/pal.rb', line 216

def with_bolt_executor(executor, inventory, pdb_client = nil, applicator = nil, &block)
  setup
  opts = {
    bolt_project: @project,
    bolt_executor: executor,
    bolt_inventory: inventory,
    bolt_pdb_client: pdb_client,
    apply_executor: applicator || Applicator.new(
      inventory,
      executor,
      full_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.
      user_modulepath,
      @project,
      pdb_client,
      @hiera_config,
      @max_compiles,
      @apply_settings
    )
  }
  Puppet.override(opts, &block)
end

#with_puppet_settingsObject

TODO: PUP-8553 should replace this


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/bolt/pal.rb', line 277

def with_puppet_settings
  dir = Dir.mktmpdir('bolt')

  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
  Puppet[:trusted_external_command] = @trusted_external
  Puppet.settings[:hiera_config] = @hiera_config
  self.class.configure_logging
  yield
ensure
  # Delete the tmpdir if it still exists. This check is needed to
  # prevent Bolt from erroring if the tmpdir is somehow deleted
  # before reaching this point.
  FileUtils.remove_entry_secure(dir) if File.exist?(dir)
end