Class: Bolt::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/catalog.rb

Instance Method Summary collapse

Instance Method Details

#compile_catalog(request) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bolt/catalog.rb', line 52

def compile_catalog(request)
  pal_main = request['code_ast'] || request['code_string']
  target = request['target']
  pdb_client = Bolt::PuppetDB::Client.new(Bolt::PuppetDB::Config.new(request['pdb_config']))

  with_puppet_settings(request['hiera_config']) do
    Puppet[:rich_data] = true
    Puppet[:node_name_value] = target['name']
    Puppet::Pal.in_tmp_environment('bolt_catalog',
                                   modulepath: request["modulepath"] || [],
                                   facts: target["facts"] || {},
                                   variables: target["variables"] || {}) do |pal|
      Puppet.override(bolt_pdb_client: pdb_client,
                      bolt_inventory: setup_inventory(request['inventory'])) do
        Puppet.lookup(:pal_current_node).trusted_data = target['trusted']
        pal.with_catalog_compiler do |compiler|
          ast = Puppet::Pops::Serialization::FromDataConverter.convert(pal_main)
          compiler.evaluate(ast)
          compiler.compile_additions
          compiler.with_json_encoding(&:encode)
        end
      end
    end
  end
end

#generate_ast(code) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bolt/catalog.rb', line 29

def generate_ast(code)
  with_puppet_settings do
    Puppet::Pal.in_tmp_environment("bolt_parse") do |pal|
      pal.with_catalog_compiler do |compiler|
        ast = compiler.parse_string(code)
        Puppet::Pops::Serialization::ToDataConverter.convert(ast,
                                                             rich_data: true,
                                                             symbol_to_string: true)
      end
    end
  end
end

#setup_inventory(inventory) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/bolt/catalog.rb', line 42

def setup_inventory(inventory)
  config = Bolt::Config.default
  config.overwrite_transport_data(inventory['config']['transport'],
                                  Bolt::Util.symbolize_top_level_keys(inventory['config']['transports']))

  Bolt::Inventory.new(inventory['data'],
                      config,
                      Bolt::Util.symbolize_top_level_keys(inventory['target_hash']))
end

#with_puppet_settings(hiera_config = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bolt/catalog.rb', line 12

def with_puppet_settings(hiera_config = {})
  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.settings[:hiera_config] = hiera_config

    # Use a special logdest that serializes all log messages and their level to stderr.
    Puppet::Util::Log.newdestination(:stderr)
    Puppet.settings[:log_level] = 'debug'
    yield
  end
end