Module: BigBang::ExplodeCmd
- Included in:
- Universe
- Defined in:
- lib/bigbang/explode.rb
Instance Method Summary collapse
- #allocate_addresses ⇒ Object
- #assign_addresses(addrs) ⇒ Object
- #create_dns_entries(universe_name) ⇒ Object
- #create_dns_entry_for(instance, universe_name, domain, addr, wildcard) ⇒ Object
- #explode(name) ⇒ Object
- #run_instance(instance, userdata, zone, size) ⇒ Object
- #run_instances(name) ⇒ Object
- #tag_instance(instance, ec2_instance, name) ⇒ Object
- #wait_for_eips(expected) ⇒ Object
- #wait_for_running(instances) ⇒ Object
Instance Method Details
#allocate_addresses ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bigbang/explode.rb', line 17 def allocate_addresses runs = @runs.find_all { |r| r.elastic_ip == true } if runs.empty? puts "no need to allocate elastic ips" return [] end free_ips = provider.free_eips ninstances = runs.inject(0) { |m,r| m += r.instances_count } toalloc = ninstances blacklist = free_ips if free_ips.size > 0 n = nil if free_ips.size >= ninstances n = ninstances else n = free_ips.size end confirm("Use #{n} of your #{free_ips.size} free eips?") do blacklist = [] toalloc -= n end end puts "need to alloc: #{toalloc}" 1.upto(toalloc) do |i| puts "allocating eip address #{i}" provider.ec2.allocate_address end addrs = wait_for_eips(free_ips.size + toalloc) avail_ips = addrs.collect { |a| a.publicIp }.to_set black_ips = blacklist.collect { |a| a.publicIp }.to_set (avail_ips - black_ips).to_a[0,ninstances] end |
#assign_addresses(addrs) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bigbang/explode.rb', line 106 def assign_addresses(addrs) @runs.each do |r| r.ec2_instances.each do |instance| addr = addrs.pop puts "associating address #{addr} " + "to instance #{instance.instanceId}" provider.ec2.associate_address( :instance_id => instance.instanceId, :public_ip => addr) r.assigned_ips[instance.instanceId] = addr end end end |
#create_dns_entries(universe_name) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/bigbang/explode.rb', line 129 def create_dns_entries(universe_name) instances_map = get_instances_map @runs.each do |r| addsufix = false if r.is_a?(ClusterRun) addsufix = true end domains = [r.domain] if r.domain.is_a?(Array) domains = r.domain end sufix = 0 r.ec2_instances.each do |instance_id| instance = instances_map[instance_id] domains.each do |domain| addr = r.assigned_ips[instance.instanceId] # use the elastic ip if addr.nil? addr = instance.ipAddress end if addsufix domain = "#{domain}#{sufix}" end create_dns_entry_for(instance, universe_name, domain, addr, r.wildcard_domain) end sufix += 1 end end end |
#create_dns_entry_for(instance, universe_name, domain, addr, wildcard) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/bigbang/explode.rb', line 120 def create_dns_entry_for(instance, universe_name, domain, addr, wildcard) domain = "#{universe_name}.#{domain}" puts "creating domain #{domain}.#{@config.domain} to #{addr}" provider.create_dns(domain, addr) if wildcard == true provider.create_dns("*.#{domain}", addr) end end |
#explode(name) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bigbang/explode.rb', line 162 def explode(name) free_eips = allocate_addresses run_instances(name) unless free_eips.empty? confirm("Will assign the following ips:\n" + "#{free_eips.join("\n")}\nConfirm") do assign_addresses(free_eips) end end create_dns_entries(name) end |
#run_instance(instance, userdata, zone, size) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bigbang/explode.rb', line 73 def run_instance(instance, userdata, zone, size) puts "launching #{size} instance(s) on availability zone '#{zone}'" provider.ec2.run_instances( :image_id => instance.ami, :key_name => instance.key_name, :instance_type => instance.type, :user_data => userdata, :availability_zone => zone, :min_count => size, :max_count => size ) end |
#run_instances(name) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bigbang/explode.rb', line 86 def run_instances(name) instances = [] gen = Ec2BootstrapGenerator.new @runs.each do |r| instance = r.instance userdata = Base64.encode64(gen.generate_from_hash( "bootstrap-repo" => instance.bootstrap_repo )) r.zones.each_pair do |zone,size| res = run_instance(instance, userdata, zone, size) res.instancesSet.item.each do |i| instances << i r.ec2_instances << i.instanceId tag_instance(instance, i, name) end end end wait_for_running(instances) end |
#tag_instance(instance, ec2_instance, name) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/bigbang/explode.rb', line 65 def tag_instance(instance, ec2_instance, name) provider.ec2.(:resource_id => ec2_instance.instanceId, :tag => [ { 'bb_name' => instance.name }, { 'bb_universe' => name}, ]) end |
#wait_for_eips(expected) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/bigbang/explode.rb', line 3 def wait_for_eips(expected) print "waiting for eips to be allocated" STDOUT.flush addrs = provider.free_eips while(addrs.size < expected) sleep(1) print "." STDOUT.flush addrs = provider.free_eips end puts addrs end |
#wait_for_running(instances) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bigbang/explode.rb', line 53 def wait_for_running(instances) ids = instances.collect { |i| i.instanceId }.to_set print "Waiting for all instances to be in running state. " STDOUT.flush while running_instances_count(ids) < instances.size print "." sleep(5) STDOUT.flush end puts end |