-
Notifications
You must be signed in to change notification settings - Fork 13
/
fabfile.py
65 lines (51 loc) · 1.67 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
import datetime
import posixpath
from fabric import task
"""
To use this file, first `pip install fabric` then run:
fab -H [email protected] deploy --prompt-for-sudo-password
"""
CODE_ROOT = "/home/cstock/www/cstock/code_root/"
VIRTUALENV_ROOT = '/home/cstock/.virtualenvs/cstock/'
BRANCH = "main"
@task
def deploy(c):
"""
Deploy code to remote host by checking out the latest via git.
"""
start = datetime.datetime.now()
update_code(c)
update_virtualenv(c)
django_stuff(c)
services_restart(c)
print(f"deploy completed in {datetime.datetime.now() - start}")
def update_code(c):
print("updating code...")
with c.cd(CODE_ROOT):
c.run("git fetch")
c.run(f"git checkout {BRANCH}")
c.run(f"git reset --hard origin/{BRANCH}")
c.run("find . -name '*.pyc' -delete")
def update_virtualenv(c):
"""
Update external dependencies on remote host assumes you've done a code update.
"""
print("updating requirements...")
files = (
posixpath.join(CODE_ROOT, "requirements.txt"),
)
with c.prefix("source {}/bin/activate".format(VIRTUALENV_ROOT)):
for req_file in files:
c.run("pip install -r {}".format(req_file))
def django_stuff(c):
"""
staticfiles, migrate, etc.
"""
print("Running migrations and building staticfiles...")
with c.cd(CODE_ROOT):
c.run("{}/bin/python manage.py migrate".format(VIRTUALENV_ROOT))
c.run("{}/bin/python manage.py collectstatic --noinput".format(VIRTUALENV_ROOT))
def services_restart(c):
print("Restarting services...")
c.sudo("sudo supervisorctl stop all")
c.sudo("sudo supervisorctl start all")