-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle_post_install.sh
executable file
·335 lines (246 loc) · 8.85 KB
/
bundle_post_install.sh
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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
set -e
set -o pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ -z "$CONFIG_NAME" ]
then
CONFIG_NAME=$1
fi
if [ -z "$CONFIG_NAME" ]
then
CONFIG_NAME=default
fi
echo "Using configuration name suffix $CONFIG_NAME"
# Create user and group 'yajudge' in case if not exists
if [ -z "$YAJUDGE_USER" ]
then
YAJUDGE_USER=yajudge
fi
if [ ! $(id -u $YAJUDGE_USER) ]
then
useradd -rmU -s /usr/sbin/nologin -d $SCRIPT_DIR $YAJUDGE_USER
fi
echo "Created user $YAJUDGE_USER and group $YAJUDGE_USER"
# Initialize directories variables
if [ -z "$YAJUDGE_DIR" ]
then
YAJUDGE_DIR="$SCRIPT_DIR"
fi
echo "Yajudge installation directory is $SCRIPT_DIR"
if [ -z "$LOG_DIR" ]
then
LOG_DIR="$YAJUDGE_DIR/log"
fi
echo "Will use $LOG_DIR as directory for log files"
if [ -z "$PID_DIR" ]
then
PID_DIR="$YAJUDGE_DIR/pid"
fi
echo "Will use $PID_DIR as directory for PID files"
if [ -z "$SOCKETS_DIR" ]
then
SOCKETS_DIR="$YAJUDGE_DIR/sock"
fi
echo "Will use $SOCKETS_DIR as directory for UNIX sockets related to services"
if [ -z "$WORK_DIR" ]
then
WORK_DIR="$YAJUDGE_DIR/work"
fi
echo "Will use $WORK_DIR as directory for grader submissions"
if [ -z "$CACHE_DIR" ]
then
CACHE_DIR="$YAJUDGE_DIR/cache"
fi
echo "Will use $CACHE_DIR as directory for grader's courses cache"
if [ -z "$COURSES_DIR" ]
then
COURSES_DIR="$YAJUDGE_DIR/courses"
fi
echo "Will use $COURSES_DIR as directory for courses root"
if [ -z "$PROBLEMS_DIR" ]
then
PROBLEMS_DIR="$YAJUDGE_DIR/problems"
fi
echo "Will use $PROBLEMS_DIR as directory for problems root"
if [ -z "$SYSTEM_DIR" ]
then
SYSTEM_DIR="$YAJUDGE_DIR/system"
fi
echo "Will use $SYSTEM_DIR as directory for grader's isolated base operating system files"
if [ -z "$CONF_DIR" ]
then
CONF_DIR=/etc/yajudge
fi
echo "Will use $CONF_DIR to make configuration files"
if [ -z "$NGINX_CONF_DIR" ]
then
NGINX_CONF_DIR=/etc/nginx
fi
echo "Will use $NGINX_CONF_DIR to update nginx configurations"
if [ -z "$SYSTEMD_DIR" ]
then
SYSTEMD_DIR=/etc/systemd/system
fi
echo "Will use $SYSTEMD_DIR to make systemd units"
if [ -z "$BIN_DIR" ]
then
BIN_DIR="$YAJUDGE_DIR/bin"
fi
echo "Yajudge executables located in $BIN_DIR"
if [ -z "$WEB_DIR" ]
then
WEB_DIR="$YAJUDGE_DIR/web"
fi
echo "Yajudge web-exposed content located in $WEB_DIR"
mkdir -p $CONF_DIR
mkdir -p $CONF_DIR/sites-available
mkdir -p $CONF_DIR/sites-enabled
mkdir -p $NGINX_CONF_DIR/sites-available
dirs=("$LOG_DIR" "$PID_DIR" "$CACHE_DIR" "$COURSES_DIR" "$PROBLEMS_DIR" "$WORK_DIR" "$SYSTEM_DIR" "$SOCKETS_DIR")
for d in ${dirs[*]}
do
mkdir -p $d
chown $YAJUDGE_USER:$YAJUDGE_USER $d
chmod g+w $d
echo "Directory $d now owned by user $YAJUDGE_USER and writable by group $YAJUDGE_USER"
done
# Prepare text replacements for config files and systemd units
function screen_slash() {
echo $1 | sed -r s'/\//\\\//g'
}
repl=" s/@LOGS_DIRECTORY/$(screen_slash $LOG_DIR)/g"
repl="$repl; s/@SOCKETS_DIR/$(screen_slash $SOCKETS_DIR)/g"
repl="$repl; s/@RUNTIME_DIRECTORY/$(screen_slash $PID_DIR)/g"
repl="$repl; s/@CONFIGURATION_DIRECTORY/$(screen_slash $CONF_DIR)/g"
repl="$repl; s/@STATE_DIRECTORY/$(screen_slash $YAJUDGE_DIR)/g"
repl="$repl; s/@BIN_DIR/$(screen_slash $BIN_DIR)/g"
repl="$repl; s/@WEB_DIRECTORY/$(screen_slash $WEB_DIR)/g"
repl="$repl; s/@PROBLEMS_DIR/$(screen_slash $PROBLEMS_DIR)/g"
repl="$repl; s/@COURSES_DIR/$(screen_slash $COURSES_DIR)/g"
repl="$repl; s/@WORK_DIR/$(screen_slash $WORK_DIR)/g"
repl="$repl; s/@SYSTEM_DIR/$(screen_slash $SYSTEM_DIR)/g"
repl="$repl; s/@CACHE_DIRECTORY/$(screen_slash $CACHE_DIR)/g"
repl="$repl; s/@YAJUDGE_USER/$(screen_slash $YAJUDGE_USER)/g"
repl="$repl; s/@CONFIG_NAME/$(screen_slash $CONFIG_NAME)/g"
# Check for existing config files not to replace
if [ -f $CONF_DIR/master-$CONFIG_NAME.yaml ]
then
MASTER_CONF=$CONF_DIR/master-$CONFIG_NAME.new.yaml
echo "Found existing master configuration"
else
MASTER_CONF=$CONF_DIR/master-$CONFIG_NAME.yaml
fi
echo "Master configuration will be created in $MASTER_CONF"
if [ -f $CONF_DIR/database-$CONFIG_NAME.yaml ]
then
DB_CONF=$CONF_DIR/database-$CONFIG_NAME.new.yaml
echo "Found existing database configuration"
else
DB_CONF=$CONF_DIR/database-$CONFIG_NAME.yaml
fi
echo "Database configuration will be created in $DB_CONF"
if [ -f $CONF_DIR/endpoints-$CONFIG_NAME.yaml ]
then
ENDPOINTS_CONF=$CONF_DIR/endpoints-$CONFIG_NAME.new.yaml
echo "Found existing service endpoints configuration"
else
ENDPOINTS_CONF=$CONF_DIR/endpoints-$CONFIG_NAME.yaml
fi
echo "Service endpoints configuration will be created in $ENDPOINTS_CONF"
if [ -f $CONF_DIR/grader-$CONFIG_NAME.yaml ]
then
GRADER_CONF=$CONF_DIR/grader-$CONFIG_NAME.new.yaml
echo "Found existing grader configuration"
else
GRADER_CONF=$CONF_DIR/grader-$CONFIG_NAME.yaml
fi
echo "Grader configuration will be created in $GRADER_CONF"
if [ -f $CONF_DIR/grpcwebserver.yaml ]
then
WEB_SERVER_CONF=$CONF_DIR/grpcwebserver.yaml.new
echo "Found existing grpcwebserver configuration"
else
WEB_SERVER_CONF=$CONF_DIR/grpcwebserver.yaml
fi
echo "GrpcWebServer configuration will be created in $WEB_SERVER_CONF"
if [ -f $CONF_DIR/sites-available/$CONFIG_NAME.yaml ]
then
WEB_SITE_CONF=$CONF_DIR/sites-available/$CONFIG_NAME.yaml.new
echo "Found existing grpcwebserver site configuration"
else
WEB_SITE_CONF=$CONF_DIR/sites-available/$CONFIG_NAME.yaml
WEB_SITE_LINK=$CONF_DIR/sites-enabled/$CONFIG_NAME.yaml
fi
echo "GrpcWebServer site configuration will be created in $WEB_SITE_CONF"
if [ -f $NGINX_CONF_DIR/sites-available/yajudge-$CONFIG_NAME.conf ]
then
NGINX_CONF=$NGINX_CONF_DIR/sites-available/yajudge-$CONFIG_NAME.conf.new
else
NGINX_CONF=$NGINX_CONF_DIR/sites-available/yajudge-$CONFIG_NAME.conf
fi
echo "Nginx configuration will be created in $NGINX_CONF"
# Create default database password file if not exists
if [ ! -f $CONF_DIR/database-password.txt ]
then
echo 'yajudge' > $CONF_DIR/database-password.txt
chown $YAJUDGE_USER:$YAJUDGE_USER $CONF_DIR/database-password.txt
chmod 0440 $CONF_DIR/database-password.txt
echo "Created database password 'yajudge' in $CONF_DIR/database-password.txt"
else
echo "Found existing database password file in $CONF_DIR/database-password.txt"
fi
# Create private token file if not exists
if [ ! -f $CONF_DIR/private-token.txt ]
then
head -c 1024 /dev/random | md5sum | cut -d ' ' -f 1 > $CONF_DIR/private-token.txt
chown $YAJUDGE_USER:$YAJUDGE_USER $CONF_DIR/private-token.txt
chmod 0440 $CONF_DIR/private-token.txt
echo "Created random private token in $CONF_DIR/private-token.txt"
else
echo "Found existing private token file in $CONF_DIR/private-token.txt"
fi
# Preprocess and create config files
sed -E "$repl" conf/grader.in.yaml > $GRADER_CONF
echo "Created file $GRADER_CONF"
sed -E "$repl" conf/master.in.yaml > $MASTER_CONF
echo "Created file $MASTER_CONF"
sed -E "$repl" conf/database.in.yaml > $DB_CONF
echo "Created file $DB_CONF"
sed -E "$repl" conf/endpoints.in.yaml > $ENDPOINTS_CONF
echo "Created file $ENDPOINTS_CONF"
sed -E "$repl" conf/grpcwebserver.in.yaml > $WEB_SERVER_CONF
echo "Created file $WEB_SERVER_CONF"
sed -E "$repl" conf/site@.in.yaml > $WEB_SITE_CONF
echo "Created file $WEB_SITE_CONF"
sed -E "$repl" conf/nginx@.in.conf > $NGINX_CONF
echo "Created file $NGINX_CONF"
# Enable web config
if [ "$WEB_SITE_LINK" ]
then
if [ ! -L "$WEB_SITE_LINK" ]
then
ln -f -s -T "../sites-available/$CONFIG_NAME.yaml" $WEB_SITE_LINK
echo "$WEB_SITE_CONF symlinked to $WEB_SITE_LINK"
fi
fi
# Preprocess and create systemd files
cat systemd/yajudge-grader.slice > $SYSTEMD_DIR/yajudge-grader.slice
echo "Created file $SYSTEMD_DIR/yajudge-grader.slice"
sed -E "$repl" systemd/yajudge-grader-prepare.in.service > $SYSTEMD_DIR/yajudge-grader-prepare.service
echo "Created file $SYSTEMD_DIR/yajudge-grader-prepare.service"
sed -E "$repl" systemd/yajudge-grader@.in.service > $SYSTEMD_DIR/yajudge-grader@.service
echo "Created file $SYSTEMD_DIR/yajudge-grader@.service"
sed -E "$repl" systemd/yajudge-master@.in.service > $SYSTEMD_DIR/yajudge-master@.service
echo "Created file $SYSTEMD_DIR/yajudge-master@.service"
sed -E "$repl" systemd/yajudge-grpcwebserver.in.service > $SYSTEMD_DIR/yajudge-grpcwebserver.service
echo "Created file $SYSTEMD_DIR/yajudge-grpcwebserver.service"
# Create systemd instance links
ln -f -s -T yajudge-master@.service $SYSTEMD_DIR/yajudge-master@$CONFIG_NAME.service
echo "$SYSTEMD_DIR/yajudge-master@.service symlinked to $SYSTEMD_DIR/yajudge-master@$CONFIG_NAME.service"
ln -f -s -T yajudge-grader@.service $SYSTEMD_DIR/yajudge-grader@$CONFIG_NAME.service
echo "$SYSTEMD_DIR/yajudge-grader@.service symlinked to $SYSTEMD_DIR/yajudge-grader@$CONFIG_NAME.service"
# Reload systemd configuration
systemctl daemon-reload
echo "Reloaded systemd configuration"
# Make message on configuration
echo "Done. See README.md for next configuration stages"