-
Notifications
You must be signed in to change notification settings - Fork 28
/
setup_all_amis.py
397 lines (322 loc) · 14.9 KB
/
setup_all_amis.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
#
# This script will start up a bunch of AMIs in different regions, download the learning locker build script on to each one, run it and create an AMI with the given name
#
# USAGE:
# python3 setup_all_amis.py -r REGION_LIST -n NAME -d DESCRIPTION -b WEBAPP_BRANCH -x XAPI_BRANCH -k AWS_KEY -s AWS_SECRET -a AWS_ACCOUNT_ID
# python3 setup_all_amis.py -r us-west-1,us-east-1,eu-west-1 -n "Learning Locker 2.5.0 from HT2 Labs" -d "Learning Locker 2.5.0 from HT2 Labs" -b v2.5.0 -x v2.2.9 -k abc -s bcd/hyt -a 000836383
#
# TODO
# validate key / sg exists
# move validate_region() data to a file
#
# REQUIREMENTS
# pip3 install paramiko
# pip3 install argparse
# pip3 install boto3
# pip3 install ubuntufinder
import os
import sys
import time
import argparse
import paramiko
import boto3
from botocore.exceptions import ClientError
from multiprocessing import Process
import ubuntufinder
# List of AWS supported regions
# us-east-1 : N. Virginia
# us-east-2 : Ohio
# us-west-1 : N. California
# us-west-2 : Oregon
# ca-central-1 : Canada Central
# eu-west-1 : Ireland
# eu-central-1 : Frankfurt
# eu-west-2 : London
# ap-southeast-1 : Singapore
# ap-southeast-2 : Sydney
# ap-northeast-2 : Seoul
# ap-northeast-1 : Tokyo
# ap-south-1 : Mumbai
# sa-east-1 : Sao Paulo
#
# List of AWS supported regions in script-ready format
# us-east-1,us-east-2,us-west-1,us-west-2,ca-central-1,eu-west-1,eu-central-1,eu-west-2,ap-southeast-1,ap-southeast-2,ap-northeast-2,ap-northeast-1,ap-south-1,sa-east-1
#
# Supported Ubuntu distros: 16.04, 17.04
################################################################
# FUNCTIONS
################################################################
def get_region_list (ec2):
# Retrieves all regions/endpoints that work with EC2
response = ec2.describe_regions()
regionList = []
for i in response['Regions']:
regionList.append(i['RegionName'])
return regionList
def validate_region (region, distro_version):
# ami ids
ami_id = False
try:
# hard-setting to ebs and amd64 as that's all we need
image = ubuntufinder.find_image(region, distro_version, "amd64", "ebs-ssd", "hvm")
ami_id = image.ami_id
except:
print("couldn't find a suitable AMI id for distro:" + distro_version + " region:" + region)
return
if ami_id == False:
print("FATAL: couldn't find ami_id for ubuntu " + distro_version + " in " + region + " - maybe this script needs updating with new starting AMIs")
sys.exit()
# security group and keypair - tried to create the same everywhere but feel free to override if needed
secgroup = "ll-build" # default SG
kp = "llbuild-" + region # default KP name
# output
#print("region: " + region + "; ami_id: " + ami_id + "; SG: " + secgroup + "; KP: " + kp)
return {"kp":kp, "sg": secgroup, "ami_id": ami_id, "region": region}
def run_on_server (hostname, region, port, username, keyfile, name, desc):
global account_id
global aws_key
global aws_secret
global main_branch
global xapi_branch
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#client.connect(hostname, port=port, username=username, password=password)
client.connect(hostname, port=port, username=username, password="", pkey=None, key_filename=keyfile)
print(hostname + " Connected to " + hostname + " in " + region)
print(hostname + " running apt-get update")
stdin, stdout, stderr = client.exec_command("sudo apt-get -y -q update")
chk = stdout.read()
print(hostname + " running dist-upgrade")
stdin, stdout, stderr = client.exec_command("sudo DEBIAN_FRONTEND=noninteractive apt-get --allow-downgrades --allow-remove-essential --allow-change-held-packages -o Dpkg::Options::='--force-confnew' -fuyq dist-upgrade")
chk = stdout.read()
print(hostname + " installing curl & wget")
stdin, stdout, stderr = client.exec_command("sudo apt-get -qq -y install curl wget")
chk = stdout.read()
print(hostname + " getting & running deploy script")
stdin, stdout, stderr = client.exec_command("curl -o- -L https://raw.githubusercontent.com/LearningLocker/deploy/master/deployll.sh > deployll.sh && sudo bash deployll.sh -y 5 " + main_branch + " " + xapi_branch)
chk = stdout.read()
print(hostname + " running AMI build script")
#print(hostname + "sudo /tmp/deploy/create_ami.sh -y 1 -n '" + name + "' -d '" + desc + "' -a " + repr(account_id) + " -k " + aws_key + " -s " + aws_secret + " -v public -t ebs -r " + region)
stdin, stdout, stderr = client.exec_command("sudo /tmp/deploy/create_ami.sh -y 1 -n '" + name + "' -d '" + desc + "' -a " + repr(account_id) + " -k " + aws_key + " -s " + aws_secret + " -v public -t ebs -r " + region)
chk = stdout.read()
print(hostname + " Finished!")
finally:
client.close()
################################################################
# DEFAULT VARS
################################################################
account_id = False
ami_name = False
ami_desc = False
ami_visibility = "public"
ami_datasets = []
aws_key = False
aws_secret = False
aws_regions = False
distro_version = "xenial"
build_name = "ll_py_build"
instance_size = "t2.small"
keyfile_path = "/etc/ht2keys/"
default_region = "eu-west-1"
################################################################
# READ CLI VARS
################################################################
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--regions", help="Region (or comma separated list of regions) to start instances in")
parser.add_argument("-n", "--name", help="Name of the AMI")
parser.add_argument("-d", "--description", help="Decription for the AMI")
parser.add_argument("-k", "--key", help="AWS Access key")
parser.add_argument("-s", "--secret", help="AWS Access Secret")
parser.add_argument("-a", "--account", type=int, help="AWS Account ID")
parser.add_argument("-v", "--visibility", help="visibility (public or private)")
parser.add_argument("-p", "--keypath", help="Path to the AWS keys")
parser.add_argument("-c", "--complete", help="complete region list")
parser.add_argument("-b", "--branch", help="branch/tag of the main learninglocker repo")
parser.add_argument("-x", "--xapi", help="branch/tag of the xapi repo")
parser.add_argument("-u", "--ubuntu", help="Name of the ubuntu version to use - ie: 'zesty'. Defaults to '" + distro_version + "'")
args = parser.parse_args()
# validate vars
main_branch = ""
if args.branch and len(args.branch) > 2:
main_branch = "-b " + args.branch
xapi_branch = ""
if args.xapi and len(args.xapi) > 2:
xapi_branch = "-x " + args.xapi
# name
if not args.name or len(args.name) < 4:
print("FATAL: no name for the AMI passed in")
sys.exit()
ami_name = args.name
# description
if not args.description or len(args.description) < 4:
print("FATAL: no description for the AMI passed in")
sys.exit()
ami_desc = args.description
# visibility
if args.visibility and args.visibility == "private":
ami_visibility = "private"
# path to the AWS keys
if args.keypath and len(args.keypath) > 4:
keyfile_path = args.keypath
# account id
if not args.account:
if os.environ['AWS_ACCOUNT_ID']:
env = os.environ['AWS_ACCOUNT_ID']
if int(env):
account_id = env
else:
print("FATAL: no account id passed or in environment variable 'AWS_ACCOUNT_ID'")
sys.exit()
else:
account_id = args.account
# key
if not args.key or len(args.key) < 10:
if 'AWS_AUTH_KEY' in os.environ and len(os.environ['AWS_AUTH_KEY']) > 10:
aws_key = os.environ['AWS_AUTH_KEY']
else:
print("FATAL: No AWS auth key passed in or in environment variable 'AWS_AUTH_KEY'")
sys.exit()
else:
aws_key = args.key
# secret
if not args.secret or len(args.secret) < 10:
if 'AWS_AUTH_SECRET' in os.environ and len(os.environ['AWS_AUTH_SECRET']) > 10:
aws_secret = os.environ['AWS_AUTH_SECRET']
else:
print("FATAL: No AWS auth secret passed in or in environment variable 'AWS_AUTH_SECRET'")
sys.exit()
else:
aws_secret = args.secret
# ubuntu version
if args.ubuntu and len(args.ubuntu) > 4:
distro_version = args.ubuntu
# regions
ec2 = boto3.client("ec2", region_name=default_region, aws_access_key_id=aws_key, aws_secret_access_key=aws_secret)
# 'all'
aws_regions = False
if args.regions and len(args.regions) > 5:
aws_regions = args.regions.split(",")
elif args.complete and args.complete == "1":
aws_regions = get_region_list(ec2)
else:
print("FATAL: no regions passed in")
sys.exit()
if aws_regions:
for region in aws_regions:
ami_datasets.append(validate_region(region, distro_version))
print("AMI Name : " + ami_name)
print("AMI Description : " + ami_desc)
print("AMI visbility : " + ami_visibility)
print("AMI temp build name : " + build_name)
print("AWS Account ID : " + repr(account_id))
print("AWS x509 Key : " + aws_key)
print("AWS x509 Secret : " + aws_secret)
print("AWS regions : " + repr(aws_regions))
print("LL Branch : " + repr(args.branch))
print("Xapi Branch : " + repr(args.xapi))
#print("AWS ami ids : " + repr(ami_ids))
print("Warning - the security groups and keypairs must be set up in advance in validate_regions() or things WILL break")
while True:
chk = input("Press enter to continue ")
if str(chk) == "":
break
################################################################
# SPIN UP AMIs
################################################################
# check keypair & security groups exist in every region we want to launch in
#print("Checking keypair and security group data")
### TODO
# start instances
print("[*] Starting servers")
instance_datasets = {}
i = 0
for ami_data in ami_datasets:
ami_id = ami_data['ami_id']
region = ami_data['region']
res = boto3.resource('ec2', region_name=region)
# start the instance
print("ImageId=" + ami_id + ", MinCount=1, MaxCount=1, InstanceType=instance_size, SecurityGroups=[" + ami_data['sg'] + "], KeyName=" + ami_data['kp'])
instance = res.create_instances(ImageId=ami_id, MinCount=1, MaxCount=1, InstanceType=instance_size, SecurityGroups=[ami_data['sg']], KeyName=ami_data['kp'], BlockDeviceMappings=[{'DeviceName':'/dev/sda1','Ebs':{'VolumeSize':20,'VolumeType':'gp2'}}])
print("....Initiated instance id: " + instance[0].id + " in " + region + " with name: " + build_name)
instance_datasets[instance[0].id] = {"region":region, "instance_id": instance[0].id, "kp": ami_data['kp']}
# set the name tag
instance[0].create_tags(Tags=[{'Key': 'Name', 'Value': build_name}])
i += 1
print("[*] initited all servers, waiting for them to start")
################################################################
# servers can take a while to start so we go into a loop waiting for them to start
################################################################
startedInstances = {}
public_ips = {}
c = 0
while True:
c += 1
i = 0
for iid, instance_data in instance_datasets.items():
# get the list of boxes that are running and called whatever the build nam
session = boto3.Session(region_name=instance_data['region'])
sec2 = session.resource('ec2', instance_data['region'])
instances = sec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']},{'Name':'tag:Name', 'Values':[build_name]}])
# check if we found it
found = False
for instance in instances:
found = True
if found == False:
print("....Waiting for server " + repr(instance_data['instance_id']) + " in " + repr(instance_data['region']) + " to start....")
elif instance_data['instance_id'] not in startedInstances:
# get public IP now it's started
ec2 = boto3.client('ec2', region_name=instance_data['region'])
response = ec2.describe_instances(InstanceIds=[iid])
public_ip = response['Reservations'][0]['Instances'][0]['NetworkInterfaces'][0]['Association']['PublicDnsName']
if not public_ip or len(public_ip) < 7:
print("[*] FATAL - instance " + instance_data['instance_id'] + " in region '" + instance_data['region'] + "' doesn't have a public ip - can't continue")
sys.exit()
print("[*] Started instance " + instance_data['instance_id'])
public_ips[instance_data['instance_id']] = public_ip
startedInstances[instance_data['instance_id']] = instance_data
i += 1
# if we have the same number of running instances as we should have - exit the loop
if len(startedInstances) >= len(instance_datasets):
break
# we don't want to itterate forever so bail out if this has taken too long
if c > 30:
print("[*] Servers seem to be taking too long to start, exiting")
sys.exit()
# wait for 10s before retrying
time.sleep(10)
print("[*] All servers alive")
# sleep for 2 mins - this is just to make damn sure everything is working. AWS is a bit weird
sleeptime = 20
print("[*] Sleeping for " + repr(sleeptime) + " seconds to make sure the servers are running and not mis-reporting")
time.sleep(sleeptime)
################################################################
# multithreading :: run install & AMI process on each server
################################################################
print("[*] Going into threading mode for running commands on servers")
ec2 = boto3.resource('ec2')
threads = []
if __name__ == '__main__':
for iid, instance_data in instance_datasets.items():
# get the hostname
hostname = public_ips[instance_data['instance_id']]
# get keyfile
ssh_keyfile = keyfile_path + instance_data['kp'] + ".pem"
p = Process(target=run_on_server, args=(hostname, instance_data['region'], 22, 'ubuntu', ssh_keyfile, ami_name, ami_desc))
p.start()
threads.append(p)
################################################################
# Terminate servers
################################################################
if __name__ == '__main__':
for p in threads:
p.join()
for iid, instance_data in instance_datasets.items():
print("[*] terminating instance " + instance_data['instance_id'] + " in " + instance_data['region'])
session = boto3.Session(region_name=instance_data['region'])
ec2 = session.resource('ec2', instance_data['region'])
instances = ec2.instances.filter(InstanceIds=[instance_data['instance_id']]).terminate()
# finish
print("[*] All Done!")