bolt 3.15.0 → 3.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.
- checksums.yaml +4 -4
- data/lib/bolt/analytics.rb +2 -19
- data/lib/bolt/application.rb +620 -0
- data/lib/bolt/bolt_option_parser.rb +12 -3
- data/lib/bolt/cli.rb +592 -792
- data/lib/bolt/fiber_executor.rb +7 -3
- data/lib/bolt/outputter/human.rb +83 -32
- data/lib/bolt/outputter/json.rb +63 -38
- data/lib/bolt/plan_creator.rb +2 -20
- data/lib/bolt/plan_future.rb +11 -6
- data/lib/bolt/plan_result.rb +1 -1
- data/lib/bolt/project.rb +0 -7
- data/lib/bolt/result_set.rb +2 -1
- data/lib/bolt/transport/local/connection.rb +17 -1
- data/lib/bolt/transport/orch/connection.rb +13 -1
- data/lib/bolt/version.rb +1 -1
- metadata +3 -3
- data/lib/bolt/secret.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d61e436c7358897d1769da6db2544c2e5b3e23310bd5a6663d2b35e2afc1ab5
|
4
|
+
data.tar.gz: 1322c9a80154199cb658cf7807a88a96448b3b197ec5576ddaeabf76da52c29f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57bddbbc4d2cb78cb2aa6964e3ecff5a61c95c88e5f6b7524bbbc5b5ce3ca070098aae37d2ebb2ba3fcb569e3a082bf9ec4ebff2013bae31fc65eedadbde4b6c
|
7
|
+
data.tar.gz: 2c64c1607a21cdebe49550ad8207201fe853478c9f18cb4af04826410ddf507f8c505fbae5340faa64c070910e3f9e86372d4ae1de4b6108fad4eb95fc454316
|
data/lib/bolt/analytics.rb
CHANGED
@@ -30,7 +30,6 @@ module Bolt
|
|
30
30
|
}.freeze
|
31
31
|
|
32
32
|
def self.build_client(enabled = true)
|
33
|
-
logger = Bolt::Logger.logger(self)
|
34
33
|
begin
|
35
34
|
config_file = config_path
|
36
35
|
config = enabled ? load_config(config_file) : {}
|
@@ -39,7 +38,7 @@ module Bolt
|
|
39
38
|
end
|
40
39
|
|
41
40
|
if !enabled || config['disabled'] || ENV['BOLT_DISABLE_ANALYTICS']
|
42
|
-
|
41
|
+
Bolt::Logger.debug "Analytics opt-out is set, analytics will be disabled"
|
43
42
|
NoopClient.new
|
44
43
|
else
|
45
44
|
unless config.key?('user-id')
|
@@ -50,7 +49,7 @@ module Bolt
|
|
50
49
|
Client.new(config['user-id'])
|
51
50
|
end
|
52
51
|
rescue StandardError => e
|
53
|
-
|
52
|
+
Bolt::Logger.debug "Failed to initialize analytics client, analytics will be disabled: #{e}"
|
54
53
|
NoopClient.new
|
55
54
|
end
|
56
55
|
|
@@ -139,18 +138,6 @@ module Bolt
|
|
139
138
|
end
|
140
139
|
end
|
141
140
|
|
142
|
-
def plan_counts(plans_path)
|
143
|
-
pp_count, yaml_count = if File.exist?(plans_path)
|
144
|
-
%w[pp yaml].map do |extension|
|
145
|
-
Find.find(plans_path.to_s).grep(/.*\.#{extension}/).length
|
146
|
-
end
|
147
|
-
else
|
148
|
-
[0, 0]
|
149
|
-
end
|
150
|
-
|
151
|
-
{ puppet_plan_count: pp_count, yaml_plan_count: yaml_count }
|
152
|
-
end
|
153
|
-
|
154
141
|
def event(category, action, label: nil, value: nil, **kwargs)
|
155
142
|
custom_dimensions = Bolt::Util.walk_keys(kwargs) do |k|
|
156
143
|
CUSTOM_DIMENSIONS[k] || raise("Unknown analytics key '#{k}'")
|
@@ -236,10 +223,6 @@ module Bolt
|
|
236
223
|
|
237
224
|
def report_bundled_content(mode, name); end
|
238
225
|
|
239
|
-
def plan_counts(_)
|
240
|
-
{}
|
241
|
-
end
|
242
|
-
|
243
226
|
def event(category, action, **_kwargs)
|
244
227
|
@logger.trace "Skipping submission of '#{category} #{action}' event because analytics is disabled"
|
245
228
|
end
|
@@ -0,0 +1,620 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
require 'bolt/util'
|
6
|
+
|
7
|
+
module Bolt
|
8
|
+
class Application
|
9
|
+
attr_reader :analytics, :config, :executor, :inventory, :logger, :pal, :plugins
|
10
|
+
private :analytics, :config, :executor, :inventory, :logger, :pal, :plugins
|
11
|
+
|
12
|
+
def initialize(
|
13
|
+
analytics:,
|
14
|
+
config:,
|
15
|
+
executor:,
|
16
|
+
inventory:,
|
17
|
+
pal:,
|
18
|
+
plugins:
|
19
|
+
)
|
20
|
+
@analytics = analytics
|
21
|
+
@config = config
|
22
|
+
@executor = executor
|
23
|
+
@inventory = inventory
|
24
|
+
@logger = Bolt::Logger.logger(self)
|
25
|
+
@pal = pal
|
26
|
+
@plugins = plugins
|
27
|
+
end
|
28
|
+
|
29
|
+
# Shuts down the application.
|
30
|
+
#
|
31
|
+
def shutdown
|
32
|
+
executor.shutdown
|
33
|
+
end
|
34
|
+
|
35
|
+
# Apply Puppet manifest code to a list of targets.
|
36
|
+
#
|
37
|
+
# @param manifest [String, NilClass] The path to a Puppet manifest file.
|
38
|
+
# @param targets [Array[String]] The targets to run on.
|
39
|
+
# @param code [String] Puppet manifest code to apply.
|
40
|
+
# @param noop [Boolean] Whether to apply in no-operation mode.
|
41
|
+
# @return [Bolt::ResultSet]
|
42
|
+
#
|
43
|
+
def apply(manifest, targets, code: '', noop: false)
|
44
|
+
manifest_code = if manifest
|
45
|
+
Bolt::Util.validate_file('manifest', manifest)
|
46
|
+
File.read(File.expand_path(manifest))
|
47
|
+
else
|
48
|
+
code
|
49
|
+
end
|
50
|
+
|
51
|
+
targets = inventory.get_targets(targets)
|
52
|
+
|
53
|
+
Puppet[:tasks] = false
|
54
|
+
ast = pal.parse_manifest(manifest_code, manifest)
|
55
|
+
|
56
|
+
if defined?(ast.body) &&
|
57
|
+
(ast.body.is_a?(Puppet::Pops::Model::HostClassDefinition) ||
|
58
|
+
ast.body.is_a?(Puppet::Pops::Model::ResourceTypeDefinition))
|
59
|
+
message = "Manifest only contains definitions and will result in no changes on the targets. "\
|
60
|
+
"Definitions must be declared for their resources to be applied. You can read more "\
|
61
|
+
"about defining and declaring classes and types in the Puppet documentation at "\
|
62
|
+
"https://puppet.com/docs/puppet/latest/lang_classes.html and "\
|
63
|
+
"https://puppet.com/docs/puppet/latest/lang_defined_types.html"
|
64
|
+
Bolt::Logger.warn("empty_manifest", message)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Apply logging looks like plan logging
|
68
|
+
executor.publish_event(type: :plan_start, plan: nil)
|
69
|
+
|
70
|
+
with_benchmark do
|
71
|
+
apply_prep_results = pal.in_plan_compiler(executor, inventory, plugins.puppetdb_client) do |compiler|
|
72
|
+
compiler.call_function('apply_prep', targets, '_catch_errors' => true)
|
73
|
+
end
|
74
|
+
|
75
|
+
apply_results = pal.with_bolt_executor(executor, inventory, plugins.puppetdb_client) do
|
76
|
+
Puppet.lookup(:apply_executor)
|
77
|
+
.apply_ast(ast, apply_prep_results.ok_set.targets, catch_errors: true, noop: noop)
|
78
|
+
end
|
79
|
+
|
80
|
+
Bolt::ResultSet.new(apply_prep_results.error_set.results + apply_results.results)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Run a command on a list of targets.
|
85
|
+
#
|
86
|
+
# @param command [String] The command.
|
87
|
+
# @param targets [Array[String]] The targets to run on.
|
88
|
+
# @param env_vars [Hash] Environment variables to set on the target.
|
89
|
+
# @return [Bolt::ResultSet]
|
90
|
+
#
|
91
|
+
def run_command(command, targets, env_vars: {})
|
92
|
+
targets = inventory.get_targets(targets)
|
93
|
+
|
94
|
+
with_benchmark do
|
95
|
+
executor.run_command(targets, command, env_vars: env_vars)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Download a file from a list of targets to a directory on the controller.
|
100
|
+
#
|
101
|
+
# @param source [String] The path to the file on the targets.
|
102
|
+
# @param destination [String] The path to the directory on the controller.
|
103
|
+
# @param targets [Array[String]] The targets to run on.
|
104
|
+
# @return [Bolt::ResultSet]
|
105
|
+
#
|
106
|
+
def download_file(source, destination, targets)
|
107
|
+
destination = File.expand_path(destination, Dir.pwd)
|
108
|
+
targets = inventory.get_targets(targets)
|
109
|
+
|
110
|
+
with_benchmark do
|
111
|
+
executor.download_file(targets, source, destination)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Upload a file from the controller to a list of targets.
|
116
|
+
#
|
117
|
+
# @param source [String] The path to the file on the controller.
|
118
|
+
# @param destination [String] The destination path on the targets.
|
119
|
+
# @param targets [Array[String]] The targets to run on.
|
120
|
+
# @return [Bolt::ResultSet]
|
121
|
+
#
|
122
|
+
def upload_file(source, destination, targets)
|
123
|
+
future = executor.future&.fetch('file_paths', false)
|
124
|
+
source = find_file(source, future)
|
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
|
+
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
|
+
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
|
+
# @return [Boolean]
|
331
|
+
#
|
332
|
+
def new_plan(name, puppet: false)
|
333
|
+
Bolt::PlanCreator.validate_input(config.project, name)
|
334
|
+
Bolt::PlanCreator.create_plan(config.project.plans_path, name, puppet)
|
335
|
+
end
|
336
|
+
|
337
|
+
# Run a plan.
|
338
|
+
#
|
339
|
+
# @param plan [String] The plan to run.
|
340
|
+
# @param targets [Array[String], NilClass] The targets to pass to the plan.
|
341
|
+
# @param params [Hash] Parameters to pass to the plan.
|
342
|
+
# @return [Bolt::PlanResult]
|
343
|
+
#
|
344
|
+
def run_plan(plan, targets, params: {})
|
345
|
+
if targets && targets.any?
|
346
|
+
if params['nodes'] || params['targets']
|
347
|
+
key = params.include?('nodes') ? 'nodes' : 'targets'
|
348
|
+
raise Bolt::CLIError,
|
349
|
+
"A plan's '#{key}' parameter can be specified using the --#{key} option, but in that " \
|
350
|
+
"case it must not be specified as a separate #{key}=<value> parameter nor included " \
|
351
|
+
"in the JSON data passed in the --params option"
|
352
|
+
end
|
353
|
+
|
354
|
+
plan_params = pal.get_plan_info(plan)['parameters']
|
355
|
+
target_param = plan_params.dig('targets', 'type') =~ /TargetSpec/
|
356
|
+
node_param = plan_params.include?('nodes')
|
357
|
+
|
358
|
+
if node_param && target_param
|
359
|
+
msg = "Plan parameters include both 'nodes' and 'targets' with type 'TargetSpec', " \
|
360
|
+
"neither will populated with the value for --nodes or --targets."
|
361
|
+
Bolt::Logger.warn("nodes_targets_parameters", msg)
|
362
|
+
elsif node_param
|
363
|
+
params['nodes'] = targets.join(',')
|
364
|
+
elsif target_param
|
365
|
+
params['targets'] = targets.join(',')
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
plan_context = { plan_name: plan, params: params }
|
370
|
+
|
371
|
+
executor.start_plan(plan_context)
|
372
|
+
result = pal.run_plan(plan, params, executor, inventory, plugins.puppetdb_client)
|
373
|
+
executor.finish_plan(result)
|
374
|
+
|
375
|
+
result
|
376
|
+
end
|
377
|
+
|
378
|
+
# Show plan information.
|
379
|
+
#
|
380
|
+
# @param plan [String] The name of the plan to show.
|
381
|
+
# @return [Hash]
|
382
|
+
#
|
383
|
+
def show_plan(plan)
|
384
|
+
pal.get_plan_info(plan)
|
385
|
+
end
|
386
|
+
|
387
|
+
# List plans available to the project.
|
388
|
+
#
|
389
|
+
# @param filter [String] A substring to filter plans by.
|
390
|
+
# @return [Hash]
|
391
|
+
#
|
392
|
+
def list_plans(filter: nil)
|
393
|
+
{
|
394
|
+
plans: filter_content(pal.list_plans_with_cache(filter_content: true), filter),
|
395
|
+
modulepath: pal.user_modulepath
|
396
|
+
}
|
397
|
+
end
|
398
|
+
|
399
|
+
# Show available plugins.
|
400
|
+
#
|
401
|
+
# @return [Hash]
|
402
|
+
#
|
403
|
+
def list_plugins
|
404
|
+
{ plugins: plugins.list_plugins, modulepath: pal.user_modulepath }
|
405
|
+
end
|
406
|
+
|
407
|
+
# Initialize the current directory as a Bolt project.
|
408
|
+
#
|
409
|
+
# @param name [String] The name of the project.
|
410
|
+
# @param [Bolt::Outputter] An outputter instance.
|
411
|
+
# @param modules [Array[String], NilClass] Modules to install.
|
412
|
+
# @return [Boolean]
|
413
|
+
#
|
414
|
+
def create_project(name, outputter, modules: nil)
|
415
|
+
Bolt::ProjectManager.new(config, outputter, pal)
|
416
|
+
.create(Dir.pwd, name, modules)
|
417
|
+
end
|
418
|
+
|
419
|
+
# Migrate a project to current best practices.
|
420
|
+
#
|
421
|
+
# @param [Bolt::Outputter] An outputter instance.
|
422
|
+
# @return [Boolean]
|
423
|
+
#
|
424
|
+
def migrate_project(outputter)
|
425
|
+
Bolt::ProjectManager.new(config, outputter, pal).migrate
|
426
|
+
end
|
427
|
+
|
428
|
+
# Run a script on a list of targets.
|
429
|
+
#
|
430
|
+
# @param script [String] The path to the script to run.
|
431
|
+
# @param targets [Array[String]] The targets to run on.
|
432
|
+
# @param arguments [Array[String], NilClass] Arguments to pass to the script.
|
433
|
+
# @param env_vars [Hash] Environment variables to set on the target.
|
434
|
+
# @return [Bolt::ResultSet]
|
435
|
+
#
|
436
|
+
def run_script(script, targets, arguments: [], env_vars: {})
|
437
|
+
future = executor.future&.fetch('file_paths', false)
|
438
|
+
script = find_file(script, future)
|
439
|
+
|
440
|
+
Bolt::Util.validate_file('script', script)
|
441
|
+
|
442
|
+
with_benchmark do
|
443
|
+
executor.run_script(inventory.get_targets(targets), script, arguments, env_vars: env_vars)
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
# Generate a keypair using the configured secret plugin.
|
448
|
+
#
|
449
|
+
# @param force [Boolean] Forcibly create a keypair.
|
450
|
+
# @param plugin [String] The secret plugin to use.
|
451
|
+
# @return [Boolean]
|
452
|
+
#
|
453
|
+
def create_secret_keys(force: false, plugin: 'pkcs7')
|
454
|
+
unless plugins.by_name(plugin)
|
455
|
+
raise Bolt::Plugin::PluginError::Unknown, plugin
|
456
|
+
end
|
457
|
+
|
458
|
+
plugins.get_hook(plugin, :secret_createkeys)
|
459
|
+
.call('force' => force)
|
460
|
+
end
|
461
|
+
|
462
|
+
# Decrypt ciphertext using the configured secret plugin.
|
463
|
+
#
|
464
|
+
# @param ciphertext [String] The ciphertext to decrypt.
|
465
|
+
# @param plugin [String] The secret plugin to use.
|
466
|
+
# @return [Boolean]
|
467
|
+
#
|
468
|
+
def decrypt_secret(ciphertext, plugin: 'pkcs7')
|
469
|
+
unless plugins.by_name(plugin)
|
470
|
+
raise Bolt::Plugin::PluginError::Unknown, plugin
|
471
|
+
end
|
472
|
+
|
473
|
+
plugins.get_hook(plugin, :secret_decrypt)
|
474
|
+
.call('encrypted_value' => ciphertext)
|
475
|
+
end
|
476
|
+
|
477
|
+
# Encrypt plaintext using the configured secret plugin.
|
478
|
+
#
|
479
|
+
# @param plaintext [String] The plaintext to encrypt.
|
480
|
+
# @param plugin [String] The secret plugin to use.
|
481
|
+
# @return [Boolean]
|
482
|
+
#
|
483
|
+
def encrypt_secret(plaintext, plugin: 'pkcs7')
|
484
|
+
unless plugins.by_name(plugin)
|
485
|
+
raise Bolt::Plugin::PluginError::Unknown, plugin
|
486
|
+
end
|
487
|
+
|
488
|
+
plugins.get_hook(plugin, :secret_encrypt)
|
489
|
+
.call('plaintext_value' => plaintext)
|
490
|
+
end
|
491
|
+
|
492
|
+
# Run a task on a list of targets.
|
493
|
+
#
|
494
|
+
# @param task [String] The name of the task.
|
495
|
+
# @param options [Hash] Additional options.
|
496
|
+
# @return [Bolt::ResultSet]
|
497
|
+
#
|
498
|
+
def run_task(task, targets, params: {})
|
499
|
+
targets = inventory.get_targets(targets)
|
500
|
+
|
501
|
+
with_benchmark do
|
502
|
+
pal.run_task(task, targets, params, executor, inventory)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
# Show task information.
|
507
|
+
#
|
508
|
+
# @param task [String] The name of the task to show.
|
509
|
+
# @return [Hash]
|
510
|
+
#
|
511
|
+
def show_task(task)
|
512
|
+
{ task: pal.get_task(task) }
|
513
|
+
end
|
514
|
+
|
515
|
+
# List available tasks.
|
516
|
+
#
|
517
|
+
# @param filter [String] A substring to filter tasks by.
|
518
|
+
# @return [Hash]
|
519
|
+
#
|
520
|
+
def list_tasks(filter: nil)
|
521
|
+
{
|
522
|
+
tasks: filter_content(pal.list_tasks_with_cache(filter_content: true), filter),
|
523
|
+
modulepath: pal.user_modulepath
|
524
|
+
}
|
525
|
+
end
|
526
|
+
|
527
|
+
# Assert that there is a project configuration file.
|
528
|
+
#
|
529
|
+
# @param project [Bolt::Project] The Bolt project.
|
530
|
+
#
|
531
|
+
private def assert_project_file(project)
|
532
|
+
unless project.project_file?
|
533
|
+
command = Bolt::Util.powershell? ? 'New-BoltProject' : 'bolt project init'
|
534
|
+
|
535
|
+
msg = "Could not find project configuration file #{project.project_file}, unable "\
|
536
|
+
"to install modules. To create a Bolt project, run '#{command}'."
|
537
|
+
|
538
|
+
raise Bolt::Error.new(msg, 'bolt/missing-project-config-error')
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
# Filter a list of content by matching substring.
|
543
|
+
#
|
544
|
+
# @param content [Hash] The content to filter.
|
545
|
+
# @param filter [String] The substring to filter content by.
|
546
|
+
#
|
547
|
+
private def filter_content(content, filter)
|
548
|
+
return content unless content && filter
|
549
|
+
content.select { |name,| name.include?(filter) }
|
550
|
+
end
|
551
|
+
|
552
|
+
# Return the path to a file. If the path is an absolute or relative path to
|
553
|
+
# a file, and the file exists, return the path as-is. Otherwise, check if
|
554
|
+
# the path is a Puppet file path and look for the file in a module's files
|
555
|
+
# directory.
|
556
|
+
#
|
557
|
+
# @param path [String] The path to the file.
|
558
|
+
# @param future_file_paths [Boolean] Whether to use future file path behavior.
|
559
|
+
#
|
560
|
+
private def find_file(path, future_file_paths)
|
561
|
+
return path if File.exist?(path) || Pathname.new(path).absolute?
|
562
|
+
modulepath = Bolt::Config::Modulepath.new(config.modulepath)
|
563
|
+
modules = Bolt::Module.discover(modulepath.full_modulepath, config.project)
|
564
|
+
mod, file = path.split(File::SEPARATOR, 2)
|
565
|
+
|
566
|
+
if modules[mod]
|
567
|
+
logger.debug("Did not find file at #{File.expand_path(path)}, checking in module '#{mod}'")
|
568
|
+
found = Bolt::Util.find_file_in_module(modules[mod].path, file || "", future_file_paths)
|
569
|
+
path = found.nil? ? File.join(modules[mod].path, 'files', file) : found
|
570
|
+
end
|
571
|
+
|
572
|
+
path
|
573
|
+
end
|
574
|
+
|
575
|
+
# Get a list of Bolt guides.
|
576
|
+
#
|
577
|
+
private def load_guides
|
578
|
+
root_path = File.expand_path(File.join(__dir__, '..', '..', 'guides'))
|
579
|
+
files = Dir.children(root_path).sort
|
580
|
+
|
581
|
+
files.each_with_object({}) do |file, guides|
|
582
|
+
next if file !~ /\.(yaml|yml)\z/
|
583
|
+
topic = File.basename(file, ".*")
|
584
|
+
guides[topic] = File.join(root_path, file)
|
585
|
+
end
|
586
|
+
rescue SystemCallError => e
|
587
|
+
raise Bolt::FileError.new("#{e.message}: unable to load guides directory", root_path)
|
588
|
+
end
|
589
|
+
|
590
|
+
# Return a hash of targets sorted by those that are found in the inventory
|
591
|
+
# and those that are provided on the command line.
|
592
|
+
#
|
593
|
+
# @param targets [Array[String]] The targets to group.
|
594
|
+
#
|
595
|
+
private def group_targets_by_source(targets)
|
596
|
+
# Retrieve the known group and target names. This needs to be done before
|
597
|
+
# updating targets, as that will add adhoc targets to the inventory.
|
598
|
+
known_names = inventory.target_names
|
599
|
+
targets = inventory.get_targets(targets)
|
600
|
+
|
601
|
+
inventory_targets, adhoc_targets = targets.partition do |target|
|
602
|
+
known_names.include?(target.name)
|
603
|
+
end
|
604
|
+
|
605
|
+
{ inventory: inventory_targets, adhoc: adhoc_targets }
|
606
|
+
end
|
607
|
+
|
608
|
+
# Benchmark the action and set the elapsed time on the result.
|
609
|
+
#
|
610
|
+
private def with_benchmark
|
611
|
+
result = nil
|
612
|
+
|
613
|
+
elapsed_time = Benchmark.realtime do
|
614
|
+
result = yield
|
615
|
+
end
|
616
|
+
|
617
|
+
result.tap { |r| r.elapsed_time = elapsed_time if r.is_a?(Bolt::ResultSet) }
|
618
|
+
end
|
619
|
+
end
|
620
|
+
end
|