puppet-debugger 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'tempfile'
2
3
 
3
4
  module PuppetDebugger
@@ -14,7 +15,6 @@ module PuppetDebugger
14
15
  def set_compiler(value)
15
16
  @compiler = value
16
17
  end
17
-
18
18
  end
19
19
  end
20
20
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module Environment
@@ -25,14 +26,13 @@ module PuppetDebugger
25
26
 
26
27
  # the cached name of the environment
27
28
  def default_puppet_env_name
28
- ENV['PUPPET_ENV'] || Puppet[:environment]
29
+ ENV['PUPPET_ENV'] || Puppet[:environment]
29
30
  end
30
31
 
31
32
  # currently this is not being used
32
33
  def environment_loaders
33
34
  name = compiler.loaders.public_environment_loader.loader_name
34
35
  end
35
-
36
36
  end
37
37
  end
38
38
  end
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Exception
3
4
  class Error < StandardError
4
5
  attr_accessor :data
5
- def initialize(data={})
6
+ def initialize(data = {})
6
7
  @data = data
7
8
  end
8
-
9
9
  end
10
10
 
11
11
  class FatalError < Error
@@ -34,7 +34,7 @@ Cannot find node with name: #{data[:name]} on remote server
34
34
  end
35
35
 
36
36
  class TimeOutError < Error
37
- #Errno::ETIMEDOUT
37
+ # Errno::ETIMEDOUT
38
38
  end
39
39
 
40
40
  class NoClassError < FatalError
@@ -70,6 +70,5 @@ You will need to edit your auth.conf or conf.d/auth.conf (puppetserver) to allow
70
70
  EOF
71
71
  end
72
72
  end
73
-
74
73
  end
75
74
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module Facts
@@ -49,16 +50,16 @@ Using filter: #{facterdb_filter}
49
50
  Bad FacterDB filter, please change the filter so it returns a result set.
50
51
  See https://github.com/camptocamp/facterdb/#with-a-string-filter
51
52
  EOS
52
- raise PuppetDebugger::Exception::BadFilter.new(:message => message)
53
+ raise PuppetDebugger::Exception::BadFilter, message: message
53
54
  end
54
55
  # fix for when --show-legacy facts are not part of the facter 3 fact set
55
- node_facts[:fqdn] = node_facts[:networking].fetch('fqdn',nil) unless node_facts[:fqdn]
56
+ node_facts[:fqdn] = node_facts[:networking].fetch('fqdn', nil) unless node_facts[:fqdn]
56
57
  node_facts
57
58
  end
58
59
 
59
60
  def default_facts
60
61
  unless @facts
61
- values = Hash[ node_facts.map { |k, v| [k.to_s, v] } ]
62
+ values = Hash[node_facts.map { |k, v| [k.to_s, v] }]
62
63
  name = values['fqdn']
63
64
  @facts ||= Puppet::Node::Facts.new(name, values)
64
65
  end
@@ -67,12 +68,11 @@ See https://github.com/camptocamp/facterdb/#with-a-string-filter
67
68
 
68
69
  def server_facts
69
70
  data = {}
70
- data["servername"] = Facter.value("fqdn") || Facter.value('networking')['fqdn']
71
- data['serverip'] = Facter.value("ipaddress")
72
- data["serverversion"] = Puppet.version.to_s
71
+ data['servername'] = Facter.value('fqdn') || Facter.value('networking')['fqdn']
72
+ data['serverip'] = Facter.value('ipaddress')
73
+ data['serverversion'] = Puppet.version.to_s
73
74
  data
74
75
  end
75
-
76
76
  end
77
77
  end
78
78
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module Functions
@@ -7,14 +8,12 @@ module PuppetDebugger
7
8
  def function_files
8
9
  search_dirs = lib_dirs.map do |lib_dir|
