Class: Luban::Deployment::Configuration::ServerSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/luban/deployment/configuration/server_set.rb

Instance Method Summary collapse

Constructor Details

#initializeServerSet

Returns a new instance of ServerSet.



7
8
9
# File 'lib/luban/deployment/configuration/server_set.rb', line 7

def initialize
  @servers = { :all => [] } 
end

Instance Method Details

#add_host(host, **properties) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/luban/deployment/configuration/server_set.rb', line 27

def add_host(host, **properties)
  new_server = Server.create(host, properties)
  if server = has_server?(new_server)
    server.user = new_server.user unless new_server.user.nil?
    server.port = new_server.port unless new_server.port.nil?
    server.add_properties(properties)
    server
  else
    all << new_server
    new_server
  end.tap do |s|
    s.roles.each do |role| 
      @servers[role] ||= []
      @servers[role] << s unless @servers[role].include?(s)
    end
  end
end

#add_hosts_for_role(role, hosts, **properties) ⇒ Object



45
46
47
48
# File 'lib/luban/deployment/configuration/server_set.rb', line 45

def add_hosts_for_role(role, hosts, **properties)
  properties_deepcopy = Marshal.dump(properties.merge(:roles => Array(role)))
  Array(hosts).each { |host| add_host(host, Marshal.load(properties_deepcopy)) }
end

#allObject



11
12
13
# File 'lib/luban/deployment/configuration/server_set.rb', line 11

def all
  @servers[:all]
end

#available_rolesObject



15
16
17
# File 'lib/luban/deployment/configuration/server_set.rb', line 15

def available_roles
  @servers.keys - [:all]
end

#eachObject



71
72
73
# File 'lib/luban/deployment/configuration/server_set.rb', line 71

def each
  all.each { |server| yield server }
end

#find_by_role(role, **opts) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/luban/deployment/configuration/server_set.rb', line 56

def find_by_role(role, **opts)
  if @servers.has_key?(role)
    opts.inject(@servers[role]) do |_servers, (action, key)|
      send(action, _servers, key)
    end
  else
    []
  end
end

#find_by_roles(roles, **opts) ⇒ Object



50
51
52
53
54
# File 'lib/luban/deployment/configuration/server_set.rb', line 50

def find_by_roles(roles, **opts)
  roles.inject([]) do |result, role|
    result.concat(find_by_role(role, opts))
  end
end

#find_primary(role) ⇒ Object



66
67
68
69
# File 'lib/luban/deployment/configuration/server_set.rb', line 66

def find_primary(role)
  _servers = find_by_role(role)
  _servers.find(&:primary?) || _servers.first
end

#has_server?(server) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/luban/deployment/configuration/server_set.rb', line 19

def has_server?(server)
  all.find do |s|
    s.user == server.user and
    s.hostname == server.hostname and
    s.port == server.port
  end
end