Class: Dockerploy::SSHClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dockerploy/ssh_client.rb

Overview

Wrapper for shell command over ssh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, port = 22) ⇒ SSHClient

Returns a new instance of SSHClient.



8
9
10
11
12
13
# File 'lib/dockerploy/ssh_client.rb', line 8

def initialize(host, username, password, port = 22)
  @host = host
  @username = username
  @password = password
  @port = port
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/dockerploy/ssh_client.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/dockerploy/ssh_client.rb', line 6

def password
  @password
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/dockerploy/ssh_client.rb', line 6

def port
  @port
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/dockerploy/ssh_client.rb', line 6

def username
  @username
end

Instance Method Details

#command(command) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dockerploy/ssh_client.rb', line 15

def command(command)
  exit_code = nil
  Dockerploy.logger.info sprintf('From server: %s Running: %s', @host, command)
  with_ssh(command) do |c, ssh|
    Dockerploy.logger.info 'Output: '
    ssh.open_channel do |channel|
      channel.exec(c) do |_, success|
        abort "FAILED: couldn't execute command (ssh.channel.exec)" unless success

        channel.on_data do |_, data|
          print data
        end

        channel.on_extended_data do |_, _, data|
          print data
        end

        channel.on_request('exit-status') do |_, data|
          exit_code = data.read_long
        end
      end
      ssh.loop
    end
  end

  result = exit_code == 0
  Dockerploy.logger.info sprintf('Exit Code: %d', exit_code) unless result
  result
end