choria-mcorpc-support 2.21.1 → 2.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mcollective.rb +1 -2
  3. data/lib/mcollective/agent/bolt_tasks.ddl +253 -0
  4. data/lib/mcollective/agent/bolt_tasks.json +365 -0
  5. data/lib/mcollective/agent/bolt_tasks.rb +178 -0
  6. data/lib/mcollective/agent/choria_util.ddl +152 -0
  7. data/lib/mcollective/agent/choria_util.json +244 -0
  8. data/lib/mcollective/agent/rpcutil.ddl +8 -4
  9. data/lib/mcollective/agent/rpcutil.json +333 -0
  10. data/lib/mcollective/agent/scout.ddl +169 -0
  11. data/lib/mcollective/agent/scout.json +224 -0
  12. data/lib/mcollective/agents.rb +7 -6
  13. data/lib/mcollective/aggregate.rb +4 -4
  14. data/lib/mcollective/aggregate/average.rb +2 -2
  15. data/lib/mcollective/aggregate/base.rb +2 -2
  16. data/lib/mcollective/aggregate/result.rb +3 -3
  17. data/lib/mcollective/aggregate/result/collection_result.rb +2 -2
  18. data/lib/mcollective/aggregate/result/numeric_result.rb +2 -2
  19. data/lib/mcollective/aggregate/sum.rb +2 -2
  20. data/lib/mcollective/aggregate/summary.rb +3 -4
  21. data/lib/mcollective/application.rb +57 -21
  22. data/lib/mcollective/application/choria.rb +189 -0
  23. data/lib/mcollective/application/completion.rb +6 -6
  24. data/lib/mcollective/application/facts.rb +11 -11
  25. data/lib/mcollective/application/federation.rb +237 -0
  26. data/lib/mcollective/application/find.rb +4 -4
  27. data/lib/mcollective/application/help.rb +3 -3
  28. data/lib/mcollective/application/inventory.rb +3 -341
  29. data/lib/mcollective/application/ping.rb +5 -51
  30. data/lib/mcollective/application/playbook.rb +207 -0
  31. data/lib/mcollective/application/plugin.rb +106 -106
  32. data/lib/mcollective/application/rpc.rb +3 -108
  33. data/lib/mcollective/application/tasks.rb +425 -0
  34. data/lib/mcollective/applications.rb +11 -10
  35. data/lib/mcollective/audit/choria.rb +33 -0
  36. data/lib/mcollective/cache.rb +2 -4
  37. data/lib/mcollective/client.rb +11 -10
  38. data/lib/mcollective/config.rb +21 -34
  39. data/lib/mcollective/connector/base.rb +2 -1
  40. data/lib/mcollective/connector/nats.ddl +9 -0
  41. data/lib/mcollective/connector/nats.rb +450 -0
  42. data/lib/mcollective/data.rb +8 -3
  43. data/lib/mcollective/data/agent_data.rb +1 -1
  44. data/lib/mcollective/data/base.rb +6 -5
  45. data/lib/mcollective/data/bolt_task_data.ddl +90 -0
  46. data/lib/mcollective/data/bolt_task_data.rb +32 -0
  47. data/lib/mcollective/data/collective_data.rb +1 -1
  48. data/lib/mcollective/data/fact_data.rb +6 -6
  49. data/lib/mcollective/data/fstat_data.rb +2 -4
  50. data/lib/mcollective/data/result.rb +7 -2
  51. data/lib/mcollective/ddl/agentddl.rb +5 -17
  52. data/lib/mcollective/ddl/base.rb +10 -13
  53. data/lib/mcollective/discovery.rb +24 -39
  54. data/lib/mcollective/discovery/choria.ddl +11 -0
  55. data/lib/mcollective/discovery/choria.rb +223 -0
  56. data/lib/mcollective/discovery/flatfile.rb +7 -8
  57. data/lib/mcollective/discovery/mc.rb +2 -2
  58. data/lib/mcollective/discovery/stdin.rb +17 -18
  59. data/lib/mcollective/exceptions.rb +13 -0
  60. data/lib/mcollective/facts/base.rb +9 -9
  61. data/lib/mcollective/facts/yaml_facts.rb +12 -12
  62. data/lib/mcollective/generators.rb +3 -3
  63. data/lib/mcollective/generators/agent_generator.rb +3 -4
  64. data/lib/mcollective/generators/base.rb +14 -15
  65. data/lib/mcollective/generators/data_generator.rb +5 -6
  66. data/lib/mcollective/log.rb +2 -2
  67. data/lib/mcollective/logger/base.rb +3 -2
  68. data/lib/mcollective/logger/console_logger.rb +10 -10
  69. data/lib/mcollective/logger/file_logger.rb +7 -7
  70. data/lib/mcollective/logger/syslog_logger.rb +11 -15
  71. data/lib/mcollective/message.rb +8 -39
  72. data/lib/mcollective/monkey_patches.rb +2 -4
  73. data/lib/mcollective/optionparser.rb +2 -1
  74. data/lib/mcollective/pluginmanager.rb +3 -5
  75. data/lib/mcollective/pluginpackager.rb +1 -3
  76. data/lib/mcollective/pluginpackager/agent_definition.rb +3 -8
  77. data/lib/mcollective/pluginpackager/forge_packager.rb +7 -9
  78. data/lib/mcollective/pluginpackager/standard_definition.rb +1 -2
  79. data/lib/mcollective/registration/base.rb +18 -16
  80. data/lib/mcollective/rpc.rb +2 -4
  81. data/lib/mcollective/rpc/actionrunner.rb +16 -18
  82. data/lib/mcollective/rpc/agent.rb +26 -43
  83. data/lib/mcollective/rpc/audit.rb +1 -0
  84. data/lib/mcollective/rpc/client.rb +67 -85
  85. data/lib/mcollective/rpc/helpers.rb +55 -62
  86. data/lib/mcollective/rpc/progress.rb +2 -2
  87. data/lib/mcollective/rpc/reply.rb +17 -19
  88. data/lib/mcollective/rpc/request.rb +7 -5
  89. data/lib/mcollective/rpc/result.rb +6 -8
  90. data/lib/mcollective/rpc/stats.rb +49 -58
  91. data/lib/mcollective/security/base.rb +13 -56
  92. data/lib/mcollective/security/choria.rb +765 -0
  93. data/lib/mcollective/shell.rb +9 -4
  94. data/lib/mcollective/signer/base.rb +28 -0
  95. data/lib/mcollective/signer/choria.rb +185 -0
  96. data/lib/mcollective/ssl.rb +8 -6
  97. data/lib/mcollective/util.rb +58 -55
  98. data/lib/mcollective/util/bolt_support.rb +176 -0
  99. data/lib/mcollective/util/bolt_support/plan_runner.rb +167 -0
  100. data/lib/mcollective/util/bolt_support/task_result.rb +94 -0
  101. data/lib/mcollective/util/bolt_support/task_results.rb +128 -0
  102. data/lib/mcollective/util/choria.rb +946 -0
  103. data/lib/mcollective/util/indifferent_hash.rb +12 -0
  104. data/lib/mcollective/util/natswrapper.rb +242 -0
  105. data/lib/mcollective/util/playbook.rb +435 -0
  106. data/lib/mcollective/util/playbook/data_stores.rb +201 -0
  107. data/lib/mcollective/util/playbook/data_stores/base.rb +99 -0
  108. data/lib/mcollective/util/playbook/data_stores/consul_data_store.rb +88 -0
  109. data/lib/mcollective/util/playbook/data_stores/environment_data_store.rb +33 -0
  110. data/lib/mcollective/util/playbook/data_stores/etcd_data_store.rb +42 -0
  111. data/lib/mcollective/util/playbook/data_stores/file_data_store.rb +106 -0
  112. data/lib/mcollective/util/playbook/data_stores/shell_data_store.rb +103 -0
  113. data/lib/mcollective/util/playbook/inputs.rb +265 -0
  114. data/lib/mcollective/util/playbook/nodes.rb +207 -0
  115. data/lib/mcollective/util/playbook/nodes/mcollective_nodes.rb +86 -0
  116. data/lib/mcollective/util/playbook/nodes/pql_nodes.rb +40 -0
  117. data/lib/mcollective/util/playbook/nodes/shell_nodes.rb +55 -0
  118. data/lib/mcollective/util/playbook/nodes/terraform_nodes.rb +65 -0
  119. data/lib/mcollective/util/playbook/nodes/yaml_nodes.rb +47 -0
  120. data/lib/mcollective/util/playbook/playbook_logger.rb +47 -0
  121. data/lib/mcollective/util/playbook/puppet_logger.rb +51 -0
  122. data/lib/mcollective/util/playbook/report.rb +152 -0
  123. data/lib/mcollective/util/playbook/task_result.rb +55 -0
  124. data/lib/mcollective/util/playbook/tasks.rb +196 -0
  125. data/lib/mcollective/util/playbook/tasks/base.rb +45 -0
  126. data/lib/mcollective/util/playbook/tasks/graphite_event_task.rb +64 -0
  127. data/lib/mcollective/util/playbook/tasks/mcollective_task.rb +356 -0
  128. data/lib/mcollective/util/playbook/tasks/shell_task.rb +93 -0
  129. data/lib/mcollective/util/playbook/tasks/slack_task.rb +105 -0
  130. data/lib/mcollective/util/playbook/tasks/webhook_task.rb +136 -0
  131. data/lib/mcollective/util/playbook/template_util.rb +98 -0
  132. data/lib/mcollective/util/playbook/uses.rb +169 -0
  133. data/lib/mcollective/util/tasks_support.rb +751 -0
  134. data/lib/mcollective/util/tasks_support/cli.rb +260 -0
  135. data/lib/mcollective/util/tasks_support/default_formatter.rb +138 -0
  136. data/lib/mcollective/util/tasks_support/json_formatter.rb +108 -0
  137. data/lib/mcollective/validator.rb +6 -1
  138. data/lib/mcollective/validator/bolt_task_name_validator.ddl +7 -0
  139. data/lib/mcollective/validator/bolt_task_name_validator.rb +11 -0
  140. data/lib/mcollective/validator/length_validator.rb +1 -3
  141. metadata +65 -6
  142. data/lib/mcollective/application/describe_filter.rb +0 -87
  143. data/lib/mcollective/matcher.rb +0 -220
  144. data/lib/mcollective/matcher/parser.rb +0 -128
  145. data/lib/mcollective/matcher/scanner.rb +0 -241
