Class: Bolt::Transport::Orch
- Inherits:
-
Base
- Object
- Base
- Bolt::Transport::Orch
show all
- Defined in:
- lib/bolt/transport/orch.rb,
lib/bolt/transport/orch/connection.rb
Defined Under Namespace
Classes: Connection
Constant Summary
collapse
- CONF_FILE =
if !ENV['HOME'].nil?
File.expand_path('~/.puppetlabs/client-tools/orchestrator.conf')
else
'/etc/puppetlabs/client-tools/orchestrator.conf'
end
- BOLT_COMMAND_TASK =
Struct.new(:name).new('bolt_shim::command').freeze
- BOLT_SCRIPT_TASK =
Struct.new(:name).new('bolt_shim::script').freeze
- BOLT_UPLOAD_TASK =
Struct.new(:name).new('bolt_shim::upload').freeze
Instance Attribute Summary collapse
Attributes inherited from Base
#logger
Instance Method Summary
collapse
-
#batch_command(targets, command, options = {}, position = [], &callback) ⇒ Object
-
#batch_connected?(targets) ⇒ Boolean
-
#batch_download(targets, *_args) ⇒ Object
-
#batch_script(targets, script, arguments, options = {}, position = [], &callback) ⇒ Object
-
#batch_task(targets, task, arguments, options = {}, position = [], &callback) ⇒ Object
-
#batch_task_with(_targets, _task, _target_mapping, _options = {}, _position = []) ⇒ Object
-
#batch_upload(targets, source, destination, options = {}, position = [], &callback) ⇒ Object
-
#batches(targets) ⇒ Object
-
#finish_plan(result) ⇒ Object
-
#get_connection(conn_opts) ⇒ Object
It’s safe to create connections here for now because the batches/threads are per connection.
-
#initialize(*args) ⇒ Orch
constructor
-
#pack(directory) ⇒ Object
-
#process_run_results(targets, results, task_name, position = []) ⇒ Object
-
#provided_features ⇒ Object
-
#run_task_job(targets, task, arguments, options, position) ⇒ Object
-
#unwrap_bolt_result(target, result, action, obj) ⇒ Object
run_task generates a result that makes sense for a generic task which needs to be unwrapped to extract stdout/stderr/exitcode.
Methods inherited from Base
#assert_batch_size_one, #connected?, #default_input_method, #download, #envify_params, #run_command, #run_script, #run_task, #select_implementation, #select_interpreter, #unwrap_sensitive_args, #upload, #with_events
Constructor Details
#initialize(*args) ⇒ Orch
Returns a new instance of Orch.
28
29
30
31
32
33
34
|
# File 'lib/bolt/transport/orch.rb', line 28
def initialize(*args)
require 'orchestrator_client'
@connections = {}
super
end
|
Instance Attribute Details
#plan_context=(value) ⇒ Object
Sets the attribute plan_context
22
23
24
|
# File 'lib/bolt/transport/orch.rb', line 22
def plan_context=(value)
@plan_context = value
end
|
Instance Method Details
#batch_command(targets, command, options = {}, position = [], &callback) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/bolt/transport/orch.rb', line 90
def batch_command(targets, command, options = {}, position = [], &callback)
if options[:env_vars] && !options[:env_vars].empty?
raise NotImplementedError, "pcp transport does not support setting environment variables"
end
params = {
'command' => command
}
results = run_task_job(targets,
BOLT_COMMAND_TASK,
params,
options,
position,
&callback)
callback ||= proc {}
results.map! { |result| unwrap_bolt_result(result.target, result, 'command', command) }
results.each do |result|
callback.call(type: :node_result, result: result)
end
end
|
#batch_connected?(targets) ⇒ Boolean
244
245
246
247
|
# File 'lib/bolt/transport/orch.rb', line 244
def batch_connected?(targets)
resp = get_connection(targets.first.options).query_inventory(targets)
resp['items'].all? { |node| node['connected'] }
end
|
#batch_download(targets, *_args) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/bolt/transport/orch.rb', line 194
def batch_download(targets, *_args)
error = {
'kind' => 'bolt/not-supported-error',
'msg' => 'pcp transport does not support downloading files',
'details' => {}
}
targets.map do |target|
Bolt::Result.new(target, error: error, action: 'download')
end
end
|
#batch_script(targets, script, arguments, options = {}, position = [], &callback) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/bolt/transport/orch.rb', line 111
def batch_script(targets, script, arguments, options = {}, position = [], &callback)
if options[:env_vars] && !options[:env_vars].empty?
raise NotImplementedError, "pcp transport does not support setting environment variables"
end
content = File.open(script, &:read)
content = Base64.encode64(content)
params = {
'content' => content,
'arguments' => arguments,
'name' => Pathname(script).basename.to_s
}
callback ||= proc {}
results = run_task_job(targets, BOLT_SCRIPT_TASK, params, options, position, &callback)
results.map! { |result| unwrap_bolt_result(result.target, result, 'script', script) }
results.each do |result|
callback.call(type: :node_result, result: result)
end
end
|
#batch_task(targets, task, arguments, options = {}, position = [], &callback) ⇒ Object
232
233
234
235
236
237
238
|
# File 'lib/bolt/transport/orch.rb', line 232
def batch_task(targets, task, arguments, options = {}, position = [], &callback)
callback ||= proc {}
results = run_task_job(targets, task, arguments, options, position, &callback)
results.each do |result|
callback.call(type: :node_result, result: result)
end
end
|
#batch_task_with(_targets, _task, _target_mapping, _options = {}, _position = []) ⇒ Object
240
241
242
|
# File 'lib/bolt/transport/orch.rb', line 240
def batch_task_with(_targets, _task, _target_mapping, _options = {}, _position = [])
raise NotImplementedError, "pcp transport does not support run_task_with()"
end
|
#batch_upload(targets, source, destination, options = {}, position = [], &callback) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/bolt/transport/orch.rb', line 165
def batch_upload(targets, source, destination, options = {}, position = [], &callback)
stat = File.stat(source)
content = if stat.directory?
pack(source)
else
File.open(source, &:read)
end
content = Base64.encode64(content)
mode = File.stat(source).mode
params = {
'path' => destination,
'content' => content,
'mode' => mode,
'directory' => stat.directory?
}
callback ||= proc {}
results = run_task_job(targets, BOLT_UPLOAD_TASK, params, options, position, &callback)
results.map! do |result|
if result.error_hash
result
else
Bolt::Result.for_upload(result.target, source, destination)
end
end
results.each do |result|
callback&.call(type: :node_result, result: result)
end
end
|
#batches(targets) ⇒ Object
206
207
208
|
# File 'lib/bolt/transport/orch.rb', line 206
def batches(targets)
targets.group_by { |target| Connection.get_key(target.options) }.values
end
|
#finish_plan(result) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'lib/bolt/transport/orch.rb', line 36
def finish_plan(result)
if result.is_a? Bolt::PlanResult
@connections.each_value do |conn|
conn.finish_plan(result)
rescue StandardError => e
@logger.trace("Failed to finish plan on #{conn.key}: #{e.message}")
end
end
end
|
#get_connection(conn_opts) ⇒ Object
It’s safe to create connections here for now because the batches/threads are per connection.
48
49
50
51
52
53
54
|
# File 'lib/bolt/transport/orch.rb', line 48
def get_connection(conn_opts)
key = Connection.get_key(conn_opts)
unless (conn = @connections[key])
conn = @connections[key] = Connection.new(conn_opts, @plan_context, logger)
end
conn
end
|
#pack(directory) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/bolt/transport/orch.rb', line 131
def pack(directory)
require 'minitar'
require 'zlib'
start_time = Time.now
io = StringIO.new
output = Minitar::Output.new(Zlib::GzipWriter.new(io))
Find.find(directory) do |file|
next unless File.file?(file)
tar_path = Pathname.new(file).relative_path_from(Pathname.new(directory))
@logger.trace("Packing #{file} to #{tar_path}")
stat = File.stat(file)
content = File.binread(file)
output.tar.add_file_simple(
tar_path.to_s,
data: content,
size: content.size,
mode: stat.mode & 0o777,
mtime: stat.mtime
)
end
duration = Time.now - start_time
@logger.trace("Packed upload in #{duration * 1000} ms")
output.close
io.string
ensure
output&.close
end
|
#process_run_results(targets, results, task_name, position = []) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/bolt/transport/orch.rb', line 56
def process_run_results(targets, results, task_name, position = [])
targets_by_name = Hash[targets.map { |t| t.host || t.name }.zip(targets)]
results.map do |node_result|
target = targets_by_name[node_result['name']]
state = node_result['state']
result = node_result['result']
if state == 'finished' || (result && result['_error'])
if result['_error']
file_line = %w[file line].zip(position).to_h.compact
result['_error']['details'].merge!(file_line) unless result['_error']['details']['file']
end
Bolt::Result.new(target, value: result, action: 'task', object: task_name)
elsif state == 'skipped'
details = %w[file line].zip(position).to_h.compact
Bolt::Result.new(
target,
value: { '_error' => {
'kind' => 'puppetlabs.tasks/skipped-node',
'msg' => "Target #{target.safe_name} was skipped",
'details' => details
} },
action: 'task', object: task_name
)
else
Bolt::Result.for_task(target, result.to_json, '', 'unknown', task_name, position)
end
end
end
|
#provided_features ⇒ Object
24
25
26
|
# File 'lib/bolt/transport/orch.rb', line 24
def provided_features
['puppet-agent']
end
|
#run_task_job(targets, task, arguments, options, position) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/bolt/transport/orch.rb', line 210
def run_task_job(targets, task, arguments, options, position)
targets.each do |target|
yield(type: :node_start, target: target) if block_given?
end
begin
arguments = unwrap_sensitive_args(arguments)
results = get_connection(targets.first.options).run_task(targets, task, arguments, options)
process_run_results(targets, results, task.name, position)
rescue OrchestratorClient::ApiError => e
targets.map do |target|
Bolt::Result.new(target, error: e.data)
end
rescue StandardError => e
targets.map do |target|
Bolt::Result.from_exception(target, e, action: 'task')
end
end
end
|
#unwrap_bolt_result(target, result, action, obj) ⇒ Object
run_task generates a result that makes sense for a generic task which needs to be unwrapped to extract stdout/stderr/exitcode.
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/bolt/transport/orch.rb', line 252
def unwrap_bolt_result(target, result, action, obj)
if result.error_hash
return result
end
Bolt::Result.for_command(target,
result.value['stdout'],
result.value['stderr'],
result.value['exit_code'],
action, obj, [])
end
|