fix: 容器重启前增加校验操作

This commit is contained in:
zhoujunhong 2024-10-12 16:53:32 +08:00
parent 175b7b89e4
commit d080f7434e
4 changed files with 60 additions and 6 deletions

View file

@ -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))

View file

@ -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

View file

@ -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))
}

View file

@ -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
}