Class: Beaker::SshConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/ssh_connection.rb

Constant Summary collapse

RETRYABLE_EXCEPTIONS =
[
  SocketError,
  Timeout::Error,
  Errno::ETIMEDOUT,
  Errno::EHOSTDOWN,
  Errno::EHOSTUNREACH,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
  Net::SSH::Disconnect
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, user = nil, options = {}) ⇒ SshConnection

Returns a new instance of SshConnection.



20
21
22
23
24
# File 'lib/beaker/ssh_connection.rb', line 20

def initialize hostname, user = nil, options = {}
  @hostname = hostname
  @user = user
  @options = options
end

Class Method Details

.connect(hostname, user = 'root', options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/beaker/ssh_connection.rb', line 26

def self.connect hostname, user = 'root', options = {}
  connection = new hostname, user, options
  connection.connect
  connection
end

Instance Method Details

#closeObject



57
58
59
60
61
62
63
64
# File 'lib/beaker/ssh_connection.rb', line 57

def close
  begin
    @ssh.close if @ssh
  rescue
    @ssh.shutdown! 
  end
  @ssh = nil
end

#connectObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/beaker/ssh_connection.rb', line 32

def connect
  try = 1
  last_wait = 0
  wait = 1
  @ssh ||= begin
             Net::SSH.start(@hostname, @user, @options)
           rescue *RETRYABLE_EXCEPTIONS => e
             if try <= 11
               puts "Try #{try} -- Host #{@hostname} unreachable: #{e.message}"
               puts "Trying again in #{wait} seconds"
               sleep wait
               (last_wait, wait) = wait, last_wait + wait
               try += 1
               retry
             else
               # why is the logger not passed into this class?
               puts "Failed to connect to #{@hostname}"
               raise
             end
           rescue Net::SSH::AuthenticationFailed => e
             raise "Unable to authenticate to #{@hostname} with user #{e.message.inspect}"
           end
  self
end

#execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/beaker/ssh_connection.rb', line 97

def execute command, options = {}, stdout_callback = nil,
            stderr_callback = stdout_callback
  attempt = true
  begin
    result = try_to_execute(command, options, stdout_callback, stderr_callback)
  rescue *RETRYABLE_EXCEPTIONS => e
    if attempt
      attempt = false
      puts "Command execution failed, attempting to reconnect to #{@hostname}"
      close
      connect 
      retry
    else
      raise
    end
  end

  result
end

#process_stdin_for(channel, stdin) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/beaker/ssh_connection.rb', line 152

def process_stdin_for channel, stdin
  # queue stdin data, force it to packets, and signal eof: this
  # triggers action in many remote commands, notably including
  # 'puppet apply'.  It must be sent at some point before the rest
  # of the action.
  channel.send_data stdin.to_s
  channel.process
  channel.eof!
end

#register_exit_code_for(channel, output) ⇒ Object



146
147
148
149
150
# File 'lib/beaker/ssh_connection.rb', line 146

def register_exit_code_for channel, output
  channel.on_request("exit-status") do |ch, data|
    output.exit_code = data.read_long
  end
end

#register_stderr_for(channel, output, callback = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/beaker/ssh_connection.rb', line 136

def register_stderr_for channel, output, callback = nil
  channel.on_extended_data do |ch, type, data|
    if type == 1
      callback[data] if callback
      output.stderr << data
      output.output << data
    end
  end
end

#register_stdout_for(channel, output, callback = nil) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/beaker/ssh_connection.rb', line 128

def register_stdout_for channel, output, callback = nil
  channel.on_data do |ch, data|
    callback[data] if callback
    output.stdout << data
    output.output << data
  end
end

#request_terminal_for(channel, command) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/beaker/ssh_connection.rb', line 117

def request_terminal_for channel, command
  channel.request_pty do |ch, success|
    if success
      puts "Allocated a PTY on #{@hostname} for #{command.inspect}"
    else
      abort "FAILED: could not allocate a pty when requested on " +
        "#{@hostname} for #{command.inspect}"
    end
  end
end

#scp_from(source, target, options = {}, dry_run = false) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/beaker/ssh_connection.rb', line 180

def scp_from source, target, options = {}, dry_run = false
  return if dry_run

  options[:recursive] = true if options[:recursive].nil?

  @ssh.scp.download! source, target, options

  result = Result.new(@hostname, [source, target])

  # Setting these values allows reporting via result.log(test_name)
  result.stdout = "SCP'ed file #{@hostname}:#{source} to #{target}"

  # Net::Scp always returns 0, so just set the return code to 0.
  result.exit_code = 0

  result
end

#scp_to(source, target, options = {}, dry_run = false) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/beaker/ssh_connection.rb', line 162

def scp_to source, target, options = {}, dry_run = false
  return if dry_run

  options[:recursive]=File.directory?(source) if options[:recursive].nil?

  @ssh.scp.upload! source, target, options

  result = Result.new(@hostname, [source, target])

  # Setting these values allows reporting via result.log(test_name)
  result.stdout = "SCP'ed file #{source} to #{@hostname}:#{target}"

  # Net::Scp always returns 0, so just set the return code to 0.
  result.exit_code = 0

  return result
end

#try_to_execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/beaker/ssh_connection.rb', line 66

def try_to_execute command, options = {}, stdout_callback = nil,
            stderr_callback = stdout_callback

  result = Result.new(@hostname, command)
  # why are we getting to this point on a dry run anyways?
  # also... the host creates connections through the class method,
  # which automatically connects, so you can't do a dry run unless you also
  # can connect to your hosts?
  return result if options[:dry_run]

  @ssh.open_channel do |channel|
    request_terminal_for( channel, command ) if options[:pty]

    channel.exec(command) do |terminal, success|
      abort "FAILED: to execute command on a new channel on #{@hostname}" unless success
      register_stdout_for terminal, result, stdout_callback
      register_stderr_for terminal, result, stderr_callback
      register_exit_code_for terminal, result

      process_stdin_for( terminal, options[:stdin] ) if options[:stdin]
    end
  end

  # Process SSH activity until we stop doing that - which is when our
  # channel is finished with...
  @ssh.loop

  result.finalize!
  result
end