bolt 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bolt might be problematic. Click here for more details.

Files changed (205) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bolt/cli.rb +30 -131
  3. data/lib/bolt/config.rb +35 -9
  4. data/lib/bolt/executor.rb +33 -7
  5. data/lib/bolt/node.rb +19 -2
  6. data/lib/bolt/node/orch.rb +4 -4
  7. data/lib/bolt/node/ssh.rb +46 -21
  8. data/lib/bolt/node/winrm.rb +12 -23
  9. data/lib/bolt/outputter.rb +9 -0
  10. data/lib/bolt/outputter/human.rb +33 -4
  11. data/lib/bolt/outputter/json.rb +6 -1
  12. data/lib/bolt/pal.rb +87 -20
  13. data/lib/bolt/version.rb +1 -1
  14. data/modules/boltlib/lib/puppet/functions/run_command.rb +1 -1
  15. data/modules/boltlib/lib/puppet/functions/run_plan.rb +18 -1
  16. data/modules/boltlib/lib/puppet/functions/run_script.rb +1 -1
  17. data/modules/boltlib/lib/puppet/functions/run_task.rb +2 -1
  18. data/vendored/facter/lib/facter/virtual.rb +2 -0
  19. data/vendored/hiera/lib/hiera/config.rb +2 -2
  20. data/vendored/hiera/lib/hiera/version.rb +1 -1
  21. data/vendored/puppet/lib/hiera/puppet_function.rb +5 -2
  22. data/vendored/puppet/lib/hiera_puppet.rb +3 -3
  23. data/vendored/puppet/lib/puppet/agent/locker.rb +6 -5
  24. data/vendored/puppet/lib/puppet/application/filebucket.rb +23 -6
  25. data/vendored/puppet/lib/puppet/daemon.rb +2 -2
  26. data/vendored/puppet/lib/puppet/defaults.rb +42 -10
  27. data/vendored/puppet/lib/puppet/error.rb +5 -15
  28. data/vendored/puppet/lib/puppet/external/pson/common.rb +2 -2
  29. data/vendored/puppet/lib/puppet/face/module/build.rb +1 -0
  30. data/vendored/puppet/lib/puppet/face/module/install.rb +1 -0
  31. data/vendored/puppet/lib/puppet/face/module/search.rb +2 -1
  32. data/vendored/puppet/lib/puppet/face/module/uninstall.rb +1 -0
  33. data/vendored/puppet/lib/puppet/face/module/upgrade.rb +1 -0
  34. data/vendored/puppet/lib/puppet/file_bucket/dipper.rb +1 -1
  35. data/vendored/puppet/lib/puppet/file_serving/base.rb +2 -1
  36. data/vendored/puppet/lib/puppet/file_serving/configuration/parser.rb +21 -9
  37. data/vendored/puppet/lib/puppet/file_serving/http_metadata.rb +1 -1
  38. data/vendored/puppet/lib/puppet/forge.rb +2 -1
  39. data/vendored/puppet/lib/puppet/functions/binary_file.rb +1 -0
  40. data/vendored/puppet/lib/puppet/functions/eyaml_lookup_key.rb +4 -2
  41. data/vendored/puppet/lib/puppet/functions/module_directory.rb +41 -0
  42. data/vendored/puppet/lib/puppet/functions/strftime.rb +1 -1
  43. data/vendored/puppet/lib/puppet/functions/unique.rb +1 -1
  44. data/vendored/puppet/lib/puppet/functions/yaml_data.rb +1 -1
  45. data/vendored/puppet/lib/puppet/gettext/config.rb +10 -5
  46. data/vendored/puppet/lib/puppet/graph/relationship_graph.rb +1 -1
  47. data/vendored/puppet/lib/puppet/indirector/catalog/compiler.rb +1 -1
  48. data/vendored/puppet/lib/puppet/indirector/exec.rb +1 -1
  49. data/vendored/puppet/lib/puppet/indirector/facts/facter.rb +3 -3
  50. data/vendored/puppet/lib/puppet/indirector/facts/network_device.rb +2 -2
  51. data/vendored/puppet/lib/puppet/indirector/file_bucket_file/file.rb +10 -3
  52. data/vendored/puppet/lib/puppet/indirector/indirection.rb +17 -9
  53. data/vendored/puppet/lib/puppet/indirector/ldap.rb +2 -2
  54. data/vendored/puppet/lib/puppet/indirector/node/write_only_yaml.rb +3 -1
  55. data/vendored/puppet/lib/puppet/indirector/rest.rb +2 -1
  56. data/vendored/puppet/lib/puppet/indirector/ssl_file.rb +2 -2
  57. data/vendored/puppet/lib/puppet/indirector/terminus.rb +3 -3
  58. data/vendored/puppet/lib/puppet/interface.rb +1 -1
  59. data/vendored/puppet/lib/puppet/interface/action.rb +41 -24
  60. data/vendored/puppet/lib/puppet/interface/action_builder.rb +14 -5
  61. data/vendored/puppet/lib/puppet/interface/action_manager.rb +1 -1
  62. data/vendored/puppet/lib/puppet/interface/documentation.rb +21 -10
  63. data/vendored/puppet/lib/puppet/interface/face_collection.rb +4 -2
  64. data/vendored/puppet/lib/puppet/interface/option.rb +36 -15
  65. data/vendored/puppet/lib/puppet/interface/option_builder.rb +23 -9
  66. data/vendored/puppet/lib/puppet/interface/option_manager.rb +8 -3
  67. data/vendored/puppet/lib/puppet/metatype/manager.rb +7 -3
  68. data/vendored/puppet/lib/puppet/module.rb +2 -1
  69. data/vendored/puppet/lib/puppet/module_tool/applications/builder.rb +4 -0
  70. data/vendored/puppet/lib/puppet/module_tool/applications/installer.rb +3 -0
  71. data/vendored/puppet/lib/puppet/module_tool/applications/uninstaller.rb +3 -0
  72. data/vendored/puppet/lib/puppet/module_tool/applications/upgrader.rb +3 -0
  73. data/vendored/puppet/lib/puppet/module_tool/errors/shared.rb +3 -3
  74. data/vendored/puppet/lib/puppet/network/auth_config_parser.rb +10 -5
  75. data/vendored/puppet/lib/puppet/network/authstore.rb +2 -2
  76. data/vendored/puppet/lib/puppet/network/client_request.rb +1 -1
  77. data/vendored/puppet/lib/puppet/network/format.rb +3 -3
  78. data/vendored/puppet/lib/puppet/network/http/api/indirection_type.rb +1 -1
  79. data/vendored/puppet/lib/puppet/network/http/api/master/v3/environment.rb +5 -2
  80. data/vendored/puppet/lib/puppet/network/http/webrick.rb +1 -3
  81. data/vendored/puppet/lib/puppet/node.rb +16 -7
  82. data/vendored/puppet/lib/puppet/parameter.rb +3 -3
  83. data/vendored/puppet/lib/puppet/parameter/value_collection.rb +5 -2
  84. data/vendored/puppet/lib/puppet/parser/ast/leaf.rb +1 -1
  85. data/vendored/puppet/lib/puppet/parser/ast/node.rb +2 -2
  86. data/vendored/puppet/lib/puppet/parser/compiler.rb +6 -5
  87. data/vendored/puppet/lib/puppet/parser/functions.rb +1 -1
  88. data/vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb +6 -2
  89. data/vendored/puppet/lib/puppet/parser/functions/generate.rb +2 -4
  90. data/vendored/puppet/lib/puppet/parser/functions/inline_template.rb +1 -2
  91. data/vendored/puppet/lib/puppet/parser/functions/sha256.rb +5 -0
  92. data/vendored/puppet/lib/puppet/parser/functions/template.rb +6 -2
  93. data/vendored/puppet/lib/puppet/parser/resource.rb +18 -91
  94. data/vendored/puppet/lib/puppet/parser/scope.rb +12 -11
  95. data/vendored/puppet/lib/puppet/parser/templatewrapper.rb +1 -1
  96. data/vendored/puppet/lib/puppet/pops/evaluator/collector_transformer.rb +2 -1
  97. data/vendored/puppet/lib/puppet/pops/evaluator/collectors/abstract_collector.rb +1 -1
  98. data/vendored/puppet/lib/puppet/pops/evaluator/epp_evaluator.rb +21 -8
  99. data/vendored/puppet/lib/puppet/pops/evaluator/evaluator_impl.rb +2 -1
  100. data/vendored/puppet/lib/puppet/pops/evaluator/external_syntax_support.rb +1 -1
  101. data/vendored/puppet/lib/puppet/pops/evaluator/runtime3_converter.rb +3 -1
  102. data/vendored/puppet/lib/puppet/pops/evaluator/runtime3_resource_support.rb +1 -1
  103. data/vendored/puppet/lib/puppet/pops/evaluator/runtime3_support.rb +5 -6
  104. data/vendored/puppet/lib/puppet/pops/functions/dispatch.rb +1 -1
  105. data/vendored/puppet/lib/puppet/pops/functions/function.rb +8 -2
  106. data/vendored/puppet/lib/puppet/pops/issue_reporter.rb +7 -2
  107. data/vendored/puppet/lib/puppet/pops/issues.rb +14 -4
  108. data/vendored/puppet/lib/puppet/pops/label_provider.rb +1 -1
  109. data/vendored/puppet/lib/puppet/pops/loader/base_loader.rb +2 -2
  110. data/vendored/puppet/lib/puppet/pops/loader/dependency_loader.rb +2 -2
  111. data/vendored/puppet/lib/puppet/pops/loader/loader.rb +6 -1
  112. data/vendored/puppet/lib/puppet/pops/loader/loader_paths.rb +2 -2
  113. data/vendored/puppet/lib/puppet/pops/loader/module_loaders.rb +27 -6
  114. data/vendored/puppet/lib/puppet/pops/loader/runtime3_type_loader.rb +2 -2
  115. data/vendored/puppet/lib/puppet/pops/loader/static_loader.rb +1 -1
  116. data/vendored/puppet/lib/puppet/pops/loaders.rb +7 -3
  117. data/vendored/puppet/lib/puppet/pops/lookup/explainer.rb +2 -1
  118. data/vendored/puppet/lib/puppet/pops/merge_strategy.rb +6 -3
  119. data/vendored/puppet/lib/puppet/pops/migration/migration_checker.rb +8 -8
  120. data/vendored/puppet/lib/puppet/pops/model/factory.rb +4 -2
  121. data/vendored/puppet/lib/puppet/pops/resource/resource_type_impl.rb +1 -1
  122. data/vendored/puppet/lib/puppet/pops/serialization/abstract_writer.rb +2 -1
  123. data/vendored/puppet/lib/puppet/pops/serialization/to_data_converter.rb +6 -2
  124. data/vendored/puppet/lib/puppet/pops/types/p_object_type.rb +36 -15
  125. data/vendored/puppet/lib/puppet/pops/types/type_calculator.rb +2 -1
  126. data/vendored/puppet/lib/puppet/pops/types/type_factory.rb +3 -1
  127. data/vendored/puppet/lib/puppet/pops/types/type_mismatch_describer.rb +4 -1
  128. data/vendored/puppet/lib/puppet/pops/types/type_parser.rb +75 -7
  129. data/vendored/puppet/lib/puppet/pops/types/types.rb +9 -5
  130. data/vendored/puppet/lib/puppet/pops/validation.rb +14 -12
  131. data/vendored/puppet/lib/puppet/property.rb +14 -6
  132. data/vendored/puppet/lib/puppet/property/ensure.rb +2 -2
  133. data/vendored/puppet/lib/puppet/provider.rb +4 -4
  134. data/vendored/puppet/lib/puppet/provider/group/aix.rb +4 -2
  135. data/vendored/puppet/lib/puppet/provider/group/groupadd.rb +25 -1
  136. data/vendored/puppet/lib/puppet/provider/ldap.rb +2 -2
  137. data/vendored/puppet/lib/puppet/provider/naginator.rb +1 -1
  138. data/vendored/puppet/lib/puppet/provider/nameservice.rb +7 -4
  139. data/vendored/puppet/lib/puppet/provider/nameservice/directoryservice.rb +4 -3
  140. data/vendored/puppet/lib/puppet/provider/nameservice/objectadd.rb +13 -24
  141. data/vendored/puppet/lib/puppet/provider/nameservice/pw.rb +14 -14
  142. data/vendored/puppet/lib/puppet/provider/package/aix.rb +1 -1
  143. data/vendored/puppet/lib/puppet/provider/package/pkg.rb +3 -2
  144. data/vendored/puppet/lib/puppet/provider/package/yum.rb +1 -1
  145. data/vendored/puppet/lib/puppet/provider/parsedfile.rb +4 -4
  146. data/vendored/puppet/lib/puppet/provider/service/base.rb +1 -1
  147. data/vendored/puppet/lib/puppet/provider/service/launchd.rb +2 -2
  148. data/vendored/puppet/lib/puppet/provider/service/runit.rb +2 -1
  149. data/vendored/puppet/lib/puppet/provider/service/systemd.rb +1 -0
  150. data/vendored/puppet/lib/puppet/provider/service/upstart.rb +1 -1
  151. data/vendored/puppet/lib/puppet/provider/user/aix.rb +13 -6
  152. data/vendored/puppet/lib/puppet/provider/user/openbsd.rb +2 -1
  153. data/vendored/puppet/lib/puppet/provider/user/useradd.rb +33 -4
  154. data/vendored/puppet/lib/puppet/provider/yumrepo/inifile.rb +2 -1
  155. data/vendored/puppet/lib/puppet/provider/zone/solaris.rb +2 -2
  156. data/vendored/puppet/lib/puppet/relationship.rb +2 -1
  157. data/vendored/puppet/lib/puppet/reports/store.rb +1 -1
  158. data/vendored/puppet/lib/puppet/resource.rb +7 -5
  159. data/vendored/puppet/lib/puppet/resource/capability_finder.rb +14 -11
  160. data/vendored/puppet/lib/puppet/resource/catalog.rb +33 -18
  161. data/vendored/puppet/lib/puppet/resource/type.rb +5 -5
  162. data/vendored/puppet/lib/puppet/settings.rb +19 -13
  163. data/vendored/puppet/lib/puppet/settings/base_setting.rb +23 -8
  164. data/vendored/puppet/lib/puppet/settings/config_file.rb +14 -4
  165. data/vendored/puppet/lib/puppet/settings/environment_conf.rb +5 -7
  166. data/vendored/puppet/lib/puppet/settings/ini_file.rb +4 -0
  167. data/vendored/puppet/lib/puppet/ssl/base.rb +9 -4
  168. data/vendored/puppet/lib/puppet/ssl/oids.rb +8 -2
  169. data/vendored/puppet/lib/puppet/transaction.rb +6 -2
  170. data/vendored/puppet/lib/puppet/transaction/persistence.rb +15 -0
  171. data/vendored/puppet/lib/puppet/transaction/report.rb +9 -1
  172. data/vendored/puppet/lib/puppet/type.rb +26 -26
  173. data/vendored/puppet/lib/puppet/type/file.rb +2 -1
  174. data/vendored/puppet/lib/puppet/type/file/data_sync.rb +2 -1
  175. data/vendored/puppet/lib/puppet/type/file/source.rb +1 -1
  176. data/vendored/puppet/lib/puppet/type/group.rb +4 -1
  177. data/vendored/puppet/lib/puppet/type/mount.rb +1 -1
  178. data/vendored/puppet/lib/puppet/type/resources.rb +1 -1
  179. data/vendored/puppet/lib/puppet/type/user.rb +12 -7
  180. data/vendored/puppet/lib/puppet/type/zone.rb +1 -1
  181. data/vendored/puppet/lib/puppet/util.rb +15 -21
  182. data/vendored/puppet/lib/puppet/util/at_fork/solaris.rb +6 -4
  183. data/vendored/puppet/lib/puppet/util/command_line/trollop.rb +6 -5
  184. data/vendored/puppet/lib/puppet/util/errors.rb +59 -8
  185. data/vendored/puppet/lib/puppet/util/fileparsing.rb +3 -5
  186. data/vendored/puppet/lib/puppet/util/http_proxy.rb +14 -6
  187. data/vendored/puppet/lib/puppet/util/instance_loader.rb +1 -3
  188. data/vendored/puppet/lib/puppet/util/ldap/manager.rb +6 -3
  189. data/vendored/puppet/lib/puppet/util/log.rb +15 -21
  190. data/vendored/puppet/lib/puppet/util/logging.rb +21 -28
  191. data/vendored/puppet/lib/puppet/util/methodhelper.rb +1 -1
  192. data/vendored/puppet/lib/puppet/util/nagios_maker.rb +1 -1
  193. data/vendored/puppet/lib/puppet/util/network_device/config.rb +20 -13
  194. data/vendored/puppet/lib/puppet/util/platform.rb +13 -0
  195. data/vendored/puppet/lib/puppet/util/posix.rb +1 -1
  196. data/vendored/puppet/lib/puppet/util/provider_features.rb +1 -1
  197. data/vendored/puppet/lib/puppet/util/rdoc/generators/puppet_generator.rb +1 -1
  198. data/vendored/puppet/lib/puppet/util/selinux.rb +1 -1
  199. data/vendored/puppet/lib/puppet/util/windows/eventlog.rb +10 -5
  200. data/vendored/puppet/lib/puppet/util/windows/file.rb +61 -9
  201. data/vendored/puppet/lib/puppet/util/windows/process.rb +1 -1
  202. data/vendored/puppet/lib/puppet/util/windows/taskscheduler.rb +2 -2
  203. data/vendored/puppet/lib/puppet/version.rb +1 -1
  204. data/vendored/puppet/lib/puppet_pal.rb +23 -22
  205. metadata +4 -2
