Class: BigBang::Universe

Inherits:
Object
  • Object
show all
Includes:
ExplodeCmd, KillCmd, TestCmd
Defined in:
lib/bigbang/universe.rb

Instance Method Summary collapse

Methods included from ExplodeCmd

#allocate_addresses, #assign_addresses, #create_dns_entries, #create_lb, #create_load_balancers, #dns_entry_for, #elb_dns_entries, #explode, #instance_dns_entries, #run_instance, #run_instances, #tag_instance, #wait_for_eips, #wait_for_running

Methods included from TestCmd

#test, #test_amis, #test_availability_zones, #test_dns

Methods included from KillCmd

#kill, #kill_dns_entry, #kill_eip, #kill_elb, #kill_instance

Constructor Details

#initialize(dsl) ⇒ Universe

Returns a new instance of Universe.



15
16
17
18
19
# File 'lib/bigbang/universe.rb', line 15

def initialize(dsl)
  @instances = dsl.instances
  @runs = dsl.runs
  @config = dsl.conf
end

Instance Method Details

#configured_elbsObject



55
56
57
58
59
60
61
62
63
# File 'lib/bigbang/universe.rb', line 55

def configured_elbs
  lbs = []
  @runs.each do |r|
    next unless r.is_a?(ClusterRun)
    next if r.lb.nil?
    lbs << r.lb
  end
  lbs
end

#confirm(msg) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/bigbang/universe.rb', line 107

def confirm(msg)
  print "#{msg}? [y/N] "
  if STDIN.gets.strip == "y"
    yield
    return true
  else
    puts "skipping"
    return false
  end
end

#get_addressesObject



65
66
67
68
69
# File 'lib/bigbang/universe.rb', line 65

def get_addresses
  aset = provider.ec2.describe_addresses.addressesSet
  return [] if aset.nil?
  aset.item
end

#get_availability_zonesObject



26
27
28
29
30
# File 'lib/bigbang/universe.rb', line 26

def get_availability_zones
  item = provider.ec2.describe_availability_zones.availabilityZoneInfo.item
  item = [] if item.nil?
  item
end

#get_instancesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bigbang/universe.rb', line 32

def get_instances
  rset = provider.ec2.describe_instances.reservationSet
  if rset.nil?
    return []
  else
    instances = []
    rset.item.each do |reservation|
      reservation.instancesSet.item.each do |i|
        instances << i
      end
    end
    return instances
  end
end

#get_instances_mapObject



47
48
49
50
51
52
53
# File 'lib/bigbang/universe.rb', line 47

def get_instances_map
  map = {}
  get_instances.each do |i|
    map[i.instanceId] = i
  end
  map
end

#get_tagsObject



79
80
81
82
83
84
85
86
87
# File 'lib/bigbang/universe.rb', line 79

def get_tags
  tag_set = provider.ec2.describe_tags(:filter => 
      [{'key' => 'bb_universe'}]).tagSet
  if tag_set.nil?
    return []
  end

  tag_set.item
end

#instance_tags(tags = nil) ⇒ Object



131
132
133
134
# File 'lib/bigbang/universe.rb', line 131

def instance_tags(tags = nil)
  tags = get_tags if tags.nil?
  tags.find_all { |t| t.resourceType == 'instance' }
end

#listObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bigbang/universe.rb', line 89

def list
  universes = Set.new
  get_tags.each do |tag|
    universes << tag.value
  end

  running = running_instances

  universes.each do |u|
    instances = universe_running_instances(running, universe_tags(u))
    if instances.empty?
      puts "#{u} (defunct)"
    else
      puts "#{u} (#{instances.size} running instances)"
    end
  end
end

#notify(msg) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bigbang/universe.rb', line 118

def notify(msg)
  print "#{msg}: "
  STDOUT.flush
  begin
    r = yield
    puts "\033[01;32mOK\033[00m"
    return r
  rescue => e
    puts "\033[01;31mERROR"
    puts "#{e.to_s}\033[00m"
  end
end

#providerObject



21
22
23
24
# File 'lib/bigbang/universe.rb', line 21

def provider
  return @provider unless @provider.nil?
  @provider = Provider.new(@config)
end

#running_instancesObject



71
72
73
# File 'lib/bigbang/universe.rb', line 71

def running_instances
  get_instances.find_all { |i| i.instanceState.name == "running" }
end

#running_instances_count(ids) ⇒ Object



75
76
77
# File 'lib/bigbang/universe.rb', line 75

def running_instances_count(ids)
  running_instances.count { |i| ids.include?(i.instanceId) }
end

#universe_running_instances(running, universe_tags) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/bigbang/universe.rb', line 136

def universe_running_instances(running, universe_tags)
  res = []
  instance_tags(universe_tags).each do |t|
    i = running.find { |i| i.instanceId == t.resourceId }
    res << i unless i.nil?
  end

  res
end

#universe_tags(name) ⇒ Object



146
147
148
149
150
# File 'lib/bigbang/universe.rb', line 146

def universe_tags(name)
  get_tags.find_all do |tag|
    tag.value == name
  end
end