@@ -67,8 +67,8 @@ class String
67
67
  unless method_defined?(:bytes)
68
68
  def bytes(&block)
69
69
  # This should not be necessary, really ...
70
- require "enumerator"
71
70
  return to_enum(:each_byte) unless block_given?
71
+
72
72
  each_byte(&block)
73
73
  end
74
74
  end
@@ -141,9 +141,7 @@ class OpenSSL::SSL::SSLContext # rubocop:disable Style/ClassAndModuleChildren
141
141
  end
142
142
 
143
143
  # ruby 1.8.5 doesn't define this constant, but has it on by default
144
- if defined?(OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS)
145
- DEFAULT_PARAMS[:options] |= OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS
146
- end
144
+ DEFAULT_PARAMS[:options] |= OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS if defined?(OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS)
147
145
 
148
146
  DEFAULT_PARAMS[:ciphers] << ":!SSLv2" if DEFAULT_PARAMS[:ciphers]
149
147
 
@@ -86,7 +86,7 @@ module MCollective
86
86
  end
87
87
 
88
88
  @parser.on("-S", "--select FILTER", "Compound filter combining facts and classes") do |f|
89
- @options[:filter]["compound"] << Matcher.create_compound_callstack(f)
89
+ @options[:filter]["compound"] << [{"expr" => f}]
90
90
  end