@@ -43,7 +43,7 @@ module Bolt
43
43
  end
44
44
  end
45
45
 
46
- def run_task(task, _input_method, arguments)
46
+ def run_task(task, _input_method, arguments, _options = nil)
47
47
  body = { task: task_name_from_path(task),
48
48
  environment: @orch_task_environment,
49
49
  noop: arguments['_noop'],
@@ -88,12 +88,12 @@ module Bolt
88
88
  Bolt::Result.for_command(@target, result.value['stdout'], result.value['stderr'], result.value['exit_code'])
89
89
  end
90
90
 
91
- def run_command(command, options = {})
91
+ def run_command(command, _options = nil)
92
92
  result = run_task(BOLT_MOCK_FILE,
93
93
  'stdin',
94
94
  action: 'command',
95
95
  command: command,
96
- options: options)
96
+ options: {})
97
97
  unwrap_bolt_result(result)
98
98
  end
99
99
 
@@ -112,7 +112,7 @@ module Bolt
112
112
  result
113
113
  end
114
114
 
115
- def run_script(script, arguments)
115
+ def run_script(script, arguments, _options = nil)
116
116
  content = File.open(script, &:read)
117
117
  content = Base64.encode64(content)
118
118
  params = {
data/lib/bolt/node/ssh.rb CHANGED
@@ -15,10 +15,25 @@ module Bolt
15
15
  }
