-
Notifications
You must be signed in to change notification settings - Fork 20
/
fabfile.py
233 lines (180 loc) · 6.08 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from fabric import Config
from fabric import Connection
from fabric import task
from invoke import Collection
from invoke.config import merge_dicts
from deploy import git
from deploy import path
import argparse
from typing import Callable
import os
def timestamp(c: Connection) -> str:
"""
Returns the server date-time, encoded as YYYYMMSS_HHMMSS.
"""
return c.run("date +%Y%m%d_%H%M%S").stdout.strip()
def create_dirs(c: Connection):
dirs = (
c.repo_path,
c.deploy_path,
c.releases_path,
c.shared_path,
c.release_path,
)
for d in dirs:
c.run(f"mkdir -p {d}")
class DeployConfig(Config):
@staticmethod
def global_defaults():
tbp_defaults = {
"deploy": {
"name": "default",
"user": "tbp",
"host": "apphost.ocf.berkeley.edu",
"conda_env": "tbpweb-prod",
"run_blackbox_postdeploy": True,
"path": {
"root": "/home/t/tb/tbp/tbpweb",
"repo": "repo",
"releases": "releases",
"current": "current",
"shared": "shared",
},
"repo_url": "https://github.com/TBP-IT/tbpweb.git",
"branch": "master",
"linked_files": [],
"linked_dirs": [],
"keep_releases": 10,
},
}
return merge_dicts(Config.global_defaults(), tbp_defaults)
targets = {
"prod": {
"deploy": {
"name": "prod",
"branch": "master",
},
},
}
configs = {target: DeployConfig(overrides=config) for target, config in targets.items()}
# pprint(vars(configs['prod']))
def create_release(c: Connection):
print("-- Creating release")
git.check(c)
git.update(c)
c.commit = git.revision_number(c, c.commit)
git.create_archive(c)
def symlink_shared(c: Connection):
print("-- Symlinking shared files")
with c.cd(c.release_path):
c.run("ln -s {}/media ./media".format(c.shared_path), echo=True)
c.run("ln -s {}/private-media ./private-media".format(c.shared_path), echo=True)
def decrypt_secrets(c):
# print("-- Decrypting secrets")
# with c.cd(c.release_path):
# c.run("blackbox_postdeploy", echo=True)
print("-- Copying Secrets")
with c.cd(c.release_path):
c.run("cp ~/tbpweb_keys.py ./settings/tbpweb_keys.py", echo=True)
def django_migrate(c: Connection):
print("-- Migrating tables")
with c.cd(c.release_path):
with c.prefix("conda activate tbpweb-prod"):
c.run(f"python ./manage.py migrate")
def django_collectstatic(c: Connection):
print("-- Collecting static files")
with c.cd(c.release_path):
with c.prefix("conda activate tbpweb-prod"):
c.run(f"python ./manage.py collectstatic --noinput")
def symlink_release(c: Connection):
print("-- Symlinking current@ to release")
c.run("ln -sfn {} {}".format(c.release_path, c.current_path), echo=True)
def run_permission(c: Connection):
print("-- Granting run file permission")
c.run("chmod +x {}/run".format(c.current_path), echo=True)
def systemd_restart(c: Connection):
print("-- Restarting systemd unit")
c.run("systemctl --user restart tbpweb.service", echo=True)
def setup(c: Connection, commit=None, release=None):
print("== Setup ==")
if release is None:
c.release = timestamp(c)
else:
c.release = release
c.deploy_path = path.deploy_path(c)
c.repo_path = path.repo_path(c)
c.releases_path = path.releases_path(c)
c.current_path = path.current_path(c)
c.shared_path = path.shared_path(c)
c.release_path = path.release_path(c)
if commit is None:
c.commit = c.deploy.branch
else:
c.commit = commit
print("release: {}".format(c.release))
print("commit: {}".format(c.commit))
create_dirs(c)
def create_conda(c: Connection):
print("-- Creating Conda Environment and Installing dependencies (Pip may take a while - about 15 mins, max 30 mins)")
with c.cd(c.release_path):
c.run("conda env create -f config/tbpweb-prod.yml")
c.run("conda info -a") # Print post-creation properties
def update_conda(c: Connection):
print("-- Update Conda Environment and dependencies")
with c.cd(c.release_path):
c.run("conda env update -f config/tbpweb-prod.yml")
c.run("conda info -a") # Print post-creation properties
def activate_conda(c: Connection):
c.run("conda activate tbpweb-prod")
def action_deploy(c: Connection, conda_action: Callable):
create_release(c)
symlink_shared(c)
decrypt_secrets(c)
conda_action(c)
activate_conda(c)
django_migrate(c)
django_collectstatic(c)
def initial_scratch(c: Connection):
print("== Initialize ==")
action_deploy(c, create_conda)
def update(c: Connection):
print("== Update ==")
action_deploy(c, update_conda)
def publish(c: Connection):
print("== Publish ==")
symlink_release(c)
run_permission(c)
systemd_restart(c)
def finish(c):
pass
@task
def deploy(c, commit=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, commit=commit)
update(c)
publish(c)
finish(c)
@task
def initialize(c, commit=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, commit=commit)
initial_scratch(c)
publish(c)
finish(c)
@task
def rollback(c, release=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, release=release)
update(c)
publish(c)
finish(c)
# Default mode is Prod
# Set a different target by setting the FAB_TARGET variable
tbpweb_mode = os.environ.get("FAB_TARGET", "prod")
if tbpweb_mode in ["dev", "prod"]:
# Validation
print("Target Set:", tbpweb_mode)
else:
raise ValueError(f"TARGET '{tbpweb_mode}' is not a valid value")
ns = Collection(deploy, rollback, initialize)
ns.configure(configs[tbpweb_mode])