This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfabfile.py
148 lines (107 loc) · 3.98 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
from contextlib import nested
from datetime import datetime
import os
import dj_database_url
from fabric.api import (
cd, env, execute, get, local, prefix, run, settings, shell_env, task
)
from fabric.context_managers import quiet
env.use_ssh_config = True
env.hosts = ['rank-me.io']
env.site_user = 'rankme'
env.site_group = 'rankme'
env.project_root = '/var/www/rank-me/rankme'
def push_commit(commit):
with settings(warn_only=True):
local("git push -f origin %s" % commit)
def checkout_tag(commit):
local("git checkout %s" % commit)
def update_remote_git(commit):
with cd(env.project_root):
run("git fetch -t -p")
run("git checkout %s" % commit)
def install_requirements():
with cd(env.project_root):
with prefix("source ../ENV/bin/activate"):
run("pip install -r requirements/base.txt")
def migrate_database():
with cd(env.project_root):
with prefix("source ../ENV/bin/activate"):
run("python manage.py migrate")
def install_static():
with cd(env.project_root):
with prefix("source ../ENV/bin/activate"):
run("python manage.py collectstatic --noinput")
@task
def restart_process():
with cd(env.project_root):
run('touch rankme/wsgi.py')
@task
def compile_css():
with cd(env.project_root):
run('npm install')
run('gulp build --production')
@task
def deploy(commit='origin/master'):
execute(push_commit, commit=commit)
execute(update_remote_git, commit=commit)
execute(install_requirements)
execute(compile_css)
execute(install_static)
execute(migrate_database)
execute(restart_process)
@task
def fetch_db(destination='.'):
"""
Dump the database on the remote host and retrieve it locally.
The destination parameter controls where the dump should be stored locally.
"""
with nested(cd(env.project_root), quiet()):
db_credentials = run('cat envdir/DATABASE_URL')
db_credentials_dict = dj_database_url.parse(db_credentials)
if not is_supported_db_engine(db_credentials_dict['ENGINE']):
raise NotImplementedError(
"The fetch_db task only supports postgresql databases"
)
outfile = datetime.now().strftime('%Y-%m-%d_%H%M%S.sql.gz')
outfile_remote = os.path.join('~', outfile)
with shell_env(PGPASSWORD=db_credentials_dict['PASSWORD'].replace('$', '\$')):
run('pg_dump -O -x -h {host} -U {user} {db}|gzip > {outfile}'.format(
host=db_credentials_dict['HOST'],
user=db_credentials_dict['USER'],
db=db_credentials_dict['NAME'],
outfile=outfile_remote))
get(outfile_remote, destination)
run('rm %s' % outfile_remote)
return outfile
@task
def import_db(dump_file=None):
"""
Restore the given database dump.
The dump must be a gzipped SQL dump. If the dump_file parameter is not set,
the database will be dumped and retrieved from the remote host.
"""
with open('envdir/DATABASE_URL', 'r') as db_credentials_file:
db_credentials = db_credentials_file.read()
db_credentials_dict = dj_database_url.parse(db_credentials)
if not is_supported_db_engine(db_credentials_dict['ENGINE']):
raise NotImplementedError(
"The import_db task only supports postgresql databases"
)
if dump_file is None:
dump_file = fetch_db()
db_info = {
'host': db_credentials_dict['HOST'],
'user': db_credentials_dict['USER'],
'db': db_credentials_dict['NAME'],
'db_dump': dump_file
}
with shell_env(PGPASSWORD=db_credentials_dict['PASSWORD']):
with settings(warn_only=True):
local('dropdb -h {host} -U {user} {db}'.format(**db_info))
local('createdb -h {host} -U {user} {db}'.format(**db_info))
local('gunzip -c {db_dump}|psql -h {host} -U {user} {db}'.format(
**db_info
))
def is_supported_db_engine(engine):
return engine == 'django.db.backends.postgresql_psycopg2'