91
91
 
92
92
  @parser.on("-F", "--wf", "--with-fact fact=val", "Match hosts with a certain fact") do |f|
@@ -155,6 +155,7 @@ module MCollective
155
155
 
156
156
  @parser.on("--dm", "--disc-method METHOD", "Which discovery method to use") do |v|
157
157
  raise "Discovery method is already set by a competing option" if @options[:discovery_method] && @options[:discovery_method] != v
158
+
158
159
  @options[:discovery_method] = v
159
160
  end
160
161
 
@@ -91,7 +91,7 @@ module MCollective
91
91
 
92
92
  # use eval to create an instance of a class
93
93
  def self.create_instance(klass)
94
- eval("#{klass}.new") # rubocop:disable Security/Eval
94
+ eval("#{klass}.new") # rubocop:disable Security/Eval, Style/EvalWithLocation
95
95
  rescue Exception => e # rubocop:disable Lint/RescueException
96
96
  raise("Could not create instance of plugin #{klass}: #{e}")
97
97
  end
@@ -146,9 +146,7 @@ module MCollective
146
146
  extension = ".#{extension}" unless extension =~ /^\./
147
147
 
148
148
  klasses = find(type, extension).map do |plugin|
149
- if block_given?
150
- next unless yield(plugin)
151
- end
149
+ next if block_given? && !yield(plugin)
152
150
 
