1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

feat: 删除运行环境级联删除镜像 (#579)

This commit is contained in:
zhengkunwang223 2023-04-11 17:08:29 +08:00 committed by GitHub
parent e935fa128f
commit 38725097a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@ -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',
},
};

View File

@ -1280,6 +1280,7 @@ const message = {
version: '版本',
status: '状态',
versionHelper: 'PHP的版本,例如 v8.0',
buildHelper: '选择的扩展越多制作镜像过程中占用 CPU 越多请尽量避免选择全部扩展',
},
};
export default {

View File

@ -63,6 +63,9 @@
<el-input v-model="runtime.image"></el-input>
</el-form-item>
<div v-if="initParam">
<el-form-item v-if="runtime.type === 'php'">
<el-alert :title="$t('runtime.buildHelper')" type="warning" :closable="false" />
</el-form-item>
<Params
v-if="mode === 'create'"
v-model:form="runtime.params"