-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
246 lines (199 loc) · 7.07 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
# -*- coding: utf-8 -*-
from fabric.api import cd, env, require, run, task
from fabric.colors import green, white
from fabric.context_managers import contextmanager, shell_env, prefix
from fabric.utils import puts
from fabutils import arguments, join, options
from fabutils.env import set_env_from_json_file
from fabutils.context import cmd_msg
from fabutils.tasks import ulocal, urun, ursync_project
from fabutils.text import SUCCESS_ART
@contextmanager
def virtualenv():
"""
Activates the virtualenv in which the commands shall be run.
"""
require('site_dir', 'django_settings')
with cd(env.site_dir):
with shell_env(DJANGO_SETTINGS_MODULE=env.django_settings):
yield
@task
def environment(env_name):
"""
Creates a dynamic environment based on the contents of the given
environments_file.
"""
if env_name == 'vagrant':
result = ulocal('vagrant ssh-config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1].replace('"', '')
set_env_from_json_file('environments.json', env_name)
def bower_install():
"""
Installs frontend dependencies with bower.
"""
with virtualenv(), prefix('nvm use stable'), shell_env(CI='true'):
run('bower install')
@task
def startapp(app_name):
"""
Starts a new app
"""
if app_name:
with virtualenv():
run('python manage.py startapp {0}'.format(app_name))
@task
def createsuperuser():
"""
Starts a new app
"""
with virtualenv():
run('python manage.py createsuperuser')
@task
def createdb():
"""
Creates a new database instance with utf-8 encoding for the project.
"""
urun('createdb luke -l en_US.UTF-8 -E UTF8 -T template0')
@task
def resetdb():
"""
Reset the project's database by dropping an creating it again.
"""
urun('dropdb luke')
createdb()
migrate()
@task
def bootstrap():
"""
Builds the environment to start the project.
"""
# Build the DB schema and collect the static files.
createdb()
migrate()
bower_install()
collectstatic()
@task
def loaddata(*args):
"""
Loads the given data fixtures into the project's database.
"""
with virtualenv():
run(join('python manage.py loaddata', arguments(*args)))
@task
def makemigrations(*args, **kwargs):
"""
Creates the new migrations based on the project's models changes.
"""
with virtualenv():
run(join('python manage.py makemigrations',
options(**kwargs), arguments(*args)))
@task
def migrate(*args, **kwargs):
"""
Syncs the DB and applies the available migrations.
"""
with virtualenv():
run(join('python manage.py migrate',
options(**kwargs), arguments(*args)))
@task
def collectstatic():
"""
Collects the static files.
"""
with virtualenv():
run('python manage.py collectstatic --noinput')
@task
def runserver():
"""
Starts the development server inside the Vagrant VM.
"""
with virtualenv():
run('python manage.py runserver_plus 0.0.0.0:8000')
@task
def deploy(git_ref, upgrade=False):
"""
Deploy the code of the given git reference to the previously selected
environment.
Pass ``upgrade=True`` to upgrade the versions of the already installed
project requirements (with pip).
"""
require('hosts', 'user', 'group', 'site_dir', 'django_settings')
# Retrives git reference metadata and creates a temp directory with the
# contents resulting of applying a ``git archive`` command.
message = white('Creating git archive from {0}'.format(git_ref), bold=True)
with cmd_msg(message):
repo = ulocal(
'basename `git rev-parse --show-toplevel`', capture=True)
commit = ulocal(
'git rev-parse --short {0}'.format(git_ref), capture=True)
branch = ulocal(
'git rev-parse --abbrev-ref HEAD', capture=True)
tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit)
ulocal('rm -fr {0}'.format(tmp_dir))
ulocal('mkdir {0}'.format(tmp_dir))
ulocal('git archive {0} ./src | tar -xC {1} --strip 1'.format(
commit, tmp_dir))
# Uploads the code of the temp directory to the host with rsync telling
# that it must delete old files in the server, upload deltas by checking
# file checksums recursivelly in a zipped way; changing the file
# permissions to allow read, write and execution to the owner, read and
# execution to the group and no permissions for any other user.
with cmd_msg(white('Uploading code to server...', bold=True)):
ursync_project(
local_dir=tmp_dir,
remote_dir=env.site_dir,
delete=True,
default_opts='-chrtvzP',
extra_opts='--chmod=750',
exclude=[
"*.pyc", "env/",
"cover/", "*.style",
"bower_components"
]
)
# Performs the deployment task, i.e. Install/upgrade project
# requirements, syncronize and migrate the database changes, collect
# static files, reload the webserver, etc.
message = white('Running deployment tasks', bold=True)
with cmd_msg(message, grouped=True):
with virtualenv():
message = white('Installing Python requirements with pip')
with cmd_msg(message, spaces=2):
run('pip install -{0}r ./requirements/production.txt'.format(
'U' if upgrade else ''))
message = white('Migrating database')
with cmd_msg(message, spaces=2):
run('python manage.py migrate --noinput')
message = white('Collecting static files')
with cmd_msg(message, spaces=2):
run('python manage.py collectstatic --noinput')
message = white('Setting file permissions')
with cmd_msg(message, spaces=2):
run('chgrp -R {0} .'.format(env.group))
run('chgrp -R {0} ../media'.format(env.group))
message = white('Restarting webserver')
with cmd_msg(message, spaces=2):
run('touch ../reload')
message = white('Registering deployment')
with cmd_msg(message, spaces=2):
register_deployment(commit, branch)
# Clean the temporary snapshot files that was just deployed to the host
message = white('Cleaning up...', bold=True)
with cmd_msg(message):
ulocal('rm -fr {0}'.format(tmp_dir))
puts(green(SUCCESS_ART), show_prefix=False)
puts(white('Code from {0} was succesfully deployed to host {1}'.format(
git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)
@task
def register_deployment(commit, branch):
"""
Register the current deployment at Opbeat with given commit and branch.
"""
with virtualenv():
run(
'opbeat -o $OPBEAT_ORGANIZATION_ID '
'-a $OPBEAT_APP_ID '
'-t $OPBEAT_SECRET_TOKEN deployment '
'--component path:. vcs:git rev:%s branch:%s '
% (commit, branch)
)