16
16
  end
17
17
 
18
+ def initialize(target, **kwargs)
19
+ super(target, **kwargs)
20
+ @conf_run_as = @run_as
21
+ end
22
+
18
23
  def protocol
19
24
  'ssh'
20
25
  end
21
26
 
27
+ if !!File::ALT_SEPARATOR
28
+ require 'ffi'
29
+ module Win
30
+ extend FFI::Library
31
+ ffi_lib 'user32'
32
+ ffi_convention :stdcall
33
+ attach_function :FindWindow, :FindWindowW, %i[buffer_in buffer_in], :int
34
+ end
35
+ end
36
+
22
37
  def connect
23
38
  transport_logger = Logging.logger[Net::SSH]
24
39
  transport_logger.level = :warn
@@ -30,13 +45,29 @@ module Bolt
30
45
  options[:port] = @target.port if @target.port
31
46
  options[:password] = @password if @password
32
47
  options[:keys] = @key if @key
33
- options[:verify_host_key] = if @insecure
34
- Net::SSH::Verifiers::Lenient.new
35
- else
48
+ options[:verify_host_key] = if @host_key_check
36
49
  Net::SSH::Verifiers::Secure.new
50
+ else
51
+ Net::SSH::Verifiers::Lenient.new
37
52
  end
38
53
  options[:timeout] = @connect_timeout if @connect_timeout
