Class: Indocker::SshSession
- Inherits:
-
Object
- Object
- Indocker::SshSession
- Defined in:
- lib/indocker/ssh_session.rb
Defined Under Namespace
Classes: ExecResult
Constant Summary collapse
- LOCALHOST =
'localhost'.freeze
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #close ⇒ Object
- #exec!(command) ⇒ Object
-
#initialize(host:, user:, port:, logger:) ⇒ SshSession
constructor
A new instance of SshSession.
- #local? ⇒ Boolean
Constructor Details
#initialize(host:, user:, port:, logger:) ⇒ SshSession
Returns a new instance of SshSession.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/indocker/ssh_session.rb', line 24 def initialize(host:, user:, port:, logger:) @host = host @user = user @port = port @logger = logger if host != LOCALHOST require 'net/ssh' @ssh = Net::SSH.start(@host, @user, {port: @port}) end end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
22 23 24 |
# File 'lib/indocker/ssh_session.rb', line 22 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
22 23 24 |
# File 'lib/indocker/ssh_session.rb', line 22 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
22 23 24 |
# File 'lib/indocker/ssh_session.rb', line 22 def port @port end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
22 23 24 |
# File 'lib/indocker/ssh_session.rb', line 22 def user @user end |
Instance Method Details
#close ⇒ Object
87 88 89 90 91 |
# File 'lib/indocker/ssh_session.rb', line 87 def close if @ssh @ssh.close end end |
#exec!(command) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 |
# File 'lib/indocker/ssh_session.rb', line 40 def exec!(command) if !@ssh res = Indocker::Shell.command_with_result(command, @logger, skip_logging: false) ExecResult.new(res.stdout, '', res.exit_status, nil) else if Indocker.export_command command = "#{Indocker.export_command} && #{command}" end stdout_data = '' stderr_data = '' exit_code = nil exit_signal = nil @logger.debug("(#{@user}:#{@host}:#{@port}): executing command: #{command}") @ssh.open_channel do |channel| channel.exec(command) do |ch, success| if !success @logger.error("(#{@user}@#{@host}:#{@port}): couldn't execute command: #{command}") abort('failed') end channel.on_data do |ch,data| stdout_data += data end channel.on_extended_data do |ch,type,data| stderr_data += data end channel.on_request('exit-status') do |ch,data| exit_code = data.read_long end channel.on_request('exit-signal') do |ch, data| exit_signal = data.read_long end end end @ssh.loop ExecResult.new(stdout_data, stderr_data, exit_code, exit_signal) end end |
#local? ⇒ Boolean
36 37 38 |
# File 'lib/indocker/ssh_session.rb', line 36 def local? !@ssh end |