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: 容器重启前增加校验操作 #6696

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 31 additions & 4 deletions backend/app/service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

ssongliu marked this conversation as resolved.
Show resolved Hide resolved
stdout, err := cmd.Exec("systemctl restart docker")
stdout, err = cmd.Exec("systemctl restart docker")
if err != nil {
return errors.New(string(stdout))
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions backend/app/service/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Copy link
Member

@ssongliu ssongliu Oct 12, 2024

Choose a reason for hiding this comment

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

这个是不是多余的代码

client, err := firewall.NewFirewallClient()
if err != nil {
return err
Expand Down
14 changes: 12 additions & 2 deletions backend/app/service/image_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 11 additions & 0 deletions backend/app/service/snapshot_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

ssongliu marked this conversation as resolved.
Show resolved Hide resolved
_, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service")
}

Expand Down Expand Up @@ -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
}
Expand Down
Loading