-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws_interface.py
196 lines (145 loc) · 5.58 KB
/
aws_interface.py
1
2
3
4
5
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
125
126
127
128
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# Script the creation of various EC2 servers
import time
from fabric.api import *
from boto.ec2.connection import EC2Connection
sys.path.append('/etc')
from lightboxkeys import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
######################################
## Fill in this section
AWS_ZONE = "" #e.g. 'us-east-1a'
OWNER_ID = ""
DEFAULT_KEY_PAIR = ""
DEFAULT_SECURITY_GROUP = [""]
# Don't image servers that might be serving traffic. Also, they could be
# compromised.
DO_NOT_IMAGE = [] #machine name list
######################################
SIZE = {'small': 'm1.small',
}
RIGHTIMAGE_AMI_32 = "ami-a8f607c1"
RIGHTIMAGE_AMI_64 = "ami-aef607c7"
AMI = {'small': RIGHTIMAGE_AMI_32,
'large': RIGHTIMAGE_AMI_64,
}
conn = None
def image_server_by_name(server_name, no_reboot=False):
"""Snapshot a server, given name
"""
conn = connect_aws()
# Match the given name to an instance
reservations = conn.get_all_instances()
all_instances = [r.instances[0] for r in reservations]
all_instance_names = [get_instance_name(inst.tags) for inst in all_instances]
instance_dict = dict(zip(all_instance_names, all_instances))
try:
instance = instance_dict[server_name]
except KeyError:
print 'Server %s not found' % server_name
raise
image_server_by_id(instance.id, no_reboot)
def image_server_by_id(instance_id, no_reboot=False):
"""Snapshot a server, given instace_id
"""
conn = connect_aws()
instance = conn.get_all_instances([instance_id])[0].instances[0]
instance_name = get_instance_name(instance.tags)
if instance_name in DO_NOT_IMAGE:
print "%s should not be imaged" % instance_name
raise ValueError("Invalid image")
all_images = conn.get_all_images(owners=[OWNER_ID])
all_image_names = [image.name for image in all_images]
new_image_name = instance_name + time.strftime('-%Y-%m-%d')
if new_image_name in all_image_names:
count = 0
while new_image_name + str(count) in all_image_names:
count +=1
new_image_name = new_image_name + str(count)
image = conn.create_image(instance.id, new_image_name, no_reboot)
wait_for_aws(image, "pending")
return image
def build_web_server(type='m1.medium', name='web test'):
"""Build a standard webnode
"""
print "Creating a %s instance with name \"%s\"" % (type, name)
connect_aws()
web_server_settings = {
'ami_id': RIGHTIMAGE_AMI_64,
'zone': AWS_ZONE,
'security_groups': DEFAULT_SECURITY_GROUP,
'key_pair': DEFAULT_KEY_PAIR,
'instance_type': type,
'instance_name': name,
}
instance = create_instance(web_server_settings)
web_server_settings['instance_id'] = instance.id
web_server_settings['ip_address'] = instance.public_dns_name
print "Created Web Server %(instance_name)s with id %(instance_id)s at %(ip_address)s" % web_server_settings
def build_log_server(name='logs', size='small', create_new_volume=False):
"""Build a standard log server
"""
connect_aws()
ami_id = AMI[size]
instance_type = SIZE[size]
log_server_settings = {
'ami_id': ami_id,
'zone': AWS_ZONE,
'security_groups': DEFAULT_SECURITY_GROUP,
'key_pair': DEFAULT_KEY_PAIR,
'instance_type': instance_type,
'instance_name': name,
'volume_size': '1000',
'volume_name': 'logs',
'mount_point': '/dev/sdk',
}
instance = create_instance(log_server_settings)
instance.add_tag('Name', 'logs')
if create_new_volume:
add_volume(instance, log_server_settings)
print "Created Logging Server, id: %s at %s" % (instance.id,
instance.public_dns_name)
def connect_server(instance_id):
"""Connect to server with instance_id, set as fabric host
"""
conn = connect_aws()
instance = conn.get_instance(instance_id)
env.hosts = [instance.public_dns_name]
def create_instance(inst_settings):
"""Create an instance with given settings
"""
image_name = inst_settings['ami_id']
run_settings = {'placement': inst_settings['zone'],
'key_name': inst_settings['key_pair'],
'instance_type': inst_settings['instance_type'],
'security_groups': inst_settings['security_groups'],
}
reservation = conn.run_instances(image_name, **run_settings)
instance = reservation.instances[0]
instance.add_tag('Name', inst_settings['instance_name'])
wait_for_aws(instance, "pending")
return instance
def add_volume(instance, inst_settings):
"""Attach a volume to instance
"""
size = inst_settings['volume_size']
name = inst_settings['volume_name']
mount_point = inst_settings['mount_point']
zone = inst_settings['zone']
volume = conn.create_volume(size, zone)
volume.add_tag('Name', name)
wait_for_aws(volume, "creating")
volume.attach(instance.id, mount_point)
return volume
def connect_aws():
"""Cache connection to AWS
"""
global conn
if conn is None:
conn = EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
return conn
def wait_for_aws(volume, wait_on_status):
"""Poll AWS to change from wait_on_status
"""
while volume.update() == wait_on_status:
time.sleep(3)
get_instance_name = lambda i: None if not i.has_key('Name') else i['Name']