Class: Confer::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/confer/connection.rb

Overview

Public: Encapsulates a connection to a remote host.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Public: Initialies a Connection instance.

host - The host to connect to.



41
42
43
44
# File 'lib/confer/connection.rb', line 41

def initialize(opts = {})
  @config ||= {}
  opts.apply_to self
end

Instance Attribute Details

#configObject

Public: A Hash containing the SSH configuration options.



34
35
36
# File 'lib/confer/connection.rb', line 34

def config
  @config
end

#hostObject

Public: A String containing the remote hostname.



24
25
26
# File 'lib/confer/connection.rb', line 24

def host
  @host
end

#userObject

Public: A String containing the remote username.



29
30
31
# File 'lib/confer/connection.rb', line 29

def user
  @user
end

Class Method Details

.start(opts = {}, &block) ⇒ Object

Public: Creates a new instance and immediately connects to the remote host, yielding the instance to the given block.

opts - The options to pass to the new Connection instance. block - The block to receive the new instance.



17
18
19
# File 'lib/confer/connection.rb', line 17

def self.start(opts = {}, &block)
  Connection.new(opts).start(&block)
end

Instance Method Details

#exec!(command, opts = {}) ⇒ Object

Public: Execute a command on the remote host.

command - The command to execute. opts - A hash of options:

:shell - True if the command should be run via the remote shell.

Returns the raw response from the command.



69
70
71
# File 'lib/confer/connection.rb', line 69

def exec!(command, opts = {})
  @ssh.exec! command
end

#start(&block) ⇒ Object

Public: Opens a connection to the remote host and yields self to the given block for further interaction. The connection will be closed once the block has executed.

block - The block to yield to.



53
54
55
56
57
58
# File 'lib/confer/connection.rb', line 53

def start(&block)
  Net::SSH.start(self.host, self.user, self.config) do |ssh|
    @ssh = ssh
    yield(self)
  end
end