-
Notifications
You must be signed in to change notification settings - Fork 4
/
aws.py
74 lines (59 loc) · 2.33 KB
/
aws.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
from boto.ec2 import connect_to_region
from ConfigParser import SafeConfigParser
from properties import loadcredentials
import sys, os, time
USERNAME = 'ec2-user'
AMI_ID = 'ami-6b73562e'
INSTANCE_TYPE = 'm1.small'
EC2_REGION = 'us-west-1'
EC2_SSH_KEY_NAME = 'us-west'
EC2_SSH_KEY_PATH = os.path.join(os.path.dirname(__file__), 'venv', 'us-west.pem')
class Node:
def __init__(self, instance):
self.hostname = instance.public_dns_name
self.ssh_key_file = EC2_SSH_KEY_PATH
self.ssh_user = USERNAME
def provision_with_boto(name):
conn = connect()
for reservation in fetch_running_reservations(conn, name):
for instance in reservation.instances:
return Node(instance)
res = conn.run_instances(AMI_ID, key_name=EC2_SSH_KEY_NAME, instance_type=INSTANCE_TYPE)
instance = res.instances[0]
print "Waiting for", name, instance.id, "to start ..."
wait_while(instance, 'pending')
conn.create_tags([instance.id], {'Name': name})
print "Instance", name, instance.id, "is running"
return Node(instance)
def get_node(name):
conn = connect()
for reservation in fetch_running_reservations(conn, name):
for instance in reservation.instances:
return Node(instance)
def connect():
credentials = loadcredentials()
access_key = credentials.access_key_id
secret_key = credentials.secret_access_key
return connect_to_region(EC2_REGION, aws_access_key_id=access_key, aws_secret_access_key=secret_key)
def fetch_running_reservations(conn, name):
filters = {'tag:Name': name, 'instance-state-name': 'running'}
return conn.get_all_instances(filters=filters)
def terminate_instance(name):
conn = connect()
for reservation in fetch_running_reservations(conn, name):
for instance in reservation.instances:
print 'Terminating instance', instance.id
instance.terminate()
wait_while(instance, 'running')
def terminate_all_instances():
conn = connect()
for reservation in conn.get_all_instances():
for instance in reservation.instances:
print 'Terminating instance', instance.id
instance.terminate()
wait_while(instance, 'running')
def wait_while(instance, status):
instance.update()
while instance.state == status:
time.sleep(5)
instance.update()