Skip to content
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

fix: 容器部分操作动态修改执行命令 #6750

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
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
62 changes: 42 additions & 20 deletions backend/app/service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 return nil 表示配置为空,不需要进行后续操作,可直接返回,不能去掉

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -312,19 +309,17 @@ 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
}

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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理不能去掉

Expand All @@ -345,16 +340,19 @@ 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
}

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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
19 changes: 12 additions & 7 deletions backend/app/service/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
13 changes: 4 additions & 9 deletions backend/app/service/image_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion backend/app/service/snapshot_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading