This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
fabfile.py
113 lines (88 loc) · 2.8 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
import time
from fabric.api import run, execute, env
# which settings file to use in production
environment = "production"
# depends on virtualenvwrapper with a named virtualenv to `workon`
virtualenv = "foia"
# name of WSGI wrapper for gunicorn to work with
wsgi = "foia_hub.wsgi:application"
# use `-H foia` or something to control where fabric runs
env.use_ssh_config = True
# using master branch for now, we could change this
branch = "master"
repo = "[email protected]:18f/foia-hub.git"
# base directory structure -
# current/
# versions/
# 20140914123412/
# ...
# shared/
# log/
# settings.py
# always UTC datestamp, so it doesn't matter what computer does the deploy
datestamp = time.strftime("%Y%m%d%H%M%S", time.gmtime(time.mktime(time.localtime())))
home = "/home/foia/hub"
shared_path = "%s/shared" % home
versions_path = "%s/versions" % home
version_path = "%s/%s" % (versions_path, datestamp)
current_path = "%s/current" % home
logs = "%s/log" % shared_path
env = "%s/foia-env" % shared_path
# keep the last 5 deployed versions on disk
keep = 5
def checkout():
run('git clone -q -b %s %s %s' % (branch, repo, version_path))
def dependencies():
run('workon %s && cd %s && pip install -r requirements.txt' % (virtualenv, version_path))
run('cd %s/deploy && npm install' % version_path)
def migrate():
run('workon %s && source %s && cd %s && python manage.py migrate' % (virtualenv, env, version_path))
def make_current():
run('rm -f %s && ln -s %s %s' % (current_path, version_path, current_path))
def cleanup():
versions = run("ls -x %s" % versions_path).split()
destroy = versions[:-keep]
for version in destroy:
command = "rm -rf %s/%s" % (versions_path, version)
run(command)
# can be run on their own
def start():
"""
Gunicorn start.
"""
run(
(
"workon %s && source %s && " +
"cd %s && " +
"gunicorn -c %s/config.py %s"
) % (virtualenv, env, current_path, shared_path, wsgi), pty=False
)
# config.py is expected to point the .pid to the shared/ dir
def stop():
run("kill `cat %s/gunicorn.pid`" % shared_path)
def restart():
run("kill -HUP `cat %s/gunicorn.pid`" % shared_path)
def deploy():
"""
Usual deploy script. Expects app to be running.
Checks out code, makes symlinks, installs dependencies,
restarts app, cleans up old deploys.
"""
execute(checkout)
execute(dependencies)
execute(migrate)
execute(make_current)
execute(restart)
execute(cleanup)
def deploy_cold():
"""
Similar to normal deploy, but expects the app to be stopped.
Doesn't bother doing cleanup.
"""
execute(checkout)
execute(dependencies)
execute(migrate)
execute(make_current)
execute(start)
def test():
run("uname -a")