From e4a634e6e8bc058c1872bf1eb01dc22bc6702bea Mon Sep 17 00:00:00 2001 From: John Bro <42930107+john1298308460@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:36:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=B9=E5=99=A8=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=89=8D=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=20(#6696)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/docker.go | 31 +++++++++++++++++++++++++++++++ backend/app/service/image_repo.go | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 300926ac7..003fce126 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "encoding/json" + "fmt" "os" "path" "strings" @@ -213,6 +214,9 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil { return err } + if err := validateDockerConfig(); err != nil { + return err + } stdout, err := cmd.Exec("systemctl restart docker") if err != nil { @@ -260,6 +264,10 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -300,6 +308,10 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -329,6 +341,10 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { _, _ = write.WriteString(req.File) write.Flush() + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -348,6 +364,13 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { } } } + + if req.Operation == "restart" { + if err := validateDockerConfig(); err != nil { + return err + } + } + stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service) if err != nil { return errors.New(string(stdout)) @@ -403,3 +426,11 @@ func changeLogOption(daemonMap map[string]interface{}, logMaxFile, logMaxSize st } } } + +func validateDockerConfig() error { + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (stdout != "" && strings.TrimSpace(stdout) != "configuration OK") { + return fmt.Errorf("Docker configuration validation failed, err: %v", stdout) + } + return nil +} diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index e5dad9e90..6954ea5c1 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -86,6 +86,10 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { } if req.Protocol == "http" { _ = u.handleRegistries(req.DownloadUrl, "", "create") + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -159,6 +163,10 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout))