[OCaml] High Intensity Training Online
1
#!
2
#
3
# hito deployment setup. One root script, three subcommands.
4
#
5
# sudo bash setup.sh install One-time host setup: create the git-owned
6
# /srv/hito layout, the hito runtime user and
7
# its data/ directory, build the shared switch as
8
# git, install the receive hooks, the scoped sudoers
9
# rule, and the systemd unit from hito.service beside
10
# this script. Idempotent. Creates no release;
11
# the first push (or 'deploy') does.
12
# sudo bash setup.sh deploy Reinstall hito.service if it changed, build
13
# master as a fresh release on the shared
14
# switch, and flip current. Health-checks and
15
# rolls back on failure. Use to go live without a
16
# push, or right after 'install'.
17
# sudo bash setup.sh reset Wipe /srv/hito except data/, then rebuild the
18
# switch as git and 'deploy'. Destructive: all
19
# else, including the switch, is removed.
20
#
21
# Two-account model. The git user owns the bare repo and /srv/hito (except
22
# data/), builds every release, and may run one privileged command,
23
# 'systemctl restart hito', through a scoped sudoers rule. The hito system user
24
# runs the service and owns only /srv/hito/data, which holds the database. A
25
# compromised service cannot rewrite releases or the deploy pipeline, and git
26
# cannot read the database. The receive hooks enforce the master check before
27
# the post-receive hook deploys each accepted push.
28
#
29
# Style: Google Shell Style Guide.
30
31
set-euopipefail
32
33
readonlySVC_USER="git"
34
readonlySVC_GROUP="git"
35
readonlyRUN_USER="hito"
36
readonlyRUN_GROUP="hito"
37
readonlySRV="/srv/hito"
38
readonlyBARE="/srv/git/hito.git"
39
readonlyUNIT="/etc/systemd/system/hito.service"
40
readonlySUDOERS="/etc/sudoers.d/hito-deploy"
41
readonlySHARED="${SRV}/shared"
42
readonlyRELEASES="${SRV}/releases"
43
readonlyDATA="${SRV}/data"
44
readonlyDB="${DATA}/hito.sqlite"
45
readonlySWITCH="${SHARED}/_opam"
46
readonlyOCAML_COMPILER="ocaml-base-compiler.5.5.0"
47
readonlyHEALTH_URL="http://127.0.0.1:8080/login"
48
readonlyOPAMROOT_DIR="/srv/git/.opam"
49
HERE="$(cd$(dirname$0"""
50
51
52
53
"$(d-u)"
54
""
55
56
57
58
59
60
61
62
"${SVC_USER}""${OPAMROOT_DIR}""$@"
63
64
65
66
"
67
""
68
69
70
71
72
73
74
"${OPAMROOT_DIR}"
75
"${OPAMROOT_DIR}"
76
77
78
"${OCAML_COMPILER}${SVC_USER}"
79
"${SHARED}""${OCAML_COMPILER}"
80
81
82
83
"${SVC_USER}""${SVC_GROUP}"\
84
"${HERE}""${BARE}"
85
"${SVC_USER}""${SVC_GROUP}"\
86
"${HERE}""${BARE}"
87
"${SVC_USER}""${SVC_GROUP}"\
88
"${HERE}""${BARE}"
89
""
90
91
92
93
94
"
95
"${RUN_USER}"
96
97
98
\
99
"${DATA}"\
100
"${RUN_USER}"
101
"${RUN_USER}"
102
103
104
105
106
107
"${RUN_USER}""${RUN_GROUP}""${DATA}"
108
109
110
111
112
"${SUDOERS}"
113
${SVC_USER}
114
115
"${SUDOERS}"
116
"${SUDOERS}"""
117
118
119
120
121
122
"${HERE}"
123
"${src}""${UNIT}"
124
""
125
126
127
"${src}"
128
"${src}""${UNIT}"
129
130
131
"${UNIT}"
132
133
134
135
136
137
"${HEALTH_URL}"
138
139
140
141
142
143
144
145
146
147
148
149
150
"${SECONDS}"
151
152
"$("%Y-%m-%dT%H-%M-%SZ")"
153
"${RELEASES}${ts}"
154
"$(readlink-f${SRV}/current"
155
"$(git--git-dir=${BARE}""""
156
"$(s_svcmktemp-d/tmp/hito-stage.XXXXXX)"
157
158
"
159
"
160
161
${BARE}${sha}${stage}
162
${stage}
163
\"\$${SHARED}\"
164
\
165
166
${HERE}
167
168
"
169
$((SECONDS-start))
170
171
"
172
"${SVC_USER}${SVC_GROUP}""${RELEASES}"
173
"${stage}""${rel}"
174
175
"${rel}"
176
"${DB}""${rel}"
177
178
"$(it--git-dir=${BARE}""${sha}""
179
"${BARE}"\
180
"${sha}""
181
\
182
"${sha}"\
183
"${subject}"\
184
""\
185
"${ts}"\
186
"${build_secs}"\
187
"${rel}"\
188
$commit$subject$branch
189
$deployed_at$build_seconds
190
$release\
191
"${rel}"
192
\
193
"${ts}"${sha:0:12}""${build_secs}""""${subject}"\
194
"
195
"${version}""${SHARED}"
196
197
""${rel}"
198
"${SVC_USER}${SVC_GROUP}""${SHARED}""${SHARED}"
199
""${SRV}"
200
"${SRV}""${SRV}"
201
""${SRV}"
202
"
203
204
205
206
"${HEALTH_URL}"
207
208
209
""${prev}"
210
"${prev}"
211
"${prev}""${SRV}"
212
""${SRV}"
213
214
""
215
216
217
218
"
219
220
221
222
223
224
"
225
"${SVC_USER}""${SVC_GROUP}"\
226
"${SRV}"${SHARED}""${RELEASES}"
227
"
228
229
230
"
231
"
232
"${SVC_USER}${SVC_GROUP}""${OPAMROOT_DIR}"
233
"
234
"
235
"
236
"
237
238
"'
239
240
241
242
243
244
''
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293