class Cabal::SSH

Generates SSH sessions for remote server interaction

Public Class Methods

connect(options = {}) click to toggle source

Create a new instance and use it to generate an SSH session @param options [Hash] the options to pass along to the new instance @option options [String] :cluster the name of the cluster key to

use (required)

@option options [String] :ssh_signature the user@hostname

that would be passed if the normal ssh CLI utility were used (required)

@option options [Cabal::Client] :client the instance of

Cabal::Client to use for key retrieval (required)

@option options [Integer] :identity_timeout the number of seconds

that keys should be forwarded in the resulting SSH session (optional)

@option options [Kernel] :kernel the Kernel-compliant object used for

issuing calls to #system for the SSH session (optional)

@return [nil]

# File lib/cabal/ssh.rb, line 25
def self.connect(options = {})
  new(
    options[:cluster],
    options[:ssh_signature],
    options[:client],
    options[:identity_timeout] || 1800,
    options[:kernel] || Kernel
  ).connect
end
new(cluster, ssh_signature, client, identity_timeout, kernel) click to toggle source
# File lib/cabal/ssh.rb, line 35
def initialize(cluster, ssh_signature, client, identity_timeout, kernel)
  @cluster = cluster
  @ssh_signature = ssh_signature
  @client = client
  @identity_timeout = identity_timeout
  @kernel = kernel
end

Public Instance Methods

connect() click to toggle source

Initiate an SSH connection in the current terminal @return [nil]

# File lib/cabal/ssh.rb, line 53
def connect
  begin
    write_private_key
    initiate_connection
  ensure
    terminate_connection
  end
end
lifetime() click to toggle source

The lifetime option that would be passed to ssh-add for this session @return [String] the empty string for immortal sessions,

"-t identity_timeout" for sessions with a defined lifetime
# File lib/cabal/ssh.rb, line 46
def lifetime
  return '' if identity_timeout == 0
  "-t #{identity_timeout}"
end

Private Instance Methods

agent() click to toggle source
# File lib/cabal/ssh.rb, line 99
def agent
  @agent ||= Cabal::IdentityManager.new
end
client() click to toggle source
# File lib/cabal/ssh.rb, line 91
def client
  @client
end
cluster() click to toggle source
# File lib/cabal/ssh.rb, line 95
def cluster
  @cluster
end
identity_timeout() click to toggle source
# File lib/cabal/ssh.rb, line 103
def identity_timeout
  @identity_timeout
end
initiate_connection() click to toggle source
# File lib/cabal/ssh.rb, line 71
def initiate_connection
  agent.start
  kernel.system("#{agent.env} ssh-add #{lifetime} #{key_file.path} > /dev/null 2>&1")
  kernel.system("#{agent.env} ssh -A #{ssh_signature}")
end
kernel() click to toggle source
# File lib/cabal/ssh.rb, line 107
def kernel
  @kernel
end
key_file() click to toggle source
# File lib/cabal/ssh.rb, line 83
def key_file
  @key_file
end
ssh_signature() click to toggle source
# File lib/cabal/ssh.rb, line 87
def ssh_signature
  @ssh_signature
end
terminate_connection() click to toggle source
# File lib/cabal/ssh.rb, line 77
def terminate_connection
  kernel.system("#{agent.env} ssh-add -D > /dev/null 2>&1")
  agent.stop
  key_file.unlink unless key_file.nil?
end
write_private_key() click to toggle source
# File lib/cabal/ssh.rb, line 63
def write_private_key
  private_key = client.private_key(cluster)
  @key_file = Tempfile.new(SecureRandom.hex(8))
  key_file.write(private_key)
  key_file.close
  FileUtils.chmod(0700, key_file.path)
end