39
54
 
55
+ # Mirroring:
56
+ # https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/authentication/agent.rb#L80
57
+ # https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/authentication/pageant.rb#L403
58
+ if defined?(UNIXSocket) && UNIXSocket
59
+ if ENV['SSH_AUTH_SOCK'].to_s.empty?
60
+ @logger.debug { "Disabling use_agent in net-ssh: ssh-agent is not available" }
61
+ options[:use_agent] = false
62
+ end
63
+ elsif !!File::ALT_SEPARATOR
64
+ pageant_wide = 'Pageant'.encode('UTF-16LE')
65
+ if Win.FindWindow(pageant_wide, pageant_wide).to_i == 0
66
+ @logger.debug { "Disabling use_agent in net-ssh: pageant process not running" }
67
+ options[:use_agent] = false
68
+ end
69
+ end
70
+
40
71
  @session = Net::SSH.start(@target.host, @user, options)
41
72
  @logger.debug { "Opened session" }
42
73
  rescue Net::SSH::AuthenticationFailed => e
@@ -163,8 +194,6 @@ module Bolt
163
194
  def upload(source, destination)
164
195
  write_remote_file(source, destination)
165
196
  Bolt::Result.for_upload(@target, source, destination)
166
- rescue StandardError => e
167
- Bolt::Result.from_exception(@target, e)
168
197
  end
