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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SshSession.



38
39
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
# File 'lib/indocker/ssh_session.rb', line 38

def initialize(host:, user:, port:, logger:, jump_host: nil)
  @host = host
  @user = user
  @port = port
  @jump_host = jump_host
  @logger = logger

  if host != LOCALHOST
    require 'net/ssh'

    options = {
      port:               @port,
      non_interactive:    true,
      timeout:            Indocker.ssh_connect_timeout,
      keepalive:          true,
      keepalive_interval: 10,
      keepalive_maxcount: 3,
    }

    if @jump_host
      require 'net/ssh/proxy/command'
      options[:proxy] = Net::SSH::Proxy::Command.new(jump_proxy_command)
    end

    @ssh = Net::SSH.start(@host, @user, options)
  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

#jump_hostObject (readonly)

Returns the value of attribute jump_host.



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

def jump_host
  @jump_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

Class Method Details

.for(server, logger:) ⇒ Object

Opens a session to a server as it is defined in the configuration, including its jump host, if any. Every remote operation goes through here, so a server reached through a jump host is reached that way by binaries sync, container deploys and the deployment checker alike.



28
29
30
31
32
33
34
35
36
# File 'lib/indocker/ssh_session.rb', line 28

def self.for(server, logger:)
  new(
    host:      server.host,
    user:      server.user,
    port:      server.port,
    jump_host: server.jump_host,
    logger:    logger
  )
end

Instance Method Details

#closeObject



78
79
80
81
82
# File 'lib/indocker/ssh_session.rb', line 78

def close
  if @ssh
    @ssh.close
  end
end

#exec!(command) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/indocker/ssh_session.rb', line 70

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

#local?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/indocker/ssh_session.rb', line 66

def local?
  !@ssh
end