-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
365 lines (234 loc) · 8.77 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
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
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
import boto
from boto.s3.key import Key
from boto import ec2
from datetime import datetime
import sys, pprint, time, ConfigParser
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('configure.ini')
ec2keypairname = parser.get('aws', 'ec2keypairname')
localkeypath = parser.get('aws', 'localkeypath')
precise_12_04_2 = 'ami-de0d9eb7'
aws_access_key_id = parser.get('aws', 'aws_access_key_id')
aws_secret_access_key =parser.get('aws', 'aws_secret_access_key')
volid = 'vol-9415bbe4'
def last():
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('lastLaunch.ini')
env.user = parser.get('lastLaunch', 'username')
env.host_string = parser.get('lastLaunch', 'host_string')
env.key_filename = [parser.get('lastLaunch', 'keypath')]
env.last_instance = parser.get('lastLaunch', 'instance')
def printssh():
print 'ssh -i ~/.ssh/ec2-keypair ubuntu@%s' % (env.host_string)
local('echo "ssh -i ~/.ssh/ec2-keypair ubuntu@%s" | pbcopy ' % (env.host_string))
def printhttp():
print 'http://%s' % (env.host_string)
local('echo "http://%s" | pbcopy ' % (env.host_string))
def terminate():
#terminate_instances
with settings(warn_only = True):
print 'killing last instance'
conn = ec2.EC2Connection(aws_access_key_id, aws_secret_access_key)
conn.terminate_instances(env.last_instance)
time.sleep(1)
def test():
run('uname -a')
run('lsb_release -a')
def createXL():
'''creates an m3.xlarge - 15 GiB memory, 13ECU (4x3.25)'''
_create('m3.xlarge')
def createL():
'''creates an m1.large - 7.5 GiB memory, 4ECU (2x2)'''
_create('m1.large')
def createCustom():
'''usage: fab --set ec2=m2.xlarge createCustom '''
_create(env.ec2)
def createS():
'''creates an m1.small - 1.7 GiB memory, 1ECU (1x1)'''
_create('m1.small')
def createXXL():
'''creates an m2.2xlarge - 34.2 GiB of memory, 13ECU (4x3.25)'''
_create('m2.2xlarge')
def _create(size):
'''Creates a new large instance on ec2'''
with settings(warn_only = True):
conn = ec2.EC2Connection(aws_access_key_id, aws_secret_access_key)
time.sleep(1)
reservation = conn.run_instances(precise_12_04_2, instance_type=size, placement='us-east-1d', key_name='ec2-keypair')
time.sleep(1)
instance = reservation.instances[0]
time.sleep(1)
print 'Starting instance %s' %(instance)
while not instance.update() == 'running':
time.sleep(1)
instance.add_tag('Name', 'ipython-deploy')
time.sleep(1)
print 'Instance started: %s' % instance.__dict__['id']
print 'Private DNS: %s' % instance.__dict__['private_dns_name']
print 'Private IP: %s' % instance.__dict__['private_ip_address']
print 'Public DNS: %s' % instance.__dict__['public_dns_name']
# write temporary settings in case something goes wrong mid-configuration
import ConfigParser
import sys
parser = ConfigParser.SafeConfigParser()
parser.add_section('lastLaunch')
parser.set('lastLaunch', 'host_string', str(instance.__dict__['public_dns_name']))
parser.set('lastLaunch', 'keypath', localkeypath)
parser.set('lastLaunch', 'username', 'ubuntu')
parser.set('lastLaunch', 'instance', instance.__dict__['id'])
parser.write(open('lastLaunch.ini', 'w'))
env.user = 'ubuntu'
env.host_string = instance.__dict__['public_dns_name']
env.key_filename = [localkeypath]
# enableNeuroDebian()
# base()
# provision()
# externals()
# notebook()
# pmip()
print 'Instance has been launched successfully'
print 'To access, open a browser to http://%s' % (instance.__dict__['public_dns_name'])
print 'ssh -i ~/.ssh/ec2-keypair ubuntu@%s' % (instance.__dict__['public_dns_name'])
def install():
with settings(warn_only=True):
_base()
setupBlenderEnvironment()
connectivity()
pass
def mountstatus():
with settings(warn_only=True):
v_to_mount = ''
conn = ec2.EC2Connection(aws_access_key_id, aws_secret_access_key)
vol = conn.get_all_volumes()
for v in vol:
if v.id == volid:
v_to_mount = v
if v_to_mount.attachment_state() == None:
print 'Volume not attached'
else:
print 'Volume attached with status: %s' % v_to_mount.attachment_state()
def attachebs():
with settings(warn_only=True):
v_to_mount = ''
conn = ec2.EC2Connection(aws_access_key_id, aws_secret_access_key)
vol = conn.get_all_volumes()
for v in vol:
if v.id == volid:
v_to_mount = v
print 'trying to attach volume %s to instance %s' % (v_to_mount, env.last_instance)
if v_to_mount.attachment_state() == None:
print 'volume not attached, continuing'
result = v_to_mount.attach(env.last_instance, '/dev/xvdf')
# if result == True:
# print 'volume attached successfully, sleep for 20s then mount'
# sudo('mkdir -m 000 /vol')
# sudo('mount /dev/xvdf /vol')
# else:
# print 'volume not attached'
else:
print v_to_mount.attachment_state()
# if exists('/vol'):
# print 'vol present'
# else:
# sudo('mkdir -m 000 /vol')
# sudo('mount /dev/xvdf /vol')
def mountebs():
with settings(warn_only=True):
if not exists('/vol'):
sudo('mkdir -m 000 /vol')
sudo('mount /dev/xvdf /vol')
# # sudo mkfs.ext4 /dev/xvdf
def unmountebs():
with settings(warn_only=True):
sudo('umount /dev/xvdf')
v_to_unmount = ''
conn = ec2.EC2Connection(aws_access_key_id, aws_secret_access_key)
vol = conn.get_all_volumes()
for v in vol:
if v.id == volid:
v_to_unmount = v
result = v_to_unmount.detach(force=True)
if result == True:
print 'volume detached successfully'
else:
print 'volume not attached successfully'
print v_to_unmount.attachment_state()
def _base():
'''[create] Basic packages for building, version control'''
with settings(warn_only=True):
# update existing tools
run("sudo apt-get -y update", pty = True)
run("sudo apt-get -y upgrade", pty = True)
# install build and CVS tools
packagelist = ' '.join(['git-core', 'mercurial', 'subversion', 'unzip', 'build-essential', 'g++', 'libav-tools', 'uuid-dev', 'libfreetype6-dev','libpng12-dev'])
run('sudo apt-get -y install %s' % packagelist, pty = True)
# install python components
packagelist = ' '.join(['python-setuptools', 'python-pip', 'python-dev', 'python-lxml', 'libxml2-dev', 'python-imaging', 'libncurses5-dev', 'cmake-curses-gui', 'imagemagick', 's3cmd'])
run('sudo apt-get -y install %s' % packagelist, pty = True)
packagelist = ['tornado', 'supervisor', 'virtualenv', 'jinja2']
for each_package in packagelist:
print each_package
run('sudo pip install %s' % each_package, pty = True)
def setupBlenderEnvironment():
_base()
with settings(warn_only=True):
sudo('add-apt-repository ppa:irie/blender')
sudo('apt-get update')
sudo('apt-get -y install blender')
put('id_rsa.pub','~/.ssh/id_rsa.pub')
put('id_rsa', '~/.ssh/id_rsa')
sudo('chmod 0600 .ssh/id_rsa')
sudo('chmod 0600 .ssh/id_rsa.pub')
put('s3cfg', '.s3cfg')
sudo('chmod 0600 .s3cfg')
run('s3cmd get s3://aibs/untitled.blend untitled.blend --force')
def _notebook():
'''install python notebook'''
with settings(warn_only=True):
put('id_rsa.pub','~/.ssh/id_rsa.pub')
put('id_rsa', '~/.ssh/id_rsa')
sudo('chmod 0600 .ssh/id_rsa')
sudo('chmod 0600 .ssh/id_rsa.pub')
sudo('easy_install readline')
sudo('apt-get install -y libfreetype6-dev libpng12-dev python-matplotlib')
sudo('pip install -U requests')
sudo('pip install -U beautifulsoup4')
sudo('pip install pyzmq')
sudo('pip install workerpool')
# sudo('pip install -U matplotlib')
run('ipython profile create default')
run('ipython profile create nbserver')
run("rm -rvf ~/.ipython/profile_nbserver")
put('profile_nbserver.zip', '.ipython/profile_nbserver.zip')
with cd('.ipython'):
run('unzip profile_nbserver.zip')
run('rm profile_nbserver.zip')
put('supervisord.conf.ipython')
sudo('mv supervisord.conf.ipython /home/ubuntu/config/supervisord.conf')
sudo('rm /etc/supervisord.conf')
sudo('ln -s /home/ubuntu/config/supervisord.conf /etc/supervisord.conf')
put('supervisor.start')
sudo('supervisord')
sudo('chmod +x supervisor.start')
sudo('chown root:root supervisor.start')
sudo('mv /home/ubuntu/supervisor.start /etc/init.d/supervisor')
sudo('update-rc.d -f supervisor remove')
sudo('update-rc.d supervisor defaults')
sudo('supervisorctl restart all')
def connectivity():
with settings(warn_only=True):
_notebook()
# get the most recent version, or clone to directory
if exists('connectivity-blend'):
with cd('connectivity-blend'):
run('git pull --rebase')
else:
run('git clone [email protected]:richstoner/connectivity-blend.git')
run('git config --global user.name "richstoner"')
run('git config --global user.email "[email protected]"')