Class: ChefMetal::Transport::SSH
- Inherits:
-
ChefMetal::Transport
- Object
- ChefMetal::Transport
- ChefMetal::Transport::SSH
- Defined in:
- lib/chef_metal/transport/ssh.rb
Defined Under Namespace
Classes: InitialConnectTimeout, SSHResult
Constant Summary
Constants inherited from ChefMetal::Transport
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#ssh_options ⇒ Object
readonly
Returns the value of attribute ssh_options.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #disconnect ⇒ Object
- #download_file(path, local_path) ⇒ Object
- #execute(command, execute_options = {}) ⇒ Object
-
#initialize(host, username, ssh_options, options, global_config) ⇒ SSH
constructor
A new instance of SSH.
- #make_url_available_to_remote(local_url) ⇒ Object
- #read_file(path) ⇒ Object
- #upload_file(local_path, path) ⇒ Object
- #write_file(path, content) ⇒ Object
Constructor Details
#initialize(host, username, ssh_options, options, global_config) ⇒ SSH
Returns a new instance of SSH.
13 14 15 16 17 18 19 |
# File 'lib/chef_metal/transport/ssh.rb', line 13 def initialize(host, username, , , global_config) @host = host @username = username @ssh_options = @options = @config = global_config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/chef_metal/transport/ssh.rb', line 25 def config @config end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
21 22 23 |
# File 'lib/chef_metal/transport/ssh.rb', line 21 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
24 25 26 |
# File 'lib/chef_metal/transport/ssh.rb', line 24 def @options end |
#ssh_options ⇒ Object (readonly)
Returns the value of attribute ssh_options.
23 24 25 |
# File 'lib/chef_metal/transport/ssh.rb', line 23 def @ssh_options end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
22 23 24 |
# File 'lib/chef_metal/transport/ssh.rb', line 22 def username @username end |
Instance Method Details
#available? ⇒ Boolean
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/chef_metal/transport/ssh.rb', line 140 def available? # If you can't pwd within 10 seconds, you can't pwd execute('pwd', :timeout => 10) true rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Net::SSH::Disconnect Chef::Log.debug("#{username}@#{host} unavailable: network connection failed or broke: #{$!.inspect}") disconnect false rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch Chef::Log.debug("#{username}@#{host} unavailable: SSH authentication error: #{$!.inspect} ") disconnect false end |
#disconnect ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/chef_metal/transport/ssh.rb', line 128 def disconnect if @session begin Chef::Log.debug("Closing SSH session on #{username}@#{host}") @session.close rescue ensure @session = nil end end end |
#download_file(path, local_path) ⇒ Object
77 78 79 80 |
# File 'lib/chef_metal/transport/ssh.rb', line 77 def download_file(path, local_path) Chef::Log.debug("Downloading file #{path} from #{username}@#{host} to local #{local_path}") download(path, local_path) end |
#execute(command, execute_options = {}) ⇒ Object
27 28 29 30 31 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 67 68 |
# File 'lib/chef_metal/transport/ssh.rb', line 27 def execute(command, = {}) Chef::Log.info("Executing #{[:prefix]}#{command} on #{username}@#{host}") stdout = '' stderr = '' exitstatus = nil session # grab session outside timeout, it has its own timeout with_execute_timeout() do channel = session.open_channel do |channel| # Enable PTY unless otherwise specified, some instances require this unless [:ssh_pty_enable] == false channel.request_pty do |chan, success| raise "could not get pty" if !success && [:ssh_pty_enable] end end channel.exec("#{[:prefix]}#{command}") do |ch, success| raise "could not execute command: #{command.inspect}" unless success channel.on_data do |ch2, data| stdout << data stream_chunk(, data, nil) end channel.on_extended_data do |ch2, type, data| stderr << data stream_chunk(, nil, data) end channel.on_request "exit-status" do |ch, data| exitstatus = data.read_long end end end channel.wait end Chef::Log.info("Completed #{command} on #{username}@#{host}: exit status #{exitstatus}") Chef::Log.debug("Stdout was:\n#{stdout}") if stdout != '' && ![:stream] && ![:stream_stdout] && config[:log_level] != :debug Chef::Log.info("Stderr was:\n#{stderr}") if stderr != '' && ![:stream] && ![:stream_stderr] && config[:log_level] != :debug SSHResult.new(command, , stdout, stderr, exitstatus) end |
#make_url_available_to_remote(local_url) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/chef_metal/transport/ssh.rb', line 110 def make_url_available_to_remote(local_url) uri = URI(local_url) if is_local_machine(uri.host) port, host = forward_port(uri.port, uri.host, uri.port, 'localhost') if !port # Try harder if the port is already taken port, host = forward_port(uri.port, uri.host, 0, 'localhost') if !port raise "Error forwarding port: could not forward #{uri.port} or 0" end end uri.host = host uri.port = port end Chef::Log.info("Port forwarded: local URL #{local_url} is available to #{self.host} as #{uri.to_s} for the duration of this SSH connection.") uri.to_s end |
#read_file(path) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/chef_metal/transport/ssh.rb', line 70 def read_file(path) Chef::Log.debug("Reading file #{path} from #{username}@#{host}") result = StringIO.new download(path, result) result.string end |
#upload_file(local_path, path) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef_metal/transport/ssh.rb', line 96 def upload_file(local_path, path) execute("mkdir -p #{File.dirname(path)}").error! if [:prefix] # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc. remote_tempfile = "/tmp/#{File.basename(path)}.#{Random.rand(2**32)}" Chef::Log.debug("Uploading #{local_path} to #{remote_tempfile} on #{username}@#{host}") Net::SCP.new(session).upload!(local_path, remote_tempfile) execute("mv #{remote_tempfile} #{path}").error! else Chef::Log.debug("Uploading #{local_path} to #{path} on #{username}@#{host}") Net::SCP.new(session).upload!(local_path, path) end end |
#write_file(path, content) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/chef_metal/transport/ssh.rb', line 82 def write_file(path, content) execute("mkdir -p #{File.dirname(path)}").error! if [:prefix] # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc. remote_tempfile = "/tmp/#{File.basename(path)}.#{Random.rand(2**32)}" Chef::Log.debug("Writing #{content.length} bytes to #{remote_tempfile} on #{username}@#{host}") Net::SCP.new(session).upload!(StringIO.new(content), remote_tempfile) execute("mv #{remote_tempfile} #{path}").error! else Chef::Log.debug("Writing #{content.length} bytes to #{path} on #{username}@#{host}") Net::SCP.new(session).upload!(StringIO.new(content), path) end end |