openbolt 9.0.0 → 9.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccffb09806ff6f277b75360ff8f804592795a3712ef3150e525e947673604737
4
- data.tar.gz: 42fcccda83424540f1ebee736234a00570d4060e560f05ba06864385364c39ad
3
+ metadata.gz: a785301d7364aa6154589a201ee8c4e1a54c46ac8a55a64dc3f4daa3db58ba6a
4
+ data.tar.gz: b35ec9d9ac26ddec706c115dc92d63d741d3e0135fba3f018b57c59da1118d0d
5
5
  SHA512:
6
- metadata.gz: e6af1fa2f0a8aeb236df11db580692c04b425c9fca04af1321636c488e3f46af603e47fee4ce604c721d0f5ff80cb2acaf076d1fd91a0ac54a76836c29981ee3
7
- data.tar.gz: a76d500e92acd698776fe5b61d4be4bfe7dd5e6860025b4d0aeba2702e81dea893ec48362968908b52878e092c0c6edf25d0133b81c8d940c7e25bda81136570
6
+ metadata.gz: 0b1ef8e10d7083222615aaa5bfaa1c29a4afe166e4a5c5ededeb8fe3d0565c8586d43e21a62350d554ea6e8fa1d786ffa0283ad899c401f3e2c89a49f49d2045
7
+ data.tar.gz: 2559e496b91fd2c482abd3e625d3d675434bbfd8c9017df2ab1cbf379f58cea5d11711455172ce56261d3eb9642a285b283f15b5774f89e77843baae5435e5de
data/Puppetfile CHANGED
@@ -49,7 +49,7 @@ mod 'puppetlabs-pkcs7', '0.2.0'
49
49
  mod 'puppetlabs-secure_env_vars', '0.3.0'
50
50
  mod 'puppetlabs-terraform', '0.8.0'
51
51
  mod 'puppetlabs-vault', '0.5.0'
52
- mod 'puppetlabs-yaml', '0.3.0'
52
+ mod 'puppetlabs-yaml', '0.3.1'
53
53
 
54
54
  # If we don't list these modules explicitly, r10k will purge them
55
55
  mod 'canary', local: true
@@ -81,23 +81,31 @@ Puppet::Functions.create_function(:run_task) do
81
81
  return Bolt::ResultSet.new([])
82
82
  end
83
83
 
84
- # TODO: use the compiler injection once PUP-8237 lands
85
- task_signature = Puppet::Pal::ScriptCompiler.new(closure_scope.compiler).task_signature(task_name)
86
- if task_signature.nil?
87
- raise Bolt::Error.unknown_task(task_name)
88
- end
84
+ # If all targets use the PCP transport, create a fake task instead of
85
+ # loading the actual task definition.
86
+ if targets.all? { |t| t.transport == 'pcp' }
87
+ task = Bolt::Task.new(task_name,
88
+ { 'supports_noop' => true },
89
+ [{ 'name' => '', 'path' => '' }])
90
+ else
91
+ # TODO: use the compiler injection once PUP-8237 lands
92
+ task_signature = Puppet::Pal::ScriptCompiler.new(closure_scope.compiler).task_signature(task_name)
93
+ if task_signature.nil?
94
+ raise Bolt::Error.unknown_task(task_name)
95
+ end
89
96
 
90
- task = Bolt::Task.from_task_signature(task_signature)
97
+ task = Bolt::Task.from_task_signature(task_signature)
91
98
 
92
- # Set the default value for any params that have one and were not provided
93
- # or are undef.
94
- params = task.parameter_defaults.merge(params) do |_, default, passed|
95
- passed.nil? ? default : passed
96
- end
99
+ # Set the default value for any params that have one and were not provided
100
+ # or are undef.
101
+ params = task.parameter_defaults.merge(params) do |_, default, passed|
102
+ passed.nil? ? default : passed
103
+ end
97
104
 
98
- task_signature.runnable_with?(params) do |mismatch_message|
99
- raise with_stack(:TYPE_MISMATCH, mismatch_message)
100
- end || (raise with_stack(:TYPE_MISMATCH, 'Task parameters do not match'))
105
+ task_signature.runnable_with?(params) do |mismatch_message|
106
+ raise with_stack(:TYPE_MISMATCH, mismatch_message)
107
+ end || (raise with_stack(:TYPE_MISMATCH, 'Task parameters do not match'))
108
+ end
101
109
 
102
110
  # Generate a helpful error message about the type-mismatch between the type
103
111
  # Data and the actual type of params.
@@ -13,6 +13,8 @@ require 'bolt/task'
13
13
  # This function does nothing if the list of targets is empty.
14
14
  #
15
15
  # > **Note:** Not available in apply block
16
+ #
17
+ # > **Note:** Not available to targets using the pcp transport
16
18
  Puppet::Functions.create_function(:run_task_with) do
17
19
  # Run a task with target-specific parameters.
18
20
  # @param task_name The task to run.
@@ -95,14 +97,22 @@ Puppet::Functions.create_function(:run_task_with) do
95
97
  return Bolt::ResultSet.new([])
96
98
  end
97
99
 
