Module: PuppetDebugger::Support::Scope

Included in:
PuppetDebugger::Support
Defined in:
lib/puppet-debugger/support/scope.rb

Instance Method Summary collapse

Instance Method Details

#catalogObject



11
12
13
# File 'lib/puppet-debugger/support/scope.rb', line 11

def catalog
  @catalog || scope.compiler.catalog
end

#create_scopePuppet::Pops::Scope

Returns - returns a puppet scope object.

Returns:

  • (Puppet::Pops::Scope)
    • returns a puppet scope object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet-debugger/support/scope.rb', line 46

def create_scope
  do_initialize
  begin
    # creates a new compiler for each scope
    scope = Puppet::Parser::Scope.new(compiler)
    # creates a node class
    scope.source = Puppet::Resource::Type.new(:node, node.name)
    scope.parent = compiler.topscope
    # compiling will load all the facts into the scope
    # without this step facts will not get resolved
    scope.compiler.compile # this will load everything into the scope
  rescue StandardError => e
    err = parse_error(e)
    raise err
  end
  scope
end

#get_catalog_text(catalog) ⇒ Object



15
16
17
18
19
# File 'lib/puppet-debugger/support/scope.rb', line 15

def get_catalog_text(catalog)
  return nil unless catalog

  Puppet::FileSystem.read(catalog, encoding: 'utf-8')
end

#scopePuppet::Pops::Scope

Returns - returns a puppet scope object.

Returns:

  • (Puppet::Pops::Scope)
    • returns a puppet scope object



41
42
43
# File 'lib/puppet-debugger/support/scope.rb', line 41

def scope
  @scope ||= create_scope
end

#scope_varsHash

Returns - returns a hash of variables that are currently in scope.

Returns:

  • (Hash)
    • returns a hash of variables that are currently in scope



65
66
67
68
# File 'lib/puppet-debugger/support/scope.rb', line 65

def scope_vars
  vars = scope.to_hash.delete_if { |key, _value| node.facts.values.key?(key.to_sym) }
  vars['facts'] = 'removed by the puppet-debugger'
end

#set_catalog(catalog_file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet-debugger/support/scope.rb', line 21

def set_catalog(catalog_file)
  return unless catalog_file

  catalog_text = get_catalog_text(catalog_file)
  scope # required
  Puppet.override({ current_environment: environment }, _('For puppet debugger')) do
    format = Puppet::Resource::Catalog.default_format
    begin
      c = Puppet::Resource::Catalog.convert_from(format, catalog_text)
    rescue StandardError => e
      raise Puppet::Error, format(_('Could not deserialize catalog from %{format}: %{detail}'), format: format, detail: e), e.backtrace
    end
    # Resolve all deferred values and replace them / mutate the catalog
    # Puppet 6 only
    Puppet::Pops::Evaluator::DeferredResolver.resolve_and_replace(node.facts, c) if Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0')
    @catalog = c
  end
end

#set_scope(value) ⇒ Object

Parameters:

  • - (Puppet::Pops::Scope)

    Scope object or nil



7
8
9
# File 'lib/puppet-debugger/support/scope.rb', line 7

def set_scope(value)
  @scope = value
end