-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
80 lines (64 loc) · 2.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
import os
from fabric.api import *
from fabric.operations import get, put
from fabric.contrib.console import confirm
from fabric.contrib.project import rsync_project
from fab_settings import PROJECT_NAME, PROJECT_HOST, PROJECT_USER, PROJECT_DB_NAME, SUDOER_USER
env.forward_agent = True
env.project_name = PROJECT_NAME
env.project_user = PROJECT_USER
env.roledefs = {
PROJECT_USER: ['%s@%s' % (PROJECT_USER, PROJECT_HOST)],
'sudoer': ['%s@%s' % (SUDOER_USER, PROJECT_HOST)]
}
env.hosts = [PROJECT_HOST]
env.project_path = "/home/" + env.project_name + "/www/" + env.project_name + "/"
env.python_path = "/home/" + env.project_name + "/.virtualenvs/" + env.project_name + "/bin/python"
env.pip_path = "/home/" + env.project_name + "/.virtualenvs/" + env.project_name + "/bin/pip"
env.project_media_dir = '/var/www/%s/media/' % env.project_name
@roles('%s' % PROJECT_USER)
def git_status():
with cd(env['project_path']):
run('git fetch && git status')
@roles('%s' % PROJECT_USER)
def _git_update(branch):
with settings(user=PROJECT_USER):
with cd(env['project_path']):
run('git fetch --all')
run('git checkout %s' % branch)
run('git reset --hard origin/%s' % branch)
@roles('sudoer')
def reloadapp():
sudo('supervisorctl restart %s' % env.project_name, shell=False)
@roles('%s' % PROJECT_USER)
def release(run_migrate=True, static=True, branch='master'):
_git_update(branch)
run('%s install -r %spip-requirements.txt' %
(env['pip_path'], env['project_path']))
with cd(env['project_path']):
if run_migrate:
migrate()
_run_manage('compilemessages')
if static:
_run_manage('collectstatic --noinput')
reloadapp()
@roles('%s' % PROJECT_USER)
def migrate():
with cd(env['project_path']):
_run_manage('migrate')
@roles('%s' % PROJECT_USER)
def pulldb():
filename = 'mysql_dumped.sql'
dump_file = '%s' % filename
run(_dump_mysql_data(dump_file))
get(dump_file, '.')
run('rm %s' % dump_file)
if confirm("Load dumped remote data into local DB?"):
local('mysql --defaults-file=".mysqldump" %s < %s' % (env['project_name'], filename))
def _run_manage(command):
run("%s ./manage.py %s" % (env['python_path'], command))
def _dump_mysql_data(file_path):
return 'mysqldump --defaults-file=".mysqldump_cnf" --single-transaction %s > %s' % (PROJECT_DB_NAME, file_path)
@roles('%s' % PROJECT_USER)
def syncmedia():
get(env['project_media_dir'], local_path=".")