98
- task_signature = Puppet::Pal::ScriptCompiler.new(closure_scope.compiler).task_signature(task_name)
100
+ # If all targets use the 'pcp' transport, use a fake task instead of loading the local definition
101
+ # Otherwise, load the local task definition
102
+ if (pcp_only = targets.all? { |t| t.transport == 'pcp' })
103
+ task = Bolt::Task.new(task_name,
104
+ { 'supports_noop' => true },
105
+ [{ 'name' => '', 'path' => '' }])
106
+ else
107
+ task_signature = Puppet::Pal::ScriptCompiler.new(closure_scope.compiler).task_signature(task_name)
108
+
109
+ if task_signature.nil?
110
+ raise Bolt::Error.unknown_task(task_name)
111
+ end
99
112
 
100
- if task_signature.nil?
101
- raise Bolt::Error.unknown_task(task_name)
113
+ task = Bolt::Task.from_task_signature(task_signature)
102
114
  end
103
115
 
104
- task = Bolt::Task.from_task_signature(task_signature)
105
-
106
116
  # Map the targets to their specific parameters and merge with the defaults
107
117
  target_mapping = targets.each_with_object({}) do |target, mapping|
108
118
  params = yield(target)
@@ -120,17 +130,19 @@ Puppet::Functions.create_function(:run_task_with) do
120
130
 
121
131
  # If parameters are mismatched, create a failing result for the target that will later
122
132
  # be added to the ResultSet.
123
- # Set the default value for any params that have one and were not provided or are undef
124
- params = task.parameter_defaults.merge(params) do |_, default, passed|
125
- passed.nil? ? default : passed
126
- end
133
+ unless pcp_only
134
+ # Set the default value for any params that have one and were not provided or are undef
135
+ params = task.parameter_defaults.merge(params) do |_, default, passed|
136
+ passed.nil? ? default : passed
137
+ end
127
138
 
128
- type_match = task_signature.runnable_with?(params) do |mismatch_message|
129
- exception = with_stack(:TYPE_MISMATCH, mismatch_message)
130
- error_set << Bolt::Result.from_exception(target, exception, action: 'task')
131
- end
139
+ type_match = task_signature.runnable_with?(params) do |mismatch_message|
140
+ exception = with_stack(:TYPE_MISMATCH, mismatch_message)
141
+ error_set << Bolt::Result.from_exception(target, exception, action: 'task')
142
+ end
132
143
 
133
- next unless type_match
144
+ next unless type_match
145
+ end
134
146
 
135
147
  # If there is a type mismatch between the type Data and the type of the params, create
136
148
  # a failing result for the target that will later be added to the ResultSet.
@@ -373,7 +373,15 @@ module Bolt
373
373
  end
374
374
  end
375
375
 
376
- pal.run_plan(plan, params, executor, inventory, plugins.puppetdb_client)
376
+ sensitive_params = params.keys.select { |param| plan_params.dig(param, 'sensitive') }
377
+
378
+ plan_context = { plan_name: plan, params: params, sensitive: sensitive_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
377
385
  rescue Bolt::Error => e
378
386
  Bolt::PlanResult.new(e, 'failure')
379
387
  end
@@ -5,6 +5,7 @@ require_relative '../../bolt/config/transport/docker'
5
5
  require_relative '../../bolt/config/transport/jail'
6
6
  require_relative '../../bolt/config/transport/local'
7
7
  require_relative '../../bolt/config/transport/lxd'
8
+ require_relative '../../bolt/config/transport/orch'
8
9
  require_relative '../../bolt/config/transport/podman'
9
10
  require_relative '../../bolt/config/transport/remote'
10
11
  require_relative '../../bolt/config/transport/ssh'
@@ -21,6 +22,7 @@ module Bolt
21
22
  'jail' => Bolt::Config::Transport::Jail,
22
23
  'local' => Bolt::Config::Transport::Local,
23
24
  'lxd' => Bolt::Config::Transport::LXD,
25
+ 'pcp' => Bolt::Config::Transport::Orch,
24
26
  'podman' => Bolt::Config::Transport::Podman,
25
27
  'remote' => Bolt::Config::Transport::Remote,
26
28
  'ssh' => Bolt::Config::Transport::SSH,
@@ -585,6 +587,12 @@ module Bolt
585
587
  _plugin: true,
586
588
  _example: { cleanup: false }
587
589
  },
590
+ "pcp" => {
591
+ description: "A map of configuration options for the pcp transport.",
592
+ type: Hash,
593
+ _plugin: true,
594
+ _example: { "job-poll-interval" => 15, "job-poll-timeout" => 30 }
595
+ },
588
596
  "podman" => {
589
597
  description: "A map of configuration options for the podman transport.",
590
598
  type: Hash,
@@ -21,6 +21,7 @@ module Bolt
21
21
  brokers
22
22
  puppet-environment
23
23
  rpc-timeout
24
+ run-as
24
25
  ssl-ca
25
26
  ssl-cert
26
27
  ssl-key
@@ -238,6 +238,20 @@ module Bolt
238
238
  _example: { "rb" => ["/usr/bin/ruby", "-r", "puppet"],
239
239
  ".py" => "/usr/bin/python3" }
240
240
  },
