forked from SuperManito/LinuxMirrors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerInstallation.sh
More file actions
executable file
·2515 lines (2449 loc) · 108 KB
/
DockerInstallation.sh
File metadata and controls
executable file
·2515 lines (2449 loc) · 108 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
996
997
998
999
1000
#!/bin/bash
## Author: SuperManito
## Modified: 2025-10-23
## License: MIT
## GitHub: https://github.com/SuperManito/LinuxMirrors
## Website: https://linuxmirrors.cn
## Docker CE 软件源列表
mirror_list_docker_ce=(
"mirrors.aliyun.com/docker-ce"
"mirrors.tencent.com/docker-ce"
"mirrors.huaweicloud.com/docker-ce"
"mirrors.cmecloud.cn/docker-ce"
"mirrors.163.com/docker-ce"
"mirrors.volces.com/docker"
"mirror.azure.cn/docker-ce"
"mirrors.tuna.tsinghua.edu.cn/docker-ce"
"mirrors.pku.edu.cn/docker-ce"
"mirrors.zju.edu.cn/docker-ce"
"mirrors.nju.edu.cn/docker-ce"
"mirror.sjtu.edu.cn/docker-ce"
"mirrors.cqupt.edu.cn/docker-ce"
"mirrors.ustc.edu.cn/docker-ce"
"mirror.iscas.ac.cn/docker-ce"
"download.docker.com"
)
## Docker Registry 仓库列表
mirror_list_registry=(
"docker.1ms.run"
"dockerproxy.net"
"docker.m.daocloud.io"
"docker.1panel.live"
"registry.cn-hangzhou.aliyuncs.com"
"registry.cn-shanghai.aliyuncs.com"
"registry.cn-qingdao.aliyuncs.com"
"registry.cn-beijing.aliyuncs.com"
"registry.cn-zhangjiakou.aliyuncs.com"
"registry.cn-huhehaote.aliyuncs.com"
"registry.cn-wulanchabu.aliyuncs.com"
"registry.cn-shenzhen.aliyuncs.com"
"registry.cn-heyuan.aliyuncs.com"
"registry.cn-guangzhou.aliyuncs.com"
"registry.cn-chengdu.aliyuncs.com"
"registry.cn-hongkong.aliyuncs.com"
"registry.ap-northeast-1.aliyuncs.com"
"registry.ap-southeast-1.aliyuncs.com"
"registry.ap-southeast-3.aliyuncs.com"
"registry.ap-southeast-5.aliyuncs.com"
"registry.eu-central-1.aliyuncs.com"
"registry.eu-west-1.aliyuncs.com"
"registry.us-west-1.aliyuncs.com"
"registry.us-east-1.aliyuncs.com"
"registry.me-east-1.aliyuncs.com"
"mirror.ccs.tencentyun.com"
"gcr.io"
"asia.gcr.io"
"eu.gcr.io"
"registry.hub.docker.com"
)
## 配置需要区分公网地址和内网地址的软件源(不分地域)
# 需要同时在两个数组变量中分别定义软件源地址,并且保证排列顺序一致
# 软件源公网地址列表
mirror_list_extranet=(
"mirrors.aliyun.com/docker-ce"
"mirrors.tencent.com/docker-ce"
"mirrors.huaweicloud.com/docker-ce"
"mirrors.volces.com/docker-ce"
)
# 软件源内网地址列表
mirror_list_intranet=(
"mirrors.cloud.aliyuncs.com/docker-ce"
"mirrors.tencentyun.com/docker-ce"
"mirrors.myhuaweicloud.com/docker-ce"
"mirrors.ivolces.com/docker-ce"
)
## 赞助商广告
SPONSOR_ADS=(
"1Panel · Linux 面板|极简运维 ➜ \033[3mhttps://1panel.cn\033[0m"
"多途云 · 智能化防护,每一次连接皆在安全之下 ➜ \033[3mhttps://www.duotuyun.com\033[0m"
"毫秒镜像 · 专为中国开发者提供Docker镜像加速下载服务 ➜ \033[3mhttps://1ms.run\033[0m"
"不死鸟CDN · 香港日本高防CDN,免实名/免备案,轻松阻断DDOS/CC攻击 ➜ \033[3mhttps://www.bsncdn.org\033[0m"
"青叶云 · 香港1T高防|自助防火墙,无视CC|大带宽回国优化线路 ➜ \033[3mhttps://www.qingyeyun.com\033[0m"
"莱卡云 · 专业云计算服务器提供商 ➜ \033[3m https://www.lcayun.com\033[0m"
"云悠YUNYOO · 全球高性价比云服务器|低至15.99元起 ➜ \033[3mhttps://yunyoo.cc\033[0m"
"速拓云 · 国内高防云28元/月|香港云100M优化线路9元/月 ➜ \033[3mhttps://www.sutuoyun.com\033[0m"
"林枫云 · 专注独立IP高频VPS|R9/i9系列定制 ➜ \033[3mhttps://www.dkdun.cn\033[0m"
"语鹿云盾 · 专业CDN加速、防御,亚太百兆三网优化CDN低至9元起 ➜ \033[3mhttps://www.lucdn.cn\033[0m"
"不二云 · 国内外建站快响应服务器的不二之选 ➜ \033[3mhttps://cb2.cn\033[0m"
"HKGserver · 全球家宽|双ISP|住宅原生云服务器54元/月起 ➜ \033[3mhttps://www.hkgserver.com\033[0m"
"浪浪云 · BGP网络让每一次连接都纵享丝滑,明码标价、无套路续费 ➜ \033[3mhttps://langlangy.cn\033[0m"
)
##############################################################################
## 定义系统判定变量
SYSTEM_DEBIAN="Debian"
SYSTEM_UBUNTU="Ubuntu"
SYSTEM_KALI="Kali"
SYSTEM_DEEPIN="Deepin"
SYSTEM_LINUX_MINT="Linuxmint"
SYSTEM_ZORIN="Zorin"
SYSTEM_RASPBERRY_PI_OS="Raspberry Pi OS"
SYSTEM_REDHAT="RedHat"
SYSTEM_RHEL="Red Hat Enterprise Linux"
SYSTEM_CENTOS="CentOS"
SYSTEM_CENTOS_STREAM="CentOS Stream"
SYSTEM_ROCKY="Rocky"
SYSTEM_ALMALINUX="AlmaLinux"
SYSTEM_FEDORA="Fedora"
SYSTEM_ORACLE="Oracle Linux"
SYSTEM_OPENCLOUDOS="OpenCloudOS"
SYSTEM_OPENCLOUDOS_STREAM="OpenCloudOS Stream"
SYSTEM_TENCENTOS="TencentOS"
SYSTEM_OPENEULER="openEuler"
SYSTEM_ANOLISOS="Anolis"
SYSTEM_OPENKYLIN="openKylin"
SYSTEM_OPENSUSE="openSUSE"
SYSTEM_ARCH="Arch"
SYSTEM_ALPINE="Alpine"
SYSTEM_GENTOO="Gentoo"
SYSTEM_NIXOS="NixOS"
## 定义系统版本文件
File_LinuxRelease=/etc/os-release
File_RedHatRelease=/etc/redhat-release
File_DebianVersion=/etc/debian_version
File_ArmbianRelease=/etc/armbian-release
File_RaspberryPiOSRelease=/etc/rpi-issue
File_openEulerRelease=/etc/openEuler-release
File_HuaweiCloudEulerOSRelease=/etc/hce-release
File_OpenCloudOSRelease=/etc/opencloudos-release
File_TencentOSServerRelease=/etc/tlinux-release
File_AnolisOSRelease=/etc/anolis-release
File_AlibabaCloudLinuxRelease=/etc/alinux-release
File_OracleLinuxRelease=/etc/oracle-release
File_ArchLinuxRelease=/etc/arch-release
File_ManjaroRelease=/etc/manjaro-release
File_AlpineRelease=/etc/alpine-release
File_GentooRelease=/etc/gentoo-release
File_openKylinVersion=/etc/kylin-version/kylin-system-version.conf
## 定义软件源相关文件或目录
File_AptSourceList=/etc/apt/sources.list
Dir_AptAdditionalSources=/etc/apt/sources.list.d
Dir_YumRepos=/etc/yum.repos.d
## 定义 Docker 相关变量
Dir_Docker=/etc/docker
File_DockerConfig=$Dir_Docker/daemon.json
File_DockerConfigBackup=$Dir_Docker/daemon.json.bak
File_DockerVersionTmp=docker-version.txt
File_DockerCEVersionTmp=docker-ce-version.txt
File_DockerCECliVersionTmp=docker-ce-cli-version.txt
File_DockerSourceList=$Dir_AptAdditionalSources/docker.list
File_DockerRepo=$Dir_YumRepos/docker-ce.repo
## 定义颜色和样式变量
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[35m'
AZURE='\033[36m'
PLAIN='\033[0m'
BOLD='\033[1m'
SUCCESS="\033[1;32m✔${PLAIN}"
COMPLETE="\033[1;32m✔${PLAIN}"
WARN="\033[1;43m 警告 ${PLAIN}"
ERROR="\033[1;31m✘${PLAIN}"
FAIL="\033[1;31m✘${PLAIN}"
TIP="\033[1;44m 提示 ${PLAIN}"
WORKING="\033[1;36m◉${PLAIN}"
function main() {
permission_judgment
collect_system_info
run_start
choose_mirrors
if [[ "${ONLY_REGISTRY}" == "true" ]]; then
only_change_docker_registry_mirror
else
choose_protocol
close_firewall_service
install_dependency_packages
configure_docker_ce_mirror
install_docker_engine
change_docker_registry_mirror
check_installed_result
fi
run_end
}
function handle_command_options() {
## 判断参数
while [ $# -gt 0 ]; do
case "$1" in
## 指定 Docker CE 软件源地址
--source)
if [ "$2" ]; then
echo "$2" | grep -Eq "\(|\)|\[|\]|\{|\}"
if [ $? -eq 0 ]; then
command_error "$2" "$(msg "error.cmd.options.validAddress")"
else
SOURCE="$(echo "$2" | sed -e 's,^http[s]\?://,,g' -e 's,/$,,')"
shift
fi
else
command_error "$1" "$(msg "error.cmd.options.sourceAddress")"
fi
;;
## 指定 Docker Registry 仓库地址
--source-registry)
if [ "$2" ]; then
echo "$2" | grep -Eq "\(|\)|\[|\]|\{|\}"
if [ $? -eq 0 ]; then
command_error "$2" "$(msg "error.cmd.options.validAddress")"
else
SOURCE_REGISTRY="$(echo "$2" | sed -e 's,^http[s]\?://,,g' -e 's,/$,,')"
shift
fi
else
command_error "$1" "$(msg "error.cmd.options.registryAddress")"
fi
;;
## 指定 Docker CE 软件源仓库
--branch)
if [ "$2" ]; then
SOURCE_BRANCH="$2"
shift
else
command_error "$1" "$(msg "error.cmd.options.sourceRepository")"
fi
;;
## 指定 Docker CE 软件源仓库版本
--branch-version)
if [ "$2" ]; then
echo "$2" | grep -Eq "^[0-9]{1,2}$"
if [ $? -eq 0 ]; then
SOURCE_BRANCH_VERSION="$2"
shift
else
command_error "$2" "$(msg "error.cmd.options.validVersion")"
fi
else
command_error "$1" "$(msg "error.cmd.options.ceRepositoryVersion")"
fi
;;
## 指定 Docker Engine 安装版本
--designated-version)
if [ "$2" ]; then
echo "$2" | grep -Eq "^[0-9][0-9].[0-9]{1,2}.[0-9]{1,2}$"
if [ $? -eq 0 ]; then
DESIGNATED_DOCKER_VERSION="$2"
shift
else
command_error "$2" "$(msg "error.cmd.options.validVersion")"
fi
else
command_error "$1" "$(msg "error.cmd.options.version")"
fi
;;
## 指定 Debian 版本代号
--codename)
if [ "$2" ]; then
DEBIAN_CODENAME="$2"
shift
else
command_error "$1" "$(msg "error.cmd.options.codename")"
fi
;;
## Web 协议(HTTP/HTTPS)
--protocol)
if [ "$2" ]; then
case "$2" in
http | https | HTTP | HTTPS)
WEB_PROTOCOL="${2,,}"
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.protocol")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.needProtocol")"
fi
;;
## 使用内网地址
--use-intranet-source)
if [ "$2" ]; then
case "$2" in
[Tt]rue | [Ff]alse)
USE_INTRANET_SOURCE="${2,,}"
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.needIntranetSource")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.needIntranetSource")"
fi
;;
## 安装最新版本
--install-latest | --install-latested)
if [ "$2" ]; then
case "$2" in
[Tt]rue | [Ff]alse)
INSTALL_LATESTED_DOCKER="${2,,}"
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.needIntranetSource")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.needIntranetSource")"
fi
;;
## 忽略覆盖备份提示
--ignore-backup-tips)
IGNORE_BACKUP_TIPS="true"
;;
## 关闭防火墙
--close-firewall)
if [ "$2" ]; then
case "$2" in
[Tt]rue | [Ff]alse)
CLOSE_FIREWALL="${2,,}"
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.needIntranetSource")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.needIntranetSource")"
fi
;;
## 清除屏幕上的所有内容
--clean-screen)
if [ "$2" ]; then
case "$2" in
[Tt]rue | [Ff]alse)
CLEAN_SCREEN="${2,,}"
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.needIntranetSource")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.needIntranetSource")"
fi
;;
## Locale
--lang)
if [ "$2" ]; then
local lang_norm="${2//_/-}"
lang_norm="${lang_norm,,}"
case "$lang_norm" in
zh | zh-cn | zh-hans | zh-hans-*)
init_msg_pack "zh-hans"
shift
;;
zh-hant | zh-hant-* | zh-tw | zh-hk)
init_msg_pack "zh-hant"
shift
;;
en | en-us | en-*)
init_msg_pack "en"
shift
;;
auto)
choose_display_language
shift
;;
*)
command_error "$2" "$(msg "error.cmd.options.validLangKey")"
;;
esac
else
command_error "$1" "$(msg "error.cmd.options.langKey")"
fi
;;
--zh | --zh-[Cc][Nn])
init_msg_pack "zh-hans"
;;
--en | --en-[Uu][Ss])
init_msg_pack "en"
;;
--zh-[Hh]an[st])
init_msg_pack "$1"
;;
## 仅更换镜像仓库模式
--only-registry)
ONLY_REGISTRY="true"
;;
## 纯净模式
--pure-mode)
PURE_MODE="true"
;;
## 命令帮助
--help)
echo -e "\n$(msg "commands.help" "https://github.com/SuperManito/LinuxMirrors/issues")\n"
exit
;;
*)
command_error "$1"
;;
esac
shift
done
## 设置部分功能的默认值
IGNORE_BACKUP_TIPS="${IGNORE_BACKUP_TIPS:-"false"}"
if [[ "${DESIGNATED_DOCKER_VERSION}" ]]; then
INSTALL_LATESTED_DOCKER="false"
fi
PURE_MODE="${PURE_MODE:-"false"}"
}
function run_start() {
if [ -z "${CLEAN_SCREEN}" ]; then
if [[ "${ONLY_REGISTRY}" == "true" ]]; then
[[ -z "${SOURCE_REGISTRY}" ]] && clear
else
[[ -z "${SOURCE}" || -z "${SOURCE_REGISTRY}" ]] && clear
fi
elif [ "${CLEAN_SCREEN}" == "true" ]; then
clear
fi
if [[ "${PURE_MODE}" == "true" ]]; then
return
fi
local system_name="${SYSTEM_PRETTY_NAME:-"${SYSTEM_NAME} ${SYSTEM_VERSION_ID}"}"
local arch="${DEVICE_ARCH}"
local date_time="$(date "+%Y-%m-%d %H:%M")"
local time_zone="$(timedatectl status 2>/dev/null | grep "Time zone" | awk -F ':' '{print$2}' | awk -F ' ' '{print$1}')"
echo -e "+-----------------------------------+"
echo -e "| \033[0;1;35;95m⡇\033[0m \033[0;1;33;93m⠄\033[0m \033[0;1;32;92m⣀⡀\033[0m \033[0;1;36;96m⡀\033[0;1;34;94m⢀\033[0m \033[0;1;35;95m⡀⢀\033[0m \033[0;1;31;91m⡷\033[0;1;33;93m⢾\033[0m \033[0;1;32;92m⠄\033[0m \033[0;1;36;96m⡀⣀\033[0m \033[0;1;34;94m⡀\033[0;1;35;95m⣀\033[0m \033[0;1;31;91m⢀⡀\033[0m \033[0;1;33;93m⡀\033[0;1;32;92m⣀\033[0m \033[0;1;36;96m⢀⣀\033[0m |"
echo -e "| \033[0;1;31;91m⠧\033[0;1;33;93m⠤\033[0m \033[0;1;32;92m⠇\033[0m \033[0;1;36;96m⠇⠸\033[0m \033[0;1;34;94m⠣\033[0;1;35;95m⠼\033[0m \033[0;1;31;91m⠜⠣\033[0m \033[0;1;33;93m⠇\033[0;1;32;92m⠸\033[0m \033[0;1;36;96m⠇\033[0m \033[0;1;34;94m⠏\033[0m \033[0;1;35;95m⠏\033[0m \033[0;1;33;93m⠣⠜\033[0m \033[0;1;32;92m⠏\033[0m \033[0;1;34;94m⠭⠕\033[0m |"
echo -e "+-----------------------------------+"
echo -e "$(msg "start.welcome")"
echo -e ''
echo -e "$(msg "start.runtimeEnv") ${BLUE}${system_name} ${arch}${PLAIN}"
echo -e "$(msg "start.dateTime") ${BLUE}${date_time} ${time_zone}${PLAIN}"
sleep 1 >/dev/null 2>&1
}
function run_end() {
if [[ "${PURE_MODE}" == "true" ]]; then
echo ''
return
fi
echo -e "\n✨ $(msg "end.moreInfo") 👉 \033[3mhttps://linuxmirrors.cn\033[0m"
if [[ "${#SPONSOR_ADS[@]}" -gt 0 ]]; then
echo -e "\n\033[2m$(msg "end.sponsorAds")\033[0m"
for ad in "${SPONSOR_ADS[@]}"; do
sleep 0.1
echo -e " \033[2m${ad}\033[0m"
done
fi
echo -e "\n\033[3;1mPowered by \033[34mLinuxMirrors\033[0m\n"
}
function output_error() {
[ "$1" ] && echo -e "\n$ERROR $1\n"
exit 1
}
function command_error() {
local tmp_text="$(msg "error.cmd.options.needConfirm")"
if [[ "${2}" ]]; then
tmp_text="$(msg "error.cmd.options.needSpecify" "${2}")"
fi
output_error "$(msg "error.cmd.options.invalid" "${BLUE}$1${PLAIN}" "${tmp_text}")"
}
function unsupport_system_error() {
if [[ "${2}" ]]; then
output_error "$(msg "error.unsupportSystem2" "${1}")\n\n${BLUE}$2${PLAIN}"
else
output_error "$(msg "error.unsupportSystem1" "${1}")"
fi
}
function input_error() {
echo -e "\n$WARN $(msg "error.input" "${1}")"
}
function command_exists() {
command -v "$@" &>/dev/null
}
function permission_judgment() {
if [ $UID -ne 0 ]; then
output_error "$(msg "error.needRoot")"
fi
}
function get_os_release_value() {
grep -E "^${1}=" $File_LinuxRelease | cut -d= -f2- | sed "s/[\'\"]//g"
}
function collect_system_info() {
if [ ! -s "${File_LinuxRelease}" ]; then
unsupport_system_error "$(msg "error.unknownSystem")"
fi
## 定义系统名称
SYSTEM_NAME="$(get_os_release_value NAME)"
SYSTEM_PRETTY_NAME="$(get_os_release_value PRETTY_NAME)"
## 定义系统版本号
SYSTEM_VERSION_ID="$(get_os_release_value VERSION_ID)"
SYSTEM_VERSION_ID_MAJOR="${SYSTEM_VERSION_ID%.*}"
SYSTEM_VERSION_ID_MINOR="${SYSTEM_VERSION_ID#*.}"
## 定义系统ID
SYSTEM_ID="$(get_os_release_value ID)"
## 判定当前系统派系
if [ -s "${File_DebianVersion}" ]; then
SYSTEM_FACTIONS="${SYSTEM_DEBIAN}"
elif [ -s "${File_RedHatRelease}" ]; then
SYSTEM_FACTIONS="${SYSTEM_REDHAT}"
elif [ -s "${File_openEulerRelease}" ] || [ -s "${File_HuaweiCloudEulerOSRelease}" ]; then
SYSTEM_FACTIONS="${SYSTEM_OPENEULER}"
elif [ -s "${File_OpenCloudOSRelease}" ]; then
SYSTEM_FACTIONS="${SYSTEM_OPENCLOUDOS}" # 自 9.0 版本起不再基于红帽
elif [ -s "${File_AnolisOSRelease}" ]; then
SYSTEM_FACTIONS="${SYSTEM_ANOLISOS}" # 自 8.8 版本起不再基于红帽
elif [ -s "${File_TencentOSServerRelease}" ]; then
SYSTEM_FACTIONS="${SYSTEM_TENCENTOS}" # 自 4 版本起不再基于红帽
elif [ -s "${File_openKylinVersion}" ]; then
[[ "${ONLY_REGISTRY}" != "true" ]] && unsupport_system_error "openKylin" "apt-get install -y docker\nsystemctl enable --now docker"
elif [ -f "${File_ArchLinuxRelease}" ]; then
[[ "${ONLY_REGISTRY}" != "true" ]] && unsupport_system_error "Arch Linux" "pacman -S docker\nsystemctl enable --now docker"
elif [ -f "${File_GentooRelease}" ]; then
[[ "${ONLY_REGISTRY}" != "true" ]] && unsupport_system_error "Gentoo"
elif [[ "${SYSTEM_NAME}" == *"openSUSE"* ]]; then
[[ "${ONLY_REGISTRY}" != "true" ]] && unsupport_system_error "openSUSE" "zypper install docker\nsystemctl enable --now docker"
elif [[ "${SYSTEM_NAME}" == *"NixOS"* ]]; then
[[ "${ONLY_REGISTRY}" != "true" ]] && unsupport_system_error "NixOS"
else
unsupport_system_error "$(msg "error.unknownSystem")"
fi
## 判定系统类型、版本、版本号
case "${SYSTEM_FACTIONS}" in
"${SYSTEM_DEBIAN}")
if command_exists lsb_release; then
SYSTEM_JUDGMENT="$(lsb_release -is)"
SYSTEM_VERSION_CODENAME="${DEBIAN_CODENAME:-"$(lsb_release -cs)"}"
else
## https://codeberg.org/gioele/lsb-release-minimal
SYSTEM_JUDGMENT="${SYSTEM_ID^}"
if [ "${SYSTEM_NAME}" ]; then
if [[ "${SYSTEM_ID,,}" == "${SYSTEM_NAME,,}" ]]; then
SYSTEM_JUDGMENT="${SYSTEM_NAME}"
fi
fi
SYSTEM_VERSION_CODENAME="${DEBIAN_CODENAME:-"$(get_os_release_value VERSION_CODENAME)"}"
fi
# Raspberry Pi OS
if [ -s "${File_RaspberryPiOSRelease}" ]; then
SYSTEM_JUDGMENT="${SYSTEM_RASPBERRY_PI_OS}"
SYSTEM_PRETTY_NAME="${SYSTEM_RASPBERRY_PI_OS}"
fi
;;
"${SYSTEM_REDHAT}")
SYSTEM_JUDGMENT="$(awk '{printf $1}' $File_RedHatRelease)"
## 特殊系统判断
# Red Hat Enterprise Linux
grep -q "${SYSTEM_RHEL}" $File_RedHatRelease && SYSTEM_JUDGMENT="${SYSTEM_RHEL}"
# CentOS Stream
grep -q "${SYSTEM_CENTOS_STREAM}" $File_RedHatRelease && SYSTEM_JUDGMENT="${SYSTEM_CENTOS_STREAM}"
# Oracle Linux
[ -s "${File_OracleLinuxRelease}" ] && SYSTEM_JUDGMENT="${SYSTEM_ORACLE}"
;;
*)
SYSTEM_JUDGMENT="${SYSTEM_FACTIONS}"
;;
esac
## 判定系统处理器架构
DEVICE_ARCH_RAW="$(uname -m)"
case "${DEVICE_ARCH_RAW}" in
x86_64)
DEVICE_ARCH="x86_64"
;;
aarch64)
DEVICE_ARCH="ARM64"
;;
armv8l)
DEVICE_ARCH="ARMv8_32"
;;
armv7l)
DEVICE_ARCH="ARMv7"
;;
armv6l)
DEVICE_ARCH="ARMv6"
;;
armv5tel)
DEVICE_ARCH="ARMv5"
;;
ppc64le)
DEVICE_ARCH="ppc64le"
;;
s390x)
DEVICE_ARCH="s390x"
;;
i386 | i686)
output_error "$(msg "error.unsupportX86_32")"
;;
*)
output_error "$(msg "error.unknownArch" "${DEVICE_ARCH_RAW}")"
;;
esac
## 定义软件源仓库名称
if [[ -z "${SOURCE_BRANCH}" ]]; then
case "${SYSTEM_FACTIONS}" in
"${SYSTEM_DEBIAN}")
case "${SYSTEM_JUDGMENT}" in
"${SYSTEM_DEBIAN}")
SOURCE_BRANCH="debian"
;;
"${SYSTEM_UBUNTU}" | "${SYSTEM_ZORIN}")
SOURCE_BRANCH="ubuntu"
;;
"${SYSTEM_KALI}")
SOURCE_BRANCH="debian"
SOURCE_BRANCH_CODENAME="trixie"
;;
"${SYSTEM_LINUX_MINT}")
if [[ "${SYSTEM_NAME}" == *"LMDE"* ]]; then
SOURCE_BRANCH="debian"
SOURCE_BRANCH_CODENAME="$(get_os_release_value DEBIAN_CODENAME)"
else
SOURCE_BRANCH="ubuntu"
SOURCE_BRANCH_CODENAME="$(get_os_release_value UBUNTU_CODENAME)"
fi
if [[ -z "${SOURCE_BRANCH_CODENAME}" ]]; then
SOURCE_BRANCH="debian"
SOURCE_BRANCH_CODENAME="bookworm"
fi
;;
"${SYSTEM_RASPBERRY_PI_OS}")
case "${DEVICE_ARCH_RAW}" in
x86_64 | aarch64)
SOURCE_BRANCH="debian"
;;
*)
# 注:自 Docker 29 版本起将不再提供此分支仓库
SOURCE_BRANCH="raspbian"
;;
esac
;;
*)
# 其余 Debian 系衍生操作系统
SOURCE_BRANCH="debian"
SOURCE_BRANCH_CODENAME="bookworm"
;;
esac
;;
"${SYSTEM_REDHAT}" | "${SYSTEM_OPENEULER}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_ANOLISOS}" | "${SYSTEM_TENCENTOS}")
case "${SYSTEM_JUDGMENT}" in
"${SYSTEM_FEDORA}")
SOURCE_BRANCH="fedora"
;;
"${SYSTEM_RHEL}")
SOURCE_BRANCH="rhel"
;;
*)
SOURCE_BRANCH="centos"
;;
esac
if [[ "${DEVICE_ARCH_RAW}" == "s390x" ]]; then
output_error "$(msg "error.unsupportS390x")"
fi
;;
esac
fi
## 定义软件源更新文字
case "${SYSTEM_FACTIONS}" in
"${SYSTEM_DEBIAN}")
SYNC_MIRROR_TEXT="$(msg "source.sync.text1")"
;;
"${SYSTEM_REDHAT}" | "${SYSTEM_OPENEULER}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_ANOLISOS}" | "${SYSTEM_TENCENTOS}")
SYNC_MIRROR_TEXT="$(msg "source.sync.text2")"
;;
esac
## 判断是否可以使用高级交互式选择器
CAN_USE_ADVANCED_INTERACTIVE_SELECTION="false"
if command_exists tput; then
CAN_USE_ADVANCED_INTERACTIVE_SELECTION="true"
fi
}
## 选择显示语言
function choose_display_language() {
local result
if command_exists tput; then
local lang_key_labels=()
local language_text
for ((i = 0; i < ${#MESSAGE_LANG_KEYS[@]}; i++)); do
language_text="${MESSAGE_LANG_DISPLAY[${MESSAGE_LANG_KEYS[$i]}]}"
if [[ "${language_text}" ]]; then
lang_key_labels+=("${language_text}")
else
lang_key_labels+=("")
fi
done
interactive_select_list "MESSAGE_LANG_KEYS" "\n ${BOLD}Please select the display language:${PLAIN}\n" "lang_key_labels"
result="${_SELECT_RESULT%%@@*}"
else
echo ''
for ((i = 0; i < ${#MESSAGE_LANG_KEYS[@]}; i++)); do
echo -e " $((i + 1)). ${MESSAGE_LANG_DISPLAY[${MESSAGE_LANG_KEYS[$i]}]}"
done
local CHOICE="$(echo -e "\n${BOLD}└─ Please select and enter the display language [ 1-${#MESSAGE_LANG_KEYS[@]} ]:${PLAIN}")"
while true; do
read -rp "${CHOICE}" INPUT
case "${INPUT}" in
[1-9] | [1-9][0-9])
local tmp_result="${MESSAGE_LANG_KEYS[$((INPUT - 1))]}"
if [[ -z "${tmp_result}" ]]; then
echo -e "\n$WARN $(msg "warn.needValidNumberIndex")"
else
result="${tmp_result}"
break
fi
;;
*)
echo -e "\n$WARN $(msg "warn.needInputNumberIndex")"
;;
esac
done
fi
init_msg_pack "${result}"
}
function choose_mirrors() {
## 打印软件源列表
function print_mirrors_list() {
local tmp_name tmp_index i j
function StringLength() {
local text=$1
echo "${#text}"
}
local list_arr=()
local list_arr_sum="$(eval echo \${#$1[@]})"
for ((i = 0; i < $list_arr_sum; i++)); do
list_arr[$i]="$(eval echo \${$1[i]})"
done
local name_width=${2:-"30"}
local list_labels=()
if [[ "${3}" ]]; then
eval "list_labels=(\"\${${3}[@]}\")"
fi
if command_exists printf; then
local tmp_uchar_1 tmp_uchar_2 tmp_uchar_3 tmp_uchar_4 tmp_default_length tmp_length tmp_unicode_length tmp_spaces_nums tmp_max_length
for ((i = 0; i < ${#list_arr[@]}; i++)); do
if [[ "${list_labels[$i]}" ]]; then
tmp_name="${list_labels[$i]}"
else
tmp_name="${list_arr[i]}"
fi
tmp_index=$((i + 1))
tmp_default_length="${name_width}"
tmp_uchar_1=$(echo "${tmp_name}" | grep -c "“")
tmp_uchar_2=$(echo "${tmp_name}" | grep -c "”")
tmp_uchar_3=$(echo "${tmp_name}" | grep -c "‘")
tmp_uchar_4=$(echo "${tmp_name}" | grep -c "’")
[[ "${tmp_uchar_1}" -gt 0 ]] && ((tmp_default_length += tmp_uchar_1))
[[ "${tmp_uchar_2}" -gt 0 ]] && ((tmp_default_length += tmp_uchar_2))
[[ "${tmp_uchar_3}" -gt 0 ]] && ((tmp_default_length += tmp_uchar_3))
[[ "${tmp_uchar_4}" -gt 0 ]] && ((tmp_default_length += tmp_uchar_4))
tmp_length=$(StringLength "${tmp_name}")
tmp_unicode_length=$(StringLength "$(echo "${tmp_name}" | sed "s|[0-9a-zA-Z -~]||g; s| ||g")")
tmp_max_length=$((tmp_default_length + tmp_unicode_length))
tmp_spaces_nums=$((((tmp_default_length - tmp_unicode_length - tmp_length)) / 2))
if [[ $tmp_spaces_nums -gt 0 ]]; then
tmp_name="${tmp_name}$(printf '%*s' ${tmp_spaces_nums} '')"
fi
printf "❖ %-${tmp_max_length}s %4s\n" "${tmp_name}" "${tmp_index})"
done
else
for ((i = 0; i < ${#list_arr[@]}; i++)); do
if [[ "${list_labels[$i]}" ]]; then
tmp_name="${list_labels[$i]}"
else
tmp_name="${list_arr[i]}"
fi
tmp_index=$((i + 1))
echo -e " ❖ $tmp_index. ${tmp_name}"
done
fi
}
## 选择使用软件源内网地址
function choose_use_intranet_address() {
local intranet_source
for ((i = 0; i < ${#mirror_list_extranet[@]}; i++)); do
if [[ "${SOURCE}" == "${mirror_list_extranet[i]}" ]]; then
intranet_source="${mirror_list_intranet[i]}"
break
else
continue
fi
done
if [[ -z "${USE_INTRANET_SOURCE}" ]]; then
if [[ "${CAN_USE_ADVANCED_INTERACTIVE_SELECTION}" == "true" ]]; then
echo ''
interactive_select_boolean "${BOLD}$(msg "interaction.source.type.select")${PLAIN}" "$(msg "interaction.source.type.public")" "$(msg "interaction.source.type.intranet")"
if [[ "${_SELECT_RESULT}" == "false" ]]; then
SOURCE="${intranet_source}"
ONLY_HTTP="true" # 强制使用 HTTP 协议
[[ "${PURE_MODE}" != "true" ]] && echo -e "\n$WARN $(msg "warn.usedIntranetSource")"
fi
else
local CHOICE="$(echo -e "\n${BOLD}└─ $(msg "interaction.source.type.usePublicAddress")? [Y/n] ${PLAIN}")"
read -rp "${CHOICE}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss]) ;;
[Nn] | [Nn][Oo])
SOURCE="${intranet_source}"
ONLY_HTTP="true" # 强制使用 HTTP 协议
[[ "${PURE_MODE}" != "true" ]] && echo -e "\n$WARN $(msg "warn.usedIntranetSource")"
;;
*)
input_error "$(msg "error.defaultBehavior.noUseIntranetSource")"
;;
esac
fi
elif [[ "${USE_INTRANET_SOURCE}" == "true" ]]; then
SOURCE="${intranet_source}"
fi
}
local mirror_list_name mirror_list_length
local mirror_list_labels=()
local label_msg_index label_msg_content
if [[ -z "${SOURCE}" ]] && [[ "${ONLY_REGISTRY}" != "true" ]]; then
mirror_list_name="mirror_list_docker_ce"
mirror_list_length=$(eval "echo \${#${mirror_list_name}[@]}")
for ((i = 0; i < ${mirror_list_length}; i++)); do
label_msg_index="mirrors.dockerCE.${i}"
label_msg_content="$(msg "${label_msg_index}")"
if [[ "${label_msg_content}" == "${label_msg_index}" ]]; then
mirror_list_labels+=("")
else
mirror_list_labels+=("${label_msg_content}")
fi
done
if [[ "${CAN_USE_ADVANCED_INTERACTIVE_SELECTION}" == "true" ]]; then
interactive_select_list "${mirror_list_name}" "\n ${BOLD}$(msg "interaction.source.dockerCE.select")${PLAIN}\n" "mirror_list_labels"
SOURCE="${_SELECT_RESULT%%@@*}"
echo -e "\n${GREEN}➜${PLAIN} ${BOLD}Docker CE: ${_SELECT_RESULT#*@@}${PLAIN}"
else
echo ''
print_mirrors_list "${mirror_list_name}" 39 "mirror_list_labels"
local CHOICE_B="$(echo -e "\n${BOLD}└─ $(msg "interaction.source.dockerCE.selectAndInput") [ 1-$(eval echo \${#${mirror_list_name}[@]}) ]:${PLAIN}")"
while true; do
read -rp "${CHOICE_B}" INPUT
case "${INPUT}" in
[1-9] | [1-9][0-9] | [1-9][0-9][0-9])
local tmp_result="$(eval echo \${${mirror_list_name}[$((INPUT - 1))]})"
if [[ -z "${tmp_result}" ]]; then
echo -e "\n$WARN $(msg "warn.needValidNumberIndex")"
else
SOURCE="$(echo "${tmp_result}" | awk -F '@' '{print$2}')"
break
fi
;;
*)
echo -e "\n$WARN $(msg "warn.needInputNumberIndex")"
;;
esac
done
fi
fi
## 选择软件源内网地址
if [[ "${mirror_list_extranet[*]}" =~ (^|[^[:alpha:]])"${SOURCE}"([^[:alpha:]]|$) ]]; then
choose_use_intranet_address
fi
if [[ -z "${SOURCE_REGISTRY}" ]]; then
mirror_list_name="mirror_list_registry"
mirror_list_labels=()
mirror_list_length=$(eval "echo \${#${mirror_list_name}[@]}")
for ((i = 0; i < ${mirror_list_length}; i++)); do
label_msg_index="mirrors.registry.${i}"
label_msg_content="$(msg "${label_msg_index}")"
if [[ "${label_msg_content}" == "${label_msg_index}" ]]; then
mirror_list_labels+=("")
else
mirror_list_labels+=("${label_msg_content}")
fi
done
if [[ "${CAN_USE_ADVANCED_INTERACTIVE_SELECTION}" == "true" ]]; then
sleep 1 >/dev/null 2>&1
interactive_select_list "${mirror_list_name}" "\n ${BOLD}$(msg "interaction.source.dockerRegistry.select")${PLAIN}\n" "mirror_list_labels"
SOURCE_REGISTRY="${_SELECT_RESULT%%@@*}"
echo -e "\n${GREEN}➜${PLAIN} ${BOLD}Docker Registry: $(echo "${_SELECT_RESULT#*@@}" | sed 's|(推荐)||g; s|(推薦)||g')${PLAIN}"
else
echo ''
print_mirrors_list "${mirror_list_name}" 45 "mirror_list_labels"
local CHOICE_C="$(echo -e "\n${BOLD}└─ $(msg "interaction.source.dockerRegistry.selectAndInput") [ 1-$(eval echo \${#${mirror_list_name}[@]}) ]:${PLAIN}")"
while true; do
read -rp "${CHOICE_C}" INPUT
case "${INPUT}" in
[1-9] | [1-9][0-9] | [1-9][0-9][0-9])
local tmp_source="$(eval echo \${${mirror_list_name}[$(($INPUT - 1))]})"
if [[ -z "${tmp_source}" ]]; then
echo -e "\n$WARN $(msg "warn.needValidNumberIndex")"
else
SOURCE_REGISTRY="$(eval echo \${${mirror_list_name}[$(($INPUT - 1))]} | awk -F '@' '{print$2}')"
break
fi
;;
*)
echo -e "\n$WARN $(msg "warn.needInputNumberIndex")"
;;
esac
done
fi
fi
}
## 选择同步或更新软件源所使用的 Web 协议( HTTP/HTTPS)
function choose_protocol() {
if [[ -z "${WEB_PROTOCOL}" ]]; then
if [[ "${ONLY_HTTP}" == "true" ]]; then
WEB_PROTOCOL="http"
else
if [[ "${CAN_USE_ADVANCED_INTERACTIVE_SELECTION}" == "true" ]]; then
echo ''
interactive_select_boolean "${BOLD}$(msg "interaction.protocol.select")${PLAIN}" "HTTP" "HTTPS"
if [[ "${_SELECT_RESULT}" == "true" ]]; then
WEB_PROTOCOL="http"
else
WEB_PROTOCOL="https"
fi
else
local CHOICE="$(echo -e "\n${BOLD}└─ $(msg "interaction.protocol.useHttp")? [Y/n] ${PLAIN}")"
read -rp "${CHOICE}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss])
WEB_PROTOCOL="http"
;;
[Nn] | [Nn][Oo])
WEB_PROTOCOL="https"
;;
*)
input_error "$(msg "error.defaultBehavior.https")"
WEB_PROTOCOL="https"
;;
esac
fi
fi
fi
WEB_PROTOCOL="${WEB_PROTOCOL,,}"
}
## 关闭防火墙和SELinux
function close_firewall_service() {
if ! command_exists systemctl; then
return
fi
if [[ "$(systemctl is-active firewalld)" == "active" ]]; then
if [[ -z "${CLOSE_FIREWALL}" ]]; then
local ask_text="$(msg "interaction.firewall.close")?"
if [[ "${CAN_USE_ADVANCED_INTERACTIVE_SELECTION}" == "true" ]]; then
echo ''
interactive_select_boolean "${BOLD}${ask_text}${PLAIN}"
if [[ "${_SELECT_RESULT}" == "true" ]]; then
CLOSE_FIREWALL="true"
fi
else
local CHOICE="$(echo -e "\n${BOLD}└─ ${ask_text} [Y/n] ${PLAIN}")"
read -rp "${CHOICE}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss])
CLOSE_FIREWALL="true"
;;
[Nn] | [Nn][Oo]) ;;
*)
input_error "$(msg "error.defaultBehavior.noClose")"
;;
esac
fi
fi
if [[ "${CLOSE_FIREWALL}" == "true" ]]; then
local SelinuxConfig=/etc/selinux/config
systemctl disable --now firewalld >/dev/null 2>&1
[ -s "${SelinuxConfig}" ] && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" $SelinuxConfig && setenforce 0 >/dev/null 2>&1
fi
fi