Module: PuppetDebugger::Support
- Included in:
- Cli
- Defined in:
- lib/puppet-debugger/support.rb,
lib/puppet-debugger/support/node.rb,
lib/puppet-debugger/support/facts.rb,
lib/puppet-debugger/support/scope.rb,
lib/puppet-debugger/support/loader.rb,
lib/puppet-debugger/support/compiler.rb,
lib/puppet-debugger/support/functions.rb,
lib/puppet-debugger/support/environment.rb
Defined Under Namespace
Modules: Compilier, Environment, Facts, Functions, Loader, Node, Scope
Instance Method Summary collapse
-
#do_initialize ⇒ Object
this is required in order to load things only when we need them.
-
#generate_ast(string = nil) ⇒ Object
Hostclass - a puppet Program object which is considered the main class.
- #initialize_from_scope(value) ⇒ Object
- #known_resource_types ⇒ Object
-
#parse_error(error) ⇒ Object
parses the error type into a more useful error message defined in errors.rb returns new error object or the original if error cannot be parsed.
-
#parser ⇒ Object
returns a future parser for evaluating code.
-
#puppet_eval(input) ⇒ Object
Object - returns either a string of the result or object from puppet evaulation.
- #puppet_lib_dir ⇒ Object
-
#puppet_repl_lib_dir ⇒ Object
this is the lib directory of this gem in order to load any puppet functions from this gem we need to add the lib path of this gem.
- #static_responder_list ⇒ Object
Methods included from Loader
Methods included from Node
#convert_remote_node, #create_node, #create_real_node, #get_remote_node, #node, #remote_node_name, #remote_node_name=, #set_node, #set_node_from_name, #set_remote_node_name
Methods included from Functions
#data_type_files, #function_files, #function_map, #lib_dirs, #load_lib_dirs, #mod_finder
Methods included from Scope
#create_scope, #scope, #scope_vars, #set_scope
Methods included from Facts
#default_facter_version, #default_facterdb_filter, #default_facts, #dynamic_facterdb_filter, #facter_os_name, #facter_os_version, #facter_version, #node_facts, #server_facts, #set_facts
Methods included from Environment
#create_environment, #create_node_environment, #default_manifests_dir, #default_modules_paths, #default_puppet_env_name, #default_site_manifest, #environment_loaders, #modules_paths, #puppet_env_name, #puppet_environment, #set_environment
Methods included from Compilier
#compiler, #create_compiler, #set_compiler
Instance Method Details
#do_initialize ⇒ Object
this is required in order to load things only when we need them
87 88 89 90 91 92 93 94 |
# File 'lib/puppet-debugger/support.rb', line 87 def do_initialize Puppet.initialize_settings Puppet[:parser] = 'future' # this is required in order to work with puppet 3.8 Puppet[:trusted_node_data] = true rescue ArgumentError => e rescue Puppet::DevError => e # do nothing otherwise calling init twice raises an error end |
#generate_ast(string = nil) ⇒ Object
Returns Hostclass - a puppet Program object which is considered the main class.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/puppet-debugger/support.rb', line 98 def generate_ast(string = nil) parse_result = parser.parse_string(string, '') # the parse_result may be # * empty / nil (no input) # * a Model::Program # * a Model::Expression # # should return nil or Puppet::Pops::Model::Program # puppet 5 does not have the method current model = parse_result.respond_to?(:current) ? parse_result.current : parse_result args = {} ::Puppet::Pops::Model::AstTransformer.new('').merge_location(args, model) ast_code = if model.is_a? ::Puppet::Pops::Model::Program ::Puppet::Parser::AST::PopsBridge::Program.new(model, args) else args[:value] = model ::Puppet::Parser::AST::PopsBridge::Expression.new(args) end # Create the "main" class for the content - this content will get merged with all other "main" content ::Puppet::Parser::AST::Hostclass.new('', code: ast_code) end |
#initialize_from_scope(value) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/puppet-debugger/support.rb', line 58 def initialize_from_scope(value) set_scope(value) if value set_environment(value.environment) set_node(value.compiler.node) set_compiler(value.compiler) end end |
#known_resource_types ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/puppet-debugger/support.rb', line 67 def known_resource_types res = { hostclasses: scope.environment.known_resource_types.hostclasses.keys, definitions: scope.environment.known_resource_types.definitions.keys, nodes: scope.environment.known_resource_types.nodes.keys } if sites = scope.environment.known_resource_types.instance_variable_get(:@sites) res[:sites] = scope.environment.known_resource_types.instance_variable_get(:@sites).first end if scope.environment.known_resource_types.respond_to?(:applications) res[:applications] = scope.environment.known_resource_types.applications.keys end # some versions of puppet do not support capabilities if scope.environment.known_resource_types.respond_to?(:capability_mappings) res[:capability_mappings] = scope.environment.known_resource_types.capability_mappings.keys end res end |
#parse_error(error) ⇒ Object
parses the error type into a more useful error message defined in errors.rb returns new error object or the original if error cannot be parsed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/puppet-debugger/support.rb', line 24 def parse_error(error) case error when SocketError PuppetDebugger::Exception::ConnectError.new(message: "Unknown host: #{Puppet[:server]}") when Net::HTTPError PuppetDebugger::Exception::AuthError.new(message: error.) when Errno::ECONNREFUSED PuppetDebugger::Exception::ConnectError.new(message: error.) when Puppet::Error if error. =~ /could\ not\ find\ class/i PuppetDebugger::Exception::NoClassError.new(default_modules_paths: default_modules_paths, message: error.) elsif error. =~ /default\ node/i PuppetDebugger::Exception::NodeDefinitionError.new(default_site_manifest: default_site_manifest, message: error.) else error end else error end end |
#parser ⇒ Object
returns a future parser for evaluating code
162 163 164 |
# File 'lib/puppet-debugger/support.rb', line 162 def parser @parser ||= ::Puppet::Pops::Parser::EvaluatingParser.new end |
#puppet_eval(input) ⇒ Object
Returns Object - returns either a string of the result or object from puppet evaulation.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet-debugger/support.rb', line 124 def puppet_eval(input) # in order to add functions to the scope the loaders must be created # in order to call native functions we need to set the global_scope ast = generate_ast(input) # record the input for puppet to retrieve and reference later file = Tempfile.new(['puppet_debugger_input', '.pp']) File.open(file, 'w') do |f| f.write(input) end Puppet.override({ current_environment: puppet_environment, code: input, global_scope: scope, loaders: scope.compiler.loaders }, 'For puppet-debugger') do # because the repl is not a module we leave the modname blank scope.environment.known_resource_types.import_ast(ast, '') exec_hook :before_eval, '', self, self if bench result = nil time = Benchmark.realtime do result = parser.evaluate_string(scope, input, File.(file)) end out = [result, "Time elapsed #{(time * 1000).round(2)} ms"] else out = parser.evaluate_string(scope, input, File.(file)) end exec_hook :after_eval, out, self, self out end end |
#puppet_lib_dir ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/puppet-debugger/support.rb', line 153 def puppet_lib_dir # returns something like "/Library/Ruby/Gems/2.0.0/gems/puppet-4.2.2/lib/puppet.rb" # "/Users/adam/.rbenv/versions/2.2.6/lib/ruby/gems/2.2.0/gems/puppet-4.9.4/lib" # this is only useful when returning a namespace with the functions @puppet_lib_dir ||= File.dirname(Puppet.method(:[]).source_location.first) end |
#puppet_repl_lib_dir ⇒ Object
this is the lib directory of this gem in order to load any puppet functions from this gem we need to add the lib path of this gem
54 55 56 |
# File 'lib/puppet-debugger/support.rb', line 54 def puppet_repl_lib_dir File.(File.join(File.dirname(File.dirname(File.dirname(__FILE__))), 'lib')) end |
#static_responder_list ⇒ Object
47 48 49 |
# File 'lib/puppet-debugger/support.rb', line 47 def static_responder_list PuppetDebugger::InputResponders::Commands.command_list end |