169
198
 
170
199
  def write_remote_file(source, destination)
@@ -253,28 +282,27 @@ SCRIPT
253
282
  end
254
283
  end
255
284
 
256
- def run_command(command)
285
+ def run_command(command, options = {})
286
+ @run_as = options['_run_as'] || @conf_run_as
257
287
  output = execute(command, sudoable: true)
258
288
  Bolt::Result.for_command(@target, output.stdout.string, output.stderr.string, output.exit_code)
259
- # TODO: We should be able to rely on the excutor for this but it will mean
260
- # a test refactor
261
- rescue StandardError => e
262
- Bolt::Result.from_exception(@target, e)
289
+ ensure
290
+ @run_as = @conf_run_as
263
291
  end
264
292
 
265
- def run_script(script, arguments)
293
+ def run_script(script, arguments, options = {})
294
+ @run_as = options['_run_as'] || @conf_run_as
266
295
  with_remote_file(script) do |remote_path|
267
296
  output = execute("'#{remote_path}' #{Shellwords.join(arguments)}",
268
297
  sudoable: true)
269
298
  Bolt::Result.for_command(@target, output.stdout.string, output.stderr.string, output.exit_code)
270
299
  end
271
- # TODO: We should be able to rely on the excutor for this but it will mean
272
- # a test refactor
273
- rescue StandardError => e
274
- Bolt::Result.from_exception(@target, e)
300
+ ensure
301
+ @run_as = @conf_run_as
275
302
  end
276
303
 
277
- def run_task(task, input_method, arguments)
304
+ def run_task(task, input_method, arguments, options = {})
305
+ @run_as = options['_run_as'] || @conf_run_as
278
306
  export_args = {}
279
307
  stdin, output = nil
280
308
 
