Class: EY::Serverside::Servers

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/engineyard-serverside/servers.rb

Defined Under Namespace

Classes: DuplicateHostname

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#to_json

Constructor Details

#initialize(servers) ⇒ Servers

Returns a new instance of Servers.



35
36
37
38
# File 'lib/engineyard-serverside/servers.rb', line 35

def initialize(servers)
  @servers = servers
  @cache = {}
end

Class Method Details

.from_hashes(server_hashes) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/engineyard-serverside/servers.rb', line 25

def self.from_hashes(server_hashes)
  servers = server_hashes.inject({}) do |memo, server_hash|
    server = Server.from_hash(server_hash)
    raise DuplicateHostname.new(server.hostname) if memo.key?(server.hostname)
    memo[server.hostname] = server
    memo
  end
  new(servers.values)
end

Instance Method Details

#==(other) ⇒ Object



22
# File 'lib/engineyard-serverside/servers.rb', line 22

def ==(other) other.respond_to?(:to_a) && other.to_a == to_a end

#localhostObject



40
41
42
# File 'lib/engineyard-serverside/servers.rb', line 40

def localhost
  @servers.find {|server| server.local? }
end

#reject(*a, &b) ⇒ Object



20
# File 'lib/engineyard-serverside/servers.rb', line 20

def reject(*a, &b) self.class.new @servers.reject(*a,&b) end

#remoteObject



44
45
46
# File 'lib/engineyard-serverside/servers.rb', line 44

def remote
  reject { |server| server.local? }
end

#roles(*select_roles, &block) ⇒ Object

We look up the same set of servers over and over. Cache them so we don’t have to find them every time Accepts a block (because it’s confusing when you send a block to this method and it doesn’t yield and it’s better than raising)



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/engineyard-serverside/servers.rb', line 52

def roles(*select_roles, &block)
  if block_given?
    return yield(roles(*select_roles))
  end

  roles_set = Set.new select_roles.flatten.compact.map{|r| r.to_sym}
  if roles_set.empty? || roles_set.include?(:all)
    self
  else
    @cache[roles_set] ||= select { |server| server.matches_roles?(roles_set) }
  end
end

#run(shell, cmd, &blk) ⇒ Object

Run a command on this set of servers.



66
67
68
# File 'lib/engineyard-serverside/servers.rb', line 66

def run(shell, cmd, &blk)
  run_on_servers(shell, 'sh -l -c', cmd, &blk)
end

#select(*a, &b) ⇒ Object



19
# File 'lib/engineyard-serverside/servers.rb', line 19

def select(*a, &b) self.class.new @servers.select(*a,&b) end

#sudo(shell, cmd, &blk) ⇒ Object

Run a sudo command on this set of servers.



71
72
73
# File 'lib/engineyard-serverside/servers.rb', line 71

def sudo(shell, cmd, &blk)
  run_on_servers(shell, 'sudo sh -l -c', cmd, &blk)
end

#to_aObject



21
# File 'lib/engineyard-serverside/servers.rb', line 21

def to_a() @servers end