-
Notifications
You must be signed in to change notification settings - Fork 29
/
rundocker.sh
executable file
·498 lines (409 loc) · 9.66 KB
/
rundocker.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
Img="https://acmesh-official.github.io/acmetest/status"
LogLink="https://github.com/acmesh-official/acmetest/blob/master"
Conf="plat.conf"
Table="table.md"
DEFAULT_SCRIPT="letest.sh"
#if [ -z "$TestingDomain" ] && [ -z "$NGROK_TOKEN" ]; then
# TestingDomain=testdocker.acme.sh
#fi
#
#if [ -z "$TestingAltDomains" ] && [ -z "$NGROK_TOKEN" ]; then
# TestingAltDomains=testdocker2.acme.sh
#fi
if [ -z "$RUN_SCRIPT" ] ; then
RUN_SCRIPT="$DEFAULT_SCRIPT"
fi
_date_u() {
date -u +"%a, %d %b %Y %T %Z"
}
_info() {
if [ -z "$2" ] ; then
echo "[$(date)] $1"
else
echo "[$(date)] $1"="'$2'"
fi
}
_err() {
_info "$@" >&2
return 1
}
_debug() {
if [ -z "$DEBUG" ] ; then
return
fi
_err "$@"
return 0
}
_debug2() {
if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
_debug "$@"
fi
return
}
_debug3() {
if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ] ; then
_debug "$@"
fi
return
}
#plat
_normalizeFilename() {
_nplat="$1"
printf "%s" "$_nplat" | tr ':/ \\' '----'
}
#plat
_getOutfile() {
_gnplat="$1"
statusfile="$(_normalizeFilename "$_gnplat")"
printf "%s" "logs/$statusfile.out"
}
#plat
_getLogfile() {
_lnplat="$1"
statusfile="$(_normalizeFilename "$_lnplat")"
printf "%s" "logs/$statusfile.log"
}
_time() {
date -u "+%s"
}
#update plat code [file]
update() {
plat="$1"
code="$2"
filename="$3"
if [ -z "$filename" ] ; then
filename="$Table"
fi
if [ -z "$code" ] ; then
_err "Usage: plat exitcode [filename:table.md]"
return 1
fi
statusfile="$(_normalizeFilename "$plat")"
if [ "$code" = "0" ] ; then
__ok "$plat"
else
__fail "$plat"
fi
if [ "$CI" = "1" ] ; then
if ! git pull >/dev/null 2>&1 ; then
_err "git pull error"
fi
_status="Passed"
if [ "$code" = "0" ] ; then
if [ -f "status/ok.svg" ] ; then
cat "status/ok.svg" > "status/$statusfile.svg"
fi
_status="Passed"
else
if [ -f "status/ng.svg" ] ; then
cat "status/ng.svg" > "status/$statusfile.svg"
fi
_status="Failed"
fi
git add "status/$statusfile.svg" >/dev/null 2>&1
outfile="$(_getOutfile "$plat")"
_setopt "$filename" "|$plat| " '!' "[]($Img/$statusfile.svg?$(_time))| $(_date_u)| [$_status]($LogLink/$outfile) |"
#_sed_i 's/\x1b\[1;31;32m//g' "$outfile" >/dev/null 2>&1
#_sed_i 's/\[1;31;32m//g' "$outfile" >/dev/null 2>&1
#
#_sed_i 's/\x1b\[0m//g' "$outfile" >/dev/null 2>&1
#_sed_i 's/\[0m//g' "$outfile" >/dev/null 2>&1
git add "$outfile" >/dev/null 2>&1
git add "$filename" >/dev/null 2>&1
cat head.md "$Table" tail.md > README.md
git add *.md >/dev/null 2>&1
git commit -m "Test for $plat" >/dev/null 2>&1
if ! git push >/dev/null 2>&1 ; then
_err "git push error"
fi
fi
}
_contains(){
_str="$1"
_sub="$2"
echo $_str | grep $_sub >/dev/null 2>&1
}
#options file
_sed_i() {
options="$1"
filename="$2"
if [ -z "$filename" ] ; then
_err "Usage:_sed_i options filename"
return 1
fi
_debug2 options "$options"
if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
_debug "Using sed -i"
sed -i "$options" "$filename"
else
_debug "No -i support in sed"
text="$(cat "$filename")"
echo "$text" | sed "$options" > "$filename"
fi
}
#setopt "file" "opt" "=" "value" [";"]
_setopt() {
__conf="$1"
__opt="$2"
__sep="$3"
__val="$4"
__end="$5"
if [ -z "$__opt" ] ; then
echo usage: _setopt '"file" "opt" "=" "value" [";"]'
return
fi
if [ ! -f "$__conf" ] ; then
touch "$__conf"
fi
if grep -n "^$__opt$__sep" "$__conf" > /dev/null ; then
_debug3 OK
if _contains "$__val" "&" ; then
__val="$(echo $__val | sed 's/&/\\&/g')"
fi
text="$(cat $__conf)"
echo "$text" | sed "s#^$__opt.*#$__opt$__sep$__val$__end#" > "$__conf"
else
_debug3 APP
echo "$__opt$__sep$__val$__end" >> "$__conf"
fi
}
#__INTERACTIVE=""
#if [ -t 1 ]; then
__INTERACTIVE="1"
#fi
__green() {
if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
printf '\033[1;31;32m'
fi
printf "$1"
if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
printf '\033[0m'
fi
}
__ok() {
__green "$1 [PASS]"
printf "\n"
}
__red() {
if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
printf '\033[1;31;40m'
fi
printf "$1"
if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" ]; then
printf '\033[0m'
fi
}
__fail() {
__red "$1 [FAIL]" >&2
printf "\n" >&2
return 1
}
#platline baseline fieldnum
_mergefield() {
platline="$1"
baseline="$2"
fieldnum="$3"
pvalue="$(echo "$platline" | cut -d '|' -f $fieldnum)"
if [ ! "$pvalue" ] ; then
pvalue="$(echo "$baseline" | cut -d '|' -f $fieldnum)"
fi
echo "$pvalue"
}
#plat
#ubuntu:14.04
#centos:6
_runplat() {
plat="$1"
if [ ! "$plat" ] ; then
_err "Usage: _runplat ubuntu:14.04"
return 1
fi
platname="$(echo $plat | tr "/" "-")"
myplat="my$platname"
platline="$(grep "^$plat[^ |]*" "$Conf" | tr -d "\r\n")"
_debug "platline" "$platline"
basetag=""
baseline=""
if _contains "$plat" ":" ; then
basetag="$(echo "$plat" | cut -d : -f 1)"
_debug "basetag" "$basetag"
baseline="$(grep "^-$basetag[^ |]*" "$Conf" | tr -d "\r\n" )"
fi
_debug "baseline" "$baseline"
_info "Running $( __green $plat), this may take a few minutes, please wait."
mkdir -p "$myplat"
echo "FROM $plat" > "$myplat/Dockerfile"
update="$(_mergefield "$platline" "$baseline" 2)"
_debug "update" "$update"
buildq="2>&1"
if [ "$DEBUG" ] || [ "$DEBUGING" ] ; then
buildq=""
fi
install="$(_mergefield "$platline" "$baseline" 3)"
_debug "install" "$install"
if [ "$install" ] ; then
echo "RUN ${update:+$update $buildq &&} $install \\" >> "$myplat/Dockerfile"
tools="$(_mergefield "$platline" "$baseline" 4)"
if [ "$tools" ] ; then
toolsline=$(echo "$tools" | tr ',' ' ' )
if [ "$toolsline" ] ; then
echo "$toolsline $buildq" >> "$myplat/Dockerfile"
fi
fi
fi
echo "USER root" >> "$myplat/Dockerfile"
if [ "$DEBUGING" ] ; then
cat "$myplat/Dockerfile"
fi
if [ ! -e "env.list" ]; then
touch env.list
fi
Log_Out="$(_getOutfile "$plat")"
_debug "Log_Out" "$Log_Out"
mkdir -p logs
touch "$Log_Out"
chmod o+rw "$Log_Out"
#docker pull $plat > "$Log_Out" 2>&1
if docker build -t "$myplat" "$myplat" > "$Log_Out" 2>&1 ; then
if [ -z "$LOG_FILE" ] ; then
LOG_FILE="$(_getLogfile "$plat")"
echo "" > "$LOG_FILE"
fi
_debug "LOG_FILE" "$LOG_FILE"
if [ "$DEBUGING" ] ; then
docker run --net=host --rm \
-e DOCKER_OS="$plat" \
--env-file docker.env \
-v $(pwd):/acmetest \
$myplat /bin/sh -c "cd /acmetest && ./$RUN_SCRIPT"
else
if [ "$TRAVIS" = "true" ] || [ "$GITHUB_ACTIONS" = "true" ] ; then
docker run --net=host --rm \
-e DOCKER_OS="$plat" \
--env-file docker.env \
-v $(pwd):/acmetest \
$myplat /bin/sh -c "cd /acmetest && ./$RUN_SCRIPT"
else
docker run --net=host --rm \
--env-file docker.env \
-e DOCKER_OS="$plat" \
-v $(pwd):/acmetest \
$myplat /bin/sh -c "cd /acmetest && ./$RUN_SCRIPT" >> "$Log_Out" 2>&1
fi
fi
code="$?"
_debug "code" "$code"
if [ "$code" != "0" ] ; then
cat "$Log_Out"
if [ "$DEBUGING" ] ; then
_info "Please debuging:"
docker run --net=host --rm \
-i -t \
-e DOCKER_OS="$plat" \
--env-file docker.env \
-v $(pwd):/acmetest \
$myplat /bin/sh
fi
fi
else
code="$?"
_debug "code" "$code"
cat "$Log_Out"
fi
update $plat $code
return $code
}
#plat
testplat() {
plat="$1"
if [ ! "$plat" ] ; then
_err "Usage: testplat ubuntu:14.04"
return 1
fi
if [ "$CASE" ] ; then
echo "Test for case: $(__green "$CASE")"
fi
platforms=$(grep -o "^$plat[^ |]*" "$Conf" )
if [ ! "$platforms" ] ; then
platforms="$plat"
fi
_debug "$platforms"
for plat in $platforms
do
_runplat "$plat"
_rret="$?"
if [ "$_rret" != "0" ]; then
if [ -z "$DEBUG" ] && [ -z "$DEBUGING" ] ; then
_info "Let's retry once more:$plat"
_runplat "$plat"
_rret="$?"
fi
if [ "$_rret" != "0" ] ; then
_info "Failed: $plat"
_FAILED_PLATS="$_FAILED_PLATS$plat "
if [ "$TRAVIS" = "true" ] || [ "$GITHUB_ACTIONS" = "true" ]; then
return "$_rret"
fi
fi
fi
done
}
#plat1 plat2 plat3
testplats() {
if [ -z "$@" ] ; then
_err "Usage: testplats plat1 plat2 plat3 ... "
return 1
fi
for tps in "$@" ; do
[ "$tps" ] && testplat "$tps"
done
}
cleardocker() {
docker rm $(docker ps -a -q)
#docker rmi $(docker images -q -f "dangling=true")
}
showhelp() {
_info "testall|testplat|testplats|cleardocker|_cron"
}
testall() {
allplats=$(grep -v '^#' plat.conf | grep -v "^-" | cut -d "|" -f 1)
for plat in $allplats
do
if [ "$plat" ] ; then
testplat $plat
pret="$?"
if [ "$pret" != "0" ] && [ "$GITHUB_ACTIONS" = "true" ]; then
return $pret
fi
fi
done
}
_pullgit() {
git checkout status/* >/dev/null 2>&1
git checkout *.md >/dev/null 2>&1
git checkout plat.conf >/dev/null 2>&1
git pull >/dev/null 2>&1
}
_cron() {
if [ "$CI" = "1" ]; then
_pullgit
rm "$Table"
fi
_FAILED_PLATS=""
testall
if [ "$_FAILED_PLATS" ] ; then
if [ "$GITHUB_ACTIONS" != "true" ]; then
_info "Let's try once more for: $_FAILED_PLATS"
_ttft="$_FAILED_PLATS"
_FAILED_PLATS=""
testplats "$_ttft"
else
return 1
fi
fi
}
if [ -z "$1" ] ; then
showhelp
else
"$@"
fi