Class: Capissh::ServerDefinition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/capissh/server_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a ServerDefinition with a string or object that describes the authority URI part, “user@host:port”, for connecting with SSH.

Any object that responds to the following methods, in order of priority, can be used in a ServerDefinition:

  1. #host, #user (optional), and #port (optional, default: 22)

  2. #authority - responding with something like “[user@]host

  3. #to_s - responding with something like “[user@]host

If options are passed for the second argument, certain keys will be used:

  • :user - sets the user if one was not given in the authority

  • :port - sets the port if one was not given in the authority

  • :ssh_options - used for connecting with Net::SSH



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/capissh/server_definition.rb', line 51

def initialize(server, options={})
  @server = server

  if @server.respond_to?(:host)
    @host = @server.host
    @port = @server.port if @server.respond_to?(:port)
    @user = @server.user if @server.respond_to?(:user)
  else
    if @server.respond_to?(:authority)
      string = @server.authority
    elsif @server.respond_to?(:to_s)
      string = @server.to_s
    else
      raise ArgumentError, "Invalid server for ServerDefinition: #{@server.inspect}. Must respond to #host, #authority, or #to_s"
    end
    @user, @host, @port = string.match(/^(?:([^;,:=]+)@|)(.*?)(?::(\d+)|)$/)[1,3]
  end

  @options = options.dup
  user_opt, port_opt = @options.delete(:user), @options.delete(:port)

  @user ||= user_opt
  @port ||= port_opt

  @port = @port.to_i if @port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



34
35
36
# File 'lib/capissh/server_definition.rb', line 34

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/capissh/server_definition.rb', line 34

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



34
35
36
# File 'lib/capissh/server_definition.rb', line 34

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



34
35
36
# File 'lib/capissh/server_definition.rb', line 34

def server
  @server
end

#userObject (readonly)

Returns the value of attribute user.



34
35
36
# File 'lib/capissh/server_definition.rb', line 34

def user
  @user
end

Class Method Details

.default_userObject

The default user name to use when a user name is not explicitly provided



30
31
32
# File 'lib/capissh/server_definition.rb', line 30

def self.default_user
  ENV['USER'] || ENV['USERNAME'] || "not-specified"
end

.wrap_list(*list) ⇒ Object

Turns a list, or something resembling a list, into a properly-formatted ServerDefinition list. Keep an eye on this one – it’s entirely too magical for its own good. In particular, if ServerDefinition ever inherits from Array, this will break.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capissh/server_definition.rb', line 14

def self.wrap_list(*list)
  options = list.last.is_a?(Hash) ? list.pop : {}
  if list.length == 1
    if list.first.nil?
      return []
    elsif list.first.is_a?(Array)
      list = list.first
    end
  end
  options.merge! list.pop if list.last.is_a?(Hash)
  list.map do |item|
    self.wrap_server item, options
  end
end

.wrap_server(item, options) ⇒ Object

Wraps a string in a ServerDefinition, if it isn’t already.



6
7
8
# File 'lib/capissh/server_definition.rb', line 6

def self.wrap_server(item, options)
  item.is_a?(ServerDefinition) ? item : ServerDefinition.new(item, options)
end

Instance Method Details

#<=>(server) ⇒ Object



78
79
80
# File 'lib/capissh/server_definition.rb', line 78

def <=>(server)
  [host, port, user] <=> [server.host, server.port, server.user]
end

#connect_to_portObject



98
99
100
# File 'lib/capissh/server_definition.rb', line 98

def connect_to_port
  port || 22
end

#eql?(server) ⇒ Boolean Also known as: ==

Redefined, so that Array#uniq will work to remove duplicate server definitions, based solely on their host names.

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/capissh/server_definition.rb', line 84

def eql?(server)
  host == server.host &&
    user == server.user &&
    port == server.port
end

#hashObject

Redefined, so that Array#uniq will work to remove duplicate server definitions, based on their connection information.



94
95
96
# File 'lib/capissh/server_definition.rb', line 94

def hash
  @hash ||= [host, user, port].hash
end

#to_sObject



102
103
104
105
106
107
108
109
# File 'lib/capissh/server_definition.rb', line 102

def to_s
  @to_s ||= begin
    s = host
    s = "#{user}@#{s}" if user
    s = "#{s}:#{port}" if port && port != 22
    s
  end
end