From 38725097a68c62f5ce1a0de5c413e2c4075057ba Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 11 Apr 2023 17:08:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E7=BA=A7=E8=81=94=E5=88=A0=E9=99=A4=E9=95=9C?= =?UTF-8?q?=E5=83=8F=20(#579)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/runtime.go | 26 ++++++++++++++++--- backend/app/service/runtime_utils.go | 16 +++++++++++- backend/utils/docker/docker.go | 22 ++++++++++++++++ frontend/src/lang/modules/en.ts | 2 ++ frontend/src/lang/modules/zh.ts | 1 + .../views/website/runtime/create/index.vue | 3 +++ 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/backend/app/service/runtime.go b/backend/app/service/runtime.go index 0bb3b8a74..a580556bc 100644 --- a/backend/app/service/runtime.go +++ b/backend/app/service/runtime.go @@ -11,6 +11,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/subosito/gotenv" "path" @@ -109,7 +110,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) { if err = runtimeRepo.Create(context.Background(), runtime); err != nil { return } - go buildRuntime(runtime, composeService) + go buildRuntime(runtime, composeService, "") return } @@ -145,7 +146,17 @@ func (r *RuntimeService) Delete(id uint) error { if website.ID > 0 { return buserr.New(constant.ErrDelWithWebsite) } - //TODO 删除镜像 + client, err := docker.NewClient() + if err != nil { + return err + } + imageID, err := client.GetImageIDByName(runtime.Image) + if err != nil { + return err + } + if err := client.DeleteImage(imageID); err != nil { + return err + } if runtime.Resource == constant.ResourceAppstore { runtimeDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name) if err := files.NewFileOp().DeleteDir(runtimeDir); err != nil { @@ -226,6 +237,7 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { if err != nil { return err } + oldImage := runtime.Image if runtime.Resource == constant.ResourceLocal { runtime.Version = req.Version return runtimeRepo.Save(runtime) @@ -248,6 +260,14 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { runtime.DockerCompose = string(composeContent) runtime.Status = constant.RuntimeBuildIng _ = runtimeRepo.Save(runtime) - go buildRuntime(runtime, composeService) + client, err := docker.NewClient() + if err != nil { + return err + } + imageID, err := client.GetImageIDByName(oldImage) + if err != nil { + return err + } + go buildRuntime(runtime, composeService, imageID) return nil } diff --git a/backend/app/service/runtime_utils.go b/backend/app/service/runtime_utils.go index 144242410..36069e57a 100644 --- a/backend/app/service/runtime_utils.go +++ b/backend/app/service/runtime_utils.go @@ -5,6 +5,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/subosito/gotenv" @@ -12,13 +13,26 @@ import ( "strings" ) -func buildRuntime(runtime *model.Runtime, service *docker.ComposeService) { +func buildRuntime(runtime *model.Runtime, service *docker.ComposeService, imageID string) { err := service.ComposeBuild() if err != nil { runtime.Status = constant.RuntimeError runtime.Message = buserr.New(constant.ErrImageBuildErr).Error() + ":" + err.Error() } else { runtime.Status = constant.RuntimeNormal + if imageID != "" { + client, err := docker.NewClient() + global.LOG.Infof("delete imageID [%s] ", imageID) + if err == nil { + if err := client.DeleteImage(imageID); err != nil { + global.LOG.Errorf("delete imageID [%s] error %v", imageID, err) + } else { + global.LOG.Infof("delete old image success") + } + } else { + global.LOG.Errorf("delete imageID [%s] error %v", imageID, err) + } + } } _ = runtimeRepo.Save(runtime) } diff --git a/backend/utils/docker/docker.go b/backend/utils/docker/docker.go index 031e7c666..1c436c420 100644 --- a/backend/utils/docker/docker.go +++ b/backend/utils/docker/docker.go @@ -65,6 +65,28 @@ func (c Client) CreateNetwork(name string) error { return err } +func (c Client) DeleteImage(imageID string) error { + if _, err := c.cli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{Force: true}); err != nil { + return err + } + return nil +} + +func (c Client) GetImageIDByName(imageName string) (string, error) { + filter := filters.NewArgs() + filter.Add("reference", imageName) + list, err := c.cli.ImageList(context.Background(), types.ImageListOptions{ + Filters: filter, + }) + if err != nil { + return "", err + } + if len(list) > 0 { + return list[0].ID, nil + } + return "", nil +} + func (c Client) NetworkExist(name string) bool { var options types.NetworkListOptions options.Filters = filters.NewArgs(filters.Arg("name", name)) diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index ea27643bd..87c33c408 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -1295,6 +1295,8 @@ const message = { version: 'Version', status: 'Status', versionHelper: 'PHP version, e.g. v8.0', + buildHelper: + 'The more extensions you select, the more CPU will be occupied during the image making process, so avoid selecting all extensions', }, }; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 987607c29..ac0016030 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -1280,6 +1280,7 @@ const message = { version: '版本', status: '状态', versionHelper: 'PHP的版本,例如 v8.0', + buildHelper: '选择的扩展越多,制作镜像过程中占用 CPU 越多,请尽量避免选择全部扩展', }, }; export default { diff --git a/frontend/src/views/website/runtime/create/index.vue b/frontend/src/views/website/runtime/create/index.vue index edf93a8d5..6b76f0757 100644 --- a/frontend/src/views/website/runtime/create/index.vue +++ b/frontend/src/views/website/runtime/create/index.vue @@ -63,6 +63,9 @@
+ + +