9
10
  [File.join(lib_dir, 'puppet', 'functions', '**', '*.rb'),
10
- File.join(lib_dir, 'functions', '**', '*.rb'),
11
- File.join(lib_dir, 'puppet', 'parser', 'functions', '*.rb')
12
- ]
11
+ File.join(lib_dir, 'functions', '**', '*.rb'),
12
+ File.join(lib_dir, 'puppet', 'parser', 'functions', '*.rb')]
13
13
  end
14
14
  # add puppet lib directories
15
15
  search_dirs << [File.join(puppet_lib_dir, 'puppet', 'functions', '**', '*.rb'),
16
- File.join(puppet_lib_dir, 'puppet', 'parser', 'functions', '*.rb')
17
- ]
16
+ File.join(puppet_lib_dir, 'puppet', 'parser', 'functions', '*.rb')]
18
17
  Dir.glob(search_dirs.flatten)
19
18
  end
20
19
 
@@ -1,20 +1,19 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module InputResponders
4
-
5
5
  def static_responder_list
6
- ["exit", "functions", "classification","vars", 'facterdb_filter', "krt", "facts",
7
- "resources", "classes", "whereami", "play","reset", "help"
8
- ]
6
+ %w(exit functions classification vars facterdb_filter krt facts
7
+ resources classes whereami play reset help)
9
8
  end
10
9
 
11
10
  # @source_file and @source_line_num instance variables must be set for this
12
11
  # method to show the surrounding code
13
12
  # @return [String] - string output of the code surrounded by the breakpoint or nil if file or line_num do not exist
14
- def whereami(command=nil, args=nil)
15
- file=@source_file
16
- line_num=@source_line_num
17
- if file and line_num
13
+ def whereami(_command = nil, _args = nil)
14
+ file = @source_file
15
+ line_num = @source_line_num
16
+ if file && line_num
18
17
  if file == :code
19
18
  source_code = Puppet[:code]
20
19
  code = DebuggerCode.from_string(source_code, :puppet)
@@ -27,11 +26,11 @@ module PuppetDebugger
27
26
 
28
27
  # displays the facterdb filter
29
28
  # @param [Array] - args is not used
30
- def facterdb_filter(args=[])
29
+ def facterdb_filter(_args = [])
31
30
  dynamic_facterdb_filter.ai
32
31
  end
33
32
 
34
- def help(args=[])
33
+ def help(_args = [])
35
34
  PuppetDebugger::Cli.print_repl_desc
36
35
  end
37
36
 
@@ -46,7 +45,7 @@ module PuppetDebugger
46
45
  reset
47
46
  set_remote_node_name(name)
48
47
  else
49
- out_buffer.puts "Must supply a valid node name"
48
+ out_buffer.puts 'Must supply a valid node name'
50
49
  end
51
50
  when /loglevel/
52
51
  if level = args.shift
@@ -58,29 +57,29 @@ module PuppetDebugger
58
57
  output
59
58
  end
60
59
 
61
- def facts(args=[])
60
+ def facts(_args = [])
62
61
  variables = node.facts.values
63
- variables.ai({:sort_keys => true, :indent => -1})
62
+ variables.ai(sort_keys: true, indent: -1)
64
63
  end
65
64
 
66
- def functions(args=[])
65
+ def functions(args = [])
67
66
  filter = args.first || ''