241
+ "job-poll-interval" => {
242
+ type: Integer,
243
+ description: "The interval, in seconds, to poll orchestrator for job status.",
244
+ minimum: 1,
245
+ _plugin: true,
246
+ _example: 2
247
+ },
248
+ "job-poll-timeout" => {
249
+ type: Integer,
250
+ description: "The time, in seconds, to wait for orchestrator job status.",
251
+ minimum: 1,
252
+ _plugin: true,
253
+ _example: 2000
254
+ },
241
255
  "kex-algorithms" => {
242
256
  type: Array,
243
257
  description: "A list of key exchange algorithms to use when establishing a connection " \
@@ -355,6 +369,13 @@ module Bolt
355
369
  _default: "production",
356
370
  _example: "staging"
357
371
  },
372
+ "read-timeout" => {
373
+ type: Integer,
374
+ description: "How long to wait in seconds when making requests to the Orchestrator.",
375
+ minimum: 1,
376
+ _plugin: true,
377
+ _example: 15
378
+ },
358
379
  "realm" => {
359
380
  type: String,
360
381
  description: "The Kerberos realm (Active Directory domain) to authenticate against.",
@@ -488,6 +509,13 @@ module Bolt
488
509
  _plugin: true,
489
510
  _example: "p@$$w0rd!"
490
511
  },
512
+ "task-environment" => {
513
+ type: String,
514
+ description: "The environment the orchestrator loads task code from.",
515
+ _plugin: true,
516
+ _default: "production",
517
+ _example: "development"
518
+ },
491
519
  "task-timeout" => {
492
520
  type: Integer,
493
521
  description: "How long to wait in seconds for tasks to complete when using the Choria transport.",
@@ -502,6 +530,12 @@ module Bolt
502
530
  _plugin: true,
503
531
  _example: "/tmp/bolt"
504
532
  },
533
+ "token-file" => {
534
+ type: String,
535
+ description: "The path to the token file.",
536
+ _plugin: true,
537
+ _example: "~/.puppetlabs/puppet/token.pem"
538
+ },
505
539
  "tty" => {
506
540
  type: [TrueClass, FalseClass],
507
541
  description: "Whether to enable tty on exec commands.",
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../bolt/error'
4
+ require_relative '../../../bolt/config/transport/base'
5
+
6
+ module Bolt
7
+ class Config
8
+ module Transport
9
+ class Orch < Base
10
+ OPTIONS = %w[
11
+ cacert
12
+ host
13
+ job-poll-interval
14
+ job-poll-timeout
15
+ read-timeout
16
+ service-url
17
+ task-environment
18
+ token-file
19
+ ].freeze
20
+
21
+ DEFAULTS = {
22
+ "task-environment" => "production"
23
+ }.freeze
24
+
25
+ private def validate
26
+ super
27
+
28
+ if @config['cacert']
29
+ @config['cacert'] = File.expand_path(@config['cacert'], @project)
30
+ Bolt::Util.validate_file('cacert', @config['cacert'])
31
+ end
32
+
33
+ if @config['token-file']
34
+ @config['token-file'] = File.expand_path(@config['token-file'], @project)
35
+ Bolt::Util.validate_file('token-file', @config['token-file'])
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/bolt/executor.rb CHANGED
@@ -18,6 +18,7 @@ require_relative '../bolt/transport/docker'
18
18
  require_relative '../bolt/transport/jail'
19
19
  require_relative '../bolt/transport/local'
20
20
  require_relative '../bolt/transport/lxd'
21
+ require_relative '../bolt/transport/orch'
21
22
  require_relative '../bolt/transport/podman'
22
23
  require_relative '../bolt/transport/remote'
23
24
  require_relative '../bolt/transport/ssh'
@@ -30,6 +31,7 @@ module Bolt
30
31
  jail: Bolt::Transport::Jail,
31
32
  local: Bolt::Transport::Local,
32
33
  lxd: Bolt::Transport::LXD,
34
+ pcp: Bolt::Transport::Orch,
33
35
  podman: Bolt::Transport::Podman,
34
36
  remote: Bolt::Transport::Remote,
35
37
  ssh: Bolt::Transport::SSH,
@@ -55,6 +57,10 @@ module Bolt
55
57
  Concurrent::Delay.new do
56
58
  val.new(self)
57
59
  end
60
+ elsif key == :pcp
61
+ Concurrent::Delay.new do
62
+ val.new.tap { |pcp| pcp.plan_context = @plan_context }
63
+ end
58
64
  else
59
65
  Concurrent::Delay.new do
60
66
  val.new
@@ -520,6 +526,21 @@ module Bolt
520
526
  value
521
527
  end
522
528
 
529
+ # Only the pcp transport uses the plan context, to report the plan to the
530
+ # orchestrator. The transport gets the context when it's created for a pcp
531
+ # target, so a plan without pcp targets never loads the orchestrator_client
532
+ # gem.
533
+ def start_plan(plan_context)
534
+ @plan_context = plan_context
535
+ pcp = @transports['pcp']
536
+ pcp.value.plan_context = plan_context if pcp.fulfilled?
537
+ end
538
+
539
+ def finish_plan(plan_result)
540
+ pcp = @transports['pcp']
541
+ pcp.value.finish_plan(plan_result) if pcp.fulfilled?
542
+ end
543
+
523
544
  def without_default_logging
524
545
  publish_event(type: :disable_default_output)
525
546
  yield
@@ -222,16 +222,19 @@ module Bolt
222
222
 
223
223
  # Fire node_start for every target, configure the client, discover
224
224
  # agents, partition targets by agent availability, and emit errors for
225
- # incapable targets.
225
+ # incapable targets. A POSIX target with a run-as user other than root
226
+ # is refused as well, since the agents run everything as the Choria
227
+ # server's user.
226
228
  #
227
229
  # @param targets [Array<Bolt::Target>] Targets to prepare
228
230
  # @param agent_name [String] Required agent name (e.g. 'shell', 'bolt_tasks')
229
231
  # @param result_opts [Hash] Options passed through to emit_results (:action, :name, :position)
232
+ # @param run_as [String, nil] The user the call names, which takes precedence over each target's run-as option
230
233
  # @param callback [Proc] Called with :node_start and :node_result events
231
234
  # @return [Array] Two-element array:
232
- # - [Array<Bolt::Target>] Targets that have the required agent
233
- # - [Array<Bolt::Result>] Error results for targets that lack the agent
234
- def prepare_targets(targets, agent_name, result_opts, &callback)
235
+ # - [Array<Bolt::Target>] Targets that have the required agent and can run as the user
236
+ # - [Array<Bolt::Result>] Error results for the other targets
237
+ def prepare_targets(targets, agent_name, result_opts, run_as: nil, &callback)
235
238
  targets.each { |target| callback&.call(type: :node_start, target: target) }
236
239
  configure_client(targets.first)
237
240
  discovery_failures = discover_agents(targets)
@@ -245,7 +248,17 @@ module Bolt
245
248
  discovery_failures[target] || undiscovered_error(target)
246
249
  end
247
250
  end
248
- incapable_results = emit_results(agent_errors, **result_opts, &callback)
251
+
252
+ run_as_errors = capable.each_with_object({}) do |target, errors|
253
+ user = run_as || target.options['run-as']
254
+ next if user.nil? || user == 'root' || windows_target?(target)
255
+
256
+ errors[target] = error_output("Cannot run as user '#{user}' on #{target.safe_name}. The Choria transport runs everything " \
257
+ "as the Choria server's user and accepts only 'root' for run-as.",
258
+ 'puppetlabs.tasks/escalate-error')
259
+ end
260
+ capable -= run_as_errors.keys
261
+ incapable_results = emit_results(agent_errors.merge(run_as_errors), **result_opts, &callback)
249
262
 
250
263
  [capable, incapable_results]
251
264
  end
@@ -20,9 +20,9 @@ module Bolt
20
20
  }.freeze
