Module: Gaptool::EC2
- Defined in:
- lib/helpers/ec2.rb,
lib/helpers/rehash.rb
Constant Summary collapse
- @@logger =
Logger.new(STDERR)
Class Method Summary collapse
- .configure_ec2(zone) ⇒ Object
- .create_ec2_instance(ec2opts, data, retries = 3, sleeptime = 0.5) ⇒ Object
- .get_ec2_instance_data(zone, id) ⇒ Object
- .get_or_create_securitygroup(role, environment, zone, groupname = nil) ⇒ Object
- .putkey(host) ⇒ Object
- .rehash ⇒ Object
- .rehash_properties_for_instance(instance_id) ⇒ Object
- .rehash_property(property) ⇒ Object
- .rehash_property_for_instance(property, instance_id, save = true) ⇒ Object
- .retag ⇒ Object
- .tag_ec2_instance(instance, key, value, retries = 5, sleeptime = 0.5) ⇒ Object
- .terminate_ec2_instance(zone, id) ⇒ Object
Class Method Details
.configure_ec2(zone) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/helpers/ec2.rb', line 11 def self.configure_ec2 zone return if ENV['DRYRUN'] id = ENV['AWS_ACCESS_KEY_ID'] secret = ENV['AWS_SECRET_ACCESS_KEY'] AWS.config(access_key_id: id, secret_access_key: secret, ec2_endpoint: "ec2.#{zone}.amazonaws.com") end |
.create_ec2_instance(ec2opts, data, retries = 3, sleeptime = 0.5) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/helpers/ec2.rb', line 51 def self.create_ec2_instance(ec2opts, data, retries=3, sleeptime=0.5) if ENV['DRYRUN'] id = "i-test#{SecureRandom.hex(2)}" return {id: id, hostname: "test-#{id}.#{data[:zone].chop}.compute.amazonaws.com", instance: nil, launch_time: Time.now.to_i} end configure_ec2 data[:zone].chop ec2 = AWS::EC2.new i = 0 begin instance = ec2.instances.create(ec2opts) @@logger.debug("Spawned instance #{instance.id}") rescue => e i += 1 raise if i > retries @@logger.error("Error while creating instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})") sleep sleeptime retry end i = 0 begin hostname = instance.public_dns_name rescue => e i += 1 if i > retries @@logger.error("Could not get hostname for instance #{instance} after #{retries} retries, setting to nil") hostname = nil Airbrake.notify_or_ignore( e, error_class: "EC2 public dns fail", parameters: {instance: instance.id, role: data['role'], environment: data['environment'], hostname: nil} ) else @@logger.error("Error getting hostname for instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})") sleep sleeptime retry end end i = 0 begin launch_time = instance.launch_time.to_i raise unless launch_time.is_a? Integer rescue => e i += 1 if i > retries @@logger.error("Cloud not get launch time for instance #{instance} after #{retries} retries. Setting to now()") launch_time = Time.now.to_i Airbrake.notify_or_ignore( e, error_class: "EC2 get launch_time fail", parameters: {instance: instance.id, role: data['role'], environment: data['environment'], hostname: nil} ) else @@logger.error("Error getting launch_time for instance: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})") sleep sleeptime retry end end @@logger.info {"Instance #{instance.id} launch_time: #{launch_time} #{instance.launch_time}"} { id: instance.id, instance: instance, hostname: hostname, launch_time: launch_time } end |
.get_ec2_instance_data(zone, id) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/helpers/ec2.rb', line 150 def self.get_ec2_instance_data(zone, id) if ENV['DRYRUN'] return { hostname: 'fake.hostname.gild.com' } end configure_ec2 zone ec2 = AWS::EC2.new instance = ec2.instances[id] return { hostname: instance.dns_name } end |
.get_or_create_securitygroup(role, environment, zone, groupname = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/helpers/ec2.rb', line 35 def self.get_or_create_securitygroup(role, environment, zone, groupname=nil) return "sg-test#{SecureRandom.hex(2)}" if ENV['DRYRUN'] configure_ec2 zone.chop ec2 = AWS::EC2.new groupname = groupname || "#{role}-#{environment}" ec2.security_groups.each do |group| if group.name == groupname return group.id end end internet = ['0.0.0.0/0'] sg = ec2.security_groups.create(groupname) sg. :tcp, 22, *internet return sg.id end |
.putkey(host) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/helpers/ec2.rb', line 19 def self.putkey(host) return "FAKEKEY", "FAKEPUB" if ENV['DRYRUN'] key = OpenSSL::PKey::RSA.new 2048 pubkey = "#{key.ssh_type} #{[key.to_blob].pack('m0')} GAPTOOL_GENERATED_KEY" ENV['SSH_AUTH_SOCK'] = '' Net::SSH.start(host, 'admin', :key_data => [$redis.hget('config', 'gaptoolkey')], :config => false, :keys_only => true, :paranoid => false) do |ssh| ssh.exec! "grep -v GAPTOOL_GENERATED_KEY ~/.ssh/authorized_keys > /tmp/pubkeys" ssh.exec! "echo #{pubkey} >> /tmp/pubkeys" ssh.exec! "mv /tmp/pubkeys ~/.ssh/authorized_keys" end return key.to_pem, pubkey end |
.rehash ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/helpers/rehash.rb', line 7 def self.rehash() Gaptool::Data::servers.each do |inst| Gaptool::Data::rmserver inst end roles = Gaptool::Data::roles Gaptool::Data::zones.each do |zone| Gaptool::EC2::configure_ec2 zone @ec2 = AWS::EC2.new ilist = [] @ec2.instances.each do |instance| if instance.['gaptool'] == 'yes' && instance.status == :running ilist << instance end end ilist.each do |instance| puts " - #{instance.['Name']}" role, environment, iid = instance.['Name'].split('-', 3) data = { "zone"=> instance.availability_zone, "role"=> role, "environment"=> environment, "hostname" => instance.public_dns_name, "launch_time" => instance.launch_time.to_s, "apps" => roles[role].to_s, "instance"=> instance.instance_id, "security_group" => instance.security_groups[0].name } Gaptool::Data::addserver(iid, data, nil) end end return {"action" => "complete"} end |
.rehash_properties_for_instance(instance_id) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/helpers/rehash.rb', line 46 def self.rehash_properties_for_instance(instance_id) res = {} %w[hostname itype security_group].each do |property| res[property] = self.rehash_property_for_instance(property, instance_id, false) end Gaptool::Data::set_server_data_attributes(instance_id, res) end |
.rehash_property(property) ⇒ Object
40 41 42 43 44 |
# File 'lib/helpers/rehash.rb', line 40 def self.rehash_property(property) Gaptool::Data::servers.each do |id| rehash_property_for_instance(property, id) end end |
.rehash_property_for_instance(property, instance_id, save = true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/helpers/rehash.rb', line 54 def self.rehash_property_for_instance(property, instance_id, save=true) logger = Logger.new(STDOUT) data = Gaptool::Data.get_server_data(instance_id) return false if data.nil? || data['zone'].nil? || data['zone'].empty? Gaptool::EC2::configure_ec2 data['zone'].chop ec2 = AWS::EC2.new(region: data['zone'].chop) logger.info("Updating #{property} for #{instance_id} in zone #{data['zone'].chop}") instance = ec2.instances[instance_id] return false if instance.nil? case property when "hostname" value = instance.public_dns_name when "itype" value = instance.instance_type when "security_group" value = instance.security_groups[0].name when "launch_time" value = instance.launch_time.to_i else return false end logger.info("Setting #{property} to #{value} for #{instance_id}") if save Gaptool::Data.set_server_data_attr(instance_id, property, value) else value end end |
.retag ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/helpers/ec2.rb', line 164 def self.retag() logger = Logger.new(STDOUT) Gaptool::Data::servers.each do |id| data = Gaptool::Data.get_server_data(id) next if data.nil? || data['zone'].nil? || data['zone'].empty? Gaptool::EC2::configure_ec2 data['zone'].chop ec2 = AWS::EC2.new(region: data['zone'].chop) instance = ec2.instances[id] next if instance..role && instance..environment logger.info("Retagging instance #{id} in zone #{data['zone'].chop}") instance..role = data['role'] instance..environment = data['environment'] end end |
.tag_ec2_instance(instance, key, value, retries = 5, sleeptime = 0.5) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/helpers/ec2.rb', line 126 def self.tag_ec2_instance(instance, key, value, retries=5, sleeptime=0.5) return true if ENV['DRYRUN'] i = 0 begin instance.add_tag(key, value: value) @@logger.debug("Added tag #{key}=#{value} to #{instance.id}") true rescue => e i += 1 raise if i > retries @@logger.error("Error adding tag #{key} to #{instance.id}: #{e}: sleeping #{sleeptime}s and retrying (#{i}/#{retries})") sleep sleeptime retry end end |
.terminate_ec2_instance(zone, id) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/helpers/ec2.rb', line 142 def self.terminate_ec2_instance(zone, id) return if ENV['DRYRUN'] configure_ec2 zone ec2 = AWS::EC2.new instance = ec2.instances[id] instance.terminate end |