fix: 容器重启前增加校验操作
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled

This commit is contained in:
zhoujunhong 2024-10-12 18:46:14 +08:00
parent d080f7434e
commit 7befee0bd2
4 changed files with 29 additions and 44 deletions

View file

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

View file

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

View file

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

View file

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