From 05e5f06cf12712619c5a0d6f69a72494b86fe1fa Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Mon, 5 Jun 2023 14:59:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E5=90=8D=E7=A7=B0=E5=A2=9E=E5=8A=A0=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=20(#1251)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/repo/app_install.go | 7 +++++++ backend/app/service/app.go | 9 +++++++++ backend/app/service/app_install.go | 17 +++++++++++++++-- backend/app/service/app_utils.go | 26 ++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/backend/app/repo/app_install.go b/backend/app/repo/app_install.go index 85ecd2e23..1655b812b 100644 --- a/backend/app/repo/app_install.go +++ b/backend/app/repo/app_install.go @@ -22,6 +22,7 @@ type IAppInstallRepo interface { WithContainerName(containerName string) DBOption WithPort(port int) DBOption WithIdNotInWebsite() DBOption + WithIDNotIs(id uint) DBOption ListBy(opts ...DBOption) ([]model.AppInstall, error) GetFirst(opts ...DBOption) (model.AppInstall, error) Create(ctx context.Context, install *model.AppInstall) error @@ -56,6 +57,12 @@ func (a *AppInstallRepo) WithAppId(appId uint) DBOption { } } +func (a *AppInstallRepo) WithIDNotIs(id uint) DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("id != ?", id) + } +} + func (a *AppInstallRepo) WithAppIdsIn(appIds []uint) DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("app_id in (?)", appIds) diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 9f5d3dffb..db6da647d 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -296,6 +296,15 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( err = buserr.New(constant.ErrContainerName) return } + containerExist := false + containerExist, err = checkContainerNameIsExist(req.ContainerName, appInstall.GetPath()) + if err != nil { + return + } + if containerExist { + err = buserr.New(constant.ErrContainerName) + return + } } req.Params[constant.ContainerName] = containerName appInstall.ContainerName = containerName diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index fb2fcd1a4..c617e5e28 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -267,7 +267,7 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error { return err } } - if err := addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil { + if err = addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil { return err } composeByte, err := yaml.Marshal(composeMap) @@ -279,7 +279,20 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error { req.Params[constant.ContainerName] = installed.ContainerName } else { req.Params[constant.ContainerName] = req.ContainerName - installed.ContainerName = req.ContainerName + if installed.ContainerName != req.ContainerName { + exist, _ := appInstallRepo.GetFirst(appInstallRepo.WithContainerName(req.ContainerName), appInstallRepo.WithIDNotIs(installed.ID)) + if exist.ID > 0 { + return buserr.New(constant.ErrContainerName) + } + containerExist, err := checkContainerNameIsExist(req.ContainerName, installed.GetPath()) + if err != nil { + return err + } + if containerExist { + return buserr.New(constant.ErrContainerName) + } + installed.ContainerName = req.ContainerName + } } } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 774364482..8231ff50a 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -35,6 +35,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/compose" composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" + dockerTypes "github.com/docker/docker/api/types" "github.com/pkg/errors" ) @@ -542,6 +543,31 @@ func getServiceFromInstall(appInstall *model.AppInstall) (service *composeV2.Com return } +func checkContainerNameIsExist(containerName, appDir string) (bool, error) { + client, err := composeV2.NewDockerClient() + if err != nil { + return false, err + } + var options dockerTypes.ContainerListOptions + list, err := client.ContainerList(context.Background(), options) + if err != nil { + return false, err + } + for _, container := range list { + if containerName == container.Names[0][1:] { + if workDir, ok := container.Labels[composeWorkdirLabel]; ok { + if workDir != appDir { + return true, nil + } + } else { + return true, nil + } + } + + } + return false, nil +} + func upApp(appInstall *model.AppInstall) { upProject := func(appInstall *model.AppInstall) (err error) { if err == nil {