Class: Dbcp::SshExecutionHost

Inherits:
ExecutionHost show all
Defined in:
lib/dbcp/execution_hosts/ssh_execution_host.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ExecutionHost

build, #local?

Class Method Details

.new_from_uri(uri_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/dbcp/execution_hosts/ssh_execution_host.rb', line 9

def new_from_uri(uri_string)
  uri = URI.parse uri_string
  raise URI::InvalidURIError.new "SSH URI must be in form 'ssh://[email protected]/path/to_application_root'. We received: '#{uri_string}'." unless uri.user && uri.host
  new({
    host:     uri.host,
    port:     uri.port,
    username: uri.user,
    path:     uri.path
  })
end

Instance Method Details

#download(source_path, destination_path = nil) ⇒ Object

Omitting destination_path will return file contents as a string



69
70
71
72
73
# File 'lib/dbcp/execution_hosts/ssh_execution_host.rb', line 69

def download(source_path, destination_path = nil)
  Net::SFTP.start host, username do |ssh|
    return ssh.download! source_path, destination_path
  end
end

#execute(command) ⇒ Object



32
33
34
35
36
37
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
65
66
# File 'lib/dbcp/execution_hosts/ssh_execution_host.rb', line 32

def execute(command)
  # http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library
  stdout_data = ""
  stderr_data = ""
  exitstatus  = nil
  exit_signal = nil

  Net::SSH.start host, username do |ssh|
    ssh.open_channel do |channel|
      channel.exec command do |ch, success|
        unless success
          raise ExecutionError.new "Exection over SSH to failed for: (ssh.channel.exec)"
        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|
          exitstatus = data.read_long
        end

        channel.on_request("exit-signal") do |ch, data|
          exit_signal = data.read_long
        end
      end
    end
    ssh.loop
  end

  raise ExecutionError.new "Execution failed with exit code #{$?.exitstatus}. Command was: #{command}" if exitstatus > 0
end

#remote?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/dbcp/execution_hosts/ssh_execution_host.rb', line 28

def remote?
  true
end

#upload(source_path, destination_path) ⇒ Object



75
76
77
78
79
# File 'lib/dbcp/execution_hosts/ssh_execution_host.rb', line 75

def upload(source_path, destination_path)
  Net::SFTP.start host, username do |ssh|
    return ssh.upload! source_path, destination_path
  end
end