Module: PuppetRepl::Support

Included in:
Cli
Defined in:
lib/puppet-repl/support.rb

Instance Method Summary collapse

Instance Method Details

#compilerObject



129
130
131
# File 'lib/puppet-repl/support.rb', line 129

def compiler
  @compiler
end

#create_compiler(node) ⇒ Object



146
147
148
# File 'lib/puppet-repl/support.rb', line 146

def create_compiler(node)
  Puppet::Parser::Compiler.new(node)
end

#create_nodeObject

creates a node object



163
164
165
166
167
168
169
170
# File 'lib/puppet-repl/support.rb', line 163

def create_node
  options = {}
  options[:parameters] = facts
  options[:facts] = facts
  options[:classes] = []
  options[:environment] = puppet_environment
  Puppet::Node.new(facts[:fqdn], options)
end

#create_scopeObject



137
138
139
140
141
142
143
144
# File 'lib/puppet-repl/support.rb', line 137

def create_scope
  @compiler = create_compiler(node)
  scope = Puppet::Parser::Scope.new(compiler)
  scope.source = Puppet::Resource::Type.new(:node, node.name)
  scope.parent = @compiler.topscope
  compiler.compile # this will load everything into the scope
  scope
end

#do_initializeObject

this is required in order to load things only when we need them



16
17
18
19
20
21
22
# File 'lib/puppet-repl/support.rb', line 16

def do_initialize
  begin
    Puppet.initialize_settings
  rescue
    # do nothing otherwise calling init twice raises an error
  end
end

#environment_loadersObject

def functions

@functions = []
@functions << compiler.loaders.static_loader.loaded.keys.find_all {|l| l.type == :function}

end



125
126
127
# File 'lib/puppet-repl/support.rb', line 125

def environment_loaders
  name = compiler.loaders.public_environment_loader.loader_name
end

#facterdb_filterObject



150
151
152
# File 'lib/puppet-repl/support.rb', line 150

def facterdb_filter
  'operatingsystem=RedHat and operatingsystemrelease=/^7/ and architecture=x86_64 and facterversion=/^2.4\./'
end

#factsObject

uses facterdb (cached facts) and retrives the facts given a filter



155
156
157
158
159
160
# File 'lib/puppet-repl/support.rb', line 155

def facts
  unless @facts
    @facts ||= FacterDB.get_facts(facterdb_filter).first
  end
  @facts
end

#function_filesObject

returns a array of function files



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet-repl/support.rb', line 48

def function_files
  search_dirs = lib_dirs.map do |lib_dir|
    [File.join(lib_dir, 'puppet', 'functions', '**', '*.rb'),
      File.join(lib_dir, 'functions', '**', '*.rb'),
     File.join(lib_dir, 'puppet', 'parser', 'functions', '*.rb')
     ]
  end
  # add puppet lib directories
  search_dirs << [File.join(puppet_lib_dir, 'puppet', 'functions', '**', '*.rb'),
    File.join(puppet_lib_dir, 'puppet', 'parser', 'functions', '*.rb')
   ]
  Dir.glob(search_dirs.flatten)
end

#function_mapObject

returns a map of functions



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/puppet-repl/support.rb', line 68

def function_map
  unless @functions
    do_initialize
    @functions = {}
    function_files.each do |file|
      obj = {}
      name = File.basename(file, '.rb')
      obj[:name] = name
      obj[:parent] = mod_finder.match(file)[1]
      @functions["#{obj[:parent]}::#{name}"] = obj
    end
  end
  @functions
end

#lib_dirsObject



83
84
85
86
87
# File 'lib/puppet-repl/support.rb', line 83

def lib_dirs
  module_dirs.map do |mod_dir|
    Dir["#{mod_dir}/*/lib"].entries
  end.flatten
end

#load_lib_dirsObject



89
90
91
92
93
# File 'lib/puppet-repl/support.rb', line 89

def load_lib_dirs
  lib_dirs.each do |lib|
    $LOAD_PATH << lib
  end
end

#manifests_dirObject



180
181
182
# File 'lib/puppet-repl/support.rb', line 180

def manifests_dir
  File.join(Puppet[:environmentpath],puppet_env_name,'manifests')
end

#mod_finderObject

returns either the module name or puppet version



63
64
65
# File 'lib/puppet-repl/support.rb', line 63

def mod_finder
  @mod_finder ||= Regexp.new('\/([\w\-\.]+)\/lib')
end

#module_dirsObject

returns an array of module directories



8
9
10
11
12
13
# File 'lib/puppet-repl/support.rb', line 8

def module_dirs
  dirs = []
  dirs << File.join(Puppet[:environmentpath],puppet_env_name,'modules')
  dirs << Puppet.settings[:basemodulepath].split(':')
  dirs.flatten
end

#new_parserObject



29
30
31
32
33
34
35
36
# File 'lib/puppet-repl/support.rb', line 29

def new_parser
  @new_parser ||= Puppet::Parser::ParserFactory.parser
  # setting the file and string results in evaulting the string
  # setting the file and not the string results in evaulting the file
  @new_parser.file = 'repl'
  #this defines what to parse, and where it came from (can use ‘repl’ as the filename)
  @new_parser
end

#nodeObject



133
134
135
# File 'lib/puppet-repl/support.rb', line 133

def node
  @node ||= create_node
end

#parserObject

returns a future parser for evaluating code



96
97
98
99
# File 'lib/puppet-repl/support.rb', line 96

def parser
  Puppet::Parser::ParserFactory.evaluating_parser
  @parser || ::Puppet::Pops::Parser::EvaluatingParser.new
end

#puppet_env_nameObject

the cached name of the environment



102
103
104
# File 'lib/puppet-repl/support.rb', line 102

def puppet_env_name
  @penv ||= ENV['PUPPET_ENV'] || Puppet[:environment]
end

#puppet_environmentObject

creates a puppet environment given a module path and environment name this is cached



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppet-repl/support.rb', line 108

def puppet_environment
  unless @puppet_environment
    do_initialize
    @puppet_environment = Puppet::Node::Environment.create(
      puppet_env_name,
      module_dirs,
      manifests_dir
      )
  end
  @puppet_environment
end

#puppet_evaluate(input) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/puppet-repl/support.rb', line 38

def puppet_evaluate(input)
  new_parser.string = input
  result = new_parser.parse
  #- this creates the top level definitions
  # repl is just a fake name for the module
  #result = result.instantiate('repl')
  result.safeevaluate(scope) #- this evaluates and returns the result
end

#puppet_lib_dirObject



24
25
26
27
# File 'lib/puppet-repl/support.rb', line 24

def puppet_lib_dir
  # returns something like "/Library/Ruby/Gems/2.0.0/gems/puppet-4.2.2/lib/puppet.rb"
  @puppet_lib_dir ||= File.dirname(Puppet.method(:[]).source_location.first)
end

#scopeObject



172
173
174
175
176
177
178
# File 'lib/puppet-repl/support.rb', line 172

def scope
  unless @scope
    do_initialize
    @scope ||= create_scope
  end
  @scope
end