153
151
  "%s::%s::%s" % ["MCollective", type.capitalize, plugin.capitalize]
154
152
  end.compact
@@ -159,7 +157,7 @@ module MCollective
159
157
  # Loads a class from file by doing some simple search/replace
160
158
  # on class names and then doing a require.
161
159
  def self.loadclass(klass, squash_failures=false)
162
- fname = klass.gsub("::", "/").downcase + ".rb"
160
+ fname = "#{klass.gsub('::', '/').downcase}.rb"
163
161
 
164
162
  Log.debug("Loading #{klass} from #{fname}")
165
163
 
@@ -84,9 +84,7 @@ module MCollective
84
84
 
85
85
  # Return the path to a plugin's core directories
86
86
  def self.get_plugin_path(target)
87
- if File.exist?(File.join(target, "lib", "mcollective"))
88
- return File.join(target, "lib", "mcollective")
89
- end
87
+ return File.join(target, "lib", "mcollective") if File.exist?(File.join(target, "lib", "mcollective"))
90
88
 
91
89
  target
92
90
  end
@@ -2,8 +2,7 @@ module MCollective
2
2
  module PluginPackager
3
3
  # MCollective Agent Plugin package
4
4
  class AgentDefinition
5
- attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :revision, :preinstall
6
- attr_accessor :plugintype, :dependencies, :postinstall, :mcname, :mcversion
5
+ attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :revision, :preinstall, :plugintype, :dependencies, :postinstall, :mcname, :mcversion
7
6
 
8
7
  def initialize(configuration, mcdependency, plugintype)
9
8
  @plugintype = plugintype
@@ -53,9 +52,7 @@ module MCollective
53
52
  agent[:files] = (Dir.glob(File.join(agentdir, "**", "**")) - ddls)
54
53
  agent[:plugindependency] = {:name => "#{@mcname}-#{@metadata[:name]}-common", :version => @metadata[:version], :revision => @revision}
55
54
 
56
- if @metadata[:provider] == "external"
57
- agent[:executable_files] << File.join(agentdir, @agent_name)
58
- end
55
+ agent[:executable_files] << File.join(agentdir, @agent_name) if @metadata[:provider] == "external"
59
56
 
60
57
  agent
61
58
  end
@@ -95,9 +92,7 @@ module MCollective
95
92
  end
96
93
 
97
94
  # We fail if there is no ddl file present
98
- if common[:files].grep(/^.*\.ddl$/).empty?
99
- raise "cannot create package - No ddl file found in #{File.join(@path, 'agent')}"
100
- end
95
+ raise "cannot create package - No ddl file found in #{File.join(@path, 'agent')}" if common[:files].grep(/^.*\.ddl$/).empty?
101
96
 
102
97
  common[:files].uniq!
103
98
 
@@ -40,7 +40,7 @@ module MCollective
40
40
 
41
41
  puts("Completed building module for %s" % module_name)
42
42
  rescue
43
- STDERR.puts("Failed to build plugin module: %s: %s" % [$!.class, $!.to_s])
43
+ warn("Failed to build plugin module: %s: %s" % [$!.class, $!.to_s])
44
44
  ensure
45
45
  if @keep_artifacts
46
46
  puts("Keeping build artifacts")
@@ -191,12 +191,12 @@ module MCollective
191
191
  end
192
192
 
193
193
  def render_template(infile, outfile)
194
- erb = ERB.new(File.read(infile), nil, "-")
194
+ erb = ERB.new(File.read(infile), 0, "%")
195
195
  File.open(outfile, "w") do |f|
196
196
  f.puts erb.result(binding)
197
197
  end
198
198
  rescue
199
- STDERR.puts("Could not render template %s to %s" % [infile, outfile])
199
+ warn("Could not render template %s to %s" % [infile, outfile])
200
200
  raise
201
201
  end
202
202
 
@@ -218,9 +218,7 @@ module MCollective
218
218
  actual_version = s.stdout.chomp
219
219
  required_version = "1.12.0"
220
220
 
221
- if Util.versioncmp(actual_version, required_version) < 0
222
- raise("Cannot build package. pdk #{required_version} or greater required. We have #{actual_version}.")
223
- end
221
+ raise("Cannot build package. pdk #{required_version} or greater required. We have #{actual_version}.") if Util.versioncmp(actual_version, required_version) < 0
224
222
  end
225
223
 
226
224
  def run_build
@@ -230,7 +228,7 @@ module MCollective
230
228
  end
