From d080f7434eb2a061112369fafc1f15a37ef3d45c Mon Sep 17 00:00:00 2001 From: zhoujunhong <1298308460@qq.com> Date: Sat, 12 Oct 2024 16:53:32 +0800 Subject: [PATCH 1/3] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/docker.go | 35 ++++++++++++++++++++++--- backend/app/service/firewall.go | 6 +++++ backend/app/service/image_repo.go | 14 ++++++++-- backend/app/service/snapshot_recover.go | 11 ++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 300926ac7efc..5ddcd118d4f1 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -213,8 +213,12 @@ 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)) + } - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -260,7 +264,12 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") + 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)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -300,7 +309,12 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") + 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)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -329,7 +343,12 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { _, _ = write.WriteString(req.File) write.Flush() - stdout, err := cmd.Exec("systemctl restart docker") + 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)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -348,6 +367,14 @@ 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)) + } + } + stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service) if err != nil { return errors.New(string(stdout)) diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index bb4556f9cf92..b0dd875c1c35 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -175,6 +175,12 @@ 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 e5dad9e90398..46cffe60ec30 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -86,7 +86,12 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { } if req.Protocol == "http" { _ = u.handleRegistries(req.DownloadUrl, "", "create") - stdout, err := cmd.Exec("systemctl restart docker") + 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)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -159,7 +164,12 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } - stdout, err := cmd.Exec("systemctl restart docker") + 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)) + } + + 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 6b707340e794..8c949c21969e 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -146,6 +146,12 @@ 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") } @@ -231,6 +237,11 @@ func recoverDaemonJson(src string, fileOp files.FileOp) 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)) + } + _, _ = cmd.Exec("systemctl restart docker") return nil } From 7befee0bd2c765c291c97e5f4c4c3ccb68195a7a Mon Sep 17 00:00:00 2001 From: zhoujunhong <1298308460@qq.com> Date: Sat, 12 Oct 2024 18:46:14 +0800 Subject: [PATCH 2/3] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/docker.go | 42 ++++++++++++++----------- backend/app/service/firewall.go | 5 --- backend/app/service/image_repo.go | 14 ++++----- backend/app/service/snapshot_recover.go | 12 ------- 4 files changed, 29 insertions(+), 44 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 5ddcd118d4f1..4c3d57bd596f 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 b0dd875c1c35..52fc633c6f60 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 46cffe60ec30..6954ea5c1e6a 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 8c949c21969e..836b45d9afc1 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 } From 9e866324d781bbc570835c34948ad251910df9b4 Mon Sep 17 00:00:00 2001 From: zhoujunhong <1298308460@qq.com> Date: Mon, 14 Oct 2024 10:24:57 +0800 Subject: [PATCH 3/3] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/docker.go | 2 +- backend/app/service/firewall.go | 1 - backend/app/service/snapshot_recover.go | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 4c3d57bd596f..003fce1266c7 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -429,7 +429,7 @@ 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") { + 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/firewall.go b/backend/app/service/firewall.go index 52fc633c6f60..bb4556f9cf92 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -175,7 +175,6 @@ func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{} } func (u *FirewallService) OperateFirewall(operation string) error { - client, err := firewall.NewFirewallClient() if err != nil { return err diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 836b45d9afc1..6b707340e794 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -230,6 +230,7 @@ func recoverDaemonJson(src string, fileOp files.FileOp) error { return fmt.Errorf("recover docker daemon.json failed, err: %v", err) } } + _, _ = cmd.Exec("systemctl restart docker") return nil }