bolt 3.15.0 → 3.18.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 (111) hide show
  1. checksums.yaml +4 -4
  2. data/Puppetfile +7 -7
  3. data/bolt-modules/boltlib/lib/puppet/functions/run_script.rb +1 -4
  4. data/bolt-modules/boltlib/lib/puppet/functions/upload_file.rb +1 -4
  5. data/bolt-modules/file/lib/puppet/functions/file/exists.rb +1 -4
  6. data/bolt-modules/file/lib/puppet/functions/file/read.rb +1 -4
  7. data/bolt-modules/file/lib/puppet/functions/file/readable.rb +1 -4
  8. data/lib/bolt/analytics.rb +4 -21
  9. data/lib/bolt/application.rb +627 -0
  10. data/lib/bolt/applicator.rb +7 -6
  11. data/lib/bolt/apply_inventory.rb +3 -3
  12. data/lib/bolt/apply_result.rb +2 -2
  13. data/lib/bolt/bolt_option_parser.rb +27 -3
  14. data/lib/bolt/catalog.rb +10 -11
  15. data/lib/bolt/cli.rb +607 -809
  16. data/lib/bolt/config/modulepath.rb +1 -1
  17. data/lib/bolt/config/options.rb +32 -13
  18. data/lib/bolt/config/transport/base.rb +4 -4
  19. data/lib/bolt/config/transport/docker.rb +2 -2
  20. data/lib/bolt/config/transport/local.rb +2 -2
  21. data/lib/bolt/config/transport/lxd.rb +2 -2
  22. data/lib/bolt/config/transport/orch.rb +2 -2
  23. data/lib/bolt/config/transport/podman.rb +2 -2
  24. data/lib/bolt/config/transport/remote.rb +2 -2
  25. data/lib/bolt/config/transport/ssh.rb +2 -2
  26. data/lib/bolt/config/transport/winrm.rb +2 -2
  27. data/lib/bolt/config.rb +5 -5
  28. data/lib/bolt/container_result.rb +2 -2
  29. data/lib/bolt/error.rb +1 -1
  30. data/lib/bolt/executor.rb +17 -14
  31. data/lib/bolt/fiber_executor.rb +9 -5
  32. data/lib/bolt/inventory/group.rb +4 -4
  33. data/lib/bolt/inventory/inventory.rb +64 -41
  34. data/lib/bolt/inventory/options.rb +1 -1
  35. data/lib/bolt/inventory/target.rb +1 -1
  36. data/lib/bolt/inventory.rb +10 -17
  37. data/lib/bolt/module_installer/installer.rb +2 -2
  38. data/lib/bolt/module_installer/puppetfile/forge_module.rb +1 -1
  39. data/lib/bolt/module_installer/puppetfile/git_module.rb +1 -1
  40. data/lib/bolt/module_installer/puppetfile/module.rb +1 -1
  41. data/lib/bolt/module_installer/puppetfile.rb +27 -13
  42. data/lib/bolt/module_installer/resolver.rb +3 -3
  43. data/lib/bolt/module_installer/specs/forge_spec.rb +1 -1
  44. data/lib/bolt/module_installer/specs/git_spec.rb +1 -1
  45. data/lib/bolt/module_installer/specs.rb +3 -3
  46. data/lib/bolt/module_installer.rb +6 -6
  47. data/lib/bolt/node/errors.rb +1 -1
  48. data/lib/bolt/node/output.rb +1 -1
  49. data/lib/bolt/outputter/human.rb +85 -34
  50. data/lib/bolt/outputter/json.rb +63 -38
  51. data/lib/bolt/outputter/logger.rb +1 -1
  52. data/lib/bolt/outputter/rainbow.rb +1 -1
  53. data/lib/bolt/outputter.rb +4 -4
  54. data/lib/bolt/pal/logging.rb +1 -1
  55. data/lib/bolt/pal/yaml_plan/evaluator.rb +1 -1
  56. data/lib/bolt/pal/yaml_plan/loader.rb +2 -2
  57. data/lib/bolt/pal/yaml_plan/step.rb +11 -11
  58. data/lib/bolt/pal/yaml_plan/transpiler.rb +3 -3
  59. data/lib/bolt/pal/yaml_plan.rb +2 -2
  60. data/lib/bolt/pal.rb +11 -11
  61. data/lib/bolt/plan_creator.rb +88 -29
  62. data/lib/bolt/plan_future.rb +11 -6
  63. data/lib/bolt/plan_result.rb +3 -3
  64. data/lib/bolt/plugin/cache.rb +2 -2
  65. data/lib/bolt/plugin/module.rb +1 -1
  66. data/lib/bolt/plugin.rb +18 -24
  67. data/lib/bolt/project.rb +4 -11
  68. data/lib/bolt/project_manager/config_migrator.rb +1 -1
  69. data/lib/bolt/project_manager/inventory_migrator.rb +1 -1
  70. data/lib/bolt/project_manager/migrator.rb +1 -1
  71. data/lib/bolt/project_manager/module_migrator.rb +5 -5
  72. data/lib/bolt/project_manager.rb +23 -4
  73. data/lib/bolt/puppetdb/config.rb +1 -1
  74. data/lib/bolt/puppetdb.rb +3 -3
  75. data/lib/bolt/result.rb +1 -1
  76. data/lib/bolt/result_set.rb +2 -1
  77. data/lib/bolt/shell/bash.rb +12 -2
  78. data/lib/bolt/shell/powershell.rb +9 -2
  79. data/lib/bolt/shell.rb +2 -2
  80. data/lib/bolt/target.rb +2 -2
  81. data/lib/bolt/transport/base.rb +1 -1
  82. data/lib/bolt/transport/docker/connection.rb +1 -1
  83. data/lib/bolt/transport/docker.rb +2 -2
  84. data/lib/bolt/transport/local/connection.rb +19 -3
  85. data/lib/bolt/transport/local.rb +3 -3
  86. data/lib/bolt/transport/lxd/connection.rb +1 -1
  87. data/lib/bolt/transport/lxd.rb +4 -4
  88. data/lib/bolt/transport/orch/connection.rb +13 -1
  89. data/lib/bolt/transport/orch.rb +2 -2
  90. data/lib/bolt/transport/podman/connection.rb +1 -1
  91. data/lib/bolt/transport/podman.rb +2 -2
  92. data/lib/bolt/transport/remote.rb +2 -2
  93. data/lib/bolt/transport/simple.rb +3 -3
  94. data/lib/bolt/transport/ssh/connection.rb +3 -3
  95. data/lib/bolt/transport/ssh.rb +5 -5
  96. data/lib/bolt/transport/winrm/connection.rb +2 -2
  97. data/lib/bolt/transport/winrm.rb +3 -3
  98. data/lib/bolt/util.rb +8 -31
  99. data/lib/bolt/validator.rb +1 -1
  100. data/lib/bolt/version.rb +1 -1
  101. data/lib/bolt.rb +1 -1
  102. data/lib/bolt_server/file_cache.rb +12 -0
  103. data/lib/bolt_server/schemas/action-apply.json +32 -0
  104. data/lib/bolt_server/schemas/action-apply_prep.json +19 -0
  105. data/lib/bolt_server/transport_app.rb +129 -26
  106. data/lib/bolt_spec/bolt_context.rb +7 -5
  107. data/lib/bolt_spec/plans/mock_executor.rb +3 -3
  108. data/lib/bolt_spec/run.rb +1 -1
  109. data/libexec/bolt_catalog +1 -1
  110. metadata +7 -5
  111. data/lib/bolt/secret.rb +0 -37
