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

Create a new ServerDefinition.

Parameters:

  • host (String)

    Name of server.

  • options (Hash) (defaults to: {})

    Override the server’s own configuration.

Options Hash (options):

  • port (Integer) — default: 22

    Server port

  • user (String)

    SSH user

  • password (String)

    SSH password



12
13
14
15
16
17
18
# File 'lib/simple_gate/server_definition.rb', line 12

def initialize(host, options = {})
  self.host = host
  self.user = options[:user]
  self.password = options[:password]
  self.port = options[:port] || 22
  self.auth_methods = options[:auth_methods] || %w[password keyboard-interactive]
end

Class Attribute Details

.serversHash

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.

Returns:

  • (Hash)

    Server name to server configuration pairs.



95
96
97
# File 'lib/simple_gate/server_definition.rb', line 95

def servers
  @servers
end

Instance Attribute Details

#auth_methodsObject

Returns the value of attribute auth_methods.



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

def auth_methods
  @auth_methods
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
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) ⇒ ServerDefinition

Factory method that chooses between a lookup and parse.

Parameters:

  • server (String)

    Server name or ssh string

Returns:

See Also:



63
64
65
# File 'lib/simple_gate/server_definition.rb', line 63

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

.lookup(server) ⇒ ServerDefinition

Factory method that uses pre-defined server configurations.

Returns:



49
50
51
52
53
54
55
56
# File 'lib/simple_gate/server_definition.rb', line 49

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

.parse(ssh_string) ⇒ ServerDefinition

Factory method that parses a connection string.

Parameters:

  • ssh_string (String)

    String formatted as “user:password@host:port”

Returns:



70
71
72
73
# File 'lib/simple_gate/server_definition.rb', line 70

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 {|host, user, options| ... } ⇒ Object

Yield connection information.

Yield Parameters:

  • host (String)

    SSH host

  • user (String)

    SSH user

  • options (Hash)

    SSH connection options

See Also:



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

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

#optionsHash

SSH options

Returns:

  • (Hash)


22
23
24
25
26
27
28
29
# File 'lib/simple_gate/server_definition.rb', line 22

def options
  {
    :user => user,
    :password => password,
    :port => port,
    :auth_methods => auth_methods
  }
end

#to_sString

Represent server definition as URL-like string

Returns:

  • (String)


43
44
45
# File 'lib/simple_gate/server_definition.rb', line 43

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