@@ -304,11 +332,8 @@ SCRIPT
304
332
  Bolt::Result.for_task(@target, output.stdout.string,
305
333
  output.stderr.string,
306
334
  output.exit_code)
307
-
308
- # TODO: We should be able to rely on the excutor for this but it will mean
309
- # a test refactor
310
- rescue StandardError => e
311
- Bolt::Result.from_exception(@target, e)
335
+ ensure
336
+ @run_as = @conf_run_as
312
337
  end
313
338
  end
314
339
  end
@@ -15,7 +15,7 @@ module Bolt
15
15
  HTTPS_PORT = 5986
16
16
 
17
17
  def port
18
- default_port = @insecure ? HTTP_PORT : HTTPS_PORT
18
+ default_port = @ssl ? HTTPS_PORT : HTTP_PORT
19
19
  @target.port || default_port
20
20
  end
21
21
 
@@ -26,12 +26,12 @@ module Bolt
26
26
  end
27
27
 
28
28
  def connect
29
- if @insecure
30
- scheme = 'http'
31
- transport = :negotiate
32
- else
29
+ if @ssl
33
30
  scheme = 'https'
34
31
  transport = :ssl
32
+ else
33
+ scheme = 'http'
34
+ transport = :negotiate
35
35
  end
36
36
  endpoint = "#{scheme}://#{@target.host}:#{port}/wsman"