21
21
 
22
22
  # Upload one file to the capable targets through the library.
23
- def batch_upload(targets, source, destination, _options = {}, position = [], &callback)
23
+ def batch_upload(targets, source, destination, options = {}, position = [], &callback)
24
24
  result_opts = { action: 'upload', name: source, position: position }
25
- capable, results = prepare_targets(targets, 'file_transfer', result_opts, &callback)
25
+ capable, results = prepare_targets(targets, 'file_transfer', result_opts, run_as: options[:run_as], &callback)
26
26
  return results if capable.empty?
27
27
 
28
28
  logger.debug { "Uploading #{source} to #{destination} on #{target_count(capable)}" }
@@ -35,9 +35,9 @@ module Bolt
35
35
  end
36
36
 
37
37
  # Download a path from each capable target into its own directory.
38
- def batch_download(targets, source, destination, _options = {}, position = [], &callback)
38
+ def batch_download(targets, source, destination, options = {}, position = [], &callback)
39
39
  directories = targets.to_h { |target| [target, download_dir(destination, target)] }
40
- download_into(directories, source, position, &callback)
40
+ download_into(directories, source, position, run_as: options[:run_as], &callback)
41
41
  end
42
42
 
43
43
  # Download a path from each capable target into the directory given
@@ -46,11 +46,12 @@ module Bolt
46
46
  # @param directories [Hash{Bolt::Target => String}] Each target and the local directory its download goes into
47
47
  # @param source [String] The path to download from each target
48
48
  # @param position [Array] Positional info for result tracking
49
+ # @param run_as [String, nil] The user the call names
49
50
  # @param callback [Proc] Called with :node_start and :node_result events
50
51
  # @return [Array<Bolt::Result>]
51
- def download_into(directories, source, position, &callback)
52
+ def download_into(directories, source, position, run_as:, &callback)
52
53
  result_opts = { action: 'download', name: source, position: position }
53
- capable, results = prepare_targets(directories.keys, 'file_transfer', result_opts, &callback)
54
+ capable, results = prepare_targets(directories.keys, 'file_transfer', result_opts, run_as: run_as, &callback)
54
55
  return results if capable.empty?
55
56
 
56
57
  logger.debug { "Downloading #{source} from #{target_count(capable)}" }
