Class: Auger::Connection

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

Direct Known Subclasses

Dns, Http, Redis, Socket, Telnet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
# File 'lib/auger/connection.rb', line 13

def initialize(port)
  @options = {:port => port, :timeout => 5}
  @roles = []
  @requests = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, arg) ⇒ Object



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

def method_missing(method, arg)
  @options[method] = arg
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/auger/connection.rb', line 5

def connection
  @connection
end

#gatewayObject

Returns the value of attribute gateway.



5
6
7
# File 'lib/auger/connection.rb', line 5

def gateway
  @gateway
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/auger/connection.rb', line 5

def options
  @options
end

#requestsObject

Returns the value of attribute requests.



5
6
7
# File 'lib/auger/connection.rb', line 5

def requests
  @requests
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/auger/connection.rb', line 5

def response
  @response
end

#roles(*names) ⇒ Object

Returns the value of attribute roles.



5
6
7
# File 'lib/auger/connection.rb', line 5

def roles
  @roles
end

Class Method Details

.load(port, &block) ⇒ Object



7
8
9
10
11
# File 'lib/auger/connection.rb', line 7

def self.load(port, &block)
  connection = new(port)
  connection.instance_eval(&block)
  connection
end

Instance Method Details

#timeout(secs) ⇒ Object

explicit method to override use of timeout.rb in modules



25
26
27
# File 'lib/auger/connection.rb', line 25

def timeout(secs)
  @options[:timeout] = secs
end

#try_close(conn) ⇒ Object

safe way to call plugin close() (rescue if the connection did not exist)



63
64
65
66
67
68
69
70
# File 'lib/auger/connection.rb', line 63

def try_close(conn)
  begin
    self.close(conn)
    @gateway.shutdown! if @gateway
  rescue => e
    e
  end
end

#try_open(server) ⇒ Object

setup options and call appropriate connection open()



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

def try_open(server)
  options = @options.merge(server.options) # merge connection and server options
  options[:tunnel] ? try_open_tunnel(server, options) : try_open_direct(server, options)
end

#try_open_direct(server, options) ⇒ Object

call plugin open() and return plugin-specific connection object, or exception



40
41
42
43
44
45
46
# File 'lib/auger/connection.rb', line 40

def try_open_direct(server, options)
  begin
    self.open(server.name, options)
  rescue => e
    e
  end
end

#try_open_tunnel(server, options) ⇒ Object

call plugin open() via an ssh tunnel



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/auger/connection.rb', line 49

def try_open_tunnel(server, options)
  host, user = options[:tunnel].split('@').reverse #for ssh to the gateway host
  user ||= ENV['USER']
  begin
    @gateway = Net::SSH::Gateway.new(host, user)
    gateway.open(server.name, options[:port]) do |port|
      self.open('127.0.0.1', options.merge({:port => port}))
    end
  rescue => e
    e
  end
end