forked from ambuda-org/ambuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
152 lines (110 loc) · 4.02 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
146
147
148
149
150
151
152
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 https://github.com/ambuda-org/ambuda.git")
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")
with c.prefix("source env/bin/activate"):
c.run("pip install -r requirements.txt")
# Build production frontend assets.
c.run("npm install")
c.run("make css-prod")
c.run("make js-prod")
# 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)
# 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.
upgrade_db(_)
print("Restarting application ...")
restart_application(_)
print("Restarting Celery task runner ...")
restart_celery(_)
print("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)
def upgrade_db(_):
"""Upgrade to the latest database migration."""
with c.prefix("source env/bin/activate"):
c.run("alembic upgrade head")
@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")