Module: Gaptool::EC2
- Defined in:
- lib/helpers/ec2.rb,
lib/helpers/rehash.rb
Class Method Summary collapse
- .configure_ec2(zone) ⇒ Object
- .create_ec2_instance(ec2opts, data) ⇒ Object
- .get_ec2_instance_data(zone, id) ⇒ Object
- .get_or_create_securitygroup(role, environment, zone, groupname = nil) ⇒ Object
- .putkey(host) ⇒ Object
- .rehash ⇒ Object
- .terminate_ec2_instance(zone, id) ⇒ Object
Class Method Details
.configure_ec2(zone) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/helpers/ec2.rb', line 8 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) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/helpers/ec2.rb', line 48 def self.create_ec2_instance(ec2opts, data) if ENV['DRYRUN'] id = "i-test#{SecureRandom.hex(2)}" return {id: id, hostname: "test-#{id}.#{data[:zone].chop}.compute.amazonaws.com", launch_time: Time.now.to_s} end configure_ec2 data[:zone].chop ec2 = AWS::EC2.new instance = ec2.instances.create(ec2opts) instance.add_tag('Name', value: "#{data[:role]}-#{data[:env]}-#{instance.id}") instance.add_tag('gaptool', :value => "yes") launch_time = instance.launch_time.to_s launch_time = launch_time.empty? ? Time.now.to_s : launch_time { id: instance.id, hostname: instance.public_dns_name, launch_time: launch_time } end |
.get_ec2_instance_data(zone, id) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/helpers/ec2.rb', line 78 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/helpers/ec2.rb', line 32 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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/helpers/ec2.rb', line 16 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
6 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 |
# File 'lib/helpers/rehash.rb', line 6 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 } Gaptool::Data::addserver(iid, data, nil) end end return {"action" => "complete"} end |
.terminate_ec2_instance(zone, id) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/helpers/ec2.rb', line 70 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 |