68
67
  function_map.keys.sort.grep(/^#{Regexp.escape(filter)}/)
69
68
  end
70
69
 
71
- def vars(args=[])
70
+ def vars(_args = [])
72
71
  # remove duplicate variables that are also in the facts hash
73
- variables = scope.to_hash.delete_if {| key, value | node.facts.values.key?(key) }
72
+ variables = scope.to_hash.delete_if { |key, _value| node.facts.values.key?(key) }
74
73
  variables['facts'] = 'removed by the puppet-debugger' if variables.key?('facts')
75
- output = "Facts were removed for easier viewing".ai + "\n"
76
- output += variables.ai({:sort_keys => true, :indent => -1})
74
+ output = 'Facts were removed for easier viewing'.ai + "\n"
75
+ output += variables.ai(sort_keys: true, indent: -1)
77
76
  end
78
77
 
79
- def environment(args=[])
78
+ def environment(_args = [])
80
79
  "Puppet Environment: #{puppet_env_name}"
81
80
  end
82
81
 
83
- def reset(args=[])
82
+ def reset(_args = [])
84
83
  set_scope(nil)
85
84
  set_remote_node_name(nil)
86
85
  set_node(nil)
@@ -100,22 +99,22 @@ module PuppetDebugger
100
99
  nil
101
100
  end
102
101
 
103
- def krt(args=[])
104
- known_resource_types.ai({:sort_keys => true, :indent => -1})
102
+ def krt(_args = [])
103
+ known_resource_types.ai(sort_keys: true, indent: -1)
105
104
  end
106
105
 
107
- def play(args=[])
106
+ def play(args = [])
108
107
  config = {}
109
108
  config[:play] = args.first
110
109
  play_back(config)
111
- return nil # we don't want to return anything
110
+ nil # we don't want to return anything
112
111
  end
113
112
 
114
- def classification(args=[])
113
+ def classification(_args = [])
115
114
  node.classes.ai
116
115
  end
117
116
 
118
- def resources(args=[])
117
+ def resources(args = [])
119
118
  res = scope.compiler.catalog.resources.map do |res|
120
119
  res.to_s.gsub(/\[/, "['").gsub(/\]/, "']") # ensure the title has quotes
121
120
  end
@@ -127,10 +126,9 @@ module PuppetDebugger
127
126
  end
128
127
  end
129
128
 
130
- def classes(args=[])
129
+ def classes(_args = [])
131
130
  scope.compiler.catalog.classes.ai
132
131
  end
133
-
134
132
  end
135
133
  end
136
134
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'puppet/indirector/node/rest'
2
3
 
3
4
  module PuppetDebugger
@@ -46,7 +47,7 @@ module PuppetDebugger
46
47
  def get_remote_node(name)
47
48
  indirection = Puppet::Indirector::Indirection.instance(:node)
48
49
  indirection.terminus_class = 'rest'
49
- remote_node = indirection.find(name, :environment => puppet_environment)
50
+ remote_node = indirection.find(name, environment: puppet_environment)
50
51
  remote_node
51
52
  end
52
53
 
@@ -59,7 +60,7 @@ module PuppetDebugger
59
60
  parameters = remote_node.parameters.dup
60
61
  trusted_data = parameters.delete('trusted')
61
62
  options[:parameters] = parameters || {}
62
- options[:facts] = Puppet::Node::Facts.new(remote_node.name,remote_node.parameters)
63
+ options[:facts] = Puppet::Node::Facts.new(remote_node.name, remote_node.parameters)
63
64
  options[:classes] = remote_node.classes
64
65
  options[:environment] = puppet_environment
65
66
  node_object = Puppet::Node.new(remote_node.name, options)
@@ -71,11 +72,11 @@ module PuppetDebugger
71
72
  # query the remote puppet server and retrieve the node object
72
73
  #
73
74
  def set_node_from_name(name)
74
- out_buffer.puts ("Fetching node #{name}")
75
+ out_buffer.puts "Fetching node #{name}"
75
76
  remote_node = get_remote_node(name)
76
- if remote_node and remote_node.parameters.empty?
77
- remote_node_name = nil # clear out the remote name
78
- raise PuppetDebugger::Exception::UndefinedNode.new(:name => remote_node.name)
77
+ if remote_node && remote_node.parameters.empty?
78
+ remote_node_name = nil # clear out the remote name
79
+ raise PuppetDebugger::Exception::UndefinedNode, name: remote_node.name
79
80
  end
80
81
  remote_node_name = remote_node.name
81
82
  node_object = convert_remote_node(remote_node)
@@ -1,15 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module Play
4
-
5
- def play_back(config={})
5
+ def play_back(config = {})
6
6
  if config[:play]
7
7
  if config[:play] =~ /^http/
8
8
  play_back_url(config[:play])
9
- elsif File.exists? config[:play]
9
+ elsif File.exist? config[:play]
10
10
  play_back_string(File.read(config[:play]))
11
11
  else config[:play]
12
- out_buffer.puts "puppet-debugger can't play #{config[:play]}'"
12
+ out_buffer.puts "puppet-debugger can't play #{config[:play]}'"
13
13
  end
14
14
  end
15
15
  end
@@ -19,18 +19,12 @@ module PuppetDebugger
19
19
  url_data = URI(url)
20
20
  case url_data.host
21
21
  when /^gist\.github*/
22
- unless url_data.path =~ /raw/
23
- url = url += '.txt'
24
- end
22
+ url = url += '.txt' unless url_data.path =~ /raw/
25
23
  url
26
24
  when /^github.com/
27
- if url_data.path =~ /blob/
28
- url.gsub('blob', 'raw')
29
- end
25
+ url.gsub('blob', 'raw') if url_data.path =~ /blob/
30
26
  when /^gist.github.com/
31
- unless url_data.path =~ /raw/
32
- url = url += '.txt'
33
- end
27
+ url = url += '.txt' unless url_data.path =~ /raw/
34
28
  url
35
29
  when /^gitlab.com/
36
30
  if url_data.path =~ /snippets/
@@ -50,15 +44,13 @@ module PuppetDebugger
50
44
  end
51
45
 
52
46
  def play_back_url(url)
53
- begin
54
- require 'open-uri'
55
- require 'net/http'
56
- converted_url = convert_to_text(url)
57
- str = fetch_url_data(converted_url)
58
- play_back_string(str)
59
- rescue SocketError
60
- abort "puppet-debugger can't play `#{converted_url}'"
61
- end
47
+ require 'open-uri'
48
+ require 'net/http'
49
+ converted_url = convert_to_text(url)
50
+ str = fetch_url_data(converted_url)
51
+ play_back_string(str)
52
+ rescue SocketError
53
+ abort "puppet-debugger can't play `#{converted_url}'"
62
54
  end
63
55
 
64
56
  # plays back the string to the output stream
@@ -70,10 +62,10 @@ module PuppetDebugger
70
62
  full_buffer += buf
71
63
  # unless this is puppet code, otherwise skip repl keywords
72
64
  if keyword_expression.match(buf)
73
- out_buffer.write(">> ")
65
+ out_buffer.write('>> ')
74
66
  else
75
67
  parser.parse_string(full_buffer)
76
- out_buffer.write(">> ")
68
+ out_buffer.write('>> ')
77
69
  end
78
70
  rescue Puppet::ParseErrorWithIssue => e
79
71
  if multiline_input?(e)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module PuppetDebugger
2
3
  module Support
3
4
  module Scope
@@ -7,9 +8,7 @@ module PuppetDebugger
7
8
 
8
9
  # @return [Scope] puppet scope object
9
10
  def scope
10
- unless @scope
11
- @scope ||= create_scope
12
- end
11
+ @scope ||= create_scope unless @scope
13
12
  @scope
14
13
  end
15
14
 
@@ -34,7 +33,7 @@ module PuppetDebugger
34
33
 
35
34
  # returns a hash of varaibles that are currently in scope
36
35
  def scope_vars
37
- vars = scope.to_hash.delete_if {| key, value | node.facts.values.key?(key.to_sym) }
36
+ vars = scope.to_hash.delete_if { |key, _value| node.facts.values.key?(key.to_sym) }
38
37
  vars['facts'] = 'removed by the puppet-debugger'
39
38
  end
40
39
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  require 'puppet/pops'
2
3
  require 'facterdb'
3
4
  require 'tempfile'
4
5
 
5
6
  # load all the generators found in the generators directory
6
- Dir.glob(File.join(File.dirname(__FILE__),'support', '*.rb')).each do |file|
7
+ Dir.glob(File.join(File.dirname(__FILE__), 'support', '*.rb')).each do |file|
7
8
  require_relative File.join('support', File.basename(file, '.rb'))
8
9
  end
9
10
 
@@ -23,18 +24,18 @@ module PuppetDebugger
23
24
  def parse_error(error)
24
25
  case error
25
26
  when SocketError
26
- PuppetDebugger::Exception::ConnectError.new(:message => "Unknown host: #{Puppet[:server]}")
27
+ PuppetDebugger::Exception::ConnectError.new(message: "Unknown host: #{Puppet[:server]}")
27
28
  when Net::HTTPError
28
- PuppetDebugger::Exception::AuthError.new(:message => error.message)
29
+ PuppetDebugger::Exception::AuthError.new(message: error.message)
29
30
  when Errno::ECONNREFUSED
30
- PuppetDebugger::Exception::ConnectError.new(:message => error.message)
31
+ PuppetDebugger::Exception::ConnectError.new(message: error.message)
31
32
  when Puppet::Error
32
33
  if error.message =~ /could\ not\ find\ class/i
33
- PuppetDebugger::Exception::NoClassError.new(:default_modules_paths => default_modules_paths,
34
- :message => error.message)
34
+ PuppetDebugger::Exception::NoClassError.new(default_modules_paths: default_modules_paths,
35
+ message: error.message)
35
36
  elsif error.message =~ /default\ node/i
36
- PuppetDebugger::Exception::NodeDefinitionError.new(:default_site_manifest => default_site_manifest,
37
- :message => error.message)
37
+ PuppetDebugger::Exception::NodeDefinitionError.new(default_site_manifest: default_site_manifest,
38
+ message: error.message)
38
39
  else
39
40
  error
40
41
  end
@@ -49,7 +50,7 @@ module PuppetDebugger
49
50
  dirs = []
50
51
  do_initialize if Puppet[:codedir].nil?
51
52
  # add the puppet-debugger directory so we can load any defined functions
52
- dirs << File.join(Puppet[:environmentpath],default_puppet_env_name,'modules') unless Puppet[:environmentpath].empty?
53
+ dirs << File.join(Puppet[:environmentpath], default_puppet_env_name, 'modules') unless Puppet[:environmentpath].empty?
53
54
  dirs << Puppet.settings[:basemodulepath].split(':')
54
55
  dirs.flatten
55
56
  end
@@ -75,40 +76,38 @@ module PuppetDebugger
75
76
  end
76
77
  end
77
78
 
78
- def keyword_expression
79
- @keyword_expression ||= Regexp.new(/^exit|^:set|^play|^classification|^facts|^vars|^functions|^whereami|^classes|^resources|^krt|^environment|^reset|^help/)
80
- end
79
+ def keyword_expression
80
+ @keyword_expression ||= Regexp.new(/^exit|^:set|^play|^classification|^facts|^vars|^functions|^whereami|^classes|^resources|^krt|^environment|^reset|^help/)
81
+ end
81
82
 
82
83
  def known_resource_types
83
84
  res = {
84
- :hostclasses => scope.environment.known_resource_types.hostclasses.keys,
85
- :definitions => scope.environment.known_resource_types.definitions.keys,
86
- :nodes => scope.environment.known_resource_types.nodes.keys,
85
+ hostclasses: scope.environment.known_resource_types.hostclasses.keys,
86
+ definitions: scope.environment.known_resource_types.definitions.keys,
87
+ nodes: scope.environment.known_resource_types.nodes.keys
87
88
  }
88
89
  if sites = scope.environment.known_resource_types.instance_variable_get(:@sites)
89
- res.merge!(:sites => scope.environment.known_resource_types.instance_variable_get(:@sites).first)
90
+ res[:sites] = scope.environment.known_resource_types.instance_variable_get(:@sites).first
90
91
  end
91
92
  if scope.environment.known_resource_types.respond_to?(:applications)
92
- res.merge!(:applications => scope.environment.known_resource_types.applications.keys)
93
+ res[:applications] = scope.environment.known_resource_types.applications.keys
93
94
  end
94
95
  # some versions of puppet do not support capabilities
95
96
  if scope.environment.known_resource_types.respond_to?(:capability_mappings)
96
- res.merge!(:capability_mappings => scope.environment.known_resource_types.capability_mappings.keys)
97
+ res[:capability_mappings] = scope.environment.known_resource_types.capability_mappings.keys
97
98
  end
98
99
  res
99
100
  end
100
101
 
101
102
  # this is required in order to load things only when we need them
102
103
  def do_initialize
103
- begin
104
- Puppet.initialize_settings
105
- Puppet[:parser] = 'future' # this is required in order to work with puppet 3.8
106
- Puppet[:trusted_node_data] = true
107
- rescue ArgumentError => e
108
-
109
- rescue Puppet::DevError => e
110
- # do nothing otherwise calling init twice raises an error
111
- end
104
+ Puppet.initialize_settings
105
+ Puppet[:parser] = 'future' # this is required in order to work with puppet 3.8
106
+ Puppet[:trusted_node_data] = true
107
+ rescue ArgumentError => e
108
+
109
+ rescue Puppet::DevError => e
110
+ # do nothing otherwise calling init twice raises an error
112
111
  end
113
112
 
114
113
  # @param String - any valid puppet language code
@@ -125,14 +124,14 @@ module PuppetDebugger
125
124
  ::Puppet::Pops::Model::AstTransformer.new('').merge_location(args, model)
126
125
 
127
126
  ast_code =
128
- if model.is_a? ::Puppet::Pops::Model::Program
129
- ::Puppet::Parser::AST::PopsBridge::Program.new(model, args)
130
- else
131
- args[:value] = model
132
- ::Puppet::Parser::AST::PopsBridge::Expression.new(args)
133
- end
127
+ if model.is_a? ::Puppet::Pops::Model::Program
128
+ ::Puppet::Parser::AST::PopsBridge::Program.new(model, args)
129
+ else
130
+ args[:value] = model
131
+ ::Puppet::Parser::AST::PopsBridge::Expression.new(args)
132
+ end
134
133
  # Create the "main" class for the content - this content will get merged with all other "main" content
135
- ::Puppet::Parser::AST::Hostclass.new('', :code => ast_code)
134
+ ::Puppet::Parser::AST::Hostclass.new('', code: ast_code)
136
135
  end
137
136
 
138
137
  # @param String - any valid puppet language code
@@ -146,10 +145,10 @@ module PuppetDebugger
146
145
  File.open(file, 'w') do |f|
147
146
  f.write(input)
148
147
  end
149
- Puppet.override( {:code => input, :global_scope => scope, :loaders => scope.compiler.loaders } , 'For puppet-debugger') do
150
- # because the repl is not a module we leave the modname blank
151
- scope.environment.known_resource_types.import_ast(ast, '')
152
- parser.evaluate_string(scope, input, File.expand_path(file))
148
+ Puppet.override({ code: input, global_scope: scope, loaders: scope.compiler.loaders }, 'For puppet-debugger') do
149
+ # because the repl is not a module we leave the modname blank
150
+ scope.environment.known_resource_types.import_ast(ast, '')
151
+ parser.evaluate_string(scope, input, File.expand_path(file))
153
152
  end
154
153
  end
155
154
 
@@ -165,12 +164,11 @@ module PuppetDebugger
165
164
  end
166
165
 
167
166
  def default_manifests_dir
168
- File.join(Puppet[:environmentpath],default_puppet_env_name,'manifests')
167
+ File.join(Puppet[:environmentpath], default_puppet_env_name, 'manifests')
169
168
  end
170
169
 
171
170
  def default_site_manifest
172
171
  File.join(default_manifests_dir, 'site.pp')
173
172
  end
174
-
175
173
  end
176
174
  end