forked from PathagarBooks/pathagar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
353 lines (265 loc) · 11.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2012 Zuza Software Foundation
# Copyright 2013 Aneesh Dogra ([email protected])
"""Fabric deployment file."""
from os.path import isfile, isdir, dirname
from os import makedirs
from os.path import exists as path_exists
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, put, get
#
# Deployment environments
#
def ubuntu():
"""Work on the ubuntu environment"""
try:
from deploy.ubuntu import fabric
except ImportError:
print("Can't load 'production' environment; is PYTHONPATH exported?")
exit(1)
env.update(fabric.SETTINGS)
env.environment = 'ubuntu'
def fedora():
"""Work on the fedora (red-hat based) environment"""
try:
from deploy.fedora import fabric
except ImportError:
print("Can't load 'fedora' environment; is PYTHONPATH exported?")
exit(1)
env.update(fabric.SETTINGS)
env.environment = 'fedora'
#
# 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('mkdir -p %(project_path)s/logs' % env)
sudo('chmod -R g=u '
'%(project_path)s' % env)
sudo('chown -R %(user)s:%(server_group)s '
'%(project_path)s' % 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')
def _clone_repo():
"""Clones the Git repository"""
run('git clone %(project_repo)s %(project_repo_path)s' % env)
def _checkout_repo(branch="master"):
"""Updates the Git repository and checks out the specified branch"""
with cd(env.project_repo_path):
run('git checkout master')
run('git pull')
run('git checkout %s' % branch)
run('chmod -R go=u,go-w %(project_repo_path)s' % env)
# now we need to mark the static_media/books directory writable
# because that's where the uploaded books are stored
books_dir = "%(project_repo_path)s/static_media/books" % env
if not path_exists(books_dir):
run("mkdir -p %s" % books_dir)
sudo("chmod g+rwxs %s" % books_dir)
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.pip' % env)
run('chmod -R go=u,go-w %(env_path)s' % env)
def _update_requirements():
"""Updates dependencies defined in the requirements file"""
with prefix('source %(env_path)s/bin/activate' % env):
run('pip install -U -r %(project_repo_path)s/requirements.pip' % env)
run('chmod -R go=u,go-w %(env_path)s' % env)
def bootstrap(branch="master"):
"""Bootstraps a Pootle deployment using the specified branch"""
require('environment', provided_by=[ubuntu, fedora])
if (not exists('%(project_path)s' % env) or
confirm('\n%(project_path)s already exists. Do you want to continue?'
% env, default=False)):
print('Bootstrapping initial directories...')
with settings(hide('stdout', 'stderr')):
_init_directories()
_init_virtualenv()
_clone_repo()
run("touch %(project_repo_path)s/../__init__.py" % env)
_checkout_repo(branch=branch)
with prefix('source %(env_path)s/bin/activate' % env):
_install_requirements()
else:
print('Aborting.')
def _create_db_mysql():
require('environment', provided_by=[ubuntu, fedora])
create_db_cmd = ("CREATE DATABASE `%(db_name)s` "
"DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
% env)
grant_db_cmd = ("GRANT ALL PRIVILEGES ON `%(db_name)s`.* TO `%(db_user)s`"
"@localhost IDENTIFIED BY \"%(db_password)s\"; "
"FLUSH PRIVILEGES;"
% env)
with settings(hide('stderr')):
run(("mysql -u %(db_user)s %(db_password_opt)s -e '" % env) +
create_db_cmd +
("' || { test root = '%(db_user)s' && exit $?; }" % env))
def _create_db_sqlite3():
db_path = env.db_name
db_dir = dirname(db_path)
try:
makedirs(db_dir)
except OSError:
pass
run("touch %s" % db_path)
sudo("chown -R %s:%s %s" % (env.user, env.server_group, db_dir))
sudo("chmod -R g+w %s" % (db_dir)) # mark the db writable
def _get_database_type():
if env.db_engine == "django.db.backends.sqlite3":
return "sqlite3"
elif env.db_engine == "django.db.backends.mysql":
return "mysql"
def create_db():
"""Creates a new DB"""
require('environment', provided_by=[ubuntu, fedora])
db_type = _get_database_type()
if db_type == 'mysql':
_create_db_mysql()
elif db_type == 'sqlite3':
_create_db_sqlite3(database)
else:
print("The database type is not currently supported by our fabfile. You'll have to create it manually.")
def drop_db():
"""Drops the current DB - losing all data!"""
require('environment', provided_by=[ubuntu, fedora])
db_type = _get_database_type()
if confirm('\nDropping the %s DB loses ALL its data! Are you sure?'
% (env['db_name']), default=False):
if db_type == 'mysql':
_drop_db_mysql()
elif db_type == 'sqlite3':
_drop_db_sqlite3(database)
else:
print('Aborting.')
def _drop_db_mysql():
run("echo 'DROP DATABASE `%s`' | mysql -u %s %s" %
(env['db_name'], env['db_user'], env['db_password_opt']))
def _drop_db_sqlite3(database):
run("rm %s" % database['NAME'])
def setup_db():
"""Runs all the necessary steps to create the DB schema from scratch"""
require('environment', provided_by=[ubuntu, fedora])
syncdb()
def syncdb():
"""Runs `syncdb` to create the DB schema"""
require('environment', provided_by=[ubuntu, fedora])
with cd('%(project_repo_path)s' % env):
with prefix('source %(env_path)s/bin/activate' % env):
run('python manage.py syncdb')
def load_db(dumpfile=None):
"""Loads data from a SQL script to Pootle DB"""
require('environment', provided_by=[production, staging])
if dumpfile is not None:
if isfile(dumpfile):
remote_filename = '%(project_path)s/DB_backup_to_load.sql' % env
if (not exists(remote_filename) or
confirm('\n%s already exists. Do you want to overwrite it?'
% remote_filename, default=False)):
print('\nLoading data into the DB...')
with settings(hide('stderr')):
put(dumpfile, remote_filename)
run('mysql -u %s %s %s < %s' %
(env['db_user'], env['db_password_opt'],
env['db_name'], remote_filename))
run('rm %s' % (remote_filename))
else:
print('\nAborting.')
else:
print('\nERROR: The file "%s" does not exist. Aborting.' % dumpfile)
else:
print('\nERROR: A (local) dumpfile must be provided. Aborting.')
def dump_db(dumpfile="pathagarh_DB_backup.sql"):
"""Dumps the DB as a SQL script and downloads it"""
require('environment', provided_by=[production, staging])
if isdir(dumpfile):
print("dumpfile '%s' is a directory! Aborting." % dumpfile)
elif (not isfile(dumpfile) or
confirm('\n%s already exists locally. Do you want to overwrite it?'
% dumpfile, default=False)):
remote_filename = '%s/%s' % (env['project_path'], dumpfile)
if (not exists(remote_filename) or
confirm('\n%s already exists. Do you want to overwrite it?'
% remote_filename, default=False)):
print('\nDumping DB...')
with settings(hide('stderr')):
run('mysqldump -u %s %s %s > %s' %
(env['db_user'], env['db_password_opt'],
env['db_name'], remote_filename))
get(remote_filename, '.')
run('rm %s' % (remote_filename))
else:
print('\nAborting.')
else:
print('\nAborting.')
def update_code(branch="master"):
"""Updates the source code and its requirements"""
require('environment', provided_by=[ubuntu, fedora])
print('Getting the latest code and dependencies...')
with settings(hide('stdout', 'stderr')):
_checkout_repo(branch=branch)
_update_requirements()
def deploy(branch="master"):
"""Updates the code and installs the production site"""
require('environment', provided_by=[ubuntu, fedora])
print('Deploying the site...')
with settings(hide('stdout', 'stderr')):
with prefix('source %(env_path)s/bin/activate' % env):
update_code(branch=branch)
install_site()
def install_site():
"""Configures the server and enables the site"""
require('environment', provided_by=[ubuntu, fedora])
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=[ubuntu, fedora])
with settings(hide('stdout', 'stderr')):
# Configure VirtualHost
upload_template('deploy/%(environment)s/virtualhost.conf' % env,
env.vhost_file, context=env, use_sudo=True)
# Configure and install settings
upload_template('deploy/%(environment)s/settings.conf' % env,
'%(project_settings_path)s'
% env, context=env)
def enable_site():
"""Enables the site"""
require('environment', provided_by=[ubuntu, fedora])
with settings(hide('stdout', 'stderr')):
_switch_site(True)
def disable_site():
"""Disables the site"""
require('environment', provided_by=[ubuntu, fedora])
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)
if env.environment == 'ubuntu':
env.apache_command = 'a2ensite' if enable else 'a2dissite'
sudo('%(apache_command)s %(project_name)s' % env)
sudo('service apache2 reload')
elif env.environment == 'fedora':
sudo('service httpd restart')
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)