Class: Indocker::SshSession

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/ssh_session.rb

Defined Under Namespace

Classes: ExecResult

Constant Summary collapse

LOCALHOST =
'localhost'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, user:, port:, logger:) ⇒ SshSession



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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,
      non_interactive:    true,
      timeout:            Indocker.ssh_connect_timeout,
      keepalive:          true,
      keepalive_interval: 10,
      keepalive_maxcount: 3,
    })
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



22
23
24
# File 'lib/indocker/ssh_session.rb', line 22

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/indocker/ssh_session.rb', line 22

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



22
23
24
# File 'lib/indocker/ssh_session.rb', line 22

def port
  @port
end

#userObject (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

#closeObject



55
56
57
58
59
# File 'lib/indocker/ssh_session.rb', line 55

def close
  if @ssh
    @ssh.close
  end
end

#exec!(command) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/indocker/ssh_session.rb', line 47

def exec!(command)
  if local?
    exec_locally!(command)
  else
    exec_remotely!(command)
  end
end

#local?Boolean



43
44
45
# File 'lib/indocker/ssh_session.rb', line 43

def local?
  !@ssh
end