Class: Chef::Provisioning::Transport::SSH
- Inherits:
-
Chef::Provisioning::Transport
- Object
- Chef::Provisioning::Transport
- Chef::Provisioning::Transport::SSH
- Defined in:
- lib/chef/provisioning/transport/ssh.rb
Defined Under Namespace
Classes: InitialConnectTimeout, SSHResult
Constant Summary
Constants inherited from Chef::Provisioning::Transport
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#ssh_options ⇒ Object
readonly
Returns the value of attribute ssh_options.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #disconnect ⇒ Object
- #download_file(path, local_path) ⇒ Object
- #execute(command, execute_options = {}) ⇒ Object
-
#initialize(host, username, init_ssh_options, options, global_config) ⇒ SSH
constructor
Create a new SSH transport.
- #make_url_available_to_remote(local_url) ⇒ Object
-
#read_file(path) ⇒ Object
TODO why does #read_file download it to the target host?.
- #remote_tempfile(path) ⇒ Object
- #upload_file(local_path, path) ⇒ Object
- #write_file(path, content) ⇒ Object
Constructor Details
#initialize(host, username, init_ssh_options, options, global_config) ⇒ SSH
Create a new SSH transport.
Arguments
-
host: the host to connect to, e.g. ‘145.14.51.45’
-
username: the username to connect with
-
ssh_options: a list of options to Net::SSH.start
-
options: a hash of options for the transport itself, including:
-
:prefix: a prefix to send before each command (e.g. “sudo ”)
-
:ssh_pty_enable: set to false to disable pty (some instances don’t support this, most do)
-
:ssh_gateway: the gateway to use, e.g. “[email protected]:222”. nil (the default) means no gateway. If the username is omitted, then the default username is used instead (i.e. the user running chef, or the username configured in .ssh/config).
-
:scp_temp_dir: a directory to use as the temporary location for files that are copied to the host via SCP. Only used if :prefix is set. Default is ‘/tmp’ if unspecified.
-
-
global_config: an options hash that looks suspiciously similar to Chef::Config, containing at least the key :log_level.
The options are used in
Net::SSH.start(host, username, )
39 40 41 42 43 44 45 46 47 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 39 def initialize(host, username, , , global_config) @host = host @username = username @ssh_options = .clone @options = @config = global_config @remote_forwards = .delete(:remote_forwards) { Array.new } @never_forward_localhost = .delete(:never_forward_localhost) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
53 54 55 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 53 def config @config end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
49 50 51 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 49 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
52 53 54 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 52 def @options end |
#ssh_options ⇒ Object (readonly)
Returns the value of attribute ssh_options.
51 52 53 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 51 def @ssh_options end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
50 51 52 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 50 def username @username end |
Instance Method Details
#available? ⇒ Boolean
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 213 def available? timeout = [:timeout] || 10 execute('pwd', :timeout => timeout) true rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::EHOSTDOWN, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Net::SSH::Disconnect, Net::SSH::ConnectionTimeout Chef::Log.debug("#{username}@#{host} unavailable: network connection failed or broke: #{$!.inspect}") disconnect false rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch Chef::Log.debug("#{username}@#{host} unavailable: SSH authentication error: #{$!.inspect} ") disconnect false end |
#disconnect ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 201 def disconnect if @session begin Chef::Log.info("Closing SSH session on #{username}@#{host}") @session.close rescue ensure @session = nil end end end |
#download_file(path, local_path) ⇒ Object
133 134 135 136 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 133 def download_file(path, local_path) Chef::Log.debug("Downloading file #{path} from #{username}@#{host} to local #{local_path}") download(path, local_path) end |
#execute(command, execute_options = {}) ⇒ Object
55 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 55 def execute(command, = {}) Chef::Log.info("#{self.object_id} Executing #{[:prefix]}#{command} on #{username}@#{host}") stdout = '' stderr = '' exitstatus = nil session # grab session outside timeout, it has its own timeout with_execute_timeout() do @remote_forwards.each do |forward_info| # -R flag to openssh client allows optional :remote_host and # requires the other values so let's do that too. remote_host = forward_info.fetch(:remote_host, 'localhost') remote_port = forward_info.fetch(:remote_port) local_host = forward_info.fetch(:local_host) local_port = forward_info.fetch(:local_port) actual_port, actual_host = forward_port(local_port, local_host, remote_port, remote_host) Chef::Log.info("#{host} forwarded remote #{actual_host}:#{actual_port} to local #{local_host}:#{local_port}") end channel = session.open_channel do |channel| # Enable PTY unless otherwise specified, some instances require this unless [:ssh_pty_enable] == false channel.request_pty do |chan, success| raise "could not get pty" if !success && [:ssh_pty_enable] end end channel.exec("#{[:prefix]}#{command}") do |ch, success| raise "could not execute command: #{command.inspect}" unless success channel.on_data do |ch2, data| stdout << data stream_chunk(, data, nil) end channel.on_extended_data do |ch2, type, data| stderr << data stream_chunk(, nil, data) end channel.on_request "exit-status" do |ch, data| exitstatus = data.read_long end end end channel.wait @remote_forwards.each do |forward_info| # -R flag to openssh client allows optional :remote_host and # requires the other values so let's do that too. remote_host = forward_info.fetch(:remote_host, 'localhost') remote_port = forward_info.fetch(:remote_port) local_host = forward_info.fetch(:local_host) local_port = forward_info.fetch(:local_port) session.forward.cancel_remote(remote_port, remote_host) session.loop { session.forward.active_remotes.include?([remote_port, remote_host]) } Chef::Log.info("#{host} canceled remote forward #{remote_host}:#{remote_port}") end end Chef::Log.info("Completed #{command} on #{username}@#{host}: exit status #{exitstatus}") Chef::Log.debug("Stdout was:\n#{stdout}") if stdout != '' && ![:stream] && ![:stream_stdout] && config[:log_level] != :debug Chef::Log.info("Stderr was:\n#{stderr}") if stderr != '' && ![:stream] && ![:stream_stderr] && config[:log_level] != :debug SSHResult.new(command, , stdout, stderr, exitstatus) end |
#make_url_available_to_remote(local_url) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 175 def make_url_available_to_remote(local_url) uri = URI(local_url) if @never_forward_localhost return uri.to_s elsif uri.scheme == 'chefzero' && !ChefZero::SocketlessServerMap.server_on_port(uri.port).server # There is no .server for a socketless, for a socket-d server it would # be a WEBrick::HTTPServer object. raise 'Cannot forward a socketless Chef Zero server, see https://docs.chef.io/deprecations_local_listen.html for more information' elsif is_local_machine(uri.host) port, host = forward_port(uri.port, uri.host, uri.port, 'localhost') if !port # Try harder if the port is already taken port, host = forward_port(uri.port, uri.host, 0, 'localhost') if !port raise "Error forwarding port: could not forward #{uri.port} or 0" end end uri.host = host uri.port = port Chef::Log.info("Port forwarded: local URL #{local_url} is available to #{self.host} as #{uri.to_s} for the duration of this SSH connection.") else Chef::Log.info("#{host} not forwarding non-local #{local_url}") end uri.to_s end |
#read_file(path) ⇒ Object
TODO why does #read_file download it to the target host?
126 127 128 129 130 131 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 126 def read_file(path) Chef::Log.debug("Reading file #{path} from #{username}@#{host}") result = StringIO.new download(path, result) result.string end |
#remote_tempfile(path) ⇒ Object
138 139 140 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 138 def remote_tempfile(path) File.join(scp_temp_dir, "#{File.basename(path)}.#{Random.rand(2**32)}") end |
#upload_file(local_path, path) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 156 def upload_file(local_path, path) execute("mkdir -p #{File.dirname(path)}").error! if [:prefix] # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc. tempfile = remote_tempfile(path) Chef::Log.debug("Uploading #{local_path} to #{tempfile} on #{username}@#{host}") Net::SCP.new(session).upload!(local_path, tempfile) begin execute("mv #{tempfile} #{path}").error! rescue # Clean up if we were unable to move execute("rm #{tempfile}").error! end else Chef::Log.debug("Uploading #{local_path} to #{path} on #{username}@#{host}") Net::SCP.new(session).upload!(local_path, path) end end |
#write_file(path, content) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chef/provisioning/transport/ssh.rb', line 142 def write_file(path, content) execute("mkdir -p #{File.dirname(path)}").error! if [:prefix] # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc. tempfile = remote_tempfile(path) Chef::Log.debug("Writing #{content.length} bytes to #{tempfile} on #{username}@#{host}") Net::SCP.new(session).upload!(StringIO.new(content), tempfile) execute("mv #{tempfile} #{path}").error! else Chef::Log.debug("Writing #{content.length} bytes to #{path} on #{username}@#{host}") Net::SCP.new(session).upload!(StringIO.new(content), path) end end |