forked from Kitware/CDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
558 lines (465 loc) · 17.1 KB
/
docker-entrypoint.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/bin/bash
declare -a __exit_callbacks
onexit() {
__exit_callbacks[${#__exit_callbacks[@]}]="$@"
}
do_exit() {
if [ -z "$exit_code" ] ; then
exit_code="$1"
local n
n=${#__exit_callbacks[@]}
for ((; n--; )) ; do
eval "${__exit_callbacks[$n]}"
done
fi
}
EXEC() {
do_exit 0
exec "$@"
}
trap "do_exit 1; exit $exit_code" INT TERM QUIT
trap "do_exit 0; exit $exit_code" EXIT
onexit 'if [ -n "$tmpdir" -a -d "$tmpdir" ] ; then rm -r "$tmpdir" ; fi'
ensure_tmp() {
if [ -z "$tmpdir" ] ; then
tmpdir="$( mktemp -d )"
fi
}
# poor man's CDash client
mksession() {
local result
ensure_tmp
mkdir -p "$tmpdir/sessions"
until mkdir "$result" 2> /dev/null ; do
result="$tmpdir/sessions/$RANDOM"
done
echo "$result"
}
ajax() {
local method
local session
local route
local curl_args
local arg
method="$1" ; shift
session="$1" ; shift
route="$1" ; shift
if [ "$method" '=' 'POST' ] ; then
for arg in "$@" ; do
curl_args="$curl_args --form '$arg'"
done
fi
local oldcookies
local newcookies
oldcookies="$session/cookies.txt"
newcookies="$session/cookies.tmp"
if [ "$session" '!=' '-' ] ; then
if [ -f "$oldcookies" ] ; then
curl_args="$curl_args --cookie '$oldcookies'"
fi
curl_args="$curl_args --cookie-jar '$newcookies'"
fi
local port="$PORT"
if [ -n "$port" ] ; then
port=":$port"
fi
curl_args="$curl_args 'http://localhost${port}/$route"
if [ "$method" '=' 'GET' ] ; then
arg="$1" ; shift
if [ -n "$arg" ] ; then
curl_args="${curl_args}?$arg"
fi
for arg in "$@" ; do
curl_args="${curl_args}&$arg"
done
fi
curl_args="${curl_args}'"
eval "curl $curl_args" 2>&-
if [ "$session" '!=' '-' ] ; then
if [ -f "$newcookies" ] ; then
mv "$newcookies" "$oldcookies"
fi
fi
sleep 0.2
}
get() {
ajax GET "$@"
}
post() {
ajax POST "$@"
}
user_prefix="__user"
user_set() {
email="$1" ; shift
key="$1" ; shift
value="$1" ; shift
email_hash="$( echo "$email" | sha1sum | cut -d\ -f 1 )"
eval "${user_prefix}_${email_hash}_${key}=\"${value}\""
}
user_get() {
email="$1" ; shift
key="$1" ; shift
email_hash="$( echo "$email" | sha1sum | cut -d\ -f 1 )"
eval "echo \"\$${user_prefix}_${email_hash}_${key}\""
}
DEBUG() {
if [ -n "$DEBUG" ] ; then
echo -n "DEBUG:: "
echo "$@"
fi
}
if [ -z "$CDASH_ROOT_ADMIN_PASS" ] ; then
cat << ____EOF
error: This container requires the CDASH_ROOT_ADMIN_PASS
environment variable to be defined.
____EOF
exit 1
fi 1>&2
local_config_file="/var/www/cdash/config/config.local.php"
(
echo '<?php'
if [ '!' -z ${CDASH_CONFIG+x} ] ; then
echo "$CDASH_CONFIG"
fi
) > "$local_config_file"
PORT="$(( (RANDOM % 20000) + 10000 ))"
sed -i 's/^Listen [0-9][0-9]*/Listen '"$PORT"'/g' /etc/apache2/ports.conf
sed -i 's/^<VirtualHost \*:[0-9][0-9]*>/<VirtualHost \*:'"$PORT"'>/g' \
/etc/apache2/sites-enabled/000-default.conf
echo "\$CDASH_FULL_EMAIL_WHEN_ADDING_USER = '1';" >> "$local_config_file"
/usr/sbin/apache2ctl -D FOREGROUND &
apache_pid="$!"
onexit '
if [ -n "$apache_pid" ] ; then
/usr/sbin/apache2ctl graceful-stop
wait
fi'
sleep 10
if [ -z "$CDASH_ROOT_ADMIN_EMAIL" ] ; then
CDASH_ROOT_ADMIN_EMAIL="[email protected]"
fi
# ENSURE ROOT ADMIN USER
final_root_pass="$CDASH_ROOT_ADMIN_PASS"
if [ -n "$CDASH_ROOT_ADMIN_NEW_PASS" ] ; then
final_root_pass="$CDASH_ROOT_ADMIN_NEW_PASS"
fi
post - install.php admin_email="$CDASH_ROOT_ADMIN_EMAIL" \
admin_password="$final_root_pass" \
Submit=Install &> /dev/null
if [ -n "$CDASH_ROOT_ADMIN_NEW_PASS" ] ; then
root_session="$( mksession )"
post "$root_session" user.php login="$CDASH_ROOT_ADMIN_EMAIL" \
passwd="$final_root_pass" \
sent='Login >>' \
| grep 'Wrong email or password' \
| ( read X ; DEBUG "|$X|" ; [ -z "$X" ] )
if [ "$?" '!=' '0' -a \
"$CDASH_ROOT_ADMIN_PASS" '!=' "$final_root_pass" ] ; then
# login failure
post "$root_session" user.php login="$CDASH_ROOT_ADMIN_EMAIL" \
passwd="$CDASH_ROOT_ADMIN_PASS" \
sent='Login >>' \
| grep 'Wrong email or password' \
| ( read X ; DEBUG "|$X|" ; [ -z "$X" ] )
if [ "$?" '=' '0' ] ; then
post "$root_session" editUser.php \
oldpasswd="$CDASH_ROOT_ADMIN_PASS" \
passwd="$final_root_pass" \
passwd2="$final_root_pass" \
updatepassword='Update Password' &> /dev/null
else
echo "Warning: could not log in as the root admin user:" \
"Wrong email or password" >&2
root_login_failed=1
fi
fi
fi
if [ "$root_login_failed" '!=' '1' ] ; then
declare -a users_list
if [ '!' -z ${CDASH_STATIC_USERS+x} ] ; then
ensure_tmp
mkfifo "$tmpdir/fifo"
eval "exec 3<>$tmpdir/fifo"
unlink "$tmpdir/fifo"
echo "$CDASH_STATIC_USERS" >&3
echo EOF >&3
oldifs="$IFS"
IFS=$'\n'
while read -u 3 line ; do
processed_line="$( echo "$line" |
sed $'s/\t\t*/ /g' | sed 's/ */ /g' | sed 's/#.*//g')"
if [ "$processed_line" '=' 'EOF' ] ; then
break
fi
if [ -z "$( echo "$processed_line" | sed $'s/[ \t]*//g' )" ] ; then
DEBUG "SKIPPING LINE"
DEBUG "[$line]"
continue
fi
eval "entry=($processed_line)"
_disp=
_email=
_pass=
_newpass=
if [ "${entry[0]}" '=' 'USER' -o \
"${entry[0]}" '=' 'ADMIN' -o \
"${entry[0]}" '=' 'DELETE' ] ; then
# explicit user entry line
_disp="${entry[0]}"
_email="${entry[1]}"
_pass="${entry[2]}"
_newpass="${entry[3]}"
parsed_user=1
elif [ "${entry[0]}" '!=' "${entry[0]/@*}" ] ; then
# implicit user entry line
_disp=USER
_email="${entry[0]}"
_pass="${entry[1]}"
_newpass="${entry[2]}"
parsed_user=1
elif [ "${entry[0]}" '=' 'INFO' ] ; then
# explicit user info line
first="${entry[1]}"
last="${entry[2]}"
institution="${entry[3]}"
parsed_user=0
else
# implicit user info line
first="${entry[0]}"
last="${entry[1]}"
institution="${entry[2]}"
parsed_user=0
fi
if [ -n "$email" ] ; then
user_set "$email" disp "$disp"
user_set "$email" pass "$pass"
user_set "$email" newpass "$newpass"
if [ "$parsed_user" '=' '0' ] ; then
user_set "$email" first "$first"
user_set "$email" last "$last"
user_set "$email" institution "$institution"
fi
if [ "$( user_get "$email" listed )" '!=' 1 ] ; then
users_list[${#users_list[@]}]="$email"
user_set "$email" listed 1
fi
fi
email="$_email"
disp="$_disp"
pass="$_pass"
newpass="$_newpass"
if [ -n "$DEBUG" ] ; then
if [ "$parsed_user" '=' '0' ] ; then
DEBUG "PARSED USER INFO"
DEBUG " First Name: |$first|"
DEBUG " Last Name: |$last|"
DEBUG " Institution: |$institution|"
else
DEBUG "PARSED USER ENTRY"
DEBUG " email: |$email|"
DEBUG " disposition: |$disp|"
DEBUG " password: |$pass|"
DEBUG " new password: |$newpass|"
fi
fi
done
if [ -n "$email" ] ; then
user_set "$email" disp "$disp"
user_set "$email" pass "$pass"
user_set "$email" newpass "$newpass"
if [ "$( user_get "$email" listed )" '!=' 1 ] ; then
users_list[${#users_list[@]}]="$email"
user_set "$email" listed 1
fi
fi
exec 3<&-
IFS="$oldifs"
fi
DEBUG "BEGIN DUMP OF USER TABLE"
if [ -n "$DEBUG" ] ; then
for (( i=0; i < ${#users_list[@]} ; ++i )) ; do
email="${users_list[$i]}"
for tuple in "disp" "pass" "newpass" "first:NONE" \
"last:NONE" "institution:NONE" ; do
fragment="${tuple/:*}"
tuple="${tuple:$(( ${#fragment} + 1 ))}"
param="$fragment"
fragment="${tuple/:*}"
tuple="${tuple:$(( ${#fragment} + 1 ))}"
default="$fragment"
value="$( user_get "$email" "$param" )"
if [ -z "$value" -a -n "$default" ] ; then
value="$default"
fi
eval "${param}=\"$value\""
done
DEBUG "$i:"
DEBUG " $email"
DEBUG " disposition: $disp"
DEBUG " password: $pass"
DEBUG " new pass: $newpass"
DEBUG " First Name: $first"
DEBUG " Last Name: $last"
DEBUG " Institution: $institution"
done
fi
for (( i=0; i < ${#users_list[@]} ; ++i )) ; do
email="${users_list[$i]}"
if [ "$email" '=' 'root' ] ; then
echo 'Warning: refusing to modify the root admin account!' \
"Use the CDASH_ROOT_ADMIN_NEW_PASS environment variable" \
"to update the root account password." >&2
continue
fi
if [ -z "$root_session" ] ; then
root_session="$( mksession )"
# LOGIN AS ROOT ADMIN USER
post "$root_session" user.php \
login="$CDASH_ROOT_ADMIN_EMAIL" \
passwd="$final_root_pass" \
sent='Login >>' \
| grep 'Wrong email or password' \
| ( read X ; DEBUG "|$X|" ; [ -z "$X" ] )
if [ "$?" '!=' '0' ] ; then
echo "Warning: could not log in as the root admin user:" \
"Wrong email or password" >&2
break
fi
fi
for tuple in "disp" "pass" "newpass" "first:NONE" \
"last:NONE" "institution:NONE" ; do
fragment="${tuple/:*}"
tuple="${tuple:$(( ${#fragment} + 1 ))}"
param="$fragment"
fragment="${tuple/:*}"
tuple="${tuple:$(( ${#fragment} + 1 ))}"
default="$fragment"
value="$( user_get "$email" "$param" )"
if [ -z "$value" -a -n "$default" ] ; then
value="$default"
fi
eval "${param}=\"$value\""
done
ids=($( \
get "$root_session" ajax/findusers.php search="$email" \
| grep '<input' \
| grep 'name="userid"' \
| grep 'type="hidden"' \
| sed 's/..*value="\([0-9][0-9]*\)"..*/\1/g'))
user_id="${ids[0]}"
if [ "$disp" '=' 'DELETE' ] ; then
# REMOVE USER
DEBUG "REMOVING USER: $email"
if [ -n "$user_id" ] ; then
post "$root_session" manageUsers.php \
userid="$user_id" \
removeuser="remove user" &> /dev/null
else
echo "Warning: could not remove user" \
"$email: user not found" >&2
fi
continue
fi
final_pass="$pass"
if [ -n "$newpass" ] ; then
final_pass="$newpass"
fi
login_pass="$pass"
if [ -z "$user_id" ] ; then
login_pass="$final_pass"
# CREATE USER
DEBUG "CREATING USER: $email"
post "$root_session" manageUsers.php \
fname="$first" \
lname="$last" \
email="$email" \
passwd="$final_pass" \
passwd2="$final_pass" \
institution="$institution" \
adduser='Add user >>' &> /dev/null
ids=($( \
get "$root_session" ajax/findusers.php search="$email" \
| grep '<input' \
| grep 'name="userid"' \
| grep 'type="hidden"' \
| sed 's/..*value="\([0-9][0-9]*\)"..*/\1/g'))
user_id="${ids[0]}"
if [ -z "$user_id" ] ; then
echo "Warning: unable to create user account" \
"$email: unknown error" >&2
continue
fi
fi
if [ "$disp" '=' 'ADMIN' ] ; then
DEBUG "PROMOTING USER: $email"
post "$root_session" manageUsers.php \
userid="$user_id" \
makeadmin="make admin" &> /dev/null
fi
if [ "$disp" '=' 'USER' ] ; then
DEBUG "DEMOTING USER: $email"
post "$root_session" manageUsers.php \
userid="$user_id" \
makenormaluser="make normal user" \
&> /dev/null
fi
user_session="$( mksession )"
# LOGIN AS NORMAL USER
login_success=0
DEBUG "LOGGING IN AS USER: $email"
post "$user_session" user.php login="$email" \
passwd="$login_pass" \
sent='Login >>' \
| grep 'Wrong email or password' \
| ( read X ; DEBUG "|$X|" ; [ -z "$X" ] )
if [ "$?" '=' '0' ] ; then # login success
login_success=1
elif [ "$login_pass" '!=' "$newpass" ] ; then # login failure
login_pass="$newpass"
DEBUG "LOGGING IN (FALLBACK) AS USER: $email"
post "$user_session" user.php login="$email" \
passwd="$login_pass" \
sent='Login >>' \
| grep 'Wrong email or password' \
| ( read X ; DEBUG "|$X|" ; [ -z "$X" ] )
if [ "$?" '=' '0' ] ; then
login_success=1
fi
fi
if [ "$login_success" '=' '0' ] ; then
echo "Warning: could not log in as user" \
"$email: Wrong email or password" >&2
continue
fi
DEBUG "UPDATING USER PROFILE: $email"
post "$user_session" editUser.php fname="$first" \
lname="$last" \
email="$email" \
institution="$institution" \
updateprofile='Update Profile' \
&> /dev/null
if [ -n "$newpass" -a "$login_pass" '!=' "$newpass" ] ; then
# update user's password
DEBUG "UPDATING USER PASSWORD: $email"
post "$user_session" editUser.php \
oldpasswd="$login_pass" \
passwd="$newpass" \
passwd2="$newpass" \
updatepassword='Update Password' &> /dev/null
fi
get "$user_session" user.php logout=1 &> /dev/null
done
get "$root_session" user.php logout=1 &> /dev/null
fi
/usr/sbin/apache2ctl graceful-stop
unset apache_pid
wait
sleep 10
sed -i 's/^Listen [0-9][0-9]*/Listen 80/g' /etc/apache2/ports.conf
sed -i 's/^<VirtualHost \*:[0-9][0-9]*>/<VirtualHost \*:80>/g' \
/etc/apache2/sites-enabled/000-default.conf
tmp="$( mktemp )"
head -n -1 "$local_config_file" > "$tmp"
cat "$tmp" > "$local_config_file"
rm "$tmp"
EXEC /usr/sbin/apache2ctl -D FOREGROUND