-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_compose.sh
More file actions
executable file
·995 lines (831 loc) · 27.6 KB
/
Copy pathrun_compose.sh
File metadata and controls
executable file
·995 lines (831 loc) · 27.6 KB
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/usr/bin/env bash
[ -n "${BASH_VERSION:-}" ] || exec bash "$0" "$@"
set -euo pipefail
# DOCX Editor Docker 启动脚本。
# 用法:
# ./run_compose.sh
# ./run_compose.sh local
# ./run_compose.sh local deploy/docker-compose.yml
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_ROOT="$SCRIPT_DIR"
cd "$REPO_ROOT"
LOG_DIR="data/docker-logs"
ENV_FILE=${DOCKER_ENV_FILE:-.env}
ENV_FILE_ABS=""
DEPLOY_MODE=""
COMPOSE_FILE=""
COMPOSE_CMD=()
PYTHON_BASE_IMAGE_DEFAULT="python:3.12-slim"
NODE_BASE_IMAGE_DEFAULT="node:22-alpine"
NGINX_BASE_IMAGE_DEFAULT="nginx:1.27-alpine"
ONLYOFFICE_IMAGE_DEFAULT="onlyoffice/documentserver:latest"
DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS=""
DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED=0
DOCKER_ONLYOFFICE_IMAGE_AUTO_FALLBACKS=""
mkdir -p "$LOG_DIR"
usage() {
cat <<'USAGE'
用法:
./run_compose.sh [local] [compose-file]
模式:
local 本地源码构建镜像并启动服务(默认)
环境变量:
DOCKER_ENV_FILE=.env
DOCKER_DEPLOY_MODE=local
DOCKER_SOURCE_PROFILE=auto|native|mirror
DOCKER_BUILD_NO_CACHE=0|1
DOCKER_BUILD_PULL=0|1
DOCKER_FORCE_RECREATE=0|1
DOCKER_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
DOCKER_PIP_INDEX_FALLBACKS="url1 url2"
DOCKER_NPM_REGISTRY=https://registry.npmmirror.com
DOCKER_REGISTRY_CANDIDATES_EXTRA= # 自定义 Docker 镜像代理候选
DOCKER_PYTHON_BASE_IMAGE=python:3.12-slim
DOCKER_NODE_BASE_IMAGE=node:22-alpine
DOCKER_NGINX_BASE_IMAGE=nginx:1.27-alpine
DOCX_EDITOR_PORT=8910
BACKEND_PUBLIC_BASE_URL=http://127.0.0.1:8910
USAGE
}
resolve_path() {
local path="$1"
if [[ "$path" = /* ]]; then
printf '%s' "$path"
return 0
fi
printf '%s' "$REPO_ROOT/${path#./}"
}
is_msys_runtime() {
if [ -n "${MSYSTEM:-}" ] || [ -n "${MSYS:-}" ]; then
return 0
fi
case "${OSTYPE:-}" in
msys*|cygwin*)
return 0
;;
esac
return 1
}
normalize_loopback_host() {
local raw_url="$1"
if [[ "$raw_url" =~ ^(https?://)localhost(:[0-9]+)?(/.*)?$ ]]; then
printf '%s127.0.0.1%s%s' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]:-}" \
"${BASH_REMATCH[3]:-}"
return 0
fi
printf '%s' "$raw_url"
}
normalize_line_endings() {
local path="$1"
local tmp_file=""
if [ ! -f "$path" ]; then
return 0
fi
if ! LC_ALL=C grep -q $'\r' "$path"; then
return 0
fi
tmp_file=$(mktemp)
sed 's/\r$//' "$path" > "$tmp_file"
if cmp -s "$path" "$tmp_file"; then
rm -f "$tmp_file"
return 0
fi
chmod --reference="$path" "$tmp_file" 2>/dev/null || true
mv "$tmp_file" "$path"
echo "已自动修复 CRLF -> LF: $path"
}
auto_fix_runtime_line_endings() {
local targets=(
"$REPO_ROOT/run_compose.sh"
"$REPO_ROOT/.env.example"
"$REPO_ROOT/deploy/onlyoffice-entrypoint.sh"
)
local target=""
local seen=$'\n'
if [ -n "${ENV_FILE:-}" ]; then
targets+=("$(resolve_path "$ENV_FILE")")
fi
if [ -n "${COMPOSE_FILE:-}" ]; then
targets+=("$(resolve_path "$COMPOSE_FILE")")
fi
for target in "${targets[@]}"; do
[ -n "$target" ] || continue
[ -f "$target" ] || continue
case "$seen" in
*$'\n'"$target"$'\n'*)
continue
;;
esac
seen+="$target"$'\n'
normalize_line_endings "$target"
done
}
load_env_file() {
local fallback_env_file_abs=""
local normalized_public_base=""
local normalized_document_server_url=""
ENV_FILE_ABS=$(resolve_path "$ENV_FILE")
fallback_env_file_abs=$(resolve_path ".env.example")
if [ -f "$ENV_FILE_ABS" ]; then
echo "检测到环境文件: $ENV_FILE_ABS"
set -a
# shellcheck disable=SC1090
source <(sed 's/\r$//' "$ENV_FILE_ABS")
set +a
elif [ -f "$fallback_env_file_abs" ]; then
echo "未找到环境文件: $ENV_FILE_ABS,回退读取示例配置: $fallback_env_file_abs"
set -a
# shellcheck disable=SC1090
source <(sed 's/\r$//' "$fallback_env_file_abs")
set +a
else
echo "警告:环境文件不存在: $ENV_FILE_ABS,且未找到 .env.example,将继续使用 compose 默认值。" >&2
fi
DOCX_EDITOR_PORT=${DOCX_EDITOR_PORT:-18080}
export DOCX_EDITOR_PORT
if [ -z "${BACKEND_PUBLIC_BASE_URL:-}" ]; then
BACKEND_PUBLIC_BASE_URL="http://127.0.0.1:${DOCX_EDITOR_PORT}"
fi
normalized_public_base=$(normalize_loopback_host "${BACKEND_PUBLIC_BASE_URL}")
if [ "$normalized_public_base" != "${BACKEND_PUBLIC_BASE_URL}" ]; then
echo "检测到 BACKEND_PUBLIC_BASE_URL 使用 localhost,已自动改为 ${normalized_public_base}(避免 Windows 本地回环兼容问题)。"
BACKEND_PUBLIC_BASE_URL="$normalized_public_base"
fi
export BACKEND_PUBLIC_BASE_URL
normalized_document_server_url=$(normalize_loopback_host "${ONLYOFFICE_DOCUMENT_SERVER_URL:-}")
if [ "$normalized_document_server_url" != "${ONLYOFFICE_DOCUMENT_SERVER_URL:-}" ]; then
echo "检测到 ONLYOFFICE_DOCUMENT_SERVER_URL 使用 localhost,已自动改为 ${normalized_document_server_url}。"
ONLYOFFICE_DOCUMENT_SERVER_URL="$normalized_document_server_url"
export ONLYOFFICE_DOCUMENT_SERVER_URL
fi
}
prompt_local_build_mode() {
local choice=""
if [ -n "${DOCKER_BUILD_NO_CACHE+x}" ]; then
if [ "${DOCKER_BUILD_NO_CACHE:-0}" = "1" ]; then
echo "本地构建方式: 从 0 构建 / 不使用缓存(由 DOCKER_BUILD_NO_CACHE=1 指定)"
else
echo "本地构建方式: 使用缓存构建(由 DOCKER_BUILD_NO_CACHE=${DOCKER_BUILD_NO_CACHE:-0} 指定)"
fi
return 0
fi
echo "请选择本地构建方式:"
echo " 1) 使用缓存构建(默认,速度更快)"
echo " 2) 从 0 构建 / 不使用缓存(适用于版本升级或网络不稳定时重试)"
while true; do
read -r -p "请输入 1 或 2(默认 1): " choice
choice=${choice:-1}
case "$choice" in
1|cache)
DOCKER_BUILD_NO_CACHE=0
export DOCKER_BUILD_NO_CACHE
echo "构建策略: 使用缓存构建"
return 0
;;
2|no-cache|rebuild|clean)
DOCKER_BUILD_NO_CACHE=1
export DOCKER_BUILD_NO_CACHE
if [ -z "${DOCKER_BUILD_PULL+x}" ]; then
DOCKER_BUILD_PULL=1
export DOCKER_BUILD_PULL
fi
echo "构建策略: 从 0 构建 / 不使用缓存"
return 0
;;
*)
echo "输入无效,请输入 1 或 2。"
;;
esac
done
}
resolve_mode_and_compose() {
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
local)
DEPLOY_MODE="local"
;;
*.yml|*.yaml)
COMPOSE_FILE="$1"
;;
*)
echo "错误:无法识别参数 '$1'" >&2
usage >&2
exit 1
;;
esac
shift
done
if [ -z "$DEPLOY_MODE" ] && [ -n "${DOCKER_DEPLOY_MODE:-}" ]; then
DEPLOY_MODE="$DOCKER_DEPLOY_MODE"
fi
DEPLOY_MODE=${DEPLOY_MODE:-local}
if [ "$DEPLOY_MODE" != "local" ]; then
echo "错误:docx-editor 当前仅支持 local 模式,当前值为 '$DEPLOY_MODE'" >&2
exit 1
fi
COMPOSE_FILE=${COMPOSE_FILE:-${DOCKER_LOCAL_COMPOSE_FILE:-deploy/docker-compose.yml}}
if [ ! -f "$COMPOSE_FILE" ]; then
echo "错误:compose 文件不存在: $COMPOSE_FILE" >&2
exit 1
fi
if [ -t 0 ] && [ -t 1 ]; then
prompt_local_build_mode
fi
echo "部署模式: $DEPLOY_MODE"
echo "使用的 compose 文件: $COMPOSE_FILE"
}
require_docker() {
if ! command -v docker >/dev/null 2>&1; then
echo "错误:未找到 docker 命令。" >&2
exit 2
fi
if docker compose version >/dev/null 2>&1; then
COMPOSE_CMD=(docker compose)
else
COMPOSE_CMD=(docker-compose)
fi
if ! docker info >/dev/null 2>&1; then
echo "错误:无法连接到 Docker daemon,请先确认 Docker 已启动。" >&2
exit 3
fi
if is_msys_runtime; then
echo "检测到 Git Bash/MSYS 环境,已为 docker compose 启用路径转换保护。"
fi
}
run_compose_command() {
if is_msys_runtime; then
local protected_env_vars="ONLYOFFICE_DOCUMENT_SERVER_URL;ONLYOFFICE_DOCUMENT_SERVER_INTERNAL_URL;ONLYOFFICE_INTERNAL_BASE_URL;BACKEND_PUBLIC_BASE_URL"
local env_conv_excl="${MSYS2_ENV_CONV_EXCL:-}"
if [ -n "$env_conv_excl" ]; then
env_conv_excl="${env_conv_excl};${protected_env_vars}"
else
env_conv_excl="$protected_env_vars"
fi
MSYS_NO_PATHCONV=1 \
MSYS2_ARG_CONV_EXCL='*' \
MSYS2_ENV_CONV_EXCL="$env_conv_excl" \
"${COMPOSE_CMD[@]}" "$@"
return $?
fi
"${COMPOSE_CMD[@]}" "$@"
}
probe_url_speed() {
local url="$1"
local timeout="${2:-5}"
local max_bytes="${3:-1048576}"
if command -v curl >/dev/null 2>&1; then
curl -sS -o /dev/null \
-H "User-Agent: Mozilla/5.0" \
--connect-timeout "$timeout" \
--max-time "$timeout" \
-r "0-$((max_bytes - 1))" \
-w '%{speed_download}' \
"$url" 2>/dev/null | awk '{
speed_bytes = $1
if (speed_bytes > 0) {
printf "%.1f", speed_bytes / 1024
} else {
exit 1
}
}'
return "${PIPESTATUS[1]:-$?}"
fi
if command -v python3 >/dev/null 2>&1; then
python3 - "$url" "$timeout" "$max_bytes" <<'PY'
import sys
import time
import urllib.request
url, timeout, max_bytes = sys.argv[1], float(sys.argv[2]), int(sys.argv[3])
total = 0
start = time.perf_counter()
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req, timeout=timeout) as response:
while total < max_bytes:
chunk = response.read(min(65536, max_bytes - total))
if not chunk:
break
total += len(chunk)
elapsed = time.perf_counter() - start
if elapsed > 0 and total > 0:
print(f"{total / 1024 / elapsed:.1f}")
else:
raise SystemExit(1)
PY
return $?
fi
return 1
}
format_speed() {
local speed_kbps="$1"
awk -v s="$speed_kbps" 'BEGIN { if (s >= 1024) printf "%.2f MB/s", s / 1024; else printf "%.1f KB/s", s }'
}
normalize_source_profile() {
case "${DOCKER_SOURCE_PROFILE:-auto}" in
native|origin|official)
printf '%s' "native"
;;
mirror|cn|accelerated)
printf '%s' "mirror"
;;
auto|"")
printf '%s' "auto"
;;
*)
echo "警告:未知 DOCKER_SOURCE_PROFILE=${DOCKER_SOURCE_PROFILE:-},回退到 auto" >&2
printf '%s' "auto"
;;
esac
}
get_default_candidates() {
case "$1" in
PyPI)
cat <<'EOF2'
official|https://pypi.org/simple|https://pypi.org/simple/pip/
tsinghua|https://pypi.tuna.tsinghua.edu.cn/simple|https://pypi.tuna.tsinghua.edu.cn/simple/pip/
ustc|https://mirrors.ustc.edu.cn/pypi/simple|https://mirrors.ustc.edu.cn/pypi/simple/pip/
aliyun|https://mirrors.aliyun.com/pypi/simple|https://mirrors.aliyun.com/pypi/simple/pip/
tencent|https://mirrors.cloud.tencent.com/pypi/simple|https://mirrors.cloud.tencent.com/pypi/simple/pip/
huaweicloud|https://mirrors.huaweicloud.com/repository/pypi/simple|https://mirrors.huaweicloud.com/repository/pypi/simple/pip/
EOF2
;;
npm)
cat <<'EOF2'
official|https://registry.npmjs.org|https://registry.npmjs.org/npm
npmmirror|https://registry.npmmirror.com|https://registry.npmmirror.com/npm
tencent|https://mirrors.cloud.tencent.com/npm|https://mirrors.cloud.tencent.com/npm/npm
huaweicloud|https://mirrors.huaweicloud.com/repository/npm|https://mirrors.huaweicloud.com/repository/npm/npm
EOF2
;;
docker)
cat <<'EOF2'
official|official|https://registry-1.docker.io/v2/
1ms|docker.1ms.run|https://docker.1ms.run/v2/
xuanyuan|docker.xuanyuan.me|https://docker.xuanyuan.me/v2/
dockerpull|dockerpull.org|https://dockerpull.org/v2/
rat|hub.rat.dev|https://hub.rat.dev/v2/
1panel|docker.1panel.live|https://docker.1panel.live/v2/
EOF2
;;
*)
return 1
;;
esac
}
get_extra_candidates() {
local env_name="$1"
local raw="${!env_name:-}"
if [ -z "$raw" ]; then
return 0
fi
printf '%s' "$raw" | tr ';' '\n' | sed '/^[[:space:]]*$/d'
}
select_source_from_candidates() {
local label="$1"
local extra_env_name="$2"
local profile="$3"
local candidates=""
local official_value=""
local candidate_name=""
local candidate_value=""
local candidate_probe=""
local tmpdir=""
candidates="$({ get_default_candidates "$label"; get_extra_candidates "$extra_env_name"; } | sed '/^[[:space:]]*$/d')"
while IFS='|' read -r candidate_name candidate_value candidate_probe; do
if [ "$candidate_name" = "official" ]; then
official_value="$candidate_value"
if [ "$profile" = "native" ]; then
printf '%s' "$official_value"
return 0
fi
break
fi
done <<< "$candidates"
tmpdir=$(mktemp -d)
echo "$label: 并行测速候选源..." >&2
while IFS='|' read -r candidate_name candidate_value candidate_probe; do
[ -n "$candidate_name" ] || continue
[ -n "$candidate_value" ] || continue
[ -n "$candidate_probe" ] || continue
if [ "$profile" = "mirror" ] && [ "$candidate_name" = "official" ]; then
continue
fi
(
local speed=""
speed=$(probe_url_speed "$candidate_probe" 12 1048576 2>/dev/null) || true
if [ -n "$speed" ]; then
printf '%s|%s|%s\n' "$candidate_name" "$candidate_value" "$speed" > "$tmpdir/$candidate_name"
echo "$label/$candidate_name: $(format_speed "$speed") => $candidate_value" >&2
else
echo "$label/$candidate_name: 探测失败" >&2
fi
) &
done <<< "$candidates"
wait || true
local results=""
local f=""
for f in "$tmpdir"/*; do
[ -f "$f" ] || continue
results+="$(cat "$f")"$'\n'
done
rm -rf "$tmpdir"
if [ -n "$results" ]; then
local best_line=""
best_line=$(printf '%s' "$results" | sort -t '|' -k 3,3rn | head -n 1)
local best_name="" best_value="" best_speed=""
IFS='|' read -r best_name best_value best_speed <<< "$best_line"
echo "$label: 选择 $best_name ($(format_speed "$best_speed")) => $best_value" >&2
printf '%s' "$best_value"
return 0
fi
if [ -n "$official_value" ]; then
echo "$label: 候选源测速失败,回退官方源 => $official_value" >&2
printf '%s' "$official_value"
return 0
fi
echo "$label: 未找到可用候选源" >&2
return 1
}
build_source_fallbacks() {
local label="$1"
local extra_env_name="$2"
local profile="$3"
local primary_value="$4"
local candidates=""
local candidate_name=""
local candidate_value=""
local candidate_probe=""
local fallbacks=""
if [ "$profile" = "native" ]; then
return 0
fi
candidates="$({ get_default_candidates "$label"; get_extra_candidates "$extra_env_name"; } | sed '/^[[:space:]]*$/d')"
while IFS='|' read -r candidate_name candidate_value candidate_probe; do
[ -n "$candidate_name" ] || continue
[ -n "$candidate_value" ] || continue
if [ "$profile" = "mirror" ] && [ "$candidate_name" = "official" ]; then
continue
fi
if [ "$candidate_value" = "$primary_value" ]; then
continue
fi
case " $fallbacks " in
*" $candidate_value "*)
continue
;;
esac
if [ -n "$fallbacks" ]; then
fallbacks="$fallbacks $candidate_value"
else
fallbacks="$candidate_value"
fi
done <<< "$candidates"
printf '%s' "$fallbacks"
}
rewrite_docker_image() {
local mirror="$1"
local image="$2"
if [ "$mirror" = "official" ] || [ -z "$mirror" ]; then
printf '%s' "$image"
return 0
fi
case "$image" in
*/*)
printf '%s/%s' "$mirror" "$image"
;;
*)
printf '%s/library/%s' "$mirror" "$image"
;;
esac
}
build_docker_image_fallbacks() {
local mirrors="$1"
local image="$2"
local fallbacks=""
local mirror=""
local candidate=""
for mirror in $mirrors; do
[ -n "$mirror" ] || continue
candidate=$(rewrite_docker_image "$mirror" "$image")
case " $fallbacks " in
*" $candidate "*)
continue
;;
esac
if [ -n "$fallbacks" ]; then
fallbacks="$fallbacks $candidate"
else
fallbacks="$candidate"
fi
done
printf '%s' "$fallbacks"
}
append_official_fallback_mirror() {
local mirrors="$1"
case " $mirrors " in
*" official "*)
printf '%s' "$mirrors"
;;
*)
if [ -n "$mirrors" ]; then
printf '%s official' "$mirrors"
else
printf '%s' "official"
fi
;;
esac
}
set_auto_selected_docker_images_for_mirror() {
local mirror="$1"
local remaining_fallback_mirrors="${2:-}"
if [ "${DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ]; then
DOCKER_PYTHON_BASE_IMAGE=$(rewrite_docker_image "$mirror" "$PYTHON_BASE_IMAGE_DEFAULT")
export DOCKER_PYTHON_BASE_IMAGE
fi
if [ "${DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ]; then
DOCKER_NODE_BASE_IMAGE=$(rewrite_docker_image "$mirror" "$NODE_BASE_IMAGE_DEFAULT")
export DOCKER_NODE_BASE_IMAGE
fi
if [ "${DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ]; then
DOCKER_NGINX_BASE_IMAGE=$(rewrite_docker_image "$mirror" "$NGINX_BASE_IMAGE_DEFAULT")
export DOCKER_NGINX_BASE_IMAGE
fi
if [ "${DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED:-0}" = "1" ]; then
DOCKER_ONLYOFFICE_IMAGE=$(rewrite_docker_image "$mirror" "$ONLYOFFICE_IMAGE_DEFAULT")
export DOCKER_ONLYOFFICE_IMAGE
DOCKER_ONLYOFFICE_IMAGE_AUTO_FALLBACKS=$(build_docker_image_fallbacks "$remaining_fallback_mirrors" "$ONLYOFFICE_IMAGE_DEFAULT")
fi
}
build_output_mentions_auto_base_image() {
local output_file="$1"
[ -f "$output_file" ] || return 1
if [ "${DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] && [ -n "${DOCKER_PYTHON_BASE_IMAGE:-}" ] && grep -Fq "$DOCKER_PYTHON_BASE_IMAGE" "$output_file"; then
return 0
fi
if [ "${DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] && [ -n "${DOCKER_NODE_BASE_IMAGE:-}" ] && grep -Fq "$DOCKER_NODE_BASE_IMAGE" "$output_file"; then
return 0
fi
if [ "${DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] && [ -n "${DOCKER_NGINX_BASE_IMAGE:-}" ] && grep -Fq "$DOCKER_NGINX_BASE_IMAGE" "$output_file"; then
return 0
fi
return 1
}
configure_download_sources() {
local profile=""
local docker_fallback_mirrors=""
profile=$(normalize_source_profile)
echo "下载源策略: $profile"
DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED=0
DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS=""
DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED=0
DOCKER_ONLYOFFICE_IMAGE_AUTO_FALLBACKS=""
if [ -z "${DOCKER_PIP_INDEX_URL:-}" ]; then
DOCKER_PIP_INDEX_URL=$(select_source_from_candidates "PyPI" "DOCKER_PIP_CANDIDATES_EXTRA" "$profile")
export DOCKER_PIP_INDEX_URL
else
echo "PyPI: 使用手动配置 $DOCKER_PIP_INDEX_URL"
fi
if [ -z "${DOCKER_PIP_INDEX_FALLBACKS:-}" ]; then
DOCKER_PIP_INDEX_FALLBACKS=$(build_source_fallbacks "PyPI" "DOCKER_PIP_CANDIDATES_EXTRA" "$profile" "$DOCKER_PIP_INDEX_URL")
export DOCKER_PIP_INDEX_FALLBACKS
else
echo "PyPI fallback: 使用手动配置 $DOCKER_PIP_INDEX_FALLBACKS"
fi
if [ -z "${DOCKER_NPM_REGISTRY:-}" ]; then
DOCKER_NPM_REGISTRY=$(select_source_from_candidates "npm" "DOCKER_NPM_CANDIDATES_EXTRA" "$profile")
export DOCKER_NPM_REGISTRY
else
echo "npm: 使用手动配置 $DOCKER_NPM_REGISTRY"
fi
echo "已选择下载源:"
echo "- PyPI: $DOCKER_PIP_INDEX_URL"
if [ -n "${DOCKER_PIP_INDEX_FALLBACKS:-}" ]; then
echo "- PyPI fallback: $DOCKER_PIP_INDEX_FALLBACKS"
fi
echo "- npm: $DOCKER_NPM_REGISTRY"
local docker_mirror=""
local user_set_all_images=""
if [ -n "${DOCKER_PYTHON_BASE_IMAGE:-}" ] && [ -n "${DOCKER_NODE_BASE_IMAGE:-}" ] && [ -n "${DOCKER_NGINX_BASE_IMAGE:-}" ]; then
user_set_all_images=1
fi
if [ -n "$user_set_all_images" ]; then
echo "Docker 基础镜像: 使用手动配置"
echo " - Python: $DOCKER_PYTHON_BASE_IMAGE"
echo " - Node: $DOCKER_NODE_BASE_IMAGE"
echo " - Nginx: $DOCKER_NGINX_BASE_IMAGE"
else
docker_mirror=$(select_source_from_candidates "docker" "DOCKER_REGISTRY_CANDIDATES_EXTRA" "$profile")
if [ -z "${DOCKER_PYTHON_BASE_IMAGE:-}" ]; then
DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED=1
fi
if [ -z "${DOCKER_NODE_BASE_IMAGE:-}" ]; then
DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED=1
fi
if [ -z "${DOCKER_NGINX_BASE_IMAGE:-}" ]; then
DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED=1
fi
if [ -z "${DOCKER_ONLYOFFICE_IMAGE:-}" ]; then
DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED=1
fi
if [ "${DOCKER_PYTHON_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] || [ "${DOCKER_NODE_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] || [ "${DOCKER_NGINX_BASE_IMAGE_AUTO_SELECTED:-0}" = "1" ] || [ "${DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED:-0}" = "1" ]; then
docker_fallback_mirrors=$(build_source_fallbacks "docker" "DOCKER_REGISTRY_CANDIDATES_EXTRA" "$profile" "$docker_mirror")
if [ "$profile" = "mirror" ]; then
docker_fallback_mirrors=$(append_official_fallback_mirror "$docker_fallback_mirrors")
fi
DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS="$docker_fallback_mirrors"
set_auto_selected_docker_images_for_mirror "$docker_mirror" "$docker_fallback_mirrors"
fi
echo "Docker 基础镜像(自动选源):"
echo " - Python: $DOCKER_PYTHON_BASE_IMAGE"
echo " - Node: $DOCKER_NODE_BASE_IMAGE"
echo " - Nginx: $DOCKER_NGINX_BASE_IMAGE"
echo " - OnlyOffice: $DOCKER_ONLYOFFICE_IMAGE"
fi
}
validate_compose_file() {
local output=""
local line_no=""
local start=1
local end=1
if output=$(run_compose_command -f "$COMPOSE_FILE" config 2>&1 >/dev/null); then
return 0
fi
echo "错误:compose 文件校验失败: $COMPOSE_FILE" >&2
echo "$output" >&2
if [[ "$output" =~ yaml:\ line\ ([0-9]+): ]]; then
line_no="${BASH_REMATCH[1]}"
start=$(( line_no > 3 ? line_no - 3 : 1 ))
end=$(( line_no + 3 ))
echo "以下是出错行附近内容(第 ${line_no} 行):" >&2
nl -ba "$COMPOSE_FILE" | sed -n "${start},${end}p" >&2 || true
fi
exit 1
}
should_retry_onlyoffice_with_official() {
local output_file="$1"
[ "${DOCKER_ONLYOFFICE_IMAGE_AUTO_SELECTED:-0}" = "1" ] || return 1
[ -n "${DOCKER_ONLYOFFICE_IMAGE:-}" ] || return 1
[ -f "$output_file" ] || return 1
grep -Fq "$DOCKER_ONLYOFFICE_IMAGE" "$output_file" || return 1
grep -Eiq 'failed to resolve reference|error response from daemon|too many requests|toomanyrequests|tls handshake timeout|i/o timeout|connection refused|connectex|no such host|deadline exceeded' "$output_file" || return 1
}
should_retry_build_with_base_images() {
local output_file="$1"
[ -n "${DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS:-}" ] || return 1
build_output_mentions_auto_base_image "$output_file" || return 1
grep -Eiq 'load metadata for|failed to resolve source metadata|failed to resolve reference|unexpected status from HEAD request|403 forbidden|too many requests|toomanyrequests|tls handshake timeout|i/o timeout|connection refused|connectex|no such host|deadline exceeded' "$output_file" || return 1
}
run_compose_build_with_base_image_fallback() {
local build_args=("$@")
local output_file=""
local fallback_mirrors="${DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS:-}"
local next_mirror=""
local status=0
output_file=$(mktemp)
while true; do
set +e
run_compose_command -f "$COMPOSE_FILE" build "${build_args[@]}" 2>&1 | tee "$output_file"
status=${PIPESTATUS[0]}
set -e
if [ "$status" -eq 0 ]; then
rm -f "$output_file"
return 0
fi
if ! should_retry_build_with_base_images "$output_file"; then
break
fi
if [ -z "$fallback_mirrors" ]; then
break
fi
next_mirror="${fallback_mirrors%% *}"
if [ "$fallback_mirrors" = "$next_mirror" ]; then
fallback_mirrors=""
else
fallback_mirrors="${fallback_mirrors#* }"
fi
echo "检测到 Docker 基础镜像拉取失败,切换到备用镜像后重试构建..."
echo " - 当前 Python: ${DOCKER_PYTHON_BASE_IMAGE:-}"
echo " - 当前 Node: ${DOCKER_NODE_BASE_IMAGE:-}"
echo " - 当前 Nginx: ${DOCKER_NGINX_BASE_IMAGE:-}"
set_auto_selected_docker_images_for_mirror "$next_mirror" "$fallback_mirrors"
DOCKER_BASE_IMAGE_AUTO_FALLBACK_MIRRORS="$fallback_mirrors"
echo " - 重试 Python: ${DOCKER_PYTHON_BASE_IMAGE:-}"
echo " - 重试 Node: ${DOCKER_NODE_BASE_IMAGE:-}"
echo " - 重试 Nginx: ${DOCKER_NGINX_BASE_IMAGE:-}"
if [ -n "$fallback_mirrors" ]; then
echo " - 剩余备用镜像源: $fallback_mirrors"
fi
done
rm -f "$output_file"
return "$status"
}
run_compose_up_with_onlyoffice_fallback() {
local up_args=("$@")
local output_file=""
local fallback_images="${DOCKER_ONLYOFFICE_IMAGE_AUTO_FALLBACKS:-}"
local next_image=""
local status=0
output_file=$(mktemp)
while true; do
set +e
run_compose_command -f "$COMPOSE_FILE" up "${up_args[@]}" 2>&1 | tee "$output_file"
status=${PIPESTATUS[0]}
set -e
if [ "$status" -eq 0 ]; then
rm -f "$output_file"
return 0
fi
if ! should_retry_onlyoffice_with_official "$output_file"; then
break
fi
if [ -z "$fallback_images" ]; then
break
fi
next_image="${fallback_images%% *}"
if [ "$fallback_images" = "$next_image" ]; then
fallback_images=""
else
fallback_images="${fallback_images#* }"
fi
echo "检测到 ONLYOFFICE 镜像拉取失败,切换到备用镜像后重试..."
echo " - 当前镜像: $DOCKER_ONLYOFFICE_IMAGE"
DOCKER_ONLYOFFICE_IMAGE="$next_image"
export DOCKER_ONLYOFFICE_IMAGE
echo " - 重试镜像: $DOCKER_ONLYOFFICE_IMAGE"
if [ -n "$fallback_images" ]; then
echo " - 剩余备用镜像: $fallback_images"
fi
done
rm -f "$output_file"
return "$status"
}
run_local_mode() {
local build_args=()
local up_args=(-d --remove-orphans)
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
configure_download_sources
validate_compose_file
if [ "${DOCKER_BUILD_NO_CACHE:-0}" = "1" ]; then
build_args+=(--no-cache)
echo "开始构建镜像(从 0 构建,不使用缓存)..."
else
echo "开始构建镜像(启用缓存)..."
fi
if [ "${DOCKER_BUILD_PULL:-0}" = "1" ]; then
build_args+=(--pull)
fi
if [ "${DOCKER_FORCE_RECREATE:-1}" = "1" ]; then
up_args+=(--force-recreate)
fi
run_compose_build_with_base_image_fallback "${build_args[@]}"
echo "启动容器..."
run_compose_up_with_onlyoffice_fallback "${up_args[@]}"
}
collect_status_and_logs() {
local services=(backend frontend onlyoffice)
local svc=""
echo "收集服务状态与日志..."
run_compose_command -f "$COMPOSE_FILE" ps
for svc in "${services[@]}"; do
echo "--- 日志: $svc(最近 200 行)---"
run_compose_command -f "$COMPOSE_FILE" logs --tail 200 "$svc" | tee "$LOG_DIR/$svc.log" || true
done
echo "日志已保存到 $LOG_DIR/*.log"
}
print_summary() {
local access_url="${BACKEND_PUBLIC_BASE_URL:-http://127.0.0.1:${DOCX_EDITOR_PORT}}"
echo "部署完成。访问 ${access_url} 使用 DOCX Editor。"
if [[ "$access_url" =~ ^https?://localhost(:[0-9]+)?$ ]]; then
echo "提示:Windows 环境下 localhost 可能导致 ONLYOFFICE API 超时,建议改用 http://127.0.0.1:${DOCX_EDITOR_PORT}。"
fi
if [[ "${BACKEND_PUBLIC_BASE_URL:-}" =~ ^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$ ]]; then
echo "提示:当前 BACKEND_PUBLIC_BASE_URL=$BACKEND_PUBLIC_BASE_URL。若要给其他机器或浏览器端嵌入,请改成对外可访问域名。"
fi
if [ -n "${DOCX_EDITOR_BOOTSTRAP_KEY:-}" ]; then
echo "可使用 DOCX_EDITOR_BOOTSTRAP_KEY 登录: ${DOCX_EDITOR_BOOTSTRAP_KEY}"
else
echo "如未预设 DOCX_EDITOR_BOOTSTRAP_KEY,请查看 backend 日志获取首次生成的 key。"
fi
}
main() {
auto_fix_runtime_line_endings
load_env_file
resolve_mode_and_compose "$@"
auto_fix_runtime_line_endings
require_docker
run_local_mode
collect_status_and_logs
print_summary
}
main "$@"