-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
114 lines (93 loc) · 3.15 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
from fabric.api import *
from fabric.contrib.project import rsync_project
from fabric.operations import local as lrun
from unipath import Path
ACTIVATE = 'source {0}/bin/activate'
UPDATE_REQS = '{0} install -r {1}requirements/{2}.txt'
MANAGE = '{0} manage.py '
COLLECT_STATIC = 'collectstatic --noinput --settings {0}.settings.{1}'
SYNCDB = 'syncdb --settings {0}.settings.{1}'
MIGRATE = 'migrate --settings {0}.settings.{1}'
@task
def local():
env.run = lrun
env.cd = lcd
env.name = 'local'
env.hosts = ['localhost']
env.path = Path(__file__).ancestor(1) + "/"
env.project = 'fairplay'
env.virtualenv = 'virtualenv -p python3'
env.environment = env.path + 'venv'
env.python = 'python'
env.pip = 'pip'
env.manage = MANAGE.format(env.python)
env.restart = ('echo "You must use manage.py runserver"',)
@task
def production():
env.run = run
env.cd = cd
env.user = 'fairland'
env.name = 'production'
env.hosts = ['scores.fairlandboysgymnastics.org']
env.path = '/srv/fairplay/'
env.project = 'fairplay'
env.virtualenv = 'virtualenv -p python3'
env.environment = env.path + 'venv'
env.warn_only = True
env.python = 'source {0}venv/bin/activate && python'.format(env.path)
env.pip = 'source {0}venv/bin/activate && pip'.format(env.path)
env.manage = MANAGE.format(env.python)
env.restart = ('sudo systemctl restart uwsgi',
'sudo systemctl restart nginx',)
@task
def bootstrap():
upload()
env.cd(env.path)
env.run('rm -rf {0}'.format(env.environment))
env.run('mkdir -p {0}'.format(env.environment))
env.run('{0} {1} --no-site-packages'.format(
env.virtualenv, env.environment))
update_requirements()
@task
def upload():
if 'localhost' not in env.hosts:
extra_opts = '--omit-dir-times'
put('requirements', env.path)
rsync_project(remote_dir=env.path,
local_dir=env.project,
delete=True,
extra_opts=extra_opts,
exclude=('{0}/local_static/'.format(env.project),
'{0}/local_media/'.format(env.project),
'{0}/local_logs/'.format(env.project),
'*.pyc',
'*.pyo',
'__pycache__',))
@task
def update_requirements():
with prefix(ACTIVATE.format(env.environment)):
env.run(UPDATE_REQS.format(env.pip, env.path, env.name))
@task
def collectstatic():
with env.cd(env.path + env.project):
with prefix(ACTIVATE.format(env.environment)):
env.run(env.manage + COLLECT_STATIC.format(env.project, env.name))
@task
def migrate():
with env.cd(env.path + env.project):
with prefix(ACTIVATE.format(env.environment)):
env.run(env.manage + MIGRATE.format(env.project, env.name))
@task
def restart():
with settings(warn_only=True):
for cmd in env.restart:
env.run(cmd)
@task
def shell():
lrun('ssh {}@{} '.format(env.user, env.hosts[0]))
@task
def deploy():
upload()
collectstatic()
migrate()
restart()