Class: Train::Transports::SSH::Connection
- Inherits:
-
BaseConnection
- Object
- BaseConnection
- Train::Transports::SSH::Connection
- Defined in:
- lib/train/transports/ssh_connection.rb
Overview
A Connection instance can be generated and re-generated, given new connection details such as connection port, hostname, credentials, etc. This object is responsible for carrying out the actions on the remote host such as executing commands, transferring files, etc.
Defined Under Namespace
Classes: OS
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
- #close ⇒ Object
- #download(remotes, local) ⇒ Object
- #file(path) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #login_command ⇒ Object
- #os ⇒ Object
- #run_command(cmd) ⇒ Object
- #upload(locals, remote) ⇒ Object
- #uri ⇒ Object
- #wait_until_ready ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/train/transports/ssh_connection.rb', line 34 def initialize() super() @username = .delete(:username) @hostname = .delete(:hostname) @port = [:port] # don't delete from options @connection_retries = .delete(:connection_retries) @connection_retry_sleep = .delete(:connection_retry_sleep) @max_wait_until_ready = .delete(:max_wait_until_ready) @max_ssh_sessions = .delete(:max_ssh_connections) { 9 } @session = nil = .delete(:transport_options) @cmd_wrapper = nil @cmd_wrapper = CommandWrapper.load(self, ) end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
33 34 35 |
# File 'lib/train/transports/ssh_connection.rb', line 33 def hostname @hostname end |
Instance Method Details
#close ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/train/transports/ssh_connection.rb', line 50 def close return if @session.nil? logger.debug("[SSH] closing connection to #{self}") session.close ensure @session = nil end |
#download(remotes, local) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/train/transports/ssh_connection.rb', line 154 def download(remotes, local) waits = [] Array(remotes).map do |remote| opts = file(remote).directory? ? { recursive: true } : {} waits.push session.scp.download(remote, local, opts) do |_ch, name, recv, total| logger.debug("Downloaded #{name} (#{total} bytes)") if recv == total end waits.shift.wait while waits.length >= @max_ssh_sessions end waits.each(&:wait) rescue Net::SSH::Exception => ex raise Train::Transports::SSHFailed, "SCP download failed (#{ex.message})" end |
#file(path) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/train/transports/ssh_connection.rb', line 62 def file(path) @files[path] ||= \ if os.aix? Train::File::Remote::Aix.new(self, path) elsif os.solaris? Train::File::Remote::Unix.new(self, path) elsif os[:name] == 'qnx' Train::File::Remote::Qnx.new(self, path) else Train::File::Remote::Linux.new(self, path) end end |
#login_command ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/train/transports/ssh_connection.rb', line 120 def login_command level = logger.debug? ? 'VERBOSE' : 'ERROR' fwd_agent = [:forward_agent] ? 'yes' : 'no' args = %w{ -o UserKnownHostsFile=/dev/null } args += %w{ -o StrictHostKeyChecking=no } args += %w{ -o IdentitiesOnly=yes } if [:keys] args += %W( -o LogLevel=#{level} ) args += %W( -o ForwardAgent=#{fwd_agent} ) if .key?(:forward_agent) Array([:keys]).each do |ssh_key| args += %W( -i #{ssh_key} ) end args += %W( -p #{@port} ) args += %W( #{@username}@#{@hostname} ) LoginCommand.new('ssh', args) end |
#os ⇒ Object
58 59 60 |
# File 'lib/train/transports/ssh_connection.rb', line 58 def os @os ||= OS.new(self) end |
#run_command(cmd) ⇒ Object
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 |
# File 'lib/train/transports/ssh_connection.rb', line 76 def run_command(cmd) stdout = stderr = '' exit_status = nil cmd.dup.force_encoding('binary') if cmd.respond_to?(:force_encoding) logger.debug("[SSH] #{self} (#{cmd})") session.open_channel do |channel| # wrap commands if that is configured cmd = @cmd_wrapper.run(cmd) unless @cmd_wrapper.nil? if [:pty] channel.request_pty do |_ch, success| fail Train::Transports::SSHPTYFailed, 'Requesting PTY failed' unless success end end channel.exec(cmd) do |_, success| abort 'Couldn\'t execute command on SSH.' unless success channel.on_data do |_, data| stdout += data end channel.on_extended_data do |_, _type, data| stderr += data end channel.on_request('exit-status') do |_, data| exit_status = data.read_long end channel.on_request('exit-signal') do |_, data| exit_status = data.read_long end end end @session.loop CommandResult.new(stdout, stderr, exit_status) rescue Net::SSH::Exception => ex raise Train::Transports::SSHFailed, "SSH command failed (#{ex.message})" end |
#upload(locals, remote) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/train/transports/ssh_connection.rb', line 139 def upload(locals, remote) waits = [] Array(locals).each do |local| opts = File.directory?(local) ? { recursive: true } : {} waits.push session.scp.upload(local, remote, opts) do |_ch, name, sent, total| logger.debug("Uploaded #{name} (#{total} bytes)") if sent == total end waits.shift.wait while waits.length >= @max_ssh_sessions end waits.each(&:wait) rescue Net::SSH::Exception => ex raise Train::Transports::SSHFailed, "SCP upload failed (#{ex.message})" end |
#uri ⇒ Object
180 181 182 |
# File 'lib/train/transports/ssh_connection.rb', line 180 def uri "ssh://#{@username}@#{@hostname}:#{@port}" end |
#wait_until_ready ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/train/transports/ssh_connection.rb', line 169 def wait_until_ready delay = 3 session( retries: @max_wait_until_ready / delay, delay: delay, message: "Waiting for SSH service on #{@hostname}:#{@port}, " \ "retrying in #{delay} seconds", ) execute(PING_COMMAND.dup) end |