forked from HDI-Project/ATM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
114 lines (91 loc) · 3.91 KB
/
fabfile.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
from __future__ import print_function
from fabric.api import *
from fabric.colors import green as _green, yellow as _yellow
import boto.ec2
import time
from atm.config import AWSConfig
def check_instances_pending(instances):
isPending = False
for instance in instances:
instance.update()
if(instance.state == u'pending'):
isPending = True
return isPending
def query_active_instances():
ec2_region = config.get(Config.AWS, Config.AWS_EC2_REGION)
ec2_key = config.get(Config.AWS, Config.AWS_ACCESS_KEY)
ec2_secret = config.get(Config.AWS, Config.AWS_SECRET_KEY)
conn = boto.ec2.connect_to_region(ec2_region, aws_access_key_id=ec2_key,
aws_secret_access_key=ec2_secret)
reservations = conn.get_all_reservations()
public_dns_names = []
for reservation in reservations:
instances = reservation.instances
for instance in instances:
if instance.state == 'running':
public_dns_names.append(instance.public_dns_name)
return public_dns_names
def create_instances():
"""
Creates EC2 Instance
"""
print(_green("Started..."))
print(_yellow("...Creating EC2 instance(s)..."))
ec2_region = config.get(Config.AWS, Config.AWS_EC2_REGION)
ec2_key = config.get(Config.AWS, Config.AWS_ACCESS_KEY)
ec2_secret = config.get(Config.AWS, Config.AWS_SECRET_KEY)
conn = boto.ec2.connect_to_region(ec2_region, aws_access_key_id=ec2_key,
aws_secret_access_key=ec2_secret)
ec2_ami = config.get(Config.AWS, Config.AWS_EC2_AMI)
image = conn.get_image(ec2_ami)
ec2_key_pair = config.get(Config.AWS, Config.AWS_EC2_KEY_PAIR)
ec2_instance_type = config.get(Config.AWS, Config.AWS_EC2_INSTANCE_TYPE)
num_instances = config.get(Config.AWS, Config.AWS_NUM_INSTANCES)
# must give num_instances twice because 1 min num and 1 max num
reservation = image[0].run(num_instances, num_instances,
key_name=ec2_key_pair,
instance_type=ec2_instance_type)
while check_instances_pending(reservation.instances):
print(_yellow("Instances still pending"))
time.sleep(10)
for instance in reservation.instances:
print(_green("Instance state: %s" % instance.state))
print(_green("Public dns: %s" % instance.public_dns_name))
#@parallel
def deploy():
code_dir = '/home/ubuntu/atm'
WORKERS_PER_MACHINE = int(config.get(Config.AWS,
Config.AWS_NUM_WORKERS_PER_INSTACNCES))
with settings(warn_only=True):
if run("test -d %s" % code_dir).failed:
run("git clone https://%s:%s@%s %s" % (
config.get(Config.GIT, Config.GIT_USER),
config.get(Config.GIT, Config.GIT_PASS),
config.get(Config.GIT, Config.GIT_REPO), code_dir))
with cd(code_dir):
run("git pull")
run("mkdir config")
put("config/atm.cnf", "config");
for i in range(1, WORKERS_PER_MACHINE + 1, 1):
run("screen -dm -S worker%d python worker.py; sleep 2" % (i,))
else:
with cd(code_dir):
run("git pull")
for i in range(1, WORKERS_PER_MACHINE + 1, 1):
if not run("screen -ls | grep \"worker%d\";" % i):
run("screen -dm -S worker%d python worker.py; sleep 2" % (i,))
#@parallel
def killworkers():
with settings(warn_only=True):
run("pkill -15 screen")
config = Config('config/atm.cnf')
# fabric env setup
env.user = config.get(Config.AWS, Config.AWS_EC2_USERNAME)
env.key_filename = config.get(Config.AWS, Config.AWS_EC2_KEYFILE)
env.skip_bad_hosts = True
env.colorize_errors = True
env.user = 'ubuntu'
env.pool_size = 4
env.timeout = 10
env.disable_known_hosts = True
env.hosts = query_active_instances()