37
37
  options = { endpoint: endpoint,
@@ -54,8 +54,8 @@ module Bolt
54
54
  rescue Timeout::Error
55
55
  # If we're using the default port with SSL, a timeout probably means the
56
56
  # host doesn't support SSL.
57
- if !@insecure && port == HTTPS_PORT
58
- theres_your_problem = "\nUse --insecure if this host isn't configured to use SSL for WinRM"
57
+ if @ssl && port == HTTPS_PORT
58
+ theres_your_problem = "\nUse --no-ssl if this host isn't configured to use SSL for WinRM"
59
59
  end
60
60
  raise Bolt::Node::ConnectError.new(
61
61
  "Timeout after #{@connect_timeout} seconds connecting to #{endpoint}#{theres_your_problem}",
@@ -68,7 +68,7 @@ module Bolt
68
68
  )
69
69
  rescue OpenSSL::SSL::SSLError => e
70
70
  # If we're using SSL with the default non-SSL port, mention that as a likely problem
71
- if !@insecure && port == HTTP_PORT
71
+ if @ssl && port == HTTP_PORT
72
72
  theres_your_problem = "\nAre you using SSL to connect to a non-SSL port?"
73
73
  end
74
74
  raise Bolt::Node::ConnectError.new(
@@ -396,6 +396,7 @@ PS
396
396
  end
397
397
  result_output
398
398
  end
399
+ private :execute
399
400
 
400
401
  # 10 minutes in seconds
401
402
  DEFAULT_EXECUTION_TIMEOUT = 10 * 60
@@ -463,9 +464,6 @@ PS
463
464
  def upload(source, destination)
464
465
  write_remote_file(source, destination)
465
466
  Bolt::Result.for_upload(@target, source, destination)
466
- # TODO: we should rely on the executor for this
467
- rescue StandardError => ex
468
- Bolt::Result.from_exception(@target, ex)
469
467
  end
470
468
 
471
469
  def write_remote_file(source, destination)
@@ -510,15 +508,12 @@ PS
510
508
  end
511
509
  end
512
510
 
513
- def run_command(command)
511
+ def run_command(command, _options = nil)
514
512
  output = execute(command)
515
513
  Bolt::Result.for_command(@target, output.stdout.string, output.stderr.string, output.exit_code)
516
- # TODO: we should rely on the executor for this
517
- rescue StandardError => e
518
- Bolt::Result.from_exception(@target, e)
519
514
  end
520
515
 
521
- def run_script(script, arguments)
516
+ def run_script(script, arguments, _options = nil)
522
517
  with_remote_file(script) do |remote_path|
523
518
  if powershell_file?(remote_path)
524
519
  mapped_args = arguments.map do |a|
@@ -547,12 +542,9 @@ catch
547
542
  end
548
543
  Bolt::Result.for_command(@target, output.stdout.string, output.stderr.string, output.exit_code)
549
544
  end
550
- # TODO: we should rely on the executor for this
551
- rescue StandardError => e
552
- Bolt::Result.from_exception(@target, e)
553
545
  end
554
546
 
555
- def run_task(task, input_method, arguments)
547
+ def run_task(task, input_method, arguments, _options = nil)
556
548
  if STDIN_METHODS.include?(input_method)
557
549
  stdin = JSON.dump(arguments)
558
550
  end
@@ -591,9 +583,6 @@ try { & "#{remote_path}" @taskArgs } catch { exit 1 }
591
583
  output.stderr.string,
592
584
  output.exit_code)
593
585
  end
594
- # TODO: we should rely on the executor for this
595
- rescue StandardError => e
596
- Bolt::Result.from_exception(@target, e)
597
586
  end
598
587
 
599
588
  def escape_arguments(arguments)
@@ -14,6 +14,15 @@ module Bolt
14
14
  def initialize(stream = $stdout)
15
15
  @stream = stream
16
16
  end
17
+
18
+ # This method replaces data types tha have the name 'Data'
19
+ # with the string 'Any'
20
+ # This was a problem when using 'bolt task show <task_name>'
21
+ def replace_data_type(params)
22
+ params.map do |_, v|
23
+ v['type'] = 'Any' if v['type'].to_s == 'Data'
24
+ end
25
+ end
17
26
  end
18
27
  end
19
28
 
@@ -101,13 +101,14 @@ module Bolt
101
101
  usage = "bolt task run --nodes, -n <node-name> #{task['name']}"
102
102
 
103
103
  if task['parameters']
104
+ replace_data_type(task['parameters'])
104
105
  task['parameters'].each do |k, v|
105
106
  pretty_params << "- #{k}: #{v['type']}\n"
106
107
  pretty_params << " #{v['description']}\n" if v['description']
107
- usage << if !v['type'].to_s.include? "Optional"
108
- " #{k}=<value>"
109
- else
108
+ usage << if v['type'].is_a?(Puppet::Pops::Types::POptionalType)
110
109
  " [#{k}=<value>]"
110
+ else
111
+ " #{k}=<value>"
111
112
  end
112
113
  end
113
114
  end
@@ -122,7 +123,35 @@ module Bolt
122
123
  @stream.puts(task_info)
123
124
  end
124
125
 
125
- def print_plan(result)
126
+ # @param [Hash] A hash representing the plan
127
+ def print_plan_info(plan)
128
+ # Building lots of strings...
129
+ pretty_params = ""
130
+ plan_info = ""
131
+ usage = "bolt plan run #{plan['name']}"
132
+
133
+ if plan['parameters']
134
+ plan['parameters'].each do |p|
135
+ name = p['name']
136
+ pretty_params << "- #{name}: #{p['type']}\n"
137
+ usage << if p.include?('default_value')
138
+ # TODO: print the default value when available
139
+ " [#{name}=<value>]"
140
+ else
141
+ " #{name}=<value>"
142
+ end
143
+ end
144
+ end
145
+
146
+ plan_info << "\n#{plan['name']}"
147
+ plan_info << "\n\n"
148
+ plan_info << "USAGE:\n#{usage}\n\n"
149
+ plan_info << "PARAMETERS:\n#{pretty_params}\n" if plan['parameters']
150
+ @stream.puts(plan_info)
151
+ end
152
+
153
+ # @param [Hash] A hash representing the plan result
154
+ def print_plan_result(result)
126
155
  # If the object has a json representation display it
127
156
  if result.respond_to?(:to_json)
128
157
  # Guard against to_json methods that don't accept options
@@ -42,10 +42,15 @@ module Bolt
42
42
  end
43
43
 
44
44
  def print_task_info(task)
45
+ replace_data_type(task['parameters'])
45
46
  @stream.puts task.to_json
46
47
  end
47
48
 
48
- def print_plan(result)
49
+ def print_plan_info(plan)
50
+ @stream.puts plan.to_json
51
+ end
52
+
53
+ def print_plan_result(result)
49
54
  # Ruby JSON patches most objects to have a to_json method.
50
55
  @stream.puts result.to_json
51
56
  end
data/lib/bolt/pal.rb CHANGED
@@ -1,17 +1,34 @@
1
- # TODO: This is currently used only for testing. I will refactor the CLI to use
2
- # this in a separate PR
1
+ require 'bolt/executor'
2
+ require 'bolt/error'
3
+
3
4
  module Bolt
4
5
  class PAL
5
6
  BOLTLIB_PATH = File.join(__FILE__, '../../../modules')
6
7
 
7
8
  def initialize(config)
8
- # TODO: how should we manage state? Does noop go here?
9
- # This allows us to copypaste from BOLT::CLI for now
9
+ # Nothing works without initialized this global state. Reinitializing
10
+ # is safe and in practice only happen in tests
11
+ self.class.load_puppet
12
+ self.class.configure_logging(config[:log_level])
13
+
10
14
  @config = config
11
15
  end
12
16
 
13
- # WARNING: Nothing in here works without calling this!
17
+ # Puppet logging is global so this is class method to avoid confusion
18
+ def self.configure_logging(log_level)
19
+ Puppet[:log_level] = log_level == :debug ? 'debug' : 'notice'
20
+ Puppet::Util::Log.newdestination(:console)
21
+ end
22
+
14
23
  def self.load_puppet
24
+ if Gem.win_platform?
25
+ # Windows 'fix' for openssl behaving strangely. Prevents very slow operation
26
+ # of random_bytes later when establishing winrm connections from a Windows host.
27
+ # See https://github.com/rails/rails/issues/25805 for background.
28
+ require 'openssl'
29
+ OpenSSL::Random.random_bytes(1)
30
+ end
31
+
15
32
  begin
16
33
  require_relative '../../vendored/require_vendored'
17
34
  rescue LoadError
@@ -20,14 +37,6 @@ module Bolt
20
37
 
21
38
  # Now that puppet is loaded we can include puppet mixins in data types
22
39
  Bolt::ResultSet.include_iterable
23
-
24
- Puppet::Util::Log.newdestination(:console)
25
- Puppet[:log_level] = 'notice'
26
- # Puppet[:log_level] = if @config[:log_level] == :debug
27
- # 'debug'
28
- # else
29
- # 'notice'
30
- # end
31
40
  end
32
41
 
33
42
  # Runs a block in a PAL script compiler configured for Bolt. Catches
@@ -41,8 +50,9 @@ module Bolt
41
50
  yield compiler
42
51
  rescue Puppet::PreformattedError => err
43
52
  # Puppet sometimes rescues exceptions notes the location and reraises
44
- # For now return the original error.
45
- if err.cause
53
+ # For now return the original error. Exception cause support was added in Ruby 2.1
54
+ # so we fall back to reporting the error we got for Ruby 2.0.
55
+ if err.respond_to?(:cause) && err.cause
46
56
  if err.cause.is_a? Bolt::Error
47
57
  err.cause
48
58
  else
@@ -73,8 +83,7 @@ module Bolt
73
83
  Puppet.override(bolt_executor: executor, &block)
74
84
  end
75
85
 
76
- def in_plan_compiler(noop)
77
- executor = Bolt::Executor.new(@config, noop, true)
86
+ def in_plan_compiler(executor)
78
87
  with_bolt_executor(executor) do
79
88
  with_puppet_settings do |opts|
80
89
  in_bolt_compiler(opts) do |compiler|
@@ -84,10 +93,9 @@ module Bolt
84
93
  end
85
94
  end
86
95
 
87
- def in_task_compiler(noop)
88
- executor = Bolt::Executor.new(@config, noop)
96
+ def in_task_compiler(executor)
89
97
  with_bolt_executor(executor) do
90
- in_bolt_compiler(opts) do |compiler|
98
+ in_bolt_compiler do |compiler|
91
99
  yield compiler
92
100
  end
93
101
  end
@@ -102,5 +110,64 @@ module Bolt
102
110
  yield cli
103
111
  end
104
112
  end
113
+
114
+ def list_tasks
115
+ in_bolt_compiler do |compiler|
116
+ tasks = compiler.list_tasks
117
+ tasks.map(&:name).sort.map do |task_name|
118
+ task_sig = compiler.task_signature(task_name)
119
+ [task_name, task_sig.task.description]
120
+ end
121
+ end
122
+ end
123
+
124
+ def get_task_info(task_name)
125
+ task = in_bolt_compiler do |compiler|
126
+ compiler.task_signature(task_name)
127
+ end
128
+ raise Bolt::CLIError, "Could not find task #{task_name} in your modulepath" if task.nil?
129
+ task.task_hash
130
+ end
131
+
132
+ def list_plans
133
+ in_bolt_compiler do |compiler|
134
+ compiler.list_plans.map { |plan| [plan.name] }.sort
135
+ end
136
+ end
137
+
138
+ def get_plan_info(plan_name)
139
+ plan = in_bolt_compiler do |compiler|
140
+ compiler.plan_signature(plan_name)
141
+ end
142
+ raise Bolt::CLIError, "Could not find plan #{plan_name} in your modulepath" if plan.nil?
143
+ elements = plan.params_type.elements
144
+ {
145
+ 'name' => plan_name,
146
+ 'parameters' =>
147
+ unless elements.nil? || elements.empty?
148
+ elements.map { |e|
149
+ p = {
150
+ 'name' => e.name,
151
+ 'type' => e.value_type
152
+ }
153
+ # TODO: when the default value can be obtained use the actual value instead of nil
154
+ p['default_value'] = nil if e.key_type.is_a?(Puppet::Pops::Types::POptionalType)
155
+ p
156
+ }
157
+ end
158
+ }
159
+ end
160
+
161
+ def run_task(object, targets, params, executor, &eventblock)
162
+ in_task_compiler(executor) do |compiler|
163
+ compiler.call_function('run_task', object, targets, params, &eventblock)
164
+ end
165
+ end
166
+
167
+ def run_plan(object, params, executor)
168
+ in_plan_compiler(executor) do |compiler|
169
+ compiler.call_function('run_plan', object, params)
170
+ end
171
+ end
105
172
  end
106
173
  end