231
229
  end
232
230
  rescue
233
- STDERR.puts("Build process has failed")
231
+ warn("Build process has failed")
234
232
  raise
235
233
  end
236
234
 
@@ -238,14 +236,14 @@ module MCollective
238
236
  package_file = File.join(@tmpdir, "pkg", module_file_name)
239
237
  FileUtils.cp(package_file, ".")
240
238
  rescue
241
- STDERR.puts("Could not copy package to working directory")
239
+ warn("Could not copy package to working directory")
242
240
  raise
243
241
  end
244
242
 
245
243
  def cleanup_tmpdirs
246
244
  FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
247
245
  rescue
248
- STDERR.puts("Could not remove temporary build directory %s" % [@tmpdir])
246
+ warn("Could not remove temporary build directory %s" % [@tmpdir])
249
247
  raise
250
248
  end
251
249
  end
@@ -1,8 +1,7 @@
1
1
  module MCollective
2
2
  module PluginPackager
3
3
  class StandardDefinition
4
- attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :revision
5
- attr_accessor :plugintype, :preinstall, :postinstall, :dependencies, :mcname, :mcversion
4
+ attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :revision, :plugintype, :preinstall, :postinstall, :dependencies, :mcname, :mcversion
6
5
 
7
6
  def initialize(configuration, mcdependency, plugintype)
8
7
  @plugintype = plugintype
@@ -10,6 +10,7 @@ module MCollective
10
10
  # Register plugins that inherits base
11
11
  def self.inherited(klass)
12
12
  PluginManager << {:type => "registration_plugin", :class => klass.to_s}
13
+ super
13
14
  end
14
15
 
15
16
  # Creates a background thread that periodically send a registration notice.
@@ -44,7 +45,7 @@ module MCollective
44
45
  collective = main_collective
45
46
  end
46
47
 
47
- return collective
48
+ collective
48
49
  end
49
50
 
50
51
  def interval
@@ -52,15 +53,15 @@ module MCollective
52
53
  end
53
54
 
54
55
  def publish(message)
55
- unless message
56
- Log.debug("Skipping registration due to nil body")
57
- else
56
+ if message
58
57
  req = Message.new(message, nil, {:type => :request, :agent => "registration", :collective => target_collective, :filter => msg_filter})
59
58
  req.encode!
60
59
 
61
60
  Log.debug("Sending registration #{req.requestid} to collective #{req.collective}")
62
61
 
63
62
  req.publish
63
+ else
64
+ Log.debug("Skipping registration due to nil body")
64
65
  end
65
66
  end
66
67
 
@@ -69,22 +70,23 @@ module MCollective
69
70
  end
70
71
 
71
72
  private
73
+
72
74
  def publish_thread(connnection)
73
75
  if config.registration_splay
74
- splay_delay = rand(interval)
75
- Log.debug("registration_splay enabled. Registration will start in #{splay_delay} seconds")
76
- sleep splay_delay
77
- end
76
+ splay_delay = rand(interval)
77
+ Log.debug("registration_splay enabled. Registration will start in #{splay_delay} seconds")
78
+ sleep splay_delay
79
+ end
78
80
 
79
- loop do
80
- begin
81
- publish(body)
82
- sleep interval
83
- rescue Exception => e
84
- Log.error("Sending registration message failed: #{e}")
85
- sleep interval
86
- end
81
+ loop do
82
+ begin
83
+ publish(body)
84
+ sleep interval
85
+ rescue Exception => e # rubocop:disable Lint/RescueException
86
+ Log.error("Sending registration message failed: #{e}")
87
+ sleep interval
87
88
  end
89
+ end
88
90
  end
89
91
  end
90
92
  end
@@ -21,13 +21,11 @@ module MCollective
21
21
  def rpcoptions
22
22
  oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true}, "filter")
23
23
 
24
- options = oparser.parse do |parser, opts|
24
+ oparser.parse do |parser, opts|
25
25
  yield(parser, opts) if block_given?
26
26
 
27
27
  Helpers.add_simplerpc_options(parser, opts)
28
28
  end
29
-
30
- options
31
29
  end
32
30
 
33
31
  # Wrapper to create clients, supposed to be used as
@@ -86,7 +84,7 @@ module MCollective
86
84
  if block_given?
87
85
  yield(rpc)
88
86
  else
89
- return rpc
87
+ rpc
90
88
  end
91
89
  end
92
90
 
