class Train::Transports::SSH

A Transport which uses the SSH protocol to execute commands and transfer files.

@author Fletcher Nichol <fnichol@nichol.ca>

Public Instance Methods

connection(state = {}, &block) click to toggle source

(see Base#connection)

# File lib/train/transports/ssh.rb, line 65
def connection(state = {}, &block)
  opts = merge_options(options, state || {})
  validate_options(opts)
  conn_opts = connection_options(opts)

  if defined?(@connection) && @connection_options == conn_opts
    reuse_connection(&block)
  else
    create_new_connection(conn_opts, &block)
  end
end

Private Instance Methods

connection_options(opts) click to toggle source

Builds the hash of options needed by the Connection object on construction.

@param opts [Hash] merged configuration and mutable state data @return [Hash] hash of connection options @api private

# File lib/train/transports/ssh.rb, line 111
def connection_options(opts)
  {
    logger:                 logger,
    user_known_hosts_file:  '/dev/null',
    paranoid:               false,
    hostname:               opts[:host],
    port:                   opts[:port],
    username:               opts[:user],
    compression:            opts[:compression],
    compression_level:      opts[:compression_level],
    keepalive:              opts[:keepalive],
    keepalive_interval:     opts[:keepalive_interval],
    timeout:                opts[:connection_timeout],
    connection_retries:     opts[:connection_retries],
    connection_retry_sleep: opts[:connection_retry_sleep],
    max_wait_until_ready:   opts[:max_wait_until_ready],
    auth_methods:           opts[:auth_methods],
    keys_only:              opts[:keys_only],
    keys:                   opts[:key_files],
    password:               opts[:password],
    forward_agent:          opts[:forward_agent],
    transport_options:      opts,
  }
end
create_new_connection(options, &block) click to toggle source

Creates a new SSH Connection instance and save it for potential future reuse.

@param options [Hash] conneciton options @return [Ssh::Connection] an SSH Connection instance @api private

# File lib/train/transports/ssh.rb, line 142
def create_new_connection(options, &block)
  if defined?(@connection)
    logger.debug("[SSH] shutting previous connection #{@connection}")
    @connection.close
  end

  @connection_options = options
  conn = Connection.new(options, &block)
  @connection = conn unless conn.nil?
end
reuse_connection() { |connection| ... } click to toggle source

Return the last saved SSH connection instance.

@return [Ssh::Connection] an SSH Connection instance @api private

# File lib/train/transports/ssh.rb, line 157
def reuse_connection
  logger.debug("[SSH] reusing existing connection #{@connection}")
  yield @connection if block_given?
  @connection
end
validate_options(options) click to toggle source
Calls superclass method
# File lib/train/transports/ssh.rb, line 79
def validate_options(options)
  super(options)

  key_files = Array(options[:key_files])
  if key_files.empty? and options[:password].nil?
    fail Train::ClientError,
         'You must configure at least one authentication method for SSH:'\
         ' Password or key.'
  end

  options[:auth_methods] ||= ['none']

  unless key_files.empty?
    options[:auth_methods].push('publickey')
    options[:keys_only] = true if options[:password].nil?
    options[:key_files] = key_files
  end

  unless options[:password].nil?
    options[:auth_methods].push('password')
  end

  super
  self
end