|
| 1 | +#!/usr/bin/env python |
| 2 | +# Script the creation of various EC2 servers |
| 3 | + |
| 4 | +import time |
| 5 | + |
| 6 | +from fabric.api import * |
| 7 | +from boto.ec2.connection import EC2Connection |
| 8 | + |
| 9 | +sys.path.append('/etc') |
| 10 | +from lightboxkeys import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY |
| 11 | + |
| 12 | +###################################### |
| 13 | +## Fill in this section |
| 14 | +AWS_ZONE = "" #e.g. 'us-east-1a' |
| 15 | +OWNER_ID = "" |
| 16 | +DEFAULT_KEY_PAIR = "" |
| 17 | +DEFAULT_SECURITY_GROUP = [""] |
| 18 | +# Don't image servers that might be serving traffic. Also, they could be |
| 19 | +# compromised. |
| 20 | +DO_NOT_IMAGE = [] #machine name list |
| 21 | + |
| 22 | +###################################### |
| 23 | + |
| 24 | +SIZE = {'small': 'm1.small', |
| 25 | + } |
| 26 | + |
| 27 | +RIGHTIMAGE_AMI_32 = "ami-a8f607c1" |
| 28 | +RIGHTIMAGE_AMI_64 = "ami-aef607c7" |
| 29 | +AMI = {'small': RIGHTIMAGE_AMI_32, |
| 30 | + 'large': RIGHTIMAGE_AMI_64, |
| 31 | + } |
| 32 | + |
| 33 | +conn = None |
| 34 | + |
| 35 | +def image_server_by_name(server_name, no_reboot=False): |
| 36 | + """Snapshot a server, given name |
| 37 | + """ |
| 38 | + |
| 39 | + conn = connect_aws() |
| 40 | + |
| 41 | + # Match the given name to an instance |
| 42 | + reservations = conn.get_all_instances() |
| 43 | + all_instances = [r.instances[0] for r in reservations] |
| 44 | + all_instance_names = [get_instance_name(inst.tags) for inst in all_instances] |
| 45 | + |
| 46 | + instance_dict = dict(zip(all_instance_names, all_instances)) |
| 47 | + |
| 48 | + try: |
| 49 | + instance = instance_dict[server_name] |
| 50 | + except KeyError: |
| 51 | + print 'Server %s not found' % server_name |
| 52 | + raise |
| 53 | + |
| 54 | + image_server_by_id(instance.id, no_reboot) |
| 55 | + |
| 56 | +def image_server_by_id(instance_id, no_reboot=False): |
| 57 | + """Snapshot a server, given instace_id |
| 58 | + """ |
| 59 | + |
| 60 | + conn = connect_aws() |
| 61 | + |
| 62 | + instance = conn.get_all_instances([instance_id])[0].instances[0] |
| 63 | + instance_name = get_instance_name(instance.tags) |
| 64 | + |
| 65 | + if instance_name in DO_NOT_IMAGE: |
| 66 | + print "%s should not be imaged" % instance_name |
| 67 | + raise ValueError("Invalid image") |
| 68 | + |
| 69 | + all_images = conn.get_all_images(owners=[OWNER_ID]) |
| 70 | + all_image_names = [image.name for image in all_images] |
| 71 | + |
| 72 | + new_image_name = instance_name + time.strftime('-%Y-%m-%d') |
| 73 | + |
| 74 | + if new_image_name in all_image_names: |
| 75 | + count = 0 |
| 76 | + while new_image_name + str(count) in all_image_names: |
| 77 | + count +=1 |
| 78 | + new_image_name = new_image_name + str(count) |
| 79 | + |
| 80 | + image = conn.create_image(instance.id, new_image_name, no_reboot) |
| 81 | + |
| 82 | + wait_for_aws(image, "pending") |
| 83 | + |
| 84 | + return image |
| 85 | + |
| 86 | +def build_web_server(type='m1.medium', name='web test'): |
| 87 | + """Build a standard webnode |
| 88 | + """ |
| 89 | + |
| 90 | + print "Creating a %s instance with name \"%s\"" % (type, name) |
| 91 | + |
| 92 | + connect_aws() |
| 93 | + |
| 94 | + web_server_settings = { |
| 95 | + 'ami_id': RIGHTIMAGE_AMI_64, |
| 96 | + 'zone': AWS_ZONE, |
| 97 | + 'security_groups': DEFAULT_SECURITY_GROUP, |
| 98 | + 'key_pair': DEFAULT_KEY_PAIR, |
| 99 | + 'instance_type': type, |
| 100 | + 'instance_name': name, |
| 101 | + } |
| 102 | + |
| 103 | + instance = create_instance(web_server_settings) |
| 104 | + |
| 105 | + web_server_settings['instance_id'] = instance.id |
| 106 | + web_server_settings['ip_address'] = instance.public_dns_name |
| 107 | + |
| 108 | + print "Created Web Server %(instance_name)s with id %(instance_id)s at %(ip_address)s" % web_server_settings |
| 109 | + |
| 110 | +def build_log_server(name='logs', size='small', create_new_volume=False): |
| 111 | + """Build a standard log server |
| 112 | + """ |
| 113 | + |
| 114 | + connect_aws() |
| 115 | + |
| 116 | + ami_id = AMI[size] |
| 117 | + instance_type = SIZE[size] |
| 118 | + |
| 119 | + log_server_settings = { |
| 120 | + 'ami_id': ami_id, |
| 121 | + 'zone': AWS_ZONE, |
| 122 | + 'security_groups': DEFAULT_SECURITY_GROUP, |
| 123 | + 'key_pair': DEFAULT_KEY_PAIR, |
| 124 | + 'instance_type': instance_type, |
| 125 | + 'instance_name': name, |
| 126 | + 'volume_size': '1000', |
| 127 | + 'volume_name': 'logs', |
| 128 | + 'mount_point': '/dev/sdk', |
| 129 | + } |
| 130 | + |
| 131 | + instance = create_instance(log_server_settings) |
| 132 | + instance.add_tag('Name', 'logs') |
| 133 | + if create_new_volume: |
| 134 | + add_volume(instance, log_server_settings) |
| 135 | + |
| 136 | + print "Created Logging Server, id: %s at %s" % (instance.id, |
| 137 | + instance.public_dns_name) |
| 138 | + |
| 139 | +def connect_server(instance_id): |
| 140 | + """Connect to server with instance_id, set as fabric host |
| 141 | + """ |
| 142 | + conn = connect_aws() |
| 143 | + instance = conn.get_instance(instance_id) |
| 144 | + env.hosts = [instance.public_dns_name] |
| 145 | + |
| 146 | +def create_instance(inst_settings): |
| 147 | + """Create an instance with given settings |
| 148 | + """ |
| 149 | + |
| 150 | + image_name = inst_settings['ami_id'] |
| 151 | + run_settings = {'placement': inst_settings['zone'], |
| 152 | + 'key_name': inst_settings['key_pair'], |
| 153 | + 'instance_type': inst_settings['instance_type'], |
| 154 | + 'security_groups': inst_settings['security_groups'], |
| 155 | + } |
| 156 | + |
| 157 | + reservation = conn.run_instances(image_name, **run_settings) |
| 158 | + instance = reservation.instances[0] |
| 159 | + instance.add_tag('Name', inst_settings['instance_name']) |
| 160 | + |
| 161 | + wait_for_aws(instance, "pending") |
| 162 | + |
| 163 | + return instance |
| 164 | + |
| 165 | +def add_volume(instance, inst_settings): |
| 166 | + """Attach a volume to instance |
| 167 | + """ |
| 168 | + |
| 169 | + size = inst_settings['volume_size'] |
| 170 | + name = inst_settings['volume_name'] |
| 171 | + mount_point = inst_settings['mount_point'] |
| 172 | + zone = inst_settings['zone'] |
| 173 | + |
| 174 | + volume = conn.create_volume(size, zone) |
| 175 | + volume.add_tag('Name', name) |
| 176 | + wait_for_aws(volume, "creating") |
| 177 | + volume.attach(instance.id, mount_point) |
| 178 | + |
| 179 | + return volume |
| 180 | + |
| 181 | +def connect_aws(): |
| 182 | + """Cache connection to AWS |
| 183 | + """ |
| 184 | + global conn |
| 185 | + if conn is None: |
| 186 | + conn = EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) |
| 187 | + return conn |
| 188 | + |
| 189 | +def wait_for_aws(volume, wait_on_status): |
| 190 | + """Poll AWS to change from wait_on_status |
| 191 | + """ |
| 192 | + while volume.update() == wait_on_status: |
| 193 | + time.sleep(3) |
| 194 | + |
| 195 | +get_instance_name = lambda i: None if not i.has_key('Name') else i['Name'] |
| 196 | + |
0 commit comments