Class: ServerDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_gate/server_definition.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ ServerDefinition

Returns a new instance of ServerDefinition.



5
6
7
8
9
10
11
# File 'lib/simple_gate/server_definition.rb', line 5

def initialize(host, options = {})
  @options = {:port => 22}.merge(options)
  self.host = host
  self.user = @options[:user]
  self.password = @options[:password]
  self.port = @options[:port]
end

Class Attribute Details

.serversObject

Access the pre-configured servers. ~/.servers.yml is parsed for this. An example entry for the servers ‘foobar’ and ‘barfoo’ would look like:

---
foobar:
  address: "127.0.0.1"
  username: "foo"
  password: "bar
  port: 22
barfoo:
  address: "192.168.0.1"
  username: "bar"
  password: "foo
  port: 22

Since the parsed Hash of servers is cached, a value can be stored and the configuration file ignored if desired.



79
80
81
# File 'lib/simple_gate/server_definition.rb', line 79

def servers
  @servers
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/simple_gate/server_definition.rb', line 3

def host
  @host
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/simple_gate/server_definition.rb', line 3

def options
  @options
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/simple_gate/server_definition.rb', line 3

def password
  @password
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/simple_gate/server_definition.rb', line 3

def port
  @port
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/simple_gate/server_definition.rb', line 3

def user
  @user
end

Class Method Details

.find(server) ⇒ Object



51
52
53
# File 'lib/simple_gate/server_definition.rb', line 51

def self.find(server)
  servers.has_key?(server) ? lookup(server) : parse(server)
end

.lookup(server) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/simple_gate/server_definition.rb', line 42

def self.lookup(server)
  server = servers[server]
  new(server['address'],{ 
    :user => server['username'],
    :password => server['password'], 
    :port => server['port']
  })
end

.parse(ssh_string) ⇒ Object



55
56
57
58
# File 'lib/simple_gate/server_definition.rb', line 55

def self.parse(ssh_string)
  user, password, host, port = ssh_string.match /\A(.*?):(.*?)@(.*?):(\d*?)\Z/
  new(host, :user => user, :password => password, :port => port)
end

Instance Method Details

#connection_info(&block) ⇒ Object



28
29
30
# File 'lib/simple_gate/server_definition.rb', line 28

def connection_info(&block)
  block.call(host, user, options.merge(ssh_options))
end

#ssh_optionsObject



32
33
34
35
36
# File 'lib/simple_gate/server_definition.rb', line 32

def ssh_options
  {
    :auth_methods => %w[password keyboard-interactive]
  }
end

#to_sObject



38
39
40
# File 'lib/simple_gate/server_definition.rb', line 38

def to_s
  "#{user}:#{'*' * password.to_s.size}@#{host}:#{port}"
end