@@ -35,13 +35,13 @@ module Bolt
35
35
  #
36
36
  # @param targets [Array<Bolt::Target>] Targets in a single collective batch
37
37
  # @param command [String] Shell command to execute
38
- # @param options [Hash] Execution options - supports :env_vars for environment variables
38
+ # @param options [Hash] Execution options - supports :env_vars for environment variables and :run_as
39
39
  # @param position [Array] Positional info for result tracking
40
40
  # @param callback [Proc] Called with :node_start and :node_result events
41
41
  # @return [Array<Bolt::Result>] Results for all targets
42
42
  def batch_command(targets, command, options = {}, position = [], &callback)
43
43
  result_opts = { action: 'command', name: command, position: position }
44
- shell_targets, results = prepare_targets(targets, 'shell', result_opts, &callback)
44
+ shell_targets, results = prepare_targets(targets, 'shell', result_opts, run_as: options[:run_as], &callback)
45
45
  return results if shell_targets.empty?
46
46
 
47
47
  logger.debug { "Running command via shell agent on #{target_count(shell_targets)}" }
@@ -75,13 +75,13 @@ module Bolt
75
75
  # @param targets [Array<Bolt::Target>] Targets in a single collective batch
76
76
  # @param script [String] Local path to the script file
77
77
  # @param arguments [Array<String>] Command-line arguments to pass to the script
78
- # @param options [Hash] Execution options; supports :script_interpreter and :env_vars
78
+ # @param options [Hash] Execution options, supports :script_interpreter, :env_vars and :run_as
79
79
  # @param position [Array] Positional info for result tracking
80
80
  # @param callback [Proc] Called with :node_start and :node_result events
81
81
  # @return [Array<Bolt::Result>] Results for all targets
82
82
  def batch_script(targets, script, arguments, options = {}, position = [], &callback)
83
83
  result_opts = { action: 'script', name: script, position: position }
84
- shell_targets, results = prepare_targets(targets, 'shell', result_opts, &callback)
84
+ shell_targets, results = prepare_targets(targets, 'shell', result_opts, run_as: options[:run_as], &callback)
85
85
  return results if shell_targets.empty?
86
86
 
87
87
  logger.debug { "Running script via shell agent on #{target_count(shell_targets)}" }
@@ -120,17 +120,17 @@ module Bolt
120
120
  # @param targets [Array<Bolt::Target>] Targets in a single collective batch
121
121
  # @param task [Bolt::Task] Task to execute
122
122
  # @param arguments [Hash] Task parameter names to values
123
- # @param options [Hash] Execution options (unused currently, passed through from Base)
123
+ # @param options [Hash] Execution options, supports :run_as
124
124
  # @param position [Array] Positional info for result tracking
125
125
  # @param callback [Proc] Called with :node_start and :node_result events
126
126
  # @return [Array<Bolt::Result>] Results for all targets (successes and failures)
127
- def batch_task(targets, task, arguments, _options = {}, position = [], &callback)
127
+ def batch_task(targets, task, arguments, options = {}, position = [], &callback)
128
128
  chosen_agent = targets.first.options['task-agent'] || 'bolt_tasks'
129
129
  result_opts = { action: 'task', name: task.name, position: position }
130
130
 
131
131
  # The results var here is the error results for incapable targets, to which we'll add in
132
132
  # the successful results from the capable targets as we go.
133
- capable, results = prepare_targets(targets, chosen_agent, result_opts, &callback)
133
+ capable, results = prepare_targets(targets, chosen_agent, result_opts, run_as: options[:run_as], &callback)
134
134
 
135
135
  logger.debug { "Task #{task.name} routing: agent: #{chosen_agent}, #{capable.size} capable / #{targets.size - capable.size} incapable" }
136
136
 
@@ -239,8 +239,8 @@ module Bolt
239
239
 
240
240
  # Downloads into the destination itself. A batch gives each target a
241
241
  # directory of its own under the destination.
242
- def download(target, source, destination, _options = {})
243
- download_into({ target => destination }, source, []).first
242
+ def download(target, source, destination, options = {})
243
+ download_into({ target => destination }, source, [], run_as: options[:run_as]).first
244
244
  end
245
245
 
