Class: Bolt::Catalog

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

Instance Method Summary collapse

Constructor Details

#initialize(log_level = 'debug') ⇒ Catalog

Returns a new instance of Catalog.



18
19
20
# File 'lib/bolt/catalog.rb', line 18

def initialize(log_level = 'debug')
  @log_level = log_level
end

Instance Method Details

#compile_catalog(request) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bolt/catalog.rb', line 56

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']))
  options = request['puppet_config'] || {}
  with_puppet_settings(request['hiera_config']) do
    Puppet[:rich_data] = true
    Puppet[:node_name_value] = target['name']
    env_conf = { modulepath: request['modulepath'] || [],
                 facts: target['facts'] || {} }
    env_conf[:variables] = {}
    Puppet::Pal.in_tmp_environment('bolt_catalog', env_conf) do |pal|
      inv = Bolt::ApplyInventory.new(request['config'])
      Puppet.override(bolt_pdb_client: pdb_client,
                      bolt_inventory: inv) do
        Puppet.lookup(:pal_current_node).trusted_data = target['trusted']
        pal.with_catalog_compiler do |compiler|
          # This needs to happen inside the catalog compiler so loaders are initialized for loading
          vars = Puppet::Pops::Serialization::FromDataConverter.convert(request['plan_vars'])
          pal.send(:add_variables, compiler.send(:topscope), target['variables'].merge(vars))

          # Configure language strictness in the CatalogCompiler. We want Bolt to be able
          # to compile most Puppet 4+ manifests, so we default to allowing deprecated functions.
          Puppet[:strict] = options['strict'] || :warning
          Puppet[:strict_variables] = options['strict_variables'] || false
          ast = Puppet::Pops::Serialization::FromDataConverter.convert(pal_main)
          # This will be a Program when running via `bolt apply`, but will
          # only be a subset of the AST when compiling an apply block in a
          # plan. In that case, we need to discover the definitions (which
          # would ordinarily be stored on the Program) and construct a Program object.
          unless ast.is_a?(Puppet::Pops::Model::Program)
            # Node definitions must be at the top level of the apply block.
            # That means the apply body either a) consists of just a
            # NodeDefinition, b) consists of a BlockExpression which may
            # contain NodeDefinitions, or c) doesn't contain NodeDefinitions.
            definitions = if ast.is_a?(Puppet::Pops::Model::BlockExpression)
                            ast.statements.select { |st| st.is_a?(Puppet::Pops::Model::NodeDefinition) }
                          elsif ast.is_a?(Puppet::Pops::Model::NodeDefinition)
                            [ast]
                          else
                            []
                          end
            ast = Puppet::Pops::Model::Factory.PROGRAM(ast, definitions, ast.locator).model
          end
          compiler.evaluate(ast)
          compiler.instance_variable_get(:@internal_compiler).send(:evaluate_ast_node)
          compiler.compile_additions
          compiler.with_json_encoding(&:encode)
        end
      end
    end
  end
end

#generate_ast(code, filename = nil) ⇒ Object



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

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

#with_puppet_settings(hiera_config = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bolt/catalog.rb', line 22

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)
    # Override module locations, Bolt includes vendored modules in its internal modulepath.
    Puppet.settings.override_default(:basemodulepath, '')
    Puppet.settings.override_default(:vendormoduledir, '')

    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] = @log_level
    yield
  end
end