From 2f147dde13c31ae887eb4f8059e3674e34060a77 Mon Sep 17 00:00:00 2001 From: John Bro <42930107+john1298308460@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:03:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=B9=E5=99=A8=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=8A=A8=E6=80=81=E4=BF=AE=E6=94=B9=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=91=BD=E4=BB=A4=20(#6750)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #3242 --- backend/app/service/docker.go | 62 +++++++++++++++++-------- backend/app/service/firewall.go | 19 +++++--- backend/app/service/image_repo.go | 13 ++---- backend/app/service/snapshot_recover.go | 4 +- 4 files changed, 61 insertions(+), 37 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 003fce126..b9cea2543 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -201,9 +201,8 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { } if len(daemonMap) == 0 { _ = os.Remove(constant.DaemonJsonPath) - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -218,9 +217,8 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -268,9 +266,8 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -312,9 +309,8 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -322,9 +318,8 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { if len(req.File) == 0 { _ = os.Remove(constant.DaemonJsonPath) - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -345,9 +340,8 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } return nil } @@ -355,6 +349,10 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { func (u *DockerService) OperateDocker(req dto.DockerOperation) error { service := "docker" sudo := cmd.SudoHandleCmd() + dockerCmd, err := getDockerRestartCommand() + if err != nil { + return err + } if req.Operation == "stop" { isSocketActive, _ := systemctl.IsActive("docker.socket") if isSocketActive { @@ -371,9 +369,9 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { } } - stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service) + stdout, err := cmd.Execf("%s %s %s", dockerCmd, req.Operation, service) if err != nil { - return errors.New(string(stdout)) + return errors.New(stdout) } return nil } @@ -434,3 +432,27 @@ func validateDockerConfig() error { } return nil } + +func getDockerRestartCommand() (string, error) { + stdout, err := cmd.Exec("which docker") + if err != nil { + return "", fmt.Errorf("failed to find docker: %v", err) + } + dockerPath := stdout + if strings.Contains(dockerPath, "snap") { + return "snap", nil + } + return "systemctl", nil +} + +func restartDocker() error { + restartCmd, err := getDockerRestartCommand() + if err != nil { + return err + } + stdout, err := cmd.Execf("%s restart docker", restartCmd) + if err != nil { + return fmt.Errorf("failed to restart Docker: %s", stdout) + } + return nil +} diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index bb4556f9c..24d7092d3 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -179,6 +179,7 @@ func (u *FirewallService) OperateFirewall(operation string) error { if err != nil { return err } + needRestartDocker := false switch operation { case "start": if err := client.Start(); err != nil { @@ -188,26 +189,30 @@ func (u *FirewallService) OperateFirewall(operation string) error { _ = client.Stop() return err } - _, _ = cmd.Exec("systemctl restart docker") - return nil + needRestartDocker = true case "stop": if err := client.Stop(); err != nil { return err } - _, _ = cmd.Exec("systemctl restart docker") - return nil + needRestartDocker = true case "restart": if err := client.Restart(); err != nil { return err } - _, _ = cmd.Exec("systemctl restart docker") - return nil + needRestartDocker = true case "disablePing": return u.updatePingStatus("0") case "enablePing": return u.updatePingStatus("1") + default: + return fmt.Errorf("not supported operation: %s", operation) } - return fmt.Errorf("not support such operation: %s", operation) + if needRestartDocker { + if err := restartDocker(); err != nil { + return err + } + } + return nil } func (u *FirewallService) OperatePortRule(req dto.PortRuleOperate, reload bool) error { diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index 6954ea5c1..c2540db99 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -89,10 +89,8 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { if err := validateDockerConfig(); err != nil { return err } - - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } ticker := time.NewTicker(3 * time.Second) defer ticker.Stop() @@ -166,13 +164,10 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if err := validateDockerConfig(); err != nil { return err } - - stdout, err := cmd.Exec("systemctl restart docker") - if err != nil { - return errors.New(string(stdout)) + if err := restartDocker(); err != nil { + return err } } - upMap := make(map[string]interface{}) upMap["download_url"] = req.DownloadUrl upMap["protocol"] = req.Protocol diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 6b707340e..83c943c04 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -231,7 +231,9 @@ func recoverDaemonJson(src string, fileOp files.FileOp) error { } } - _, _ = cmd.Exec("systemctl restart docker") + if err := restartDocker(); err != nil { + return err + } return nil }