From 326941ad484dd9dad69ac9fdbe5d7ce958dce7d5 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 20 Dec 2022 13:23:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=20docker=20=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E6=8E=88=E4=BF=A1=E5=88=A0=E9=99=A4=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/image_repo.go | 4 +- backend/app/dto/image_repo.go | 5 ++ backend/app/service/image_repo.go | 35 ++++++---- frontend/src/api/interface/container.ts | 4 ++ frontend/src/api/modules/container.ts | 2 +- frontend/src/lang/modules/en.ts | 3 + frontend/src/lang/modules/zh.ts | 3 + frontend/src/views/container/image/index.vue | 2 +- .../src/views/container/image/pull/index.vue | 13 ++-- .../src/views/container/repo/delete/index.vue | 66 +++++++++++++++++++ frontend/src/views/container/repo/index.vue | 9 +-- .../views/container/repo/operator/index.vue | 22 +++++-- frontend/src/views/host/terminal/index.vue | 3 + 13 files changed, 140 insertions(+), 31 deletions(-) create mode 100644 frontend/src/views/container/repo/delete/index.vue diff --git a/backend/app/api/v1/image_repo.go b/backend/app/api/v1/image_repo.go index 3a84d8747..bb86db2be 100644 --- a/backend/app/api/v1/image_repo.go +++ b/backend/app/api/v1/image_repo.go @@ -59,7 +59,7 @@ func (b *BaseApi) CreateRepo(c *gin.Context) { } func (b *BaseApi) DeleteRepo(c *gin.Context) { - var req dto.BatchDeleteReq + var req dto.ImageRepoDelete if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -69,7 +69,7 @@ func (b *BaseApi) DeleteRepo(c *gin.Context) { return } - if err := imageRepoService.BatchDelete(req.Ids); err != nil { + if err := imageRepoService.BatchDelete(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } diff --git a/backend/app/dto/image_repo.go b/backend/app/dto/image_repo.go index 748c1d27e..ffa1bc313 100644 --- a/backend/app/dto/image_repo.go +++ b/backend/app/dto/image_repo.go @@ -38,3 +38,8 @@ type ImageRepoOption struct { Name string `json:"name"` DownloadUrl string `json:"downloadUrl"` } + +type ImageRepoDelete struct { + DeleteInsecure bool `json:"deleteInsecure"` + Ids []uint `json:"ids" validate:"required"` +} diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index 487695039..a60638ddd 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -23,7 +23,7 @@ type IImageRepoService interface { List() ([]dto.ImageRepoOption, error) Create(req dto.ImageRepoCreate) error Update(req dto.ImageRepoUpdate) error - BatchDelete(ids []uint) error + BatchDelete(req dto.ImageRepoDelete) error } func NewIImageRepoService() IImageRepoService { @@ -70,6 +70,13 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { if err := copier.Copy(&imageRepo, &req); err != nil { return errors.WithMessage(constant.ErrStructTransform, err.Error()) } + + cmd := exec.Command("systemctl", "restart", "docker") + stdout, err := cmd.CombinedOutput() + if err != nil { + return errors.New(string(stdout)) + } + imageRepo.Status = constant.StatusSuccess if err := u.checkConn(req.DownloadUrl, req.Username, req.Password); err != nil { imageRepo.Status = constant.StatusFailed @@ -79,21 +86,22 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { return err } - cmd := exec.Command("systemctl", "restart", "docker") - stdout, err := cmd.CombinedOutput() - if err != nil { - return errors.New(string(stdout)) - } return nil } -func (u *ImageRepoService) BatchDelete(ids []uint) error { - for _, id := range ids { +func (u *ImageRepoService) BatchDelete(req dto.ImageRepoDelete) error { + for _, id := range req.Ids { if id == 1 { return errors.New("The default value cannot be edit !") } } - repos, err := imageRepoRepo.List(commonRepo.WithIdsIn(ids)) + if !req.DeleteInsecure { + if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil { + return err + } + return nil + } + repos, err := imageRepoRepo.List(commonRepo.WithIdsIn(req.Ids)) if err != nil { return err } @@ -106,7 +114,7 @@ func (u *ImageRepoService) BatchDelete(ids []uint) error { _, _ = cmd.CombinedOutput() } } - if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(ids)); err != nil { + if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil { return err } cmd := exec.Command("systemctl", "restart", "docker") @@ -129,9 +137,14 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { 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 := exec.Command("docker", "logout", repo.DownloadUrl) _, _ = cmd.CombinedOutput() } + cmd := exec.Command("systemctl", "restart", "docker") + stdout, err := cmd.CombinedOutput() + if err != nil { + return errors.New(string(stdout)) + } } upMap := make(map[string]interface{}) diff --git a/frontend/src/api/interface/container.ts b/frontend/src/api/interface/container.ts index a2d6ec184..c42409ffd 100644 --- a/frontend/src/api/interface/container.ts +++ b/frontend/src/api/interface/container.ts @@ -152,6 +152,10 @@ export namespace Container { password: string; auth: boolean; } + export interface RepoDelete { + ids: Array; + deleteInsecure: boolean; + } export interface RepoInfo { id: number; createdAt: Date; diff --git a/frontend/src/api/modules/container.ts b/frontend/src/api/modules/container.ts index 1aa8a43b6..7a9a378ab 100644 --- a/frontend/src/api/modules/container.ts +++ b/frontend/src/api/modules/container.ts @@ -88,7 +88,7 @@ export const createImageRepo = (params: Container.RepoCreate) => { export const updateImageRepo = (params: Container.RepoUpdate) => { return http.post(`/containers/repo/update`, params); }; -export const deleteImageRepo = (params: { ids: number[] }) => { +export const deleteImageRepo = (params: Container.RepoDelete) => { return http.post(`/containers/repo/del`, params); }; diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 4f7b7f16b..0c803aff5 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -439,6 +439,9 @@ export default { repo: 'Repo', name: 'Name', protocol: 'protocol', + httpRepo: 'The http repository needs to restart the docker service to add credit', + delInsecure: 'Deletion of credit', + delInsecureHelper: 'docker service needs to be restarted to delete the credit. Do you want to delete it?', downloadUrl: 'Download URL', imageRepo: 'Image repo', repoHelper: 'Does it include a mirror repository/organization/project?', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index bdfde33e5..3960f2634 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -415,6 +415,9 @@ export default { imageDelete: '删除镜像', repoName: '仓库名', imageName: '镜像名', + httpRepo: 'http 仓库添加授信需要重启 docker 服务', + delInsecure: '删除授信', + delInsecureHelper: '删除授信需要重启 docker 服务,是否删除?', pull: '拉取', path: '路径', importImage: '导入镜像', diff --git a/frontend/src/views/container/image/index.vue b/frontend/src/views/container/image/index.vue index b71ff8062..061a7674f 100644 --- a/frontend/src/views/container/image/index.vue +++ b/frontend/src/views/container/image/index.vue @@ -144,7 +144,7 @@ const search = async () => { }; const loadRepos = async () => { const res = await listImageRepo(); - repos.value = res.data; + repos.value = res.data || []; }; const onOpenPull = () => { diff --git a/frontend/src/views/container/image/pull/index.vue b/frontend/src/views/container/image/pull/index.vue index 4b5702177..a88a176da 100644 --- a/frontend/src/views/container/image/pull/index.vue +++ b/frontend/src/views/container/image/pull/index.vue @@ -22,7 +22,7 @@ prop="repoID" > - + @@ -75,7 +75,7 @@ import { LoadFile } from '@/api/modules/files'; const pullVisiable = ref(false); const form = reactive({ fromRepo: true, - repoID: 1, + repoID: null as number, imageName: '', }); @@ -89,16 +89,13 @@ let timer: NodeJS.Timer | null = null; interface DialogProps { repos: Array; } -const dialogData = ref({ - repos: [] as Array, -}); +const repos = ref(); const acceptParams = async (params: DialogProps): Promise => { pullVisiable.value = true; form.fromRepo = true; - form.repoID = 1; form.imageName = ''; - dialogData.value.repos = params.repos; + repos.value = params.repos; buttonDisabled.value = false; logInfo.value = ''; }; @@ -138,7 +135,7 @@ const onCloseLog = async () => { }; function loadDetailInfo(id: number) { - for (const item of dialogData.value.repos) { + for (const item of repos.value) { if (item.id === id) { return item.downloadUrl; } diff --git a/frontend/src/views/container/repo/delete/index.vue b/frontend/src/views/container/repo/delete/index.vue new file mode 100644 index 000000000..a858bb57c --- /dev/null +++ b/frontend/src/views/container/repo/delete/index.vue @@ -0,0 +1,66 @@ + + + diff --git a/frontend/src/views/container/repo/index.vue b/frontend/src/views/container/repo/index.vue index f115eb739..f7bba5c36 100644 --- a/frontend/src/views/container/repo/index.vue +++ b/frontend/src/views/container/repo/index.vue @@ -47,18 +47,19 @@ +