-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfabfile.py
498 lines (414 loc) · 16.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/bin/python
# coding=utf-8
"""Fabfile for changelog app."""
# ~/fabfile.py
# A Fabric file for carrying out various administrative tasks.
# Tim Sutton, Jan 2013
# To use this script make sure you have fabric and fabtools.
# pip install fabric fabtools
import os
import getpass
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabric.api import (
task, env, fastprint, cd, run, sudo, local, prompt, put, get)
from fabric.decorators import hosts
from fabric.contrib.files import exists
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabric.contrib.project import rsync_project
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabric.contrib.files import exists, sed
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabric.colors import red, blue, green
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabtools import require
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabtools.deb import update_index
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabtools.postgres import (
database_exists,
create_database,
create_user,
user_exists
)
# Don't remove even though its unused
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabtools.vagrant import vagrant
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.dropbox import setup_dropbox, setup_dropbox_daemon
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.django import setup_apache, build_pil, set_media_permissions
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.git import update_git_checkout
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.virtualenv import setup_venv
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.common import setup_env, show_environment
# noinspection PyUnresolvedReferences,PyPackageRequirements
from fabgis.postgres import (
create_user,
get_postgres_dump,
restore_postgres_dump,
setup_postgis_2,
create_postgis_2_template)
def get_vars():
"""Helper method to get standard deployment vars.
:returns: A tuple containing the following:
* base_path: Workspace dir e.g. ``/home/foo/python``
* code_path: Project dir e.g. ``/home/foo/python/projecta``
* git_url: Url for git checkout - use http for read only checkout
* repo_alias: Name of checkout folder e.g. ``projecta``
* site_name: Name for the web site e.g. ``projecta``
:rtype: tuple
"""
setup_env()
site_name = 'projecta'
base_path = '/home/web/'
git_url = 'http://github.com/timlinux/projecta.git'
repo_alias = 'projecta'
code_path = os.path.abspath(os.path.join(base_path, repo_alias))
return base_path, code_path, git_url, repo_alias, site_name
@task
def update_venv(code_path):
"""Update the virtual environment to ensure it has node etc. installed.
:param code_path: Directory in which project is located.
:type code_path: str
e.g.::
fab -H localhost update_venv:/home/timlinux/dev/python/projecta
"""
setup_venv(code_path, requirements_file='REQUIREMENTS.txt')
#yuglify needed by django-compress needs to have node installed which
# is provided by virtual-node in the REQUIREMENTS file above,
# but we still need to install yuglify manually since we cant add it
# using REQUIREMENTS.
# See also https://github.com/elbaschid/virtual-node
# for more info on how virtualnode works
with cd(code_path):
run('venv/bin/npm -g install yuglify')
build_pil(code_path)
@task
def setup_postgres_user():
"""Set up the postgresql instance."""
create_user('wsgi', '')
@task
def upload_postgres_dump(dump_path):
"""Upload the dump found in dump_path and restore it to the server.
.. note existing data on the server will be destroyed.
:param dump_path: Local file containing dump to be restored.
:type dump_path; str
"""
restore_postgres_dump(
'changelog',
user='wsgi',
ignore_permissions=True,
file_name=dump_path)
@task
def update_apache(code_path):
"""Update the apache configuration prompting for github account info.
.. note:: The config file is taken from the local system.
:param code_path: Usually '/home/web/<site_name>'
"""
git_url = 'https://api.github.com/repos/timlinux/projecta/issues'
git_user = prompt(
'Please enter the github user account that issues submitted via the\n'
'web ui should be created as. This will be written into the apache\n'
'configuration file under /etc/apache2/sites-available so you\n'
'should take appropriate security measures.\nUser :\n')
git_password = getpass.getpass()
domain = 'changelog.kartoza.com'
setup_apache(
site_name='projecta',
code_path=code_path,
domain=domain,
github_url=git_url,
github_password=git_password,
github_user=git_user)
@task
def deploy():
"""Initialise or update the git clone - you can safely rerun this.
e.g. to update the server
fab -H <host> deploy
"""
# Ensure we have a mailserver setup for our domain
# Note that you may have problems if you intend to run more than one
# site from the same server
setup_env()
show_environment()
setup_postgis_2()
base_path, code_path, git_url, repo_alias, site_name = get_vars()
fastprint('Checking out %s to %s as %s' % (git_url, base_path, repo_alias))
update_git_checkout(base_path, git_url, repo_alias)
update_index()
require.postfix.server(site_name)
update_apache(code_path)
require.deb.package('python-dev')
require.deb.package('libpq-dev')
require.deb.package('libgeos-c1')
require.deb.package('vim')
require.deb.package('curl')
require.deb.package('pandoc')
update_venv(code_path)
set_db_permissions()
with cd(os.path.join(code_path, 'django_project')):
run('../venv/bin/python manage.py syncdb --noinput ')
run('../venv/bin/python manage.py migrate')
# if we are testing under vagrant, deploy our local media and db
#if 'vagrant' in env.fg.home:
# with cd(code_path):
# run('cp /vagrant/projecta.db .')
# run('touch django_project/core/wsgi.py')
#sync_media_to_server()
collectstatic()
fastprint('*******************************************\n')
fastprint(red(' Don\'t forget set ALLOWED_HOSTS in '))
fastprint(' django_project/core/settings/prod.py')
fastprint(' to the domain name for the site.')
fastprint('*******************************************\n')
@hosts('kartoza3')
@task
def backup():
"""Make a local backup of the production instance."""
get_live_media() # should fetch from kartoza3
get_private() # should fetch from kartoza3
get_live_db() # should fetch from kartoza3
@hosts('kartoza3')
@task
def freshen():
"""Freshen the server with latest git copy and touch wsgi.
.. note:: Preferred normal way of doing this is rather to use the
sync_project_to_server task and not to checkout from git.
"""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
git_url = 'http://github.com/timlinux/projecta.git'
update_git_checkout(base_path, git_url, repo_alias)
put_private()
update_venv(code_path)
update_migrations()
with cd(os.path.join(code_path, 'django_project')):
run('touch core/wsgi.py')
collectstatic()
fastprint('*******************************************\n')
fastprint(red(' Don\'t forget set ALLOWED_HOSTS in \n'))
fastprint(' django_project/core/settings/prod.py\n')
fastprint(' to the domain name for the site.\n')
fastprint('*******************************************\n')
# noinspection PyUnresolvedReferences
@task
def sync_media_to_server():
"""Sync media to server from local filesystem."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
remote_path = os.path.join(code_path, 'django_project', 'media')
local_path = os.path.join(
os.path.dirname(__file__), 'django_project', 'media/')
rsync_project(
remote_path,
local_dir=local_path,
exclude=['*.pyc', '*.py', '.DS_Store'])
# Now our sqlite db
remote_path = os.path.join(
code_path, 'resources', 'sqlite', 'projecta.db')
local_path = os.path.join(
os.path.dirname(__file__), 'resources/sqlite/projecta.db')
rsync_project(
remote_path,
local_dir=local_path,
exclude=['*.pyc', '*.py', '.DS_Store'])
set_media_permissions(code_path)
set_db_permissions()
@task
def sync_project_to_server():
"""Synchronize project with webserver ignoring venv and sqlite db..
This is a handy way to get your secret key to the server too...
"""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
update_venv(code_path)
#noinspection PyArgumentEqualDefault
rsync_project(
base_path,
delete=False,
exclude=[
'*.pyc',
'.git',
'*.dmp',
'.DS_Store',
'projecta.db',
'venv',
'django_project/static'])
update_migrations()
with cd(os.path.join(code_path, 'django_project')):
run('touch core/wsgi.py')
set_media_permissions(code_path)
set_db_permissions()
update_migrations()
collectstatic()
fastprint(blue(
'Your server is now in synchronised to your local project\n'))
@task
def server_to_debug_mode():
"""Put the server in debug mode (normally not recommended)."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
config_file = os.path.join(
code_path, 'django_project', 'core', 'settings', 'project.py')
sed(
config_file,
'DEBUG = TEMPLATE_DEBUG = False',
'DEBUG = TEMPLATE_DEBUG = True')
with cd(os.path.join(code_path, 'django_project')):
run('touch core/wsgi.py')
set_db_permissions()
collectstatic()
fastprint(red('Warning: your server is now in DEBUG mode!\n'))
@task
def server_to_production_mode():
"""Put the server in production mode (recommended)."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
config_file = os.path.join(
code_path, 'django_project', 'core', 'settings', 'project.py')
sed(
config_file,
'DEBUG = TEMPLATE_DEBUG = True',
'DEBUG = TEMPLATE_DEBUG = False')
with cd(os.path.join(code_path, 'django_project')):
run('touch core/wsgi.py')
set_db_permissions()
collectstatic()
fastprint(blue('Note: your server is now in PRODUCTION mode!\n'))
@task
def set_db_permissions():
"""Set the db so user wsgi has all permissions.
"""
user = 'wsgi'
dbname = 'changelog'
if not user_exists(user):
create_user(user, password='vagrant')
if not database_exists(dbname):
create_database(dbname, user)
grant_sql = 'GRANT ALL ON schema public to %s;' % user
# assumption is env.repo_alias is also database name
run('psql %s -c "%s"' % (dbname, grant_sql))
grant_sql = (
'GRANT ALL ON ALL TABLES IN schema public to %s;' % user)
# assumption is env.repo_alias is also database name
run('psql %s -c "%s"' % (dbname, grant_sql))
grant_sql = (
'GRANT ALL ON ALL SEQUENCES IN schema public to %s;' % user)
run('psql %s -c "%s"' % (dbname, grant_sql))
@hosts('kartoza3')
@task
def get_live_db():
"""Get the live db - will overwrite your local copy."""
get_postgres_dump('changelog')
@hosts('kartoza3')
@task
def get_docker_live_db():
"""Get a copy of the db as deployed in docker on the remote host."""
date = run('date +%d-%B-%Y')
my_file = 'projecta-%s.dmp' % date
ip = run("export PATH=$PATH:/home/timlinux/bin/docker-helpers/; dipall | grep projecta_db_1 | awk '{print $3}'")
run(
'export PGPASSWORD=docker; '
'pg_dump -Fc -f /tmp/%s -h %s -U docker gis' % (my_file, ip))
get('/tmp/%s' % my_file, my_file)
@hosts('kartoza3')
@task
def get_docker_live_media():
"""Get the live media - will overwrite your local copy."""
local('rsync -ave ssh %s:/home/timlinux/production-sites/projecta/django_project/media deployment/media' % env['host_string'])
@hosts('kartoza3')
@task
def get_live_media():
"""Get the live media - will overwrite your local copy."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
path = '%s/django_project/media' % code_path
local_path = os.path.join(
os.path.dirname(__file__),'deployment', 'media')
if not exists(local_path):
run('mkdir -p %s' % local_path)
local('rsync -ave ssh %s:%s/django_project/media/* %s'
% (env['host_string'], code_path, local_path))
@task
def collectstatic():
"""Collect static using proper path for django-compressor / yuglify.
.. note:: We are using python-node to run node in a virtual environment.
Node is needed for yuglify, which is in turn needed by
django-compressor which combines all js resources into a single file
and all css into a single file. These are 'compiled' but yuglify.
Yuglify does not appear in the path properly thus we explicitly
ensure the path points to the node_modules dir before running
collectstatic.
All the above will prevent run time errors like:
[Django] ERROR: Failed to submit message: u"ValueError: The file
'css/contrib.css' could not be found with <pipeline.storage.
PipelineCachedStorage object at 0x7f0df18b3ad0>."
"""
command = ('PATH=$PATH:../node_modules/yuglify/bin/:../venv/bin/ '
'../venv/bin/python manage.py collectstatic --noinput')
base_path, code_path, git_url, repo_alias, site_name = get_vars()
with cd(os.path.join(code_path, 'django_project')):
run(command)
@task
def update_migrations():
"""Apply any pending south migrations.
"""
command = '../venv/bin/python manage.py migrate'
base_path, code_path, git_url, repo_alias, site_name = get_vars()
with cd(os.path.join(code_path, 'django_project')):
run(command)
run('touch core/wsgi.py')
fastprint(green('Note: your server is now has the latest SOUTH '
'migrations applied.\n'))
@task
def put_private():
"""Copy the private.py with site specific settings to the server."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
local_path = os.path.join(
'django_project', 'core', 'settings', 'private.py')
if not os.path.exists(local_path):
fastprint(red('Please create your local copy of private.py\n'))
fastprint(red('See README.md for more instructions.'))
raise Exception('Could not copy local private settings to server')
remote_path = os.path.join(code_path, local_path)
put(local_path=local_path, remote_path=remote_path)
@hosts('kartoza3')
@task
def get_private():
"""Copy the private.py with site specific settings to the server."""
base_path, code_path, git_url, repo_alias, site_name = get_vars()
local_path = os.path.join(
'django_project', 'core', 'settings', 'private.py')
remote_path = os.path.join(code_path, local_path)
if not exists(remote_path):
fastprint(red('Please create your remove copy of private.py\n'))
fastprint(red('See README.md for more instructions.'))
raise Exception('Could not get remote private settings')
get(remote_path=remote_path, local_path=local_path)
@task
def set_up_disqus(shortname):
"""Set up configuration for disqus.
You can get shortname from http://disqus.com/admin/create/ and you must
have disqus account to do it.
"""
# Absolute filesystem path to the Django project directory:
django_root = os.path.dirname(os.path.abspath(__file__))
secret_file = os.path.join(
django_root, 'django_project', 'core', 'settings', 'secret.py')
with open(secret_file) as f:
lines = f.readlines()
# Update existing secret_file with new shortname
with open(secret_file, 'w') as f:
added = False
added_line = "DISQUS_WEBSITE_SHORTNAME = " + repr(shortname) + "\n"
for line in lines:
if 'DISQUS_WEBSITE_SHORTNAME' in line:
line = added_line
added = True
f.write(line)
if not added:
f.write(added_line)
@hosts('localhost')
@task
def restore_postgres_dump_locally():
"""Restore postgresql dump to local host db: changelog."""
restore_postgres_dump(
dbname='changelog',
user='docker',
password='docker')