Class: Minestrone::SSH
- Inherits:
-
Object
- Object
- Minestrone::SSH
- Defined in:
- lib/minestrone/ssh.rb
Overview
A helper class for dealing with SSH connections.
Defined Under Namespace
Modules: Server
Class Method Summary collapse
-
.connect(server, options = {}) ⇒ Object
An abstraction to make it possible to connect to the server via public key.
-
.connection_strategy(server, options = {}) {|host, user, connection_options| ... } ⇒ Object
Abstracts the logic for establishing an SSH connection.
Class Method Details
.connect(server, options = {}) ⇒ Object
An abstraction to make it possible to connect to the server via public key.
server must be an instance of ServerDefinition.
If a block is given, the new session is yielded to it, otherwise the new session is returned.
If an :ssh_options key exists in options, it is passed to the Net::SSH constructor. Values in options are then merged into it, and any connection information in server is added last, so that server info takes precedence over options, which takes precendence over ssh_options.
35 36 37 38 39 40 |
# File 'lib/minestrone/ssh.rb', line 35 def self.connect(server, = {}) connection_strategy(server, ) do |host, user, | connection = Net::SSH.start(host, user, ) Server.apply_to(connection, server) end end |
.connection_strategy(server, options = {}) {|host, user, connection_options| ... } ⇒ Object
Abstracts the logic for establishing an SSH connection.
This will yield the hostname, username, and a hash of connection options to the given block, which should return a new connection.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/minestrone/ssh.rb', line 47 def self.connection_strategy(server, = {}, &block) # construct the hash of ssh options that should be passed more-or-less # directly to Net::SSH. This will be the general ssh options, merged with # the server-specific ssh-options. = ([:ssh_options] || {}).merge(server.[:ssh_options] || {}) # load any SSH configuration files that were specified in the SSH options. This # will load from ~/.ssh/config and /etc/ssh_config by default (see Net::SSH # for details). Merge the explicitly given ssh_options over the top of the info # from the config file. = Net::SSH.configuration_for(server.host, .fetch(:config, true)).merge() # Once we've loaded the config, we don't need Net::SSH to do it again. [:config] = false [:verbose] = :debug if [:verbose] && [:verbose] > 0 user = server.user || [:user] || [:username] || [:user] || ServerDefinition.default_user port = server.port || [:port] || [:port] # the .ssh/config file might have changed the host-name on us host = .fetch(:host_name, server.host) [:port] = port if port # delete these, since we've determined which username to use by this point .delete(:username) .delete(:user) = .merge(:auth_methods => ['publickey']) yield host, user, end |