-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfabfile.py
95 lines (80 loc) · 2.65 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
import os
from fabric import Connection
from fabric.tasks import task
from patchwork.transfers import rsync
USERNAME = os.getenv('USERNAME')
SSH_PASSPHRASE = os.getenv('SSH_PASSPHRASE')
PIPENV = f'/home/{USERNAME}/.local/bin/pipenv'
TARGET_DIRECTORY = os.getenv('TARGET_DIRECTORY', f'/home/{USERNAME}/project-root')
def get_instance_ips():
"""
Function to get instance IPs of your servers.
Sometimes you'd want to do this, if your IPs are
allocated dynamically, e.g. an auto-scaling group
within AWS.
"""
return []
def get_hosts():
"""
Returns a list of dictionaries which can be passed as keyword arguments
to instantiate a `Connection`.
Example:
>>> hosts = get_hosts()
>>> c = Connection(**hosts[0])
"""
ips = sorted(get_instance_ips())
return [{
'host': f"{USERNAME}@{ip}",
"connect_kwargs": {"passphrase": SSH_PASSPHRASE},
} for ip in ips]
@task
def rsync_to_remote(c):
"""
Does an rsync to the target directory in
the remote server. Only copies over versioned files
(ignores everything in `.gitignore`)
"""
rsync(
c,
source="./",
target=TARGET_DIRECTORY,
exclude=(".git/",),
delete=True,
rsync_opts="--filter=\":- .gitignore\"",
)
@task
def host_full_deploy(c, migrate=True, collectstatic=True, dependencies=True):
"""
Full deployment for a host, including migrations and collectstatic
"""
rsync_to_remote(c)
with c.cd(TARGET_DIRECTORY):
if dependencies:
print("Installing dependencies...\n")
c.run(f"{PIPENV} install")
if migrate:
print("Migrating database...\n")
c.run(f"{PIPENV} run python manage.py migrate")
if collectstatic:
print("Running collectstatic...\n")
c.run(f"{PIPENV} run python manage.py collectstatic")
c.run("sudo supervisorctl reload gunicorn")
@task
def deploy(c, migrate=True, dependencies=True, collectstatic=False):
"""
Main command line task to deploy
"""
print("START DEPLOYING....")
for index, host in enumerate(get_hosts()):
print(f"****** Deploying to host {index} at {host['host']} ******")
remote = Connection(**host)
if index == 0:
host_full_deploy(
remote,
migrate=migrate,
collectstatic=collectstatic,
dependencies=dependencies,
)
else:
# already migrated and run collectstatic, no need to run again for other hosts
host_full_deploy(remote, migrate=False, collectstatic=False, dependencies=dependencies)