-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
95 lines (72 loc) · 2.91 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
from datetime import datetime
from fabric.api import *
env.roledefs = {
'production': [],
'staging': [],
}
@roles('production')
def deploy_production():
# Remove this line when you're happy that this task is correct
raise RuntimeError("Please check the fabfile before using it")
run('git pull')
run('pip install -r requirements.txt')
run('django-admin migrate --noinput')
run('django-admin collectstatic --noinput')
run('django-admin compress')
# 'restart' should be an alias to a script that restarts the web server
run('restart')
@roles('staging')
def deploy_staging():
# Remove this line when you're happy that this task is correct
raise RuntimeError("Please check the fabfile before using it")
run('git pull')
run('pip install -r requirements.txt')
run('django-admin migrate --noinput')
run('django-admin collectstatic --noinput')
run('django-admin compress')
# 'restart' should be an alias to a script that restarts the web server
run('restart')
def _pull_data(env_name, remote_db_name, local_db_name, remote_dump_path, local_dump_path):
timestamp = datetime.now().strftime('%Y%m%d-%I%M%S')
filename = '.'.join([env_name, remote_db_name, timestamp, 'sql'])
remote_filename = remote_dump_path + filename
local_filename = local_dump_path + filename
params = {
'remote_db_name': remote_db_name,
'remote_filename': remote_filename,
'local_db_name': local_db_name,
'local_filename': local_filename,
}
# Dump/download database from server
run('pg_dump {remote_db_name} -xOf {remote_filename}'.format(**params))
run('gzip {remote_filename}'.format(**params))
get('{remote_filename}.gz'.format(**params), '{local_filename}.gz'.format(**params))
run('rm {remote_filename}.gz'.format(**params))
# Load database locally
local('gunzip {local_filename}.gz'.format(**params))
local('dropdb {local_db_name}'.format(**params))
local('createdb {local_db_name}'.format(**params))
local('psql {local_db_name} -f {local_filename}'.format(**params))
local('rm {local_filename}'.format(**params))
@roles('production')
def pull_production_data():
# Remove this line when you're happy that this task is correct
raise RuntimeError("Please check the fabfile before using it")
_pull_data(
env_name='production',
remote_db_name='webwonking',
local_db_name='webwonking',
remote_dump_path='/usr/local/django/webwonking/tmp/',
local_dump_path='/tmp/',
)
@roles('staging')
def pull_staging_data():
# Remove this line when you're happy that this task is correct
raise RuntimeError("Please check the fabfile before using it")
_pull_data(
env_name='staging',
remote_db_name='webwonking',
local_db_name='webwonking',
remote_dump_path='/usr/local/django/webwonking/tmp/',
local_dump_path='/tmp/',
)