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,
  Net::SSH::AuthenticationFailed,
  IOError,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SshConnection.



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

def initialize hostname, user = nil, ssh_opts = {}, options = {}
  @hostname = hostname
  @user = user
  @ssh_opts = ssh_opts
  @logger = options[:logger]
  @options = options
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/beaker/ssh_connection.rb', line 8

def logger
  @logger
end

Class Method Details

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



32
33
34
35
36
# File 'lib/beaker/ssh_connection.rb', line 32

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

Instance Method Details

#closeObject

closes this SshConnection



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/beaker/ssh_connection.rb', line 62

def close
  begin
    if @ssh and not @ssh.closed?
      @ssh.close
    else
      @logger.warn("ssh.close: connection is already closed, no action needed")
    end
  rescue *RETRYABLE_EXCEPTIONS => e
    @logger.warn "Attemped ssh.close, (caught #{e.class.name} - #{e.message})."
  rescue => e
    @logger.warn "ssh.close threw unexpected Error: #{e.class.name} - #{e.message}.  Shutting down, and re-raising error below"
    @ssh.shutdown!
    raise e
  ensure
    @ssh = nil
    @logger.warn("ssh connection to #{@hostname} has been terminated")
  end
end

#connectObject

connect to the host



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/beaker/ssh_connection.rb', line 39

def connect
  try = 1
  last_wait = 0
  wait = 1
  @ssh ||= begin
             @logger.debug "Attempting ssh connection to #{@hostname}, user: #{@user}, opts: #{@ssh_opts}"
             Net::SSH.start(@hostname, @user, @ssh_opts)
           rescue *RETRYABLE_EXCEPTIONS => e
             if try <= 11
               @logger.warn "Try #{try} -- Host #{@hostname} unreachable: #{e.class.name} - #{e.message}"
               @logger.warn "Trying again in #{wait} seconds"
               sleep wait
              (last_wait, wait) = wait, last_wait + wait
               try += 1
               retry
             else
               @logger.error "Failed to connect to #{@hostname}"
               raise
             end
           end
end

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/beaker/ssh_connection.rb', line 119

def execute command, options = {}, stdout_callback = nil,
            stderr_callback = stdout_callback
  try = 1
  wait = 1
  last_wait = 0
  begin
    # ensure that we have a current connection object
    connect
    result = try_to_execute(command, options, stdout_callback, stderr_callback)
  rescue *RETRYABLE_EXCEPTIONS => e
    if try < 11
       sleep wait
      (last_wait, wait) = wait, last_wait + wait
       try += 1
      @logger.error "Command execution '#{@hostname}$ #{command}' failed (#{e.class.name} - #{e.message})"
      close
      @logger.debug "Preparing to retry: closed ssh object"
      retry
    else
      raise
    end
  end

  result
end

#process_stdin_for(channel, stdin) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/beaker/ssh_connection.rb', line 180

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



174
175
176
177
178
# File 'lib/beaker/ssh_connection.rb', line 174

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



164
165
166
167
168
169
170
171
172
# File 'lib/beaker/ssh_connection.rb', line 164

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



156
157
158
159
160
161
162
# File 'lib/beaker/ssh_connection.rb', line 156

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



145
146
147
148
149
150
151
152
153
154
# File 'lib/beaker/ssh_connection.rb', line 145

def request_terminal_for channel, command
  channel.request_pty do |ch, success|
    if success
      @logger.info "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



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/beaker/ssh_connection.rb', line 215

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

  local_opts = options.dup
  if local_opts[:recursive].nil?
    local_opts[:recursive] = true
  end
  local_opts[:chunk_size] ||= 16384

  result = Result.new(@hostname, [source, target])
  result.stdout = "\n"
  @ssh.scp.download! source, target, local_opts do |ch, name, sent, total|
    result.stdout << "\tcopying %s: %10d/%d\n" % [name, sent, total]
  end

  # 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.finalize!
  result
end

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/beaker/ssh_connection.rb', line 190

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

  local_opts = options.dup
  if local_opts[:recursive].nil?
    local_opts[:recursive] = File.directory?(source)
  end
  local_opts[:chunk_size] ||= 16384

  result = Result.new(@hostname, [source, target])
  result.stdout = "\n"
  @ssh.scp.upload! source, target, local_opts do |ch, name, sent, total|
    result.stdout << "\tcopying %s: %10d/%d\n" % [name, sent, total]
  end

  # 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

  result.finalize!
  return result
end

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/beaker/ssh_connection.rb', line 81

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...
  begin
    @ssh.loop
  rescue *RETRYABLE_EXCEPTIONS => e
    # this would indicate that the connection failed post execution, since the channel exec was successful
    @logger.warn "ssh channel on #{@hostname} received exception post command execution #{e.class.name} - #{e.message}"
    close
  end

  result.finalize!
  @logger.last_result = result
  result
end