forked from NYU-NEWS/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
267 lines (228 loc) · 7.71 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
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
import logging
import os
import traceback
import time
import random
from fabric.api import env, task, run, local, hosts
from fabric.api import execute, cd, runs_once, sudo, parallel
from fabric.contrib.files import exists, append
from fabric.decorators import roles
from fabric.context_managers import prefix
from fabric.operations import reboot
# tasks are exported under the module names
from pylib import ec2
from pylib import cluster
import pylib
def run_python(*args, **kwargs):
activate_virtual_env = \
"source {venv}/bin/activate".format(
venv=env.py_virtual_env)
with prefix(activate_virtual_env):
run(*args, **kwargs)
def init():
logging.basicConfig(level=logging.INFO)
environment()
def environment():
if '__envset__' not in env:
env.__envset__ = True
# standard fabric variables
env.connection_attempts = 10
env.timeout = 30
env.use_ssh_config = True
# custom variables
env.local_cwd = os.path.dirname(os.path.realpath(__file__))
env.setdefault('remote_home', '/mnt/janus')
env.setdefault('nfs_home', '/export/janus')
env.setdefault('git_repo', '[email protected]:NYU-NEWS/janus.git')
env.setdefault('git_revision', 'master')
env.setdefault('py_virtual_env',
'{home}/py_venv'.format(home=env.nfs_home))
env.setdefault('data_dir', '.ec2-data')
@task
@hosts('localhost')
def deploy_continue():
execute('ec2.set_instance_roles')
execute('cluster.config_nfs_server')
execute('cluster.config_nfs_client')
execute('cluster.config_ssh')
execute('retrieve_code')
execute('create_work_dirs')
teardown_instances = False
attempts = 0
done = False
while attempts < 3 and done == False:
try:
execute('install_apt_packages')
execute('install_leader_apt_packages')
execute('build', args="-t")
execute('cluster.put_janus_config')
execute('cluster.put_limits_config')
execute('cluster.mount_nfs')
execute('ec2.reboot_all')
done = True
except:
attempts = attempts + 1
@task
@runs_once
@hosts('localhost')
def deploy_all(regions='us-west-2', servers_per_region=[3], instance_type='t2.small'):
"""
keyword arguments:
regions (string) - colon separated list of regions to deploy to;
default 'us-west-2'
servers_per_region (string) - colon separated list indicating
the number of servers in each region;
default 3
instance_type - the ec2 instance type; default 't2.small'
example:
fab deploy_all:regions=us-west-2:eu-west-1,servers_per_region=3:2
"""
if isinstance(regions, basestring):
regions = regions.split(':')
if isinstance(servers_per_region, basestring):
servers_per_region = [ int(i) for i in servers_per_region.split(':') ]
teardown_instances = True
assert(len(servers_per_region) == len(regions))
start = time.time()
try:
logging.info('deploy to regions: {}'.format(','.join(regions)))
for region in regions:
execute('cluster.delete_security_group', region=region)
execute('cluster.setup_security_groups', regions=regions)
region_index = 0
for region in regions:
logging.debug('create in region: {}'.format(region))
execute('ec2.create',
region=region,
num=servers_per_region[region_index],
instance_type=instance_type)
region_index += 1
execute('cluster.load_security_grp_ips')
execute('ec2.set_instance_roles')
ec2.wait_for_all_servers()
execute('cluster.config_nfs_server')
execute('cluster.config_nfs_client')
execute('cluster.config_ssh')
execute('retrieve_code')
execute('create_work_dirs')
teardown_instances = False
attempts = 0
done = False
while attempts < 3 and done == False:
try:
execute('install_leader_apt_packages')
execute('build', args="-t")
execute('cluster.put_janus_config')
execute('cluster.put_limits_config')
execute('cluster.mount_nfs')
execute('ec2.reboot_all')
done = True
except:
attempts = attempts + 1
except Exception as e:
traceback.print_exc()
if teardown_instances:
logging.info("Terminating ec2 instances...")
ec2.terminate_instances()
finally:
print("{:.2f} seconds elapsed".format(time.time() - start))
@task
@runs_once
@roles('leaders')
def create_virtual_env():
venv = env.py_virtual_env
append('/home/ubuntu/.bash_profile',
'source {venv}/bin/activate'.format(venv=venv))
#if exists(env.py_virtual_env):
# return
with cd(env.nfs_home):
execute('retrieve_code')
run('pyenv local 2.7.11')
run('virtualenv {venv}'.format(venv=venv))
run('{venv}/bin/pip install -r requirements.txt'.format(venv=venv))
@task
@runs_once
@roles('leaders')
def create_work_dirs():
dirs = ['tmp/', 'log/', 'archive/']
for d in dirs:
dir_path = os.path.join(env.nfs_home, d)
run("mkdir -p {}".format(dir_path))
@task
@runs_once
@roles('leaders')
def install_leader_apt_packages():
sudo('apt-get -y install gnuplot5')
@task
@roles('all')
@parallel
def install_apt_packages():
sudo('apt-get -y update')
sudo('apt-get -y install pkg-config libgoogle-perftools-dev')
@task
@runs_once
@roles('leaders')
def build(args=None, clean=True):
execute('retrieve_code')
execute('create_virtual_env')
execute('install_apt_packages')
return
opts = ["./waf"]
if args:
opts.insert(1, args)
if clean:
opts.extend(["distclean", "configure"])
opts.append("build")
with cd(env.nfs_home):
run_python(' '.join(opts))
@task
@runs_once
@roles('leaders')
def retrieve_boost():
return
with cd(env.nfs_home):
# get from beaker-1
boost_archive = 'boost_1_58_0.tar.gz'
boost = 'http://216.165.108.10/~lamont/{boost_archive}'.format(**locals())
run('wget {boost}'.format(**locals()))
run('tar -xzvf {boost_archive}'.format(boost_archive=boost_archive))
with cd(env.nfs_home + "/" + boost_archive.replace('.tar.gz','')):
sudo('./bootstrap')
sudo('./b2 install')
@task
@runs_once
@roles('leaders')
def retrieve_code():
parent = os.path.dirname(env.nfs_home)
with cd(parent):
logging.info("check out code in {}".format(parent))
if not exists(env.nfs_home):
cmd = 'git clone --recursive {repo}'.format(repo=env.git_repo)
run(cmd)
with cd(env.nfs_home):
cmd = 'git checkout {rev}'.format(rev=env.git_revision)
run(cmd)
else:
with cd(env.nfs_home):
run('git fetch origin')
run('git checkout master')
run('git pull origin master')
run('git checkout {rev}'.format(rev=env.git_revision))
execute('retrieve_boost')
@task
@hosts('localhost')
def ping():
execute('cluster.ping')
@task
@roles('leaders')
def download_archive():
chars = 'ABCDEF'
id = ''.join(random.choice(chars) for _ in range(10))
archive_fn = "archive_{}.tgz".format(id)
remote_path = "/tmp/{}".format(archive_fn)
with cd('/home/ubuntu/janus/archive'):
tar_cmd = 'tar -czvf {} .'.format(remote_path)
run(tar_cmd)
execute('cluster.download', remote_path)
run('rm {}'.format(remote_path))
init()