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

Constructor Details

#initialize(servers, shell) ⇒ Servers

Returns a new instance of Servers.


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

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

Class Method Details

.from_hashes(server_hashes, shell) ⇒ Object


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

def self.from_hashes(server_hashes, shell)
  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, shell)
end

Instance Method Details

#==(other) ⇒ Object


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

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

#in_groups(number) ⇒ Object


50
51
52
53
54
55
56
57
58
# File 'lib/engineyard-serverside/servers.rb', line 50

def in_groups(number)
  div, mod = size.divmod number
  start = 0
  number.times do |index|
    length = div + (mod > 0 && mod > index ? 1 : 0)
    yield self.class.new(@servers.slice(start, length), @shell)
    start += length
  end
end

#localhostObject


42
43
44
# File 'lib/engineyard-serverside/servers.rb', line 42

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

#reject(*a, &b) ⇒ Object


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

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

#remoteObject


46
47
48
# File 'lib/engineyard-serverside/servers.rb', line 46

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)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/engineyard-serverside/servers.rb', line 64

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_for_each(&block) ⇒ Object


106
107
108
109
110
# File 'lib/engineyard-serverside/servers.rb', line 106

def run_for_each(&block)
  spawner = Spawner.new
  each { |server| spawner.add(block.call(server), @shell, server) }
  spawner.run
end

#run_for_each!(&block) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/engineyard-serverside/servers.rb', line 112

def run_for_each!(&block)
  failures = run_for_each(&block).reject {|result| result.success? }

  if failures.any?
    commands = failures.map { |f| f.command }.uniq
    servers = failures.map { |f| f.server }.compact.map { |s| s.inspect }
    outputs = failures.map { |f| f.output }.uniq
    message = "The following command#{commands.size == 1 ? '' : 's'} failed"
    if servers.any?
      message << " on server#{servers.size == 1 ? '' : 's'} [#{servers.join(', ')}]"
    end
    message << "\n\n"
    commands.each do |cmd|
      message << "$ #{cmd}\n"
    end
    message << "\n" << outputs.join("\n\n") << "\n"
    raise EY::Serverside::RemoteFailure.new(message)
  end
end

#run_on_each(cmd, &block) ⇒ Object


77
78
79
80
81
# File 'lib/engineyard-serverside/servers.rb', line 77

def run_on_each(cmd, &block)
  run_for_each do |server|
    server.command_on_server('sh -l -c', cmd, &block)
  end
end

#run_on_each!(cmd, &block) ⇒ Object Also known as: run

Run a command on this set of servers.


84
85
86
87
88
# File 'lib/engineyard-serverside/servers.rb', line 84

def run_on_each!(cmd, &block)
  run_for_each! do |server|
    server.command_on_server('sh -l -c', cmd, &block)
  end
end

#select(*a, &b) ⇒ Object


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

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

#sudo_on_each(cmd, &block) ⇒ Object

Run a sudo command on this set of servers.


92
93
94
95
96
# File 'lib/engineyard-serverside/servers.rb', line 92

def sudo_on_each(cmd, &block)
  run_for_each do |server|
    server.command_on_server('sudo sh -l -c', cmd, &block)
  end
end

#sudo_on_each!(cmd, &block) ⇒ Object Also known as: sudo

Run a sudo command on this set of servers.


99
100
101
102
103
# File 'lib/engineyard-serverside/servers.rb', line 99

def sudo_on_each!(cmd, &block)
  run_for_each! do |server|
    server.command_on_server('sudo sh -l -c', cmd, &block)
  end
end

#to_aObject


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

def to_a() @servers end