Skip to content

feat: 自动化linux上docker换源过程 #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions src/recipe/ware/Docker-Hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <[email protected]>
* Contributors : Nil Null <[email protected]>
* Contributors : happy game <[email protected]>
* Created On : <2024-06-08>
* Last Modified : <2024-08-16>
* Last Modified : <2024-10-28>
* ------------------------------------------------------------*/

static MirrorSite
Expand Down Expand Up @@ -43,13 +43,14 @@ wr_dockerhub_sources[] = {

def_sources_n(wr_dockerhub);

#define WARE_DockerHub_SourceConfig "/etc/docker/daemon.json"

void
wr_dockerhub_getsrc (char *option)
{
if (xy_on_linux || xy_on_bsd)
{
chsrc_view_file ("/etc/docker/daemon.json");
chsrc_view_file (WARE_DockerHub_SourceConfig);
}
else
{
Expand All @@ -66,19 +67,70 @@ wr_dockerhub_getsrc (char *option)
void
wr_dockerhub_setsrc (char *option)
{
chsrc_ensure_root ();
chsrc_yield_source_and_confirm (wr_dockerhub);

if (xy_on_linux || xy_on_bsd)
{
char *to_add = xy_strjoin (3, "{\n"
" \"registry-mirrors\": [\"", source.url, "\"]\n"
"}");
chsrc_note2 ("请向 /etc/docker/daemon.json 中添加下述内容:");
puts (to_add);
if (chsrc_check_file (WARE_DockerHub_SourceConfig))
{
chsrc_note2 ("已找到 配置文件,将自动换源");
chsrc_backup (WARE_DockerHub_SourceConfig);
if (chsrc_check_program_quietly ("jq"))
{
// 检查是否已经存在 source.url
char *cmd = xy_strjoin (4, "jq '.[\"registry-mirrors\"] | index(\"",
source.url,
"\")' ",
WARE_DockerHub_SourceConfig);
char *ret = xy_run(cmd, 0, NULL);
if (ret && !xy_streql(ret, "null\n"))
{
chsrc_note2 ("已存在源,无需重复添加");
}
else
{
cmd = xy_strjoin (6, "jq '.[\"registry-mirrors\"] |= [\"",
source.url,
"\"] + .' ",
xy_2strjoin(WARE_DockerHub_SourceConfig, ".bak"),
" > ",
WARE_DockerHub_SourceConfig);
chsrc_run (cmd, RunOpt_Default);
chsrc_note2 ("源已添加");
}
}
else
{
chsrc_note2("未找到 jq 命令, 将使用 sed 换源");
char *cmd = xy_strjoin (5, "sed ",
"-z -i 's|\"registry-mirrors\":[^]]*]|\"registry-mirrors\":[\"",
source.url,
"\"]|' ",
WARE_DockerHub_SourceConfig);
chsrc_run (cmd, RunOpt_Default);
}
}
else
{
// 不存在 /etc/docker/daemon.jsons时可以直接写入文件
chsrc_note2 ("未找到 配置文件, 将自动创建");
chsrc_ensure_dir ("/etc/docker");
chsrc_run ( xy_2strjoin("touch ", WARE_DockerHub_SourceConfig), RunOpt_Default);

chsrc_append_to_file (to_add, WARE_DockerHub_SourceConfig);
}
// chsrc_note2 ("请向 /etc/docker/daemon.json 中添加下述内容:");
// puts (to_add);
if (xy_on_linux)
{
chsrc_note2 ("然后请运行:");
puts ("sudo systemctl restart docker");
// 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启
chsrc_note2 ("请自行运行: sudo systemctl restart docker");
chsrc_note2 ("该命令会重启所有容器, 请在合适的时机执行");
// puts ("sudo systemctl restart docker");
}
else
{
Expand Down