From 0fea645692f0b353755cc0d020be1f7f06a07067 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 19 Dec 2022 12:50:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20http=20=E9=95=9C=E5=83=8F=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E5=8A=A8=E6=80=81=E6=8E=88=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/container_compose.go | 2 +- backend/app/service/image_repo.go | 158 +++++++++--------- .../views/container/compose/detail/index.vue | 2 +- .../src/views/container/compose/index.vue | 2 +- frontend/src/views/database/mysql/index.vue | 2 +- 5 files changed, 80 insertions(+), 86 deletions(-) diff --git a/backend/app/service/container_compose.go b/backend/app/service/container_compose.go index e994b0c72..6085b2daa 100644 --- a/backend/app/service/container_compose.go +++ b/backend/app/service/container_compose.go @@ -79,7 +79,7 @@ func (u *ContainerService) PageCompose(req dto.PageInfo) (int64, interface{}, er } for i := 0; i < len(composeCreatedByLocal); i++ { if composeCreatedByLocal[i].Name == name { - composeItem.CreatedBy = "1panel" + composeItem.CreatedBy = "1Panel" composeCreatedByLocal = append(composeCreatedByLocal[:i], composeCreatedByLocal[i+1:]...) break } diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index 9c2318095..487695039 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "os/exec" + "path" "strings" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -62,45 +63,8 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { if imageRepo.ID != 0 { return constant.ErrRecordExist } - - fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath")) - if err != nil { - return err - } - if len(fileSetting.Value) == 0 { - return errors.New("error daemon.json") - } - if _, err := os.Stat(fileSetting.Value); err != nil && os.IsNotExist(err) { - if err = os.MkdirAll(fileSetting.Value, os.ModePerm); err != nil { - if err != nil { - return err - } - } - } if req.Protocol == "http" { - file, err := ioutil.ReadFile(fileSetting.Value) - if err != nil { - return err - } - - deamonMap := make(map[string]interface{}) - if err := json.Unmarshal(file, &deamonMap); err != nil { - return err - } - if _, ok := deamonMap["insecure-registries"]; ok { - if k, v := deamonMap["insecure-registries"].([]interface{}); v { - deamonMap["insecure-registries"] = common.RemoveRepeatElement(append(k, req.DownloadUrl)) - } - } else { - deamonMap["insecure-registries"] = []string{req.DownloadUrl} - } - newJson, err := json.MarshalIndent(deamonMap, "", "\t") - if err != nil { - return err - } - if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil { - return err - } + _ = u.handleRegistries(req.DownloadUrl, "", "create") } if err := copier.Copy(&imageRepo, &req); err != nil { @@ -123,10 +87,6 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { return nil } -type DeamonJson struct { - InsecureRegistries []string `json:"insecure-registries"` -} - func (u *ImageRepoService) BatchDelete(ids []uint) error { for _, id := range ids { if id == 1 { @@ -137,47 +97,10 @@ func (u *ImageRepoService) BatchDelete(ids []uint) error { if err != nil { return err } - fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath")) - if err != nil { - return err - } - if len(fileSetting.Value) == 0 { - return errors.New("error daemon.json") - } - - deamonMap := make(map[string]interface{}) - file, err := ioutil.ReadFile(fileSetting.Value) - if err != nil { - return err - } - if err := json.Unmarshal(file, &deamonMap); err != nil { - return err - } - iRegistries := deamonMap["insecure-registries"] - registries, _ := iRegistries.([]string) - if len(registries) != 0 { - for _, repo := range repos { - if repo.Protocol == "http" { - for i, regi := range registries { - if regi == repo.DownloadUrl { - registries = append(registries[:i], registries[i+1:]...) - } - } - } - } - } - if len(registries) == 0 { - delete(deamonMap, "insecure-registries") - } - newJson, err := json.MarshalIndent(deamonMap, "", "\t") - if err != nil { - return err - } - if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil { - return err - } - for _, repo := range repos { + if repo.Protocol == "http" { + _ = u.handleRegistries("", repo.DownloadUrl, "delete") + } if repo.Auth { cmd := exec.Command("docker", "logout", fmt.Sprintf("%s://%s", repo.Protocol, repo.DownloadUrl)) _, _ = cmd.CombinedOutput() @@ -199,6 +122,17 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if req.ID == 1 { return errors.New("The default value cannot be deleted !") } + repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.ID)) + if err != nil { + return err + } + if repo.DownloadUrl != req.DownloadUrl { + _ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update") + if repo.Auth { + cmd := exec.Command("docker", "logout", fmt.Sprintf("%s://%s", repo.Protocol, repo.DownloadUrl)) + _, _ = cmd.CombinedOutput() + } + } upMap := make(map[string]interface{}) upMap["download_url"] = req.DownloadUrl @@ -227,3 +161,63 @@ func (u *ImageRepoService) checkConn(host, user, password string) error { } return errors.New(string(stdout)) } + +func (u *ImageRepoService) handleRegistries(newHost, delHost, handle string) error { + fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath")) + if err != nil { + return err + } + if len(fileSetting.Value) == 0 { + return errors.New("error daemon.json in settings") + } + if _, err := os.Stat(path.Dir(fileSetting.Value)); err != nil && os.IsNotExist(err) { + if err = os.MkdirAll(fileSetting.Value, os.ModePerm); err != nil { + if err != nil { + return err + } + } + _, _ = os.Create(fileSetting.Value) + } + + deamonMap := make(map[string]interface{}) + file, err := ioutil.ReadFile(fileSetting.Value) + if err != nil { + return err + } + if err := json.Unmarshal(file, &deamonMap); err != nil { + return err + } + + iRegistries := deamonMap["insecure-registries"] + registries, _ := iRegistries.([]interface{}) + switch handle { + case "create": + registries = common.RemoveRepeatElement(append(registries, newHost)) + case "update": + registries = common.RemoveRepeatElement(append(registries, newHost)) + for i, regi := range registries { + if regi == delHost { + registries = append(registries[:i], registries[i+1:]...) + } + } + case "delete": + for i, regi := range registries { + if regi == delHost { + registries = append(registries[:i], registries[i+1:]...) + } + } + } + if len(registries) == 0 { + delete(deamonMap, "insecure-registries") + } else { + deamonMap["insecure-registries"] = registries + } + newJson, err := json.MarshalIndent(deamonMap, "", "\t") + if err != nil { + return err + } + if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil { + return err + } + return nil +} diff --git a/frontend/src/views/container/compose/detail/index.vue b/frontend/src/views/container/compose/detail/index.vue index 0bc607f94..7b421d78c 100644 --- a/frontend/src/views/container/compose/detail/index.vue +++ b/frontend/src/views/container/compose/detail/index.vue @@ -1,7 +1,7 @@