246
246
  def connected?(target)
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bolt
4
+ module Transport
5
+ class Orch < Base
6
+ class Connection
7
+ attr_reader :logger, :key
8
+
9
+ CONTEXT_KEYS = Set.new(%i[plan_name description params sensitive]).freeze
10
+
11
+ def self.get_key(opts)
12
+ [
13
+ opts['service-url'],
14
+ opts['task-environment'],
15
+ opts['token-file']
16
+ ].join('-')
17
+ end
18
+
19
+ def initialize(opts, plan_context, logger)
20
+ require 'addressable/uri'
21
+
22
+ @logger = logger
23
+ @key = self.class.get_key(opts)
24
+ client_opts = opts.slice('token-file', 'cacert', 'job-poll-interval', 'job-poll-timeout', 'read-timeout')
25
+
26
+ if opts['service-url']
27
+ uri = Addressable::URI.parse(opts['service-url'])
28
+ uri&.port ||= 8143
29
+ client_opts['service-url'] = uri.to_s
30
+ end
31
+
32
+ client_opts['User-Agent'] = "Bolt/#{VERSION}"
33
+
34
+ %w[token-file cacert].each do |f|
35
+ client_opts[f] = File.expand_path(client_opts[f]) if client_opts[f]
36
+ end
37
+ logger.debug("Creating orchestrator client for #{client_opts}")
38
+ @client = OrchestratorClient.new(client_opts, true)
39
+ @plan_context = plan_context
40
+ @plan_job = start_plan(@plan_context)
41
+ logger.debug("Started plan #{@plan_job}")
42
+ @environment = opts["task-environment"]
43
+ end
44
+
45
+ def start_plan(plan_context)
46
+ if plan_context
47
+ begin
48
+ opts = plan_context.slice(*CONTEXT_KEYS)
49
+ opts[:params] = opts[:params].except(*plan_context[:sensitive])
50
+ @client.command.plan_start(opts)['name']
51
+ rescue OrchestratorClient::ApiError => e
52
+ if e.code == '404'
53
+ @logger.debug("Orchestrator #{key} does not support plans")
54
+ else
55
+ @logger.error("Failed to start a plan with orchestrator #{key}: #{e.message}")
56
+ end
57
+ nil
58
+ end
59
+ end
60
+ end
61
+
62
+ def finish_plan(plan_result)
63
+ if @plan_job
64
+ @client.command.plan_finish(
65
+ plan_job: @plan_job,
66
+ result: plan_result.value || '',
67
+ status: plan_result.status
68
+ )
69
+ end
70
+ end
71
+
72
+ def get_certnames(targets)
73
+ targets.map { |t| t.host || t.name }
74
+ end
75
+
76
+ def build_request(targets, task, arguments, description = nil)
77
+ body = { task: task.name,
78
+ environment: @environment,
79
+ noop: arguments['_noop'],
80
+ params: arguments.reject { |k, _| k.start_with?('_') },
81
+ scope: {
82
+ nodes: get_certnames(targets)
83
+ } }
84
+ body[:description] = description if description
85
+ body[:plan_job] = @plan_job if @plan_job
86
+ body
87
+ end
88
+
89
+ def run_task(targets, task, arguments, options)
90
+ body = build_request(targets, task, arguments, options[:description])
91
+ @client.run_task(body)
92
+ rescue OrchestratorClient::ApiError => e
93
+ if e.data['kind'] == 'puppetlabs.orchestrator/plan-already-finished'
94
+ @logger.debug("Retrying the task")
95
+ # Instead of recursing, just retry once
96
+ @plan_job = start_plan(@plan_context)
97
+ # Rebuild the request with the new plan job ID
98
+ body = build_request(targets, task, arguments, options[:description])
99
+ @client.run_task(body)
100
+ else
101
+ raise e
102
+ end
103
+ end
104
+
105
+ def query_inventory(targets)
106
+ @client.post('inventory', nodes: get_certnames(targets))
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'find'
5
+ require 'json'
6
+ require 'pathname'
7
+ require_relative '../../bolt/transport/base'
8
+ require_relative 'orch/connection'
9
+
10
+ module Bolt
11
+ module Transport
12
+ class Orch < Base
13
+ BOLT_COMMAND_TASK = Struct.new(:name).new('bolt_shim::command').freeze
14
+ BOLT_SCRIPT_TASK = Struct.new(:name).new('bolt_shim::script').freeze
15
+ BOLT_UPLOAD_TASK = Struct.new(:name).new('bolt_shim::upload').freeze
16
+ POSITION_KEYS = %w[file line].freeze
17
+
18
+ attr_writer :plan_context
19
+
20
+ def provided_features
21
+ ['puppet-agent']
22
+ end
23
+
24
+ def initialize(*args)
25
+ load_orchestrator_client
26
+
27
+ @connections = {}
28
+ super
29
+ end
30
+
31
+ # The orchestrator_client gem is not a dependency of OpenBolt, so it's
32
+ # loaded the first time the pcp transport is used.
33
+ private def load_orchestrator_client
34
+ require 'orchestrator_client'
35
+ rescue LoadError => e
36
+ gem_command = File.join(RbConfig::CONFIG['bindir'], 'gem')
37
+ raise Bolt::Error.new(
38
+ "The pcp transport needs the orchestrator_client gem, which OpenBolt does not install. Install it with " \
39
+ "'#{gem_command} install --user-install orchestrator_client' (#{e.class}: #{e.message}).",
40
+ 'bolt/pcp-orchestrator-client-missing'
41
+ )
42
+ end
43
+
44
+ def finish_plan(result)
45
+ if result.is_a? Bolt::PlanResult
46
+ @connections.each_value do |conn|
47
+ conn.finish_plan(result)
48
+ rescue StandardError => e
49
+ @logger.trace("Failed to finish plan on #{conn.key}: #{e.message}")
50
+ end
51
+ end
52
+ end
53
+
54
+ # It's safe to create connections here for now because the
55
+ # batches/threads are per connection.
56
+ def get_connection(conn_opts)
57
+ key = Connection.get_key(conn_opts)
58
+ unless (conn = @connections[key])
59
+ conn = @connections[key] = Connection.new(conn_opts, @plan_context, logger)
60
+ end
61
+ conn
62
+ end
63
+
64
+ def process_run_results(targets, results, task_name, position = [])
65
+ targets_by_name = targets.map { |t| t.host || t.name }.zip(targets).to_h
66
+ results.map do |node_result|
67
+ target = targets_by_name[node_result['name']]
68
+ state = node_result['state']
69
+ result = node_result['result']
70
+
71
+ # If it's finished or already has a proper error simply pass it to the
72
+ # the result otherwise make sure an error is generated
73
+ if state == 'finished' || (result && result['_error'])
74
+ if result['_error']
75
+ unless result['_error'].is_a?(Hash)
76
+ result['_error'] = { 'kind' => 'puppetlabs.tasks/task-error',
77
+ 'issue_code' => 'TASK_ERROR',
78
+ 'msg' => result['_error'],
79
+ 'details' => {} }
80
+ end
81
+
82
+ result['_error']['details'] ||= {}
83
+ unless result['_error']['details'].is_a?(Hash)
84
+ deets = result['_error']['details']
85
+ result['_error']['details'] = { 'msg' => deets }
86
+ end
87
+ file_line = POSITION_KEYS.zip(position).to_h.compact
88
+ result['_error']['details'].merge!(file_line) unless result['_error']['details']['file']
89
+ end
90
+
91
+ Bolt::Result.new(target, value: result, action: 'task', object: task_name)
92
+ elsif state == 'skipped'
93
+ details = POSITION_KEYS.zip(position).to_h.compact
94
+ Bolt::Result.new(
95
+ target,
96
+ value: { '_error' => {
97
+ 'kind' => 'puppetlabs.tasks/skipped-node',
98
+ 'msg' => "Target #{target.safe_name} was skipped",
99
+ 'details' => details
100
+ } },
101
+ action: 'task', object: task_name
102
+ )
103
+ else
104
+ # Make a generic error with a unkown exit_code
105
+ Bolt::Result.for_task(target, result.to_json, '', 'unknown', task_name, position)
106
+ end
107
+ end
108
+ end
109
+
110
+ def batch_command(targets, command, options = {}, position = [], &callback)
111
+ if options[:env_vars] && !options[:env_vars].empty?
112
+ raise NotImplementedError, "pcp transport does not support setting environment variables"
113
+ end
114
+
115
+ params = {
116
+ 'command' => command
117
+ }
118
+ results = run_task_job(targets,
119
+ BOLT_COMMAND_TASK,
120
+ params,
121
+ options,
122
+ position,
123
+ &callback)
124
+ callback ||= proc {}
125
+ results.map! { |result| unwrap_bolt_result(result.target, result, 'command', command) }
126
+ results.each do |result|
127
+ callback.call(type: :node_result, result: result)
128
+ end
129
+ end
130
+
131
+ def batch_script(targets, script, arguments, options = {}, position = [], &callback)
132
+ if options[:env_vars] && !options[:env_vars].empty?
133
+ raise NotImplementedError, "pcp transport does not support setting environment variables"
134
+ end
135
+
136
+ content = File.read(script)
137
+ content = Base64.encode64(content)
138
+ params = {
139
+ 'content' => content,
140
+ 'arguments' => arguments,
141
+ 'name' => Pathname(script).basename.to_s
142
+ }
143
+ callback ||= proc {}
144
+ results = run_task_job(targets, BOLT_SCRIPT_TASK, params, options, position, &callback)
145
+ results.map! { |result| unwrap_bolt_result(result.target, result, 'script', script) }
146
+ results.each do |result|
147
+ callback.call(type: :node_result, result: result)
148
+ end
149
+ end
150
+
151
+ def pack(directory)
152
+ # lazy-load expensive gem code
153
+ require 'minitar'
154
+ require 'zlib'
155
+
156
+ start_time = Time.now
157
+ io = StringIO.new
158
+ output = Minitar::Output.new(Zlib::GzipWriter.new(io))
159
+ Find.find(directory) do |file|
160
+ next unless File.file?(file)
161
+
162
+ tar_path = Pathname.new(file).relative_path_from(Pathname.new(directory))
163
+ @logger.trace("Packing #{file} to #{tar_path}")
164
+ stat = File.stat(file)
165
+ content = File.binread(file)
166
+ output.tar.add_file_simple(
167
+ tar_path.to_s,
168
+ data: content,
169
+ size: content.size,
170
+ mode: stat.mode & 0o777,
171
+ mtime: stat.mtime
172
+ )
173
+ end
174
+
175
+ duration = Time.now - start_time
176
+ @logger.trace("Packed upload in #{duration * 1000} ms")
177
+
178
+ output.close
179
+ io.string
180
+ ensure
181
+ # Closes both tar and sgz.
182
+ output&.close
183
+ end
184
+
185
+ def batch_upload(targets, source, destination, options = {}, position = [], &callback)
186
+ stat = File.stat(source)
187
+ content = if stat.directory?
188
+ pack(source)
189
+ else
190
+ File.read(source)
191
+ end
192
+ content = Base64.encode64(content)
193
+ mode = File.stat(source).mode
194
+ params = {
195
+ 'path' => destination,
196
+ 'content' => content,
197
+ 'mode' => mode,
198
+ 'directory' => stat.directory?
199
+ }
200
+ callback ||= proc {}
201
+ results = run_task_job(targets, BOLT_UPLOAD_TASK, params, options, position, &callback)
202
+ results.map! do |result|
203
+ if result.error_hash
204
+ result
205
+ else
206
+ Bolt::Result.for_upload(result.target, source, destination)
207
+ end
208
+ end
209
+ results.each do |result|
210
+ callback&.call(type: :node_result, result: result)
211
+ end
212
+ end
213
+
214
+ def batch_download(targets, *_args)
215
+ error = {
216
+ 'kind' => 'bolt/not-supported-error',
217
+ 'msg' => 'pcp transport does not support downloading files',
218
+ 'details' => {}
219
+ }
220
+
221
+ targets.map do |target|
222
+ Bolt::Result.new(target, error: error, action: 'download')
223
+ end
224
+ end
225
+
226
+ def batches(targets)
227
+ targets.group_by { |target| Connection.get_key(target.options) }.values
228
+ end
229
+
230
+ def run_task_job(targets, task, arguments, options, position)
231
+ targets.each do |target|
232
+ yield(type: :node_start, target: target) if block_given?
233
+ end
234
+
235
+ begin
236
+ # unpack any Sensitive data
237
+ arguments = unwrap_sensitive_args(arguments)
238
+ results = get_connection(targets.first.options).run_task(targets, task, arguments, options)
239
+
240
+ process_run_results(targets, results, task.name, position)
241
+ rescue OrchestratorClient::ApiError => e
242
+ targets.map do |target|
243
+ Bolt::Result.new(target, error: e.data)
244
+ end
245
+ rescue StandardError => e
246
+ targets.map do |target|
247
+ Bolt::Result.from_exception(target, e, action: 'task')
248
+ end
249
+ end
250
+ end
251
+
252
+ def batch_task(targets, task, arguments, options = {}, position = [], &callback)
253
+ callback ||= proc {}
254
+ results = run_task_job(targets, task, arguments, options, position, &callback)
255
+ results.each do |result|
256
+ callback.call(type: :node_result, result: result)
257
+ end
258
+ end
259
+
260
+ def batch_task_with(_targets, _task, _target_mapping, _options = {}, _position = [])
261
+ raise NotImplementedError, "pcp transport does not support run_task_with()"
262
+ end
263
+
264
+ def batch_connected?(targets)
265
+ resp = get_connection(targets.first.options).query_inventory(targets)
266
+ resp['items'].all? { |node| node['connected'] }
267
+ end
268
+
269
+ # run_task generates a result that makes sense for a generic task which
270
+ # needs to be unwrapped to extract stdout/stderr/exitcode.
271
+ #
272
+ def unwrap_bolt_result(target, result, action, obj)
273
+ if result.error_hash
274
+ # something went wrong return the failure
275
+ return result
276
+ end
277
+
278
+ # If we get here, there's no error so we don't need the file or line
279
+ # number
280
+ Bolt::Result.for_command(target, result.value, action, obj, [])
281
+ end
282
+ end
283
+ end
284
+ end
data/lib/bolt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bolt
4
- VERSION = '9.0.0'
4
+ VERSION = '9.1.0'
5
5
  end
