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



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

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[:code] = ''
    Puppet[:node_name_value] = target['name']
    Puppet::Pal.in_tmp_environment(
      'bolt_catalog',
      modulepath: request["modulepath"] || [],
      facts: target["facts"] || {},
      variables: target["variables"] || {}
    ) do |_pal|
      node = Puppet.lookup(:pal_current_node)
      setup_node(node, target["trusted"])

      Puppet.override(pal_main: pal_main, bolt_pdb_client: pdb_client) do
        compile_node(node)
      end
    end
  end
end

#compile_node(node) ⇒ Object



39
40
41
42
# File 'lib/bolt/catalog.rb', line 39

def compile_node(node)
  compiler = Puppet::Parser::BoltCompiler.new(node)
  compiler.compile(&:to_resource)
end

#generate_ast(code) ⇒ Object



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

def generate_ast(code)
  with_puppet_settings do
    Puppet::Pal.in_tmp_environment("bolt_parse") do |_pal|
      node = Puppet.lookup(:pal_current_node)
      compiler = Puppet::Parser::BoltCompiler.new(node)
      compiler.dump_ast(compiler.parse_string(code))
    end
  end
end

#setup_node(node, trusted) ⇒ Object



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

def setup_node(node, trusted)
  facts = Puppet.lookup(:pal_facts)
  node_facts = Puppet::Node::Facts.new(Puppet[:node_name_value], facts)
  node.fact_merge(node_facts)

  node.parameters = node.parameters.merge(Puppet.lookup(:pal_variables))
  node.trusted_data = trusted
end

#with_puppet_settings(hiera_config) ⇒ Object



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

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