This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
317 lines (228 loc) · 7.78 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from fabric.api import hide
from fabric.api import cd
from fabric.api import env
from fabric.api import local
from fabric.api import put
from fabric.api import run
from fabric.api import require
from fabric.api import settings
from fabric.api import sudo
from fabric.colors import cyan
from fabric.colors import green
from fabric.colors import red
from fabric.decorators import task
env.repo_url = 'ssh://hg@bitbucket.org/iamFIREcracker/expensio'
def vagrant_key():
''' Get the ssh key to access the virtual box machine. '''
with settings(hide('running')):
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
return result.split()[1]
@task
def dev():
''' Use virtual machine settings. '''
env.user = 'vagrant'
env.hosts = ['127.0.0.1:2222']
env.key_filename = vagrant_key()
env.site_path = '/srv/www/expenses'
env.venv_path = '/srv/www/expenses/venv'
env.uploads_path = '/srv/www/expenses/static/uploads'
env.site_url = 'http://expenses.matteolandi.net:8080'
def _happy():
print(green('\nLooks good from here!\n'))
def _sad():
print(red(r'''
___ ___
/ /\ /__/\
/ /::\ \ \:\
/ /:/\:\ \__\:\
/ /:/ \:\ ___ / /::\
/__/:/ \__\:\ /__/\ /:/\:\
\ \:\ / /:/ \ \:\/:/__\/
\ \:\ /:/ \ \::/
\ \:\/:/ \ \:\
\ \::/ \ \:\
\__\/ \__\/
___ ___ ___ ___
/__/\ / /\ / /\ / /\ ___
\ \:\ / /::\ / /:/_ / /:/_ /__/\
\ \:\ / /:/\:\ / /:/ /\ / /:/ /\ \ \:\
_____\__\:\ / /:/ \:\ / /:/ /:/_ / /:/ /::\ \ \:\
/__/::::::::\ /__/:/ \__\:\ /__/:/ /:/ /\ /__/:/ /:/\:\ \ \:\
\ \:\~~\~~\/ \ \:\ / /:/ \ \:\/:/ /:/ \ \:\/:/~/:/ \ \:\
\ \:\ ~~~ \ \:\ /:/ \ \::/ /:/ \ \::/ /:/ \__\/
\ \:\ \ \:\/:/ \ \:\/:/ \__\/ /:/ __
\ \:\ \ \::/ \ \::/ /__/:/ /__/\
\__\/ \__\/ \__\/ \__\/ \__\/
Something seems to have gone wrong!
You should probably take a look at that.
'''))
@task
def cmd(cmd=""):
'''Run a command in the site directory. Usable from other commands or the CLI.'''
require('site_path')
if not cmd:
sys.stdout.write(cyan("Command to run: "))
cmd = raw_input().strip()
if cmd:
with cd(env.site_path):
run(cmd)
@task
def sdo(cmd=""):
'''Sudo a command in the site directory. Usable from other commands or the CLI.'''
require('site_path')
if not cmd:
sys.stdout.write(cyan("Command to run: sudo "))
cmd = raw_input().strip()
if cmd:
with cd(env.site_path):
sudo(cmd)
@task
def vcmd(cmd=""):
'''Run a virtualenv-based command in the site directory. Usable from other commands or the CLI.'''
require('site_path')
require('venv_path')
if not cmd:
sys.stdout.write(cyan("Command to run: %s/bin/" % env.venv_path.rstrip('/')))
cmd = raw_input().strip()
if cmd:
with cd(env.site_path):
run(env.venv_path.rstrip('/') + '/bin/' + cmd)
@task
def vsdo(cmd=""):
'''Sudo a virtualenv-based command in the site directory. Usable from other commands or the CLI.'''
require('site_path')
require('venv_path')
if not cmd:
sys.stdout.write(cyan("Command to run: sudo %s/bin/" % env.venv_path.rstrip('/')))
cmd = raw_input().strip()
if cmd:
with cd(env.site_path):
sudo(env.venv_path.rstrip('/') + '/bin/' + cmd)
@task
def cupload():
'''Upload the configuration file on the remote server.'''
with cd(env.site_path):
put('prod_config.py', 'prod_config.py')
@task
def dbupdate():
'''Update the database schema.'''
vcmd('alembic upgrade head')
@task
def papply():
'''Apply Puppet manifest. Usable from other commands or the CLI.'''
require('user')
sdo('FACTER_APPNAME=expenses FACTER_USER=%s puppet apply --modulepath=puppet/modules/ puppet/base.pp' % env.user)
@task
def vcreate():
'''Create the virtualenv. Usable from other commands or the CLI.'''
require('venv_path')
run('mkdir -p %s' % env.venv_path)
run('virtualenv %s --no-site-packages --distribute' % env.venv_path)
vupdate()
@task
def vupdate():
'''Update the virtualenv. Usable from other commands or from the CLI.'''
vcmd('python setup.py install')
@task
def check():
'''Check that the home page of the site returns an HTTP 200.'''
require('site_url')
print('Checking site status...')
with settings(warn_only=True):
result = local('curl --silent -I "%s"' % env.site_url, capture=True)
if not '200 OK' in result:
_sad()
else:
_happy()
@task
def rclone():
''' Clone the app repository repository. '''
require('repo_url')
require('site_path')
run('sudo mkdir -p %s' % env.site_path)
run('sudo chown %s:%s %s' % (env.user, env.user, env.site_path))
run('hg clone %s %s' % (env.repo_url, env.site_path))
@task
def rupdate():
''' Update the repository. '''
cmd('hg pull -u')
@task
def rtag():
'''Check which revision the site is at and update the local tag.
Useful if someone else has deployed (which makes your production/staging local
tag incorrect.
'''
require('site_path', provided_by=['prod', 'stag'])
require('env_name', provided_by=['prod', 'stag'])
with cd(env.site_path):
current = run('hg id --rev . --quiet').strip(' \n+')
local('hg tag --local --force %s --rev %s' % (env.env_name, current))
@task
def prerequisites():
''' Prepare the server installing essential packages. '''
run('sudo aptitude -q2 update')
run('sudo apt-get -y install git mercurial')
@task
def compile():
''' Compile all the dynamic resources (e.g. png from svg). '''
cmd("find static/images/ -iname '*svg' -exec convert -background none {} {}.png \;")
@task
def bootstrap():
''' Configure the app '''
print(cyan("Prerequisites..."))
prerequisites()
if 'skip_repo' not in env:
print(cyan("Cloning repo..."))
rclone()
print(cyan("Uploading config..."))
cupload()
print(cyan("Applying puppet manifest..."))
papply()
print(cyan('Creating venv...'))
vcreate()
print(cyan('Initialize database...'))
dbupdate()
print(cyan('Compiling stuff...'))
compile()
restart()
@task
def update():
''' Update everything related to the app. '''
if 'skip_repo' not in env:
print(cyan("Updating repo..."))
rupdate()
print(cyan("Uploading config..."))
cupload()
print(cyan("Applying puppet manifest..."))
papply()
print(cyan('Updating venv...'))
vupdate()
print(cyan('Updating database...'))
dbupdate()
print(cyan('Compiling stuff...'))
compile()
restart()
@task
def restart():
''' Restart the app. Usable from other commands or from the CLI.'''
print(cyan("Restarting rabbitmq..."))
sdo("service rabbitmq-server restart")
print(cyan("Restarting celery..."))
cmd("sudo supervisorctl restart celery")
print(cyan("Restarting gunicorn..."))
cmd("sudo supervisorctl restart gunicorn")
@task
def pull_uploads():
'''Copy the uploads from the site to your local machine.'''
require('uploads_path')
sudo('chmod -R a+r "%s"' % env.uploads_path)
rsync_command = r"""rsync -av -e 'ssh -p %s' %s@%s:%s %s""" % (
env.port,
env.user, env.host,
env.uploads_path.rstrip('/') + '/',
'static/uploads'
)
print local(rsync_command, capture=False)