@@ -335,6 +335,8 @@ module BoltSpec
335
335
 
336
336
  def batch_execute(_targets); end
337
337
 
338
+ def finish_plan(_plan_result); end
339
+
338
340
  def handle_event(_event); end
339
341
 
340
342
  def prompt(_prompt, _options); end
@@ -353,6 +355,8 @@ module BoltSpec
353
355
 
354
356
  def shutdown; end
355
357
 
358
+ def start_plan(_plan_context); end
359
+
356
360
  def subscribe(_subscriber, _types = nil); end
357
361
 
358
362
  def unsubscribe(_subscriber, _types = nil); end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openbolt
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.0
4
+ version: 9.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenVox Project
@@ -590,6 +590,7 @@ files:
590
590
  - lib/bolt/config/transport/local.rb
591
591
  - lib/bolt/config/transport/lxd.rb
592
592
  - lib/bolt/config/transport/options.rb
593
+ - lib/bolt/config/transport/orch.rb
593
594
  - lib/bolt/config/transport/podman.rb
594
595
  - lib/bolt/config/transport/remote.rb
595
596
  - lib/bolt/config/transport/ssh.rb
@@ -696,6 +697,8 @@ files:
696
697
  - lib/bolt/transport/local/connection.rb
697
698
  - lib/bolt/transport/lxd.rb
698
699
  - lib/bolt/transport/lxd/connection.rb
700
+ - lib/bolt/transport/orch.rb
701
+ - lib/bolt/transport/orch/connection.rb
699
702
  - lib/bolt/transport/podman.rb
700
703
  - lib/bolt/transport/podman/connection.rb
701
704
  - lib/bolt/transport/remote.rb