diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 5ddcd118d..4c3d57bd5 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,12 +214,11 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil { return err } - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -264,12 +264,11 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -309,12 +308,11 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -343,12 +341,11 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { _, _ = write.WriteString(req.File) write.Flush() - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -369,9 +366,8 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { } if req.Operation == "restart" { - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } } @@ -430,3 +426,11 @@ func changeLogOption(daemonMap map[string]interface{}, logMaxFile, logMaxSize st } } } + +func validateDockerConfig() error { + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return fmt.Errorf("Docker configuration validation failed, err: %v", stdout) + } + return nil +} diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index b0dd875c1..52fc633c6 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -176,11 +176,6 @@ func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{} func (u *FirewallService) OperateFirewall(operation string) error { - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) - } - client, err := firewall.NewFirewallClient() if err != nil { return err diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index 46cffe60e..6954ea5c1 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -86,12 +86,11 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { } if req.Protocol == "http" { _ = u.handleRegistries(req.DownloadUrl, "", "create") - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -164,12 +163,11 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) + if err := validateDockerConfig(); err != nil { + return err } - stdout, err = cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 8c949c219..836b45d9a 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -146,12 +146,6 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b global.LOG.Debugf("remove the file %s after the operation is successful", path.Dir(snapFileDir)) _ = os.RemoveAll(path.Dir(snapFileDir)) } - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - global.LOG.Errorf("Docker configuration validation failed: " + string(stdout)) - return - } - _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") } @@ -236,12 +230,6 @@ func recoverDaemonJson(src string, fileOp files.FileOp) error { return fmt.Errorf("recover docker daemon.json failed, err: %v", err) } } - - stdout, err := cmd.Exec("dockerd --validate") - if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { - return errors.New("Docker configuration validation failed: " + string(stdout)) - } - _, _ = cmd.Exec("systemctl restart docker") return nil }