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_dns_entry_for, #explode, #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_instance

Constructor Details

#initialize(dsl) ⇒ Universe

Returns a new instance of Universe.



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

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

Instance Method Details

#confirm(msg) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/bigbang/universe.rb', line 98

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

#get_addressesObject



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

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

#get_availability_zonesObject



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

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

#get_instancesObject



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

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



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

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

#get_tagsObject



70
71
72
73
74
75
76
77
78
# File 'lib/bigbang/universe.rb', line 70

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



109
110
111
112
# File 'lib/bigbang/universe.rb', line 109

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

#listObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bigbang/universe.rb', line 80

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

#providerObject



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

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

#running_instancesObject



62
63
64
# File 'lib/bigbang/universe.rb', line 62

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

#running_instances_count(ids) ⇒ Object



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

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

#universe_running_instances(running, universe_tags) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/bigbang/universe.rb', line 114

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



124
125
126
127
128
# File 'lib/bigbang/universe.rb', line 124

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