@@ -35,11 +35,11 @@ module MCollective
35
35
 
36
36
  def run
37
37
  unless canrun?(command)
38
- Log.warn("Cannot run #{to_s}")
39
- raise RPCAborted, "Cannot execute #{to_s}"
38
+ Log.warn("Cannot run #{self}")
39
+ raise RPCAborted, "Cannot execute #{self}"
40
40
  end
41
41
 
42
- Log.debug("Running #{to_s}")
42
+ Log.debug("Running #{self}")
43
43
 
44
44
  request_file = saverequest(request)
45
45
  reply_file = tempfile("reply")
@@ -51,13 +51,13 @@ module MCollective
51
51
 
52
52
  Log.debug("#{command} exited with #{runner.status.exitstatus}")
53
53
 
54
- stderr.each_line {|l| Log.error("#{to_s}: #{l.chomp}")} unless stderr.empty?
55
- stdout.each_line {|l| Log.info("#{to_s}: #{l.chomp}")} unless stdout.empty?
54
+ stderr.each_line {|l| Log.error("#{self}: #{l.chomp}")} unless stderr.empty?
55
+ stdout.each_line {|l| Log.info("#{self}: #{l.chomp}")} unless stdout.empty?
56
56
 
57
57
  {:exitstatus => runner.status.exitstatus,
58
- :stdout => runner.stdout,
59
- :stderr => runner.stderr,
60
- :data => load_results(reply_file.path)}
58
+ :stdout => runner.stdout,
59
+ :stderr => runner.stderr,
60
+ :data => load_results(reply_file.path)}
61
61
  ensure
62
62
  request_file.close! if request_file.respond_to?("close!")
63
63
  reply_file.close! if reply_file.respond_to?("close")
@@ -65,7 +65,7 @@ module MCollective
65
65
 
66
66
  def shell(command, infile, outfile)
67
67
  env = {"MCOLLECTIVE_REQUEST_FILE" => infile,
68
- "MCOLLECTIVE_REPLY_FILE" => outfile}
68
+ "MCOLLECTIVE_REPLY_FILE" => outfile}
69
69
 
70
70
  Shell.new("#{command} #{infile} #{outfile}", :cwd => Dir.tmpdir, :stdout => stdout, :stderr => stderr, :environment => env)
71
71
  end
@@ -78,20 +78,20 @@ module MCollective
78
78
  if respond_to?("load_#{format}_results")
79
79
  tempdata = send("load_#{format}_results", file)
80
80
 
81
- tempdata.each_pair do |k,v|
81
+ tempdata.each_pair do |k, v|
82
82
  data[k.to_sym] = v
83
83
  end
84
84
  end
85
85
 
86
86
  data
87
- rescue Exception => e
87
+ rescue Exception # rubocop:disable Lint/RescueException
88
88
  {}
89
89
  end
90
90
 
91
91
  def load_json_results(file)
92
92
  return {} unless File.readable?(file)
93
93
 
94
- JSON.load(File.read(file)) || {}
94
+ JSON.parse(File.read(file)) || {}
95
95
  rescue JSON::ParserError
96
96
  {}
97
97
  end
@@ -119,7 +119,7 @@ module MCollective
119
119
  end
120
120
 
121
121
  def to_s
122
- "%s#%s command: %s" % [ agent, action, command ]
122
+ "%s#%s command: %s" % [agent, action, command]
123
123
  end
124
124
 
125
125
  def tempfile(prefix)
@@ -127,15 +127,13 @@ module MCollective
127
127
  end
128
128
 
129
129
  def path_to_command(command)
130
- if Util.absolute_path?(command)
131
- return command
132
- end
130
+ return command if Util.absolute_path?(command)
133
131
 
134
132
  Config.instance.libdir.each do |libdir|
135
133
  command_file_old = File.join(libdir, "agent", agent, command)
136
134
  command_file_new = File.join(libdir, "mcollective", "agent", agent, command)
137
- command_file_old_exists = File.exists?(command_file_old)
138
- command_file_new_exists = File.exists?(command_file_new)
135
+ command_file_old_exists = File.exist?(command_file_old)
136
+ command_file_new_exists = File.exist?(command_file_new)
139
137
 
140
138
  if command_file_new_exists && command_file_old_exists
141
139
  Log.debug("Found 'implemented_by' scripts found in two locations #{command_file_old} and #{command_file_new}")
@@ -53,8 +53,7 @@ module MCollective
53
53
  @ddl = DDL.new(@agent_name, :agent)
