Class: Awsborn::ServerCluster

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/awsborn/server_cluster.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name) ⇒ ServerCluster

Returns a new instance of ServerCluster.



34
35
36
37
38
39
40
# File 'lib/awsborn/server_cluster.rb', line 34

def initialize (klass, name)
  @klass = klass
  @name = name.to_s
  @instances = []
  @load_balancers = []
  self.class.clusters << self
end

Instance Attribute Details

#load_balancersObject

Returns the value of attribute load_balancers.



5
6
7
# File 'lib/awsborn/server_cluster.rb', line 5

def load_balancers
  @load_balancers
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/awsborn/server_cluster.rb', line 5

def name
  @name
end

Class Method Details

.build(klass, name, &block) ⇒ Object



7
8
9
10
11
# File 'lib/awsborn/server_cluster.rb', line 7

def self.build (klass, name, &block)
  cluster = new(klass, name)
  block.bind(cluster, 'cluster').call
  cluster
end

.cluster_for(instance) ⇒ Object



17
18
19
20
21
22
# File 'lib/awsborn/server_cluster.rb', line 17

def self.cluster_for (instance)
  clusters.each do |cluster|
    return cluster if cluster.detect { |i| i == instance }
  end
  nil
end

.clustersObject



13
14
15
# File 'lib/awsborn/server_cluster.rb', line 13

def self.clusters
  @clusters ||= []
end

.next_nameObject



24
25
26
27
28
29
30
31
32
# File 'lib/awsborn/server_cluster.rb', line 24

def self.next_name
  @next_name_counter ||= 1
  old_names = clusters.map { |c| c.name }
  begin
    next_name = "cluster#{@next_name_counter}"
    @next_name_counter += 1
  end while old_names.include?(next_name)
  next_name
end

Instance Method Details

#[](name) ⇒ Object



102
103
104
# File 'lib/awsborn/server_cluster.rb', line 102

def [] (name)
  @instances.detect { |i| i.name == name }
end

#delete_key_pair(instances) ⇒ Object



94
95
96
# File 'lib/awsborn/server_cluster.rb', line 94

def delete_key_pair (instances)
  instances.first.ec2.delete_key_pair(@key_pair)
end

#domain(*args) ⇒ Object



42
43
44
45
# File 'lib/awsborn/server_cluster.rb', line 42

def domain (*args)
  @domain = args.first unless args.empty?
  @domain
end

#each(&block) ⇒ Object



98
99
100
# File 'lib/awsborn/server_cluster.rb', line 98

def each (&block)
  @instances.each &block
end

#generate_key_pair(instances) ⇒ Object



90
91
92
# File 'lib/awsborn/server_cluster.rb', line 90

def generate_key_pair (instances)
  @key_pair = instances.first.ec2.generate_key_pair
end

#launch(names) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/awsborn/server_cluster.rb', line 58

def launch (names)
  requested = names.nil? ? @instances : @instances.select { |s| names.include?(s.name.to_s) }
  running, missing = requested.partition { |e| e.running? }
  refresh_running(running) if running.any?
  start_missing_instances(missing) if missing.any?
  all_running = @instances.select{ |i| i.running? }
  update_load_balancing(all_running) unless @load_balancers.empty?
end

#load_balancer(name, options = {}) ⇒ Object



47
48
49
50
# File 'lib/awsborn/server_cluster.rb', line 47

def load_balancer (name, options={})
  options = add_domain_to_dns_alias(options)
  @load_balancers << Awsborn::LoadBalancer.new(name, options)
end

#load_balancer_infoObject



83
84
85
86
87
88
# File 'lib/awsborn/server_cluster.rb', line 83

def load_balancer_info
  info = load_balancers.map do |lb|
    lb.dns_info
  end.compact
  info.empty? ? nil : info
end

#refresh_running(instances) ⇒ Object



67
68
69
# File 'lib/awsborn/server_cluster.rb', line 67

def refresh_running (instances)
  instances.each { |e| e.refresh }
end

#server(name, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/awsborn/server_cluster.rb', line 52

def server (name, options = {})
  options = add_domain_to_ip(options)
  instance = @klass.new name, options
  @instances << instance
end

#start_missing_instances(instances) ⇒ Object



71
72
73
74
75
# File 'lib/awsborn/server_cluster.rb', line 71

def start_missing_instances (instances)
  generate_key_pair(instances)
  instances.each { |e| e.start(@key_pair) }
  delete_key_pair(instances)
end

#update_load_balancing(running) ⇒ Object



77
78
79
80
81
# File 'lib/awsborn/server_cluster.rb', line 77

def update_load_balancing(running)
  @load_balancers.each do |lb|
    lb.launch_or_update(running)
  end
end