-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
fabfile.py
145 lines (106 loc) · 3.84 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from pathlib import Path
from dotenv import load_dotenv
from fabric import Connection, task
load_dotenv()
APP_DIRECTORY = Path(os.environ["SERVER_APP_DIRECTORY"])
UPLOADS_DIRECTORY = Path(os.environ["SERVER_UPLOADS_DIRECTORY"])
SECRETS_DIRECTORY = Path(os.environ["SERVER_SECRETS_DIRECTORY"])
USER = os.environ["SERVER_USER"]
HOST = os.environ["SERVER_HOST"]
r = Connection(f"root@{HOST}")
c = Connection(f"{USER}@{HOST}")
@task
def init_python_3_10(_):
py_3_10 = "https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz"
r.sudo("apt-get install libssl-dev openssl make gcc")
r.sudo("apt-get install libsqlite3-dev")
with c.cd("/opt"):
c.sudo("cd /opt")
c.sudo(f"wget {py_3_10}")
c.sudo("tar xzvf Python-3.10.4.tgz")
with c.cd("/opt/Python-3.10.4"):
c.sudo("./configure --enable-loadable-sqlite-extensions")
c.sudo("make")
c.sudo("make install")
c.sudo("ln -fs /opt/Python-3.10.4 /usr/bin/python3.10")
c.run("python3.10 --version")
@task
def init_secrets(_):
c.run(f"mkdir -p {SECRETS_DIRECTORY}")
json_path = str(SECRETS_DIRECTORY / "google-cloud-credentials.json")
c.put("production/google-cloud-credentials.json", json_path)
@task
def init_repo(_):
url = "https://github.com/ambuda-org/ambuda.git"
with c.cd(APP_DIRECTORY):
c.run("git init .")
c.run(f"git remote add origin {url}")
deploy(c)
def deploy_to_commit(_, pointer: str):
"""Deploy the given branch pointer to production.
:param pointer: a commit SHA, branch name, etc.
"""
with c.cd(APP_DIRECTORY):
# Fetch the application code.
c.run("git fetch origin")
c.run("git checkout main")
c.run(f"git reset --hard {pointer}")
# Install project requirements.
c.run("python3.10 -m venv env")
c.run("make install-python")
c.run("make install-frontend")
# Verify that unit tests pass on prod.
with c.prefix("source env/bin/activate"):
c.run("make test")
# Copy production config settings.
env_path = str(APP_DIRECTORY / ".env")
c.put("production/prod-env", env_path)
# Build i18n and l10n files
with c.prefix("source env/bin/activate"):
c.run("make install-i18n")
# Verify that the production setup is well-formed.
with c.prefix("source env/bin/activate"):
c.run("python -m scripts.check_prod_setup")
# Upgrade the database last -- If we upgrade and a downstream check
# fails, we'll affect the production application.
# FIXME: but, what if we upgrade then app restart fails? Should we stop
# the prod server first? Surely there's a saner way to manage this.
c.run("make upgrade")
print("Restarting application ...")
restart_application(_)
print("Restarting Celery task runner ...")
restart_celery(_)
c.local("python test_prod.py")
print("Deploy complete")
@task
def deploy(_):
"""Deploy the latest production commit."""
deploy_to_commit(_, "origin/main")
@task
def rollback(_, commit):
"""Roll back to a specific prior commit.
:param commit: the commit SHA to roll back to.
"""
deploy_to_commit(_, commit)
@task
def restart_application(_):
"""Restart the production gunicorn instance."""
r.run("systemctl restart ambuda")
@task
def restart_celery(_):
"""Restart the production celery instance."""
r.run("systemctl restart celery")
@task
def run_module(_, module):
assert module
with c.cd(APP_DIRECTORY):
with c.prefix("source env/bin/activate"):
print(f"{module}")
print("=" * len(module))
c.run(f"python -m {module}")
@task
def seed_gretil(_):
with c.cd(APP_DIRECTORY):
c.run("./scripts/fetch-gretil-data.sh")
run_module(_, "ambuda.seed.gretil")