54
54
  @meta = @ddl.meta
55
55
  @timeout = @meta[:timeout] || 10
56
-
57
- rescue Exception => e
56
+ rescue Exception => e # rubocop:disable Lint/RescueException
58
57
  Log.error("Failed to load DDL for the '%s' agent, DDLs are required: %s: %s" % [@agent_name, e.class, e.to_s])
59
58
  raise DDLValidationError
60
59
  end
@@ -89,35 +88,29 @@ module MCollective
89
88
  end
90
89
  rescue RPCAborted => e
91
90
  @reply.fail e.to_s, 1
92
-
93
91
  rescue UnknownRPCAction => e
94
92
  @reply.fail e.to_s, 2
95
-
96
93
  rescue MissingRPCData => e
97
94
  @reply.fail e.to_s, 3
98
-
99
95
  rescue InvalidRPCData, DDLValidationError => e
100
96
  @reply.fail e.to_s, 4
101
-
102
97
  rescue UnknownRPCError => e
103
98
  Log.error("%s#%s failed: %s: %s" % [@agent_name, @request.action, e.class, e.to_s])
104
99
  Log.error(e.backtrace.join("\n\t"))
105
100
  @reply.fail e.to_s, 5
106
-
107
- rescue Exception => e
101
+ rescue Exception => e # rubocop:disable Lint/RescueException
108
102
  Log.error("%s#%s failed: %s: %s" % [@agent_name, @request.action, e.class, e.to_s])
109
103
  Log.error(e.backtrace.join("\n\t"))
110
104
  @reply.fail e.to_s, 5
111
-
112
105
  end
113
106
 
114
107
  after_processing_hook
115
108
 
116
109
  if @request.should_respond?
117
- return @reply.to_hash
110
+ @reply.to_hash
118
111
  else
119
112
  Log.debug("Client did not request a response, surpressing reply")
120
- return nil
113
+ nil
121
114
  end
122
115
  end
123
116
 
@@ -137,7 +130,7 @@ module MCollective
137
130
  # File.exist?("/usr/bin/puppet")
138
131
  # end
139
132
  def self.activate?
140
- agent_name = self.to_s.split("::").last.downcase
133
+ agent_name = to_s.split("::").last.downcase
141
134
  config = Config.instance
142
135
 
143
136
  Log.debug("Starting default activation checks for #{agent_name}")
@@ -146,14 +139,12 @@ module MCollective
146
139
  should_activate = config.activate_agents
147
140
 
148
141
  # Check agent specific state to determine if agent should be loaded
149
- should_activate = Util.str_to_bool(config.pluginconf.fetch("#{agent_name}.activate_agent",
150
- should_activate))
142
+ should_activate = Util.str_to_bool(config.pluginconf.fetch("#{agent_name}.activate_agent",
143
+ should_activate))
151
144
 
152
- unless should_activate
153
- Log.debug("Found plugin configuration '#{agent_name}.activate_agent' with value '#{should_activate}'")
154
- end
145
+ Log.debug("Found plugin configuration '#{agent_name}.activate_agent' with value '#{should_activate}'") unless should_activate
155
146
 
156
- return should_activate
147
+ should_activate
157
148
  end
158
149
 
159
150
  # Returns an array of actions this agent support
@@ -163,7 +154,6 @@ module MCollective
163
154
  end
164
155
  end
165
156
 
166
- private
167
157
  # Runs a command via the MC::Shell wrapper, options are as per MC::Shell
168
158
  #
169
159
  # The simplest use is:
@@ -206,22 +196,18 @@ module MCollective
206
196
  [:stderr, :stdout].each do |k|
207
197
  if options.include?(k)
208
198
  if options[k].is_a?(Symbol)
209
- reply[ options[k] ] = ""
210
- shellopts[k] = reply[ options[k] ]
199
+ reply[options[k]] = ""
200
+ shellopts[k] = reply[options[k]]
201
+ elsif options[k].respond_to?("<<")
202
+ shellopts[k] = options[k]
211
203
  else
212
- if options[k].respond_to?("<<")
213
- shellopts[k] = options[k]
214
- else
215
- reply.fail! "#{k} should support << while calling run(#{command})"
216
- end
204
+ reply.fail! "#{k} should support << while calling run(#{command})"
217
205
  end
218
206
  end
219
207
  end
220
208
 
221
209
  [:stdin, :cwd, :environment, :timeout].each do |k|
