-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfabfile.py
155 lines (117 loc) · 3.97 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
153
154
155
# encoding: utf-8
"""
Fabfile to drive development and deployment of recoco
authors : [email protected], [email protected]
created : 2021-06-01 09:54:36 CEST
"""
import json
import os
from distutils.core import run_setup
import requests
from dotenv import load_dotenv
from fabric import task
from invoke import run as local
import recoco
load_dotenv()
PACKAGE = f"recoco-{recoco.VERSION}.tar.gz"
# TODO make target folder being
# - prod if branch == main,
# - develop if branch == develop, and
# - error otherwise.
@task
def upgrade(cnx, site=None):
"""Upgrade requirements to last version on server for site"""
if site not in ["production", "development"]:
print("Usage: fab upgrade --site={production,development} --hosts=...")
return
cnx.put(
"./requirements.txt",
remote=f"./recoco-{site}/requirements.txt",
)
cnx.run(f"cd recoco-{site} " "&& ./.venv/bin/uv pip install -r requirements.txt")
@task
def setup(cnx, site=None):
"""Setup a server with the minimum for deploying"""
if site not in ["production", "development"]:
print("Usage: fab deploy --site={production,development} --hosts=...")
return
cnx.run(
f"mkdir -p recoco-{site}/dist"
f"&& virtualenv recoco-{site}/.venv"
f"&& recoco-{site}/.venv/bin/pip install uv"
)
@task
def deploy(cnx, site=None):
"""Deploy new version of project to server for site"""
if site not in ["production", "development"]:
print("Usage: fab deploy --site={production,development} --hosts=...")
return
local("cd recoco/frontend && yarn build")
run_setup("setup.py", script_args=["sdist"])
cnx.put(
f"./dist/{PACKAGE}",
remote=f"./recoco-{site}/dist/{PACKAGE}",
)
cnx.run(
f"cd recoco-{site} "
f'&& ./.venv/bin/uv pip install "recoco @ ./dist/{PACKAGE}" --reinstall-package "recoco"'
"&& ./manage.py migrate"
"&& ./manage.py compilescss"
"&& ./manage.py collectstatic --noinput"
)
def ad_staging_create_database(db_name: str):
"""Create a new database on alwaysdata infrastructure"""
address = "https://api.alwaysdata.com/v1/database/"
fullname = f"uvstaging_{db_name}"
database = {
"name": fullname,
"type": "POSTGRESQL",
"locale": "fr_FR.utf8",
"extensions": ["pg_trgm", "postgis", "postgis_topology", "unaccent"],
"permissions": {"uvstaging": "FULL"},
}
data = json.dumps(database)
credentials = (
"{API_KEY} account=uvstaging".format(
API_KEY=os.getenv("AD_RECOCO_STAGING_CREDENTIALS")
),
"",
)
requests.post(address, auth=credentials, data=data, timeout=10)
def ad_staging_drop_database(db_name: str):
api_root = "https://api.alwaysdata.com/"
credentials = (
"{API_KEY} account=uvstaging".format(
API_KEY=os.getenv("AD_RECOCO_STAGING_CREDENTIALS")
),
"",
)
response = requests.get(f"{api_root}/v1/database/", auth=credentials, timeout=10)
db_json = response.json()
for db in db_json:
if db["name"] == f"uvstaging_{db_name}":
response = requests.delete(
f"{api_root}{db['href']}", auth=credentials, timeout=10
)
print(response)
return
@task
def replicate_prod_to_staging(cnx, site=None):
if site != "production":
print("This can only be run on the prod site.")
cnx.run("./replicate_staging_to_prod.sh")
@task
def load_prod_db_to_staging(cnx, site=None):
db_name = "development"
if site != "development":
print("This can only be run on the dev site.")
ad_staging_drop_database(db_name)
ad_staging_create_database(db_name)
cnx.run("./load_prod_dump_to_db.sh")
cnx.run(f"cd recoco-{site}/multisites" "&& git pull")
cnx.run(
f"cd recoco-{site} "
"&& ./manage.py compilescss"
"&& ./manage.py collectstatic --noinput"
)
# eof