@@ -0,0 +1,627 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'benchmark'
4
+
5
+ require_relative '../bolt/plan_creator'
6
+ require_relative '../bolt/util'
7
+
8
+ module Bolt
9
+ class Application
10
+ attr_reader :analytics, :config, :executor, :inventory, :logger, :pal, :plugins
11
+ private :analytics, :config, :executor, :inventory, :logger, :pal, :plugins
12
+
13
+ def initialize(
14
+ analytics:,
15
+ config:,
16
+ executor:,
17
+ inventory:,
18
+ pal:,
19
+ plugins:
20
+ )
21
+ @analytics = analytics
22
+ @config = config
23
+ @executor = executor
24
+ @inventory = inventory
25
+ @logger = Bolt::Logger.logger(self)
26
+ @pal = pal
27
+ @plugins = plugins
28
+ end
29
+
30
+ # Shuts down the application.
31
+ #
32
+ def shutdown
33
+ executor.shutdown
34
+ end
35
+
36
+ # Apply Puppet manifest code to a list of targets.
37
+ #
38
+ # @param manifest [String, NilClass] The path to a Puppet manifest file.
39
+ # @param targets [Array[String]] The targets to run on.
40
+ # @param code [String] Puppet manifest code to apply.
41
+ # @param noop [Boolean] Whether to apply in no-operation mode.
42
+ # @return [Bolt::ResultSet]
43
+ #
44
+ def apply(manifest, targets, code: '', noop: false)
45
+ manifest_code = if manifest
46
+ Bolt::Util.validate_file('manifest', manifest)
47
+ File.read(File.expand_path(manifest))
48
+ else
49
+ code
50
+ end
51
+
52
+ targets = inventory.get_targets(targets)
53
+
54
+ Puppet[:tasks] = false
55
+ ast = pal.parse_manifest(manifest_code, manifest)
56
+
57
+ if defined?(ast.body) &&
58
+ (ast.body.is_a?(Puppet::Pops::Model::HostClassDefinition) ||
59
+ ast.body.is_a?(Puppet::Pops::Model::ResourceTypeDefinition))
60
+ message = "Manifest only contains definitions and will result in no changes on the targets. "\
61
+ "Definitions must be declared for their resources to be applied. You can read more "\
62
+ "about defining and declaring classes and types in the Puppet documentation at "\
63
+ "https://puppet.com/docs/puppet/latest/lang_classes.html and "\
64
+ "https://puppet.com/docs/puppet/latest/lang_defined_types.html"
65
+ Bolt::Logger.warn("empty_manifest", message)
66
+ end
67
+
68
+ # Apply logging looks like plan logging
69
+ executor.publish_event(type: :plan_start, plan: nil)
70
+
71
+ with_benchmark do
72
+ apply_prep_results = pal.in_plan_compiler(executor, inventory, plugins.puppetdb_client) do |compiler|
73
+ compiler.call_function('apply_prep', targets, '_catch_errors' => true)
74
+ end
75
+
76
+ apply_results = pal.with_bolt_executor(executor, inventory, plugins.puppetdb_client) do
77
+ Puppet.lookup(:apply_executor)
78
+ .apply_ast(ast, apply_prep_results.ok_set.targets, catch_errors: true, noop: noop)
79
+ end
80
+
81
+ Bolt::ResultSet.new(apply_prep_results.error_set.results + apply_results.results)
82
+ end
83
+ end
84
+
85
+ # Run a command on a list of targets.
86
+ #
87
+ # @param command [String] The command.
88
+ # @param targets [Array[String]] The targets to run on.
89
+ # @param env_vars [Hash] Environment variables to set on the target.
90
+ # @return [Bolt::ResultSet]
91
+ #
92
+ def run_command(command, targets, env_vars: nil)
93
+ targets = inventory.get_targets(targets)
94
+
95
+ with_benchmark do
96
+ executor.run_command(targets, command, env_vars: env_vars)
97
+ end
98
+ end
99
+
100
+ # Download a file from a list of targets to a directory on the controller.
101
+ #
102
+ # @param source [String] The path to the file on the targets.
103
+ # @param destination [String] The path to the directory on the controller.
104
+ # @param targets [Array[String]] The targets to run on.
105
+ # @return [Bolt::ResultSet]
106
+ #
107
+ def download_file(source, destination, targets)
108
+ destination = File.expand_path(destination, Dir.pwd)
109
+ targets = inventory.get_targets(targets)
110
+
111
+ with_benchmark do
112
+ executor.download_file(targets, source, destination)
113
+ end
114
+ end
115
+
116
+ # Upload a file from the controller to a list of targets.
117
+ #
118
+ # @param source [String] The path to the file on the controller.
119
+ # @param destination [String] The destination path on the targets.
120
+ # @param targets [Array[String]] The targets to run on.
121
+ # @return [Bolt::ResultSet]
122
+ #
123
+ def upload_file(source, destination, targets)
124
+ source = find_file(source)
125
+ targets = inventory.get_targets(targets)
126
+
127
+ Bolt::Util.validate_file('source file', source, true)
128
+
129
+ with_benchmark do
130
+ executor.upload_file(targets, source, destination)
131
+ end
132
+ end
133
+
134
+ # Show groups in the inventory.
135
+ #
136
+ # @return [Hash]
137
+ #
138
+ def list_groups
139
+ {
140
+ count: inventory.group_names.count,
141
+ groups: inventory.group_names.sort,
142
+ inventory: {
143
+ default: config.default_inventoryfile.to_s,
144
+ source: inventory.source
145
+ }
146
+ }
147
+ end
148
+
149
+ # Show available guides.
150
+ #
151
+ # @param guides [Hash] A map of topics to paths to guides.
152
+ # @param outputter [Bolt::Outputter] An outputter instance.
153
+ # @return [Boolean]
154
+ #
155
+ def list_guides
156
+ { topics: load_guides.keys }
157
+ end
158
+
159
+ # Show a guide.
160
+ #
161
+ # @param topic [String] The topic to show.
162
+ # @param guides [Hash] A map of topics to paths to guides.
163
+ # @param outputter [Bolt::Outputter] An outputter instance.
164
+ # @return [Boolean]
165
+ #
166
+ def show_guide(topic)
167
+ if (path = load_guides[topic])
168
+ analytics.event('Guide', 'known_topic', label: topic)
169
+
170
+ begin
171
+ guide = Bolt::Util.read_yaml_hash(path, 'guide')
172
+ rescue SystemCallError => e
173
+ raise Bolt::FileError("#{e.message}: unable to load guide page", filepath)
174
+ end
175
+
176
+ # Make sure both topic and guide keys are defined
177
+ unless (%w[topic guide] - guide.keys).empty?
178
+ msg = "Guide file #{path} must have a 'topic' key and 'guide' key, but has #{guide.keys} keys."
179
+ raise Bolt::Error.new(msg, 'bolt/invalid-guide')
180
+ end
181
+
182
+ Bolt::Util.symbolize_top_level_keys(guide)
183
+ else
184
+ analytics.event('Guide', 'unknown_topic', label: topic)
185
+ raise Bolt::Error.new(
186
+ "Unknown topic '#{topic}'. For a list of available topics, run 'bolt guide'.",
187
+ 'bolt/unknown-topic'
188
+ )
189
+ end
190
+ end
191
+
192
+ # Show inventory information.
193
+ #
194
+ # @param targets [Array[String]] The targets to show.
195
+ # @return [Hash]
196
+ #
197
+ def show_inventory(targets = nil)
198
+ targets = group_targets_by_source(targets || ['all'])
199
+
200
+ {
201
+ adhoc: {
202
+ count: targets[:adhoc].count,
203
+ targets: targets[:adhoc].map(&:detail)
204
+ },
205
+ inventory: {
206
+ count: targets[:inventory].count,
207
+ targets: targets[:inventory].map(&:detail),
208
+ file: (inventory.source || config.default_inventoryfile).to_s,
209
+ default: config.default_inventoryfile.to_s
210
+ },
211
+ targets: targets.values.flatten.map(&:detail),
212
+ count: targets.values.flatten.count
213
+ }
214
+ end
215
+
216
+ # Lookup a value with Hiera.
217
+ #
218
+ # @param key [String] The key to look up in the hierarchy.
219
+ # @param targets [Array[String]] The targets to use as context.
220
+ # @param vars [Hash] Variables to set in the scope.
221
+ # @return [Bolt::ResultSet, String] The result of the lookup.
222
+ #
223
+ def lookup(key, targets, vars: {})
224
+ executor.publish_event(type: :plan_start, plan: nil)
225
+
226
+ with_benchmark do
227
+ pal.lookup(key,
228
+ inventory.get_targets(targets),
229
+ inventory,
230
+ executor,
231
+ plan_vars: vars)
232
+ end
233
+ end
234
+
235
+ # Lookup a value with Hiera using plan_hierarchy.
236
+ #
237
+ # @param key [String] The key to lookup up in the plan_hierarchy.
238
+ # @param vars [Hash] Variables to set in the scope.
239
+ # @return [String] The result of the lookup.
240
+ #
241
+ def plan_lookup(key, vars: {})
242
+ pal.plan_hierarchy_lookup(key, plan_vars: vars)
243
+ end
244
+
245
+ # Add a new module to the project.
246
+ #
247
+ # @param name [String] The name of the module to add.
248
+ # @param outputter [Bolt::Outputter] An outputter instance.
249
+ # @return [Boolean]
250
+ #
251
+ def add_module(name, outputter)
252
+ assert_project_file(config.project)
253
+
254
+ installer = Bolt::ModuleInstaller.new(outputter, pal)
255
+
256
+ installer.add(name,
257
+ config.project.modules,
258
+ config.project.puppetfile,
259
+ config.project.managed_moduledir,
260
+ config.project.project_file,
261
+ @plugins.resolve_references(config.module_install))
262
+ end
263
+
264
+ # Generate Puppet data types from project modules.
265
+ #
266
+ # @return [Boolean]
267
+ #
268
+ def generate_types
269
+ pal.generate_types(cache: true)
270
+ end
271
+
272
+ # Install the project's modules.
273
+ #
274
+ # @param outputter [Bolt::Outputter] An outputter instance.
275
+ # @param force [Boolean] Forcibly install modules.
276
+ # @param resolve [Boolean] Resolve module dependencies.
277
+ # @return [Boolean]
278
+ #
279
+ def install_modules(outputter, force: false, resolve: true)
280
+ assert_project_file(config.project)
281
+
282
+ if config.project.modules.empty? && resolve
283
+ outputter.print_message(
284
+ "Project configuration file #{config.project.project_file} does not "\
285
+ "specify any module dependencies. Nothing to do."
286
+ )
287
+ return true
288
+ end
289
+
290
+ installer = Bolt::ModuleInstaller.new(outputter, pal)
291
+
292
+ installer.install(config.project.modules,
293
+ config.project.puppetfile,
294
+ config.project.managed_moduledir,
295
+ @plugins.resolve_references(config.module_install),
296
+ force: force,
297
+ resolve: resolve)
298
+ end
299
+
300
+ # Show modules available to the project.
301
+ #
302
+ # @return [Hash] A map of module directories to module definitions.
303
+ #
304
+ def list_modules
305
+ pal.list_modules
306
+ end
307
+
308
+ # Show module information.
309
+ #
310
+ # @param name [String] The name of the module.
311
+ # @return [Hash] The module information.
312
+ #
313
+ def show_module(name)
314
+ pal.show_module(name)
315
+ end
316
+
317
+ # Convert a YAML plan to a Puppet language plan.
318
+ #
319
+ # @param plan [String] The plan to convert. Can be a plan name or a path.
320
+ # @return [String] The converted plan.
321
+ #
322
+ def convert_plan(plan)
323
+ pal.convert_plan(plan)
324
+ end
325
+
326
+ # Create a new project-level plan.
327
+ #
328
+ # @param name [String] The name of the new plan.
329
+ # @param puppet [Boolean] Create a Puppet language plan.
330
+ # @param plan_script [String] Reference to the script to run in the new plan.
331
+ # @return [Boolean]
332
+ #
333
+ def new_plan(name, puppet: false, plan_script: nil)
334
+ Bolt::PlanCreator.validate_plan_name(config.project, name)
335
+
336
+ if plan_script
337
+ Bolt::Util.validate_file('script', find_file(plan_script))
338
+ end
339
+
340
+ Bolt::PlanCreator.create_plan(config.project.plans_path,
341
+ name,
342
+ is_puppet: puppet,
343
+ script: plan_script)
344
+ end
345
+
346
+ # Run a plan.
347
+ #
348
+ # @param plan [String] The plan to run.
349
+ # @param targets [Array[String], NilClass] The targets to pass to the plan.
350
+ # @param params [Hash] Parameters to pass to the plan.
351
+ # @return [Bolt::PlanResult]
352
+ #
353
+ def run_plan(plan, targets, params: {})
354
+ if targets && targets.any?
355
+ if params['nodes'] || params['targets']
356
+ key = params.include?('nodes') ? 'nodes' : 'targets'
357
+ raise Bolt::CLIError,
358
+ "A plan's '#{key}' parameter can be specified using the --#{key} option, but in that " \
359
+ "case it must not be specified as a separate #{key}=<value> parameter nor included " \
360
+ "in the JSON data passed in the --params option"
361
+ end
362
+
363
+ plan_params = pal.get_plan_info(plan)['parameters']
364
+ target_param = plan_params.dig('targets', 'type') =~ /TargetSpec/
365
+ node_param = plan_params.include?('nodes')
366
+
367
+ if node_param && target_param
368
+ msg = "Plan parameters include both 'nodes' and 'targets' with type 'TargetSpec', " \
369
+ "neither will populated with the value for --nodes or --targets."
370
+ Bolt::Logger.warn("nodes_targets_parameters", msg)
371
+ elsif node_param
372
+ params['nodes'] = targets.join(',')
373
+ elsif target_param
374
+ params['targets'] = targets.join(',')
375
+ end
376
+ end
377
+
378
+ plan_context = { plan_name: plan, params: params }
379
+
380
+ executor.start_plan(plan_context)
381
+ result = pal.run_plan(plan, params, executor, inventory, plugins.puppetdb_client)
382
+ executor.finish_plan(result)
383
+
384
+ result
385
+ end
386
+
387
+ # Show plan information.
388
+ #
389
+ # @param plan [String] The name of the plan to show.
390
+ # @return [Hash]
391
+ #
392
+ def show_plan(plan)
393
+ pal.get_plan_info(plan)
394
+ end
395
+
396
+ # List plans available to the project.
397
+ #
398
+ # @param filter [String] A substring to filter plans by.
399
+ # @return [Hash]
400
+ #
401
+ def list_plans(filter: nil)
402
+ {
403
+ plans: filter_content(pal.list_plans_with_cache(filter_content: true), filter),
404
+ modulepath: pal.user_modulepath
405
+ }
406
+ end
407
+
408
+ # Show available plugins.
409
+ #
410
+ # @return [Hash]
411
+ #
412
+ def list_plugins
413
+ { plugins: plugins.list_plugins, modulepath: pal.user_modulepath }
414
+ end
415
+
416
+ # Initialize the current directory as a Bolt project.
417
+ #
418
+ # @param name [String] The name of the project.
419
+ # @param [Bolt::Outputter] An outputter instance.
420
+ # @param modules [Array[String], NilClass] Modules to install.
421
+ # @return [Boolean]
422
+ #
423
+ def create_project(name, outputter, modules: nil)
424
+ Bolt::ProjectManager.new(config, outputter, pal)
425
+ .create(Dir.pwd, name, modules)
426
+ end
427
+
428
+ # Migrate a project to current best practices.
429
+ #
430
+ # @param [Bolt::Outputter] An outputter instance.
431
+ # @return [Boolean]
432
+ #
433
+ def migrate_project(outputter)
434
+ Bolt::ProjectManager.new(config, outputter, pal).migrate
435
+ end
436
+
437
+ # Run a script on a list of targets.
438
+ #
439
+ # @param script [String] The path to the script to run.
440
+ # @param targets [Array[String]] The targets to run on.
441
+ # @param arguments [Array[String], NilClass] Arguments to pass to the script.
442
+ # @param env_vars [Hash] Environment variables to set on the target.
443
+ # @return [Bolt::ResultSet]
444
+ #
445
+ def run_script(script, targets, arguments: [], env_vars: nil)
446
+ script = find_file(script)
447
+
448
+ Bolt::Util.validate_file('script', script)
449
+
450
+ with_benchmark do
451
+ executor.run_script(inventory.get_targets(targets), script, arguments, env_vars: env_vars)
452
+ end
453
+ end
454
+
455
+ # Generate a keypair using the configured secret plugin.
456
+ #
457
+ # @param force [Boolean] Forcibly create a keypair.
458
+ # @param plugin [String] The secret plugin to use.
459
+ # @return [Boolean]
460
+ #
461
+ def create_secret_keys(force: false, plugin: 'pkcs7')
462
+ unless plugins.by_name(plugin)
463
+ raise Bolt::Plugin::PluginError::Unknown, plugin
464
+ end
465
+
466
+ plugins.get_hook(plugin, :secret_createkeys)
467
+ .call('force' => force)
468
+ end
469
+
470
+ # Decrypt ciphertext using the configured secret plugin.
471
+ #
472
+ # @param ciphertext [String] The ciphertext to decrypt.
473
+ # @param plugin [String] The secret plugin to use.
474
+ # @return [Boolean]
475
+ #
476
+ def decrypt_secret(ciphertext, plugin: 'pkcs7')
477
+ unless plugins.by_name(plugin)
478
+ raise Bolt::Plugin::PluginError::Unknown, plugin
479
+ end
480
+
481
+ plugins.get_hook(plugin, :secret_decrypt)
482
+ .call('encrypted_value' => ciphertext)
483
+ end
484
+
485
+ # Encrypt plaintext using the configured secret plugin.
486
+ #
487
+ # @param plaintext [String] The plaintext to encrypt.
488
+ # @param plugin [String] The secret plugin to use.
489
+ # @return [Boolean]
490
+ #
491
+ def encrypt_secret(plaintext, plugin: 'pkcs7')
492
+ unless plugins.by_name(plugin)
493
+ raise Bolt::Plugin::PluginError::Unknown, plugin
494
+ end
495
+
496
+ plugins.get_hook(plugin, :secret_encrypt)
497
+ .call('plaintext_value' => plaintext)
498
+ end
499
+
500
+ # Run a task on a list of targets.
501
+ #
502
+ # @param task [String] The name of the task.
503
+ # @param options [Hash] Additional options.
504
+ # @return [Bolt::ResultSet]
505
+ #
506
+ def run_task(task, targets, params: {})
507
+ targets = inventory.get_targets(targets)
508
+
509
+ with_benchmark do
510
+ pal.run_task(task, targets, params, executor, inventory)
511
+ end
512
+ end
513
+
514
+ # Show task information.
515
+ #
516
+ # @param task [String] The name of the task to show.
517
+ # @return [Hash]
518
+ #
519
+ def show_task(task)
520
+ { task: pal.get_task(task) }
521
+ end
522
+
523
+ # List available tasks.
524
+ #
525
+ # @param filter [String] A substring to filter tasks by.
526
+ # @return [Hash]
527
+ #
528
+ def list_tasks(filter: nil)
529
+ {
530
+ tasks: filter_content(pal.list_tasks_with_cache(filter_content: true), filter),
531
+ modulepath: pal.user_modulepath
532
+ }
533
+ end
534
+
535
+ # Assert that there is a project configuration file.
536
+ #
537
+ # @param project [Bolt::Project] The Bolt project.
538
+ #
539
+ private def assert_project_file(project)
540
+ unless project.project_file?
541
+ command = Bolt::Util.powershell? ? 'New-BoltProject' : 'bolt project init'
542
+
543
+ msg = "Could not find project configuration file #{project.project_file}, unable "\
544
+ "to install modules. To create a Bolt project, run '#{command}'."
545
+
546
+ raise Bolt::Error.new(msg, 'bolt/missing-project-config-error')
547
+ end
548
+ end
549
+
550
+ # Filter a list of content by matching substring.
551
+ #
552
+ # @param content [Hash] The content to filter.
553
+ # @param filter [String] The substring to filter content by.
554
+ #
555
+ private def filter_content(content, filter)
556
+ return content unless content && filter
557
+ content.select { |name,| name.include?(filter) }
558
+ end
559
+
560
+ # Return the path to a file. If the path is an absolute or relative path to
561
+ # a file, and the file exists, return the path as-is. Otherwise, check if
562
+ # the path is a Puppet file path and look for the file in a module's files
563
+ # directory.
564
+ #
565
+ # @param path [String] The path to the file.
566
+ #
567
+ private def find_file(path)
568
+ return path if File.exist?(path) || Pathname.new(path).absolute?
569
+ modulepath = Bolt::Config::Modulepath.new(config.modulepath)
570
+ modules = Bolt::Module.discover(modulepath.full_modulepath, config.project)
571
+ mod, file = path.split(File::SEPARATOR, 2)
572
+
573
+ if modules[mod]
574
+ logger.debug("Did not find file at #{File.expand_path(path)}, checking in module '#{mod}'")
575
+ found = Bolt::Util.find_file_in_module(modules[mod].path, file || "")
576
+ path = found.nil? ? File.join(modules[mod].path, 'files', file) : found
577
+ end
578
+
579
+ path
580
+ end
581
+
582
+ # Get a list of Bolt guides.
583
+ #
584
+ private def load_guides
585
+ root_path = File.expand_path(File.join(__dir__, '..', '..', 'guides'))
586
+ files = Dir.children(root_path).sort
587
+
588
+ files.each_with_object({}) do |file, guides|
589
+ next if file !~ /\.(yaml|yml)\z/
590
+ topic = File.basename(file, ".*")
591
+ guides[topic] = File.join(root_path, file)
592
+ end
593
+ rescue SystemCallError => e
594
+ raise Bolt::FileError.new("#{e.message}: unable to load guides directory", root_path)
595
+ end
596
+
597
+ # Return a hash of targets sorted by those that are found in the inventory
598
+ # and those that are provided on the command line.
599
+ #
600
+ # @param targets [Array[String]] The targets to group.
601
+ #
602
+ private def group_targets_by_source(targets)
603
+ # Retrieve the known group and target names. This needs to be done before
604
+ # updating targets, as that will add adhoc targets to the inventory.
605
+ known_names = inventory.target_names
606
+ targets = inventory.get_targets(targets)
607
+
608
+ inventory_targets, adhoc_targets = targets.partition do |target|
609
+ known_names.include?(target.name)
610
+ end
611
+
612
+ { inventory: inventory_targets, adhoc: adhoc_targets }
613
+ end
614
+
615
+ # Benchmark the action and set the elapsed time on the result.
616
+ #
617
+ private def with_benchmark
618
+ result = nil
619
+
620
+ elapsed_time = Benchmark.realtime do
621
+ result = yield
622
+ end
623
+
624
+ result.tap { |r| r.elapsed_time = elapsed_time if r.is_a?(Bolt::ResultSet) }
625
+ end
626
+ end
627
+ end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'base64'
4
- require 'bolt/apply_result'
5
- require 'bolt/apply_target'
6
- require 'bolt/config'
7
- require 'bolt/error'
8
- require 'bolt/task'
9
- require 'bolt/util/puppet_log_level'
4
+ require_relative '../bolt/apply_result'
5
+ require_relative '../bolt/apply_target'
6
+ require_relative '../bolt/config'
7
+ require_relative '../bolt/error'
8
+ require_relative '../bolt/task'
9
+ require_relative '../bolt/util/puppet_log_level'
10
10
  require 'find'
11
11
  require 'json'
12
12
  require 'logging'
@@ -239,6 +239,7 @@ module Bolt
239
239
  search_dirs << mod.plugins if mod.plugins?
240
240
  search_dirs << mod.pluginfacts if mod.pluginfacts?
241
241
  search_dirs << mod.files if mod.files?
242
+ search_dirs << mod.scripts if mod.scripts?
242
243
  type_files = "#{mod.path}/types"
243
244
  search_dirs << type_files if File.exist?(type_files)
244
245
  search_dirs
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bolt/project'
4
- require 'bolt/config'
5
- require 'bolt/error'
3
+ require_relative '../bolt/project'
4
+ require_relative '../bolt/config'
5
+ require_relative '../bolt/error'
6
6
 
7
7
  module Bolt
8
8
  class ApplyInventory
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require 'bolt/error'
5
- require 'bolt/result'
4
+ require_relative '../bolt/error'
5
+ require_relative '../bolt/result'
6
6
 
7
7
  module Bolt
8
8
  class ApplyResult < Result