Class: Bolt::Transport::SSH
- Inherits:
-
Base
- Object
- Base
- Bolt::Transport::SSH
show all
- Defined in:
- lib/bolt/transport/ssh.rb,
lib/bolt/transport/ssh/connection.rb
Defined Under Namespace
Classes: Connection
Constant Summary
Constants inherited
from Base
Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS
Instance Attribute Summary
Attributes inherited from Base
#logger
Class Method Summary
collapse
Instance Method Summary
collapse
-
#connected?(target) ⇒ Boolean
-
#initialize ⇒ SSH
constructor
-
#make_wrapper_stringio(task_path, stdin) ⇒ Object
-
#provided_features ⇒ Object
-
#run_command(target, command, options = {}) ⇒ Object
-
#run_script(target, script, arguments, options = {}) ⇒ Object
-
#run_task(target, task, arguments, options = {}) ⇒ Object
-
#upload(target, source, destination, options = {}) ⇒ Object
-
#with_connection(target, load_config = true) ⇒ Object
Methods inherited from Base
#assert_batch_size_one, #batch_command, #batch_connected?, #batch_script, #batch_task, #batch_upload, #batches, #default_input_method, #envify_params, filter_options, #select_implementation, #unwrap_sensitive_args, #with_events
Constructor Details
#initialize ⇒ SSH
Returns a new instance of SSH.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/bolt/transport/ssh.rb', line 52
def initialize
super
require 'net/ssh'
require 'net/scp'
begin
require 'net/ssh/krb'
rescue LoadError
logger.debug("Authentication method 'gssapi-with-mic' (Kerberos) is not available.")
end
@transport_logger = Logging.logger[Net::SSH]
@transport_logger.level = :warn
end
|
Class Method Details
.options ⇒ Object
11
12
13
14
|
# File 'lib/bolt/transport/ssh.rb', line 11
def self.options
%w[port user password sudo-password private-key host-key-check
connect-timeout tmpdir run-as tty run-as-command proxyjump]
end
|
.validate(options) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/bolt/transport/ssh.rb', line 20
def self.validate(options)
logger = Logging.logger[self]
if options['sudo-password'] && options['run-as'].nil?
logger.warn("--sudo-password will not be used without specifying a " \
"user to escalate to with --run-as")
end
host_key = options['host-key-check']
unless !!host_key == host_key
raise Bolt::ValidationError, 'host-key-check option must be a Boolean true or false'
end
if (key_opt = options['private-key'])
unless key_opt.instance_of?(String) || (key_opt.instance_of?(Hash) && key_opt.include?('key-data'))
raise Bolt::ValidationError,
"private-key option must be the path to a private key file or a hash containing the 'key-data'"
end
end
timeout_value = options['connect-timeout']
unless timeout_value.is_a?(Integer) || timeout_value.nil?
error_msg = "connect-timeout value must be an Integer, received #{timeout_value}:#{timeout_value.class}"
raise Bolt::ValidationError, error_msg
end
run_as_cmd = options['run-as-command']
if run_as_cmd && (!run_as_cmd.is_a?(Array) || run_as_cmd.any? { |n| !n.is_a?(String) })
raise Bolt::ValidationError, "run-as-command must be an Array of Strings, received #{run_as_cmd}"
end
end
|
Instance Method Details
#connected?(target) ⇒ Boolean
190
191
192
193
194
|
# File 'lib/bolt/transport/ssh.rb', line 190
def connected?(target)
with_connection(target) { true }
rescue Bolt::Node::ConnectError
false
end
|
#make_wrapper_stringio(task_path, stdin) ⇒ Object
181
182
183
184
185
186
187
188
|
# File 'lib/bolt/transport/ssh.rb', line 181
def make_wrapper_stringio(task_path, stdin)
StringIO.new(<<-SCRIPT)
#!/bin/sh
'#{task_path}' <<'EOF'
#{stdin}
EOF
SCRIPT
end
|
#provided_features ⇒ Object
16
17
18
|
# File 'lib/bolt/transport/ssh.rb', line 16
def provided_features
['shell']
end
|
#run_command(target, command, options = {}) ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/bolt/transport/ssh.rb', line 99
def run_command(target, command, options = {})
with_connection(target) do |conn|
conn.running_as(options['_run_as']) do
output = conn.execute(command, sudoable: true)
Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
end
end
end
|
#run_script(target, script, arguments, options = {}) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/bolt/transport/ssh.rb', line 108
def run_script(target, script, arguments, options = {})
arguments = unwrap_sensitive_args(arguments)
with_connection(target) do |conn|
conn.running_as(options['_run_as']) do
conn.with_remote_tempdir do |dir|
remote_path = conn.write_remote_executable(dir, script)
dir.chown(conn.run_as)
output = conn.execute([remote_path, *arguments], sudoable: true)
Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
end
end
end
end
|
#run_task(target, task, arguments, options = {}) ⇒ Object
124
125
126
127
128
129
130
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/bolt/transport/ssh.rb', line 124
def run_task(target, task, arguments, options = {})
implementation = select_implementation(target, task)
executable = implementation['path']
input_method = implementation['input_method']
= implementation['files']
arguments = unwrap_sensitive_args(arguments)
with_connection(target, options.fetch('_load_config', true)) do |conn|
conn.running_as(options['_run_as']) do
stdin, output = nil
command = []
execute_options = {}
conn.with_remote_tempdir do |dir|
if .empty?
task_dir = dir
else
arguments['_installdir'] = dir.to_s
task_dir = File.join(dir.to_s, task.tasks_dir)
dir.mkdirs([task.tasks_dir] + .map { |file| File.dirname(file['name']) })
.each do |file|
conn.write_remote_file(file['path'], File.join(dir.to_s, file['name']))
end
end
remote_task_path = conn.write_remote_executable(task_dir, executable)
if STDIN_METHODS.include?(input_method)
stdin = JSON.dump(arguments)
end
if ENVIRONMENT_METHODS.include?(input_method)
execute_options[:environment] = envify_params(arguments)
end
if conn.run_as && stdin
wrapper = make_wrapper_stringio(remote_task_path, stdin)
remote_wrapper_path = conn.write_remote_executable(dir, wrapper, 'wrapper.sh')
command << remote_wrapper_path
else
command << remote_task_path
execute_options[:stdin] = stdin
end
dir.chown(conn.run_as)
execute_options[:sudoable] = true if conn.run_as
output = conn.execute(command, execute_options)
end
Bolt::Result.for_task(target, output.stdout.string,
output.stderr.string,
output.exit_code)
end
end
end
|
#upload(target, source, destination, options = {}) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/bolt/transport/ssh.rb', line 79
def upload(target, source, destination, options = {})
with_connection(target) do |conn|
conn.running_as(options['_run_as']) do
conn.with_remote_tempdir do |dir|
basename = File.basename(destination)
tmpfile = "#{dir}/#{basename}"
conn.write_remote_file(source, tmpfile)
dir.chown(conn.run_as)
result = conn.execute(['mv', tmpfile, destination], sudoable: true)
if result.exit_code != 0
message = "Could not move temporary file '#{tmpfile}' to #{destination}: #{result.stderr.string}"
raise Bolt::Node::FileError.new(message, 'MV_ERROR')
end
end
Bolt::Result.for_upload(target, source, destination)
end
end
end
|
#with_connection(target, load_config = true) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/bolt/transport/ssh.rb', line 67
def with_connection(target, load_config = true)
conn = Connection.new(target, @transport_logger, load_config)
conn.connect
yield conn
ensure
begin
conn&.disconnect
rescue StandardError => ex
logger.info("Failed to close connection to #{target.uri} : #{ex.message}")
end
end
|