Browse Source

fix: 容器重启前增加校验操作 (#6696)

pull/6705/head
John Bro 1 month ago committed by GitHub
parent
commit
e4a634e6e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      backend/app/service/docker.go
  2. 8
      backend/app/service/image_repo.go

31
backend/app/service/docker.go

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"path" "path"
"strings" "strings"
@ -213,6 +214,9 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error {
if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil { if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil {
return err return err
} }
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
@ -260,6 +264,10 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error {
return err return err
} }
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
@ -300,6 +308,10 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error {
return err return err
} }
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
@ -329,6 +341,10 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error {
_, _ = write.WriteString(req.File) _, _ = write.WriteString(req.File)
write.Flush() write.Flush()
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
return errors.New(string(stdout)) 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) stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service)
if err != nil { if err != nil {
return errors.New(string(stdout)) 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
}

8
backend/app/service/image_repo.go

@ -86,6 +86,10 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error {
} }
if req.Protocol == "http" { if req.Protocol == "http" {
_ = u.handleRegistries(req.DownloadUrl, "", "create") _ = u.handleRegistries(req.DownloadUrl, "", "create")
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))
@ -159,6 +163,10 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
if repo.Auth { if repo.Auth {
_, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl)
} }
if err := validateDockerConfig(); err != nil {
return err
}
stdout, err := cmd.Exec("systemctl restart docker") stdout, err := cmd.Exec("systemctl restart docker")
if err != nil { if err != nil {
return errors.New(string(stdout)) return errors.New(string(stdout))

Loading…
Cancel
Save