222
- if options.include?(k)
223
- shellopts[k] = options[k]
224
- end
210
+ shellopts[k] = options[k] if options.include?(k)
225
211
  end
226
212
 
227
213
  shell = Shell.new(command, shellopts)
@@ -263,7 +249,7 @@ module MCollective
263
249
  def self.action(name, &block)
264
250
  raise "Need to pass a body for the action" unless block_given?
265
251
 
266
- self.module_eval { define_method("#{name}_action", &block) }
252
+ module_eval { define_method("#{name}_action", &block) }
267
253
  end
268
254
 
269
255
  # Helper that creates a method on the class that will call your authorization
@@ -272,16 +258,18 @@ module MCollective
272
258
  plugin = plugin.to_s.capitalize
273
259
 
274
260
  # turns foo_bar into FooBar
275
- plugin = plugin.to_s.split("_").map {|v| v.capitalize}.join
261
+ plugin = plugin.to_s.split("_").map(&:capitalize).join
276
262
  pluginname = "MCollective::Util::#{plugin}"
277
263
 
278
264
  PluginManager.loadclass(pluginname) unless MCollective::Util.constants.include?(plugin)
279
265
 
266
+ # rubocop:disable Style/EvalWithLocation
280
267
  class_eval("
281
268
  def authorization_hook(request)
282
269
  #{pluginname}.authorize(request)
283
270
  end
284
271
  ")
272
+ # rubocop:enable Style/EvalWithLocation
285
273
  end
286
274
 
287
275
  # Validates a data member, if validation is a regex then it will try to match it
@@ -304,7 +292,7 @@ module MCollective
304
292
 
305
293
  Validator.validate(@request[key], validation)
306
294
  rescue ValidatorError => e
307
- raise InvalidRPCData, "Input %s did not pass validation: %s" % [ key, e.message ]
295
+ raise InvalidRPCData, "Input %s did not pass validation: %s" % [key, e.message]
308
296
  end
309
297
 
310
298
  # convenience wrapper around Util#shellescape
@@ -323,10 +311,8 @@ module MCollective
323
311
 
324
312
  reply.data.merge!(res[:data])
325
313
 
326
- if res[:exitstatus] > 0
327
- reply.fail "Failed to run #{command}: #{res[:stderr]}", res[:exitstatus]
328
- end
329
- rescue Exception => e
314
+ reply.fail "Failed to run #{command}: #{res[:stderr]}", res[:exitstatus] if res[:exitstatus] > 0
315
+ rescue Exception => e # rubocop:disable Lint/RescueException
330
316
  Log.warn("Unhandled #{e.class} exception during #{request.agent}##{request.action}: #{e}")
331
317
  reply.fail! "Unexpected failure calling #{command}: #{e.class}: #{e}"
332
318
  end
@@ -338,15 +324,13 @@ module MCollective
338
324
  # This will not be called right when the daemon starts up, we use
339
325
  # lazy loading and initialization so it will only be called the first
340
326
  # time a request for this agent arrives.
341
- def startup_hook
342
- end
327
+ def startup_hook; end
343
328
 
344
329
  # Called just after a message was received from the middleware before
345
330
  # it gets passed to the handlers. @request and @reply will already be
346
331
  # set, the msg passed is the message as received from the normal
347
332
  # mcollective runner and the connection is the actual connector.
348
- def before_processing_hook(msg, connection)
349
- end
333
+ def before_processing_hook(msg, connection); end
350
334
 
351
335
  # Called at the end of processing just before the response gets sent
352
336
  # to the middleware.
@@ -356,8 +340,7 @@ module MCollective
356
340
  # it is outside of the block is so you'll have access to even status codes
357
341
  # set by the exception handlers. If you do raise an exception it will just
358
342
  # be passed onto the runner and processing will fail.
359
- def after_processing_hook
360
- end
343
+ def after_processing_hook; end
361
344
 
362
345
  # Gets called right after a request was received and calls audit plugins
363
346
  #
@@ -366,7 +349,7 @@ module MCollective
366
349
  # do not care for the auditing in a specific agent.
367
350
  def audit_request(msg, connection)
368
351
  PluginManager["rpcaudit_plugin"].audit_request(msg, connection) if @config.rpcaudit
369
- rescue Exception => e
352
+ rescue Exception => e # rubocop:disable Lint/RescueException
370
353
  Log.warn("Audit failed - #{e} - continuing to process message")
371
354
  end
372
355
  end