Class: Beaker::SshConnection
- Inherits:
-
Object
- Object
- Beaker::SshConnection
- Defined in:
- lib/beaker/ssh_connection.rb
Constant Summary collapse
- SUPPORTED_CONNECTION_METHODS =
[:ip, :vmhostname, :hostname]
- RETRYABLE_EXCEPTIONS =
[ SocketError, Timeout::Error, Errno::ETIMEDOUT, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ENETUNREACH, Net::SSH::Exception, Net::SSH::Disconnect, Net::SSH::AuthenticationFailed, Net::SSH::ChannelRequestFailed, Net::SSH::ChannelOpenFailed, IOError, ]
Instance Attribute Summary collapse
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#ssh_connection_preference ⇒ Object
Returns the value of attribute ssh_connection_preference.
-
#vmhostname ⇒ Object
Returns the value of attribute vmhostname.
Class Method Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
closes this SshConnection.
-
#connect(options = {}) ⇒ Object
Connect to the host, creating a new connection if required.
-
#connect_block(host, user, ssh_opts, options) ⇒ Net::SSH::Connection::Session
Setup and return the ssh connection object.
-
#execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object
Execute a command on a host, ensuring a connection exists first.
-
#initialize(name_hash, user = nil, ssh_opts = {}, options = {}) ⇒ SshConnection
constructor
A new instance of SshConnection.
- #process_stdin_for(channel, stdin) ⇒ Object
- #register_exit_code_for(channel, output) ⇒ Object
- #register_stderr_for(channel, output, callback = nil) ⇒ Object
- #register_stdout_for(channel, output, callback = nil) ⇒ Object
- #request_terminal_for(channel, command) ⇒ Object
- #scp_from(source, target, options = {}) ⇒ Object
- #scp_to(source, target, options = {}) ⇒ Object
- #try_to_execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object
-
#wait_for_connection_failure(options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Boolean
Wait for the ssh connection to fail, returns true on connection failure and false otherwise.
Constructor Details
#initialize(name_hash, user = nil, ssh_opts = {}, options = {}) ⇒ SshConnection
Returns a new instance of SshConnection.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/beaker/ssh_connection.rb', line 30 def initialize name_hash, user = nil, ssh_opts = {}, = {} @vmhostname = name_hash[:vmhostname] @ip = name_hash[:ip] @hostname = name_hash[:hostname] @user = user @ssh_opts = ssh_opts @logger = [:logger] = @ssh_connection_preference = [:ssh_connection_preference] end |
Instance Attribute Details
#hostname ⇒ Object
Returns the value of attribute hostname.
9 10 11 |
# File 'lib/beaker/ssh_connection.rb', line 9 def hostname @hostname end |
#ip ⇒ Object
Returns the value of attribute ip.
9 10 11 |
# File 'lib/beaker/ssh_connection.rb', line 9 def ip @ip end |
#logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/beaker/ssh_connection.rb', line 8 def logger @logger end |
#ssh_connection_preference ⇒ Object
Returns the value of attribute ssh_connection_preference.
9 10 11 |
# File 'lib/beaker/ssh_connection.rb', line 9 def ssh_connection_preference @ssh_connection_preference end |
#vmhostname ⇒ Object
Returns the value of attribute vmhostname.
9 10 11 |
# File 'lib/beaker/ssh_connection.rb', line 9 def vmhostname @vmhostname end |
Class Method Details
.connect(name_hash, user = 'root', ssh_opts = {}, options = {}) ⇒ Object
41 42 43 44 45 |
# File 'lib/beaker/ssh_connection.rb', line 41 def self.connect name_hash, user = 'root', ssh_opts = {}, = {} connection = new name_hash, user, ssh_opts, connection.connect connection end |
Instance Method Details
#close ⇒ Object
closes this SshConnection
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/beaker/ssh_connection.rb', line 129 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.debug("ssh connection to #{@hostname} has been terminated") end end |
#connect(options = {}) ⇒ Object
Connect to the host, creating a new connection if required
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/beaker/ssh_connection.rb', line 104 def connect = {} # Try three ways to connect to host (vmhostname, ip, hostname) # Try each method in turn until we succeed methods = @ssh_connection_preference.dup while (not @ssh) && (not methods.empty?) do unless instance_variable_get("@#{methods[0]}").nil? if SUPPORTED_CONNECTION_METHODS.include?(methods[0]) @ssh = connect_block(instance_variable_get("@#{methods[0].to_s}"), @user, @ssh_opts, ) else @logger.warn "Beaker does not support #{methods[0]} to SSH to host, trying next available method." @ssh_connection_preference.delete(methods[0]) end else @logger.warn "Skipping #{methods[0]} method to ssh to host as its value is not set. Refer to https://github.com/puppetlabs/beaker/tree/master/docs/how_to/ssh_connection_preference.md to remove this warning" end methods.shift end unless @ssh @logger.error "Failed to connect to #{@hostname}, attempted #{@ssh_connection_preference.join(', ')}" raise RuntimeError, "Cannot connect to #{@hostname}" end @ssh end |
#connect_block(host, user, ssh_opts, options) ⇒ Net::SSH::Connection::Session
For more information about Net::SSH library, check out these docs:
Setup and return the ssh connection object
64 65 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 96 97 |
# File 'lib/beaker/ssh_connection.rb', line 64 def connect_block host, user, ssh_opts, try = 1 last_wait = 2 wait = 3 max_connection_tries = [:max_connection_tries] || 11 begin @logger.debug "Attempting ssh connection to #{host}, user: #{user}, opts: #{ssh_opts}" # Work around net-ssh 6+ incompatibilities if ssh_opts.include?(:strict_host_key_checking) && (Net::SSH::Version::CURRENT.major > 5) strict_host_key_checking = ssh_opts.delete(:strict_host_key_checking) unless ssh_opts[:verify_host_key].is_a?(Symbol) ssh_opts[:verify_host_key] ||= strict_host_key_checking ? :always : :never end end Net::SSH.start(host, user, ssh_opts) rescue *RETRYABLE_EXCEPTIONS => e if try <= max_connection_tries @logger.warn "Try #{try} -- Host #{host} unreachable: #{e.class.name} - #{e.message}" unless [:silent] @logger.warn "Trying again in #{wait} seconds" unless [:silent] sleep wait (last_wait, wait) = wait, last_wait + wait try += 1 retry else @logger.warn "Failed to connect to #{host}, after #{try} attempts" unless [:silent] nil end end end |
#execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object
Execute a command on a host, ensuring a connection exists first
238 239 240 241 242 243 |
# File 'lib/beaker/ssh_connection.rb', line 238 def execute command, = {}, stdout_callback = nil, stderr_callback = stdout_callback # ensure that we have a current connection object connect() try_to_execute(command, , stdout_callback, stderr_callback) end |
#process_stdin_for(channel, stdin) ⇒ Object
280 281 282 283 284 285 286 287 288 |
# File 'lib/beaker/ssh_connection.rb', line 280 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
274 275 276 277 278 |
# File 'lib/beaker/ssh_connection.rb', line 274 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
264 265 266 267 268 269 270 271 272 |
# File 'lib/beaker/ssh_connection.rb', line 264 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
256 257 258 259 260 261 262 |
# File 'lib/beaker/ssh_connection.rb', line 256 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
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/beaker/ssh_connection.rb', line 245 def request_terminal_for channel, command channel.request_pty do |ch, success| if success @logger.debug "Allocated a PTY on #{@hostname} for #{command.inspect}" else raise Net::SSH::Exception.new("FAILED: could not allocate a pty when requested on " + "#{@hostname} for #{command.inspect}") end end end |
#scp_from(source, target, options = {}) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/beaker/ssh_connection.rb', line 326 def scp_from source, target, = {} local_opts = .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" begin # This is probably windows with an environment variable so we need to # expand it. source = self.execute(%{echo "#{source}"}).output.strip.gsub('"','') if source.include?('%') @ssh.scp.download! source, target, local_opts do |ch, name, sent, total| result.stdout << "\tcopying %s: %10d/%d\n" % [name, sent, total] end rescue => e logger.warn "#{e.class} error in scp'ing. Forcing the connection to close, which should " << "raise an error." close 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 = {}) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/beaker/ssh_connection.rb', line 290 def scp_to source, target, = {} local_opts = .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" begin # This is probably windows with an environment variable so we need to # expand it. target = self.execute(%{echo "#{target}"}).output.strip.gsub('"','') if target.include?('%') @ssh.scp.upload! source, target, local_opts do |ch, name, sent, total| result.stdout << "\tcopying %s: %10d/%d\n" % [name, sent, total] end rescue => e logger.warn "#{e.class} error in scp'ing. Forcing the connection to close, which should " << "raise an error." close 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/beaker/ssh_connection.rb', line 200 def try_to_execute command, = {}, stdout_callback = nil, stderr_callback = stdout_callback result = Result.new(@hostname, command) @ssh.open_channel do |channel| request_terminal_for( channel, command ) if [:pty] channel.exec(command) do |terminal, success| raise Net::SSH::Exception.new("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, [:stdin] ) if [: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 |
#wait_for_connection_failure(options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Boolean
Wait for the ssh connection to fail, returns true on connection failure and false otherwise
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/beaker/ssh_connection.rb', line 156 def wait_for_connection_failure = {}, stdout_callback = nil, stderr_callback = stdout_callback try = 1 last_wait = 2 wait = 3 command = 'echo echo' #can be run on all platforms (I'm looking at you, windows) while try < 11 result = Result.new(@hostname, command) begin @logger.notify "Waiting for connection failure on #{@hostname} (attempt #{try}, try again in #{wait} second(s))" @logger.debug("\n#{@hostname} #{Time.new.strftime('%H:%M:%S')}$ #{command}") @ssh.open_channel do |channel| request_terminal_for( channel, command ) if [:pty] channel.exec(command) do |terminal, success| raise Net::SSH::Exception.new("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, [:stdin] ) if [:stdin] end end loop_tries = 0 #loop is actually loop_forever, so let it try 3 times and then quit instead of endless blocking @ssh.loop { loop_tries += 1 ; loop_tries < 4 } rescue *RETRYABLE_EXCEPTIONS => e @logger.debug "Connection on #{@hostname} failed as expected (#{e.class.name} - #{e.message})" close #this connection is bad, shut it down return true end slept = 0 stdout_callback.call("sleep #{wait} second(s): ") while slept < wait sleep slept stdout_callback.call('.') slept += 1 end stdout_callback.call("\n") (last_wait, wait) = wait, last_wait + wait try += 1 end false end |