forked from compserv/hknweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
229 lines (175 loc) · 5.8 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
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
TARGET_FLAG = "--target"
DEFAULT_TARGET = "prod"
production_python = "HKNWEB_MODE=prod python"
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("mkdir -p {}".format(d))
class DeployConfig(Config):
@staticmethod
def global_defaults():
hkn_defaults = {
"deploy": {
"name": "default",
"user": "hkn",
"host": "apphost.ocf.berkeley.edu",
"path": {
"root": "/home/h/hk/hkn/hknweb",
"repo": "repo",
"releases": "releases",
"current": "current",
"shared": "shared",
},
"repo_url": "https://github.com/compserv/hknweb.git",
"branch": "master",
"linked_files": [],
"linked_dirs": [],
"keep_releases": 10,
},
}
return merge_dicts(Config.global_defaults(), hkn_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)
def decrypt_secrets(c):
print("-- Decrypting secrets")
with c.cd(c.release_path):
c.run("blackbox_postdeploy", echo=True)
def install_deps(c: Connection):
print("-- Installing dependencies")
with c.cd(c.release_path):
with c.prefix("conda activate hknweb"):
c.run("make install-prod")
def django_migrate(c: Connection):
print("-- Migrating tables")
with c.cd(c.release_path):
with c.prefix("conda activate hknweb"):
c.run("make check-conda-env")
c.run(f"{production_python} ./manage.py migrate")
def django_collectstatic(c: Connection):
print("-- Collecting static files")
with c.cd(c.release_path):
with c.prefix("conda activate hknweb"):
c.run("make check-conda-env")
c.run(f"{production_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 systemd_restart(c: Connection):
print("-- Restarting systemd unit")
c.run("systemctl --user restart hknweb.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):
with c.cd(c.release_path):
c.run("make conda")
def update(c: Connection):
print("== Update ==")
create_release(c)
symlink_shared(c)
decrypt_secrets(c)
create_conda(c)
install_deps(c)
django_migrate(c)
django_collectstatic(c)
def publish(c: Connection):
print("== Publish ==")
symlink_release(c)
systemd_restart(c)
def finish(c):
pass
# For the following @task functions, "target" is an ignored parameter
# It is a workaround to allow for use in using command line arguments
# For selecting a "target" in ns.configure
# Otherwise, fabfile will claim to not recognize it
@task
def deploy(c, target=DEFAULT_TARGET, 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 rollback(c, target=DEFAULT_TARGET, 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)
# Please add the "target" parameter if you are adding more @task functions
# to allow custom targets to be used (regardless if your function itself will use it or not)
def get_target(args):
target = args.target
if target not in configs:
message = '\n\tTarget Configuration "{}" is not a valid entry'.format(
TARGET_FLAG
)
message += "\n\tInvalid Entry: " + target
assert target in configs, message
return target
parser = argparse.ArgumentParser(
description='Target parameters for the fab file through the "fab" library'
)
parser.add_argument(
"--target",
default="prod",
help="The Target Configuration key to set the deployment setting",
)
args, unknown = parser.parse_known_args()
target_key = get_target(args)
print("Target Set:", target_key)
ns = Collection(deploy, rollback)
ns.configure(configs[target_key])