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

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

#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



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

def close
  if @ssh
    @ssh.close
  end
end

#exec!(command) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/indocker/ssh_session.rb', line 40

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

#local?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/indocker/ssh_session.rb', line 36

def local?
  !@ssh
end