forked from translate/pootle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
executable file
·239 lines (168 loc) · 6.96 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2012 Zuza Software Foundation
#
# This file is part of Pootle.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Fabric deployment file."""
from fabric.api import cd, env
from fabric.context_managers import hide, prefix, settings
from fabric.contrib.console import confirm
from fabric.contrib.files import exists, upload_template
from fabric.operations import require, run, sudo
#
# Deployment environments
#
def production():
"""Work on the production environment"""
from deploy.production import fabric
env.update(fabric.SETTINGS)
env.environment = 'production'
def staging():
"""Work on the staging environment"""
from deploy.staging import fabric
env.update(fabric.SETTINGS)
env.environment = 'staging'
#
# Commands
#
def _init_directories():
"""Creates initial directories"""
if exists('%(project_path)s' % env):
sudo('rm -rf %(project_path)s' % env)
sudo('mkdir -p %(project_path)s' % env)
sudo('chown %(user)s:%(server_group)s %(project_path)s' % env)
run('mkdir -m g+w %(project_path)s/logs' % env)
def _init_virtualenv():
"""Creates initial virtualenv"""
run('virtualenv -p %(python)s --no-site-packages %(env_path)s' % env)
with prefix('source %(env_path)s/bin/activate' % env):
run('easy_install pip' % env)
def _clone_repo():
"""Clones the git repository"""
run('git clone %(project_repo)s %(project_repo_path)s' % env)
# TODO: Accept branches other than the default
def _checkout_repo():
"""Updates the git repository"""
with cd(env.project_repo_path):
run('git pull')
def _install_requirements():
"""Installs dependencies defined in the requirements file"""
with prefix('source %(env_path)s/bin/activate' % env):
run('pip install -r %(project_repo_path)s/requirements/deploy.txt' % env)
def _update_requirements():
"""Updates dependencies installed via pip"""
with prefix('source %(env_path)s/bin/activate' % env):
run('pip install -U -r %(project_repo_path)s/requirements/deploy.txt' % env)
def bootstrap():
"""Creates initial directories and virtualenv"""
require('environment', provided_by=[production, staging])
if (exists('%(project_path)s' % env) and
confirm('%(project_path)s already exists. Do you want to continue?'
% env, default=False)) or not exists('%(project_path)s' % env):
print('Bootstrapping initial directories...')
with settings(hide('stdout', 'stderr')):
_init_directories()
_init_virtualenv()
_clone_repo()
_checkout_repo()
_install_requirements()
else:
print('Aborting.')
def update_db():
"""Updates database schemas"""
require('environment', provided_by=[production, staging])
with settings(hide('stdout', 'stderr')):
with cd('%(project_repo_path)s' % env):
with prefix('source %(env_path)s/bin/activate' % env):
run('python manage.py updatedb')
def update_code():
"""Updates the source code and its requirements"""
require('environment', provided_by=[production, staging])
print('Getting the latest code and dependencies...')
with settings(hide('stdout', 'stderr')):
_checkout_repo()
_update_requirements()
def deploy_static():
"""Runs `collectstatic` to collect all the static files"""
require('environment', provided_by=[production, staging])
print('Collecting and building static files...')
with settings(hide('stdout', 'stderr')):
with cd('%(project_repo_path)s' % env):
with prefix('source %(env_path)s/bin/activate' % env):
run('mkdir -p pootle/assets')
run('python manage.py collectstatic --noinput')
run('python manage.py assets build')
def deploy():
"""Updates the code and installs the production site"""
require('environment', provided_by=[production, staging])
print('Deploying the site...')
with settings(hide('stdout', 'stderr')):
update_code()
deploy_static()
install_site()
def install_site():
"""Configures the server and enables the site"""
require('environment', provided_by=[production, staging])
print('Configuring and installing site...')
with settings(hide('stdout', 'stderr')):
update_config()
enable_site()
def update_config():
"""Updates server configuration files"""
require('environment', provided_by=[production, staging])
with settings(hide('stdout', 'stderr')):
# Configure VirtualHost
upload_template('deploy/%(environment)s/virtualhost.conf' % env,
env.vhost_file, context=env, use_sudo=True)
# Configure WSGI application
upload_template('deploy/pootle.wsgi',
env.wsgi_file, context=env)
# Configure and install settings
upload_template('deploy/%(environment)s/settings.conf' % env,
'%(project_settings_path)s/90-%(environment)s-local.conf'
% env, context=env)
def enable_site():
"""Enables the site"""
require('environment', provided_by=[production, staging])
with settings(hide('stdout', 'stderr')):
_switch_site(True)
def disable_site():
"""Disables the site"""
require('environment', provided_by=[production, staging])
with settings(hide('stdout', 'stderr')):
_switch_site(False)
def _switch_site(enable):
"""Switches site's status to enabled or disabled"""
action = "Enabling" if enable else "Disabling"
print('%s site...' % action)
env.apache_command = 'a2ensite' if enable else 'a2dissite'
sudo('%(apache_command)s %(project_name)s' % env)
sudo('service apache2 reload')
def touch():
"""Reloads daemon processes by touching the WSGI file"""
require('environment', provided_by=[production, staging])
print('Running touch...')
with settings(hide('stdout', 'stderr')):
run('touch %(wsgi_file)s' % env)
def compile_translations():
"""Compiles PO translations"""
require('environment', provided_by=[production, staging])
print('Compiling translations...')
with settings(hide('stdout', 'stderr')):
with cd(env.project_repo_path):
with prefix('source %(env_path)s/bin/activate' % env):
run('python setup.py build_mo')