feat: 修改应用容器名称增加校验 (#1251)

pull/1251/merge
zhengkunwang223 2023-06-05 14:59:26 +08:00 committed by GitHub
parent 3a17f4f29f
commit 05e5f06cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 2 deletions

View File

@ -22,6 +22,7 @@ type IAppInstallRepo interface {
WithContainerName(containerName string) DBOption WithContainerName(containerName string) DBOption
WithPort(port int) DBOption WithPort(port int) DBOption
WithIdNotInWebsite() DBOption WithIdNotInWebsite() DBOption
WithIDNotIs(id uint) DBOption
ListBy(opts ...DBOption) ([]model.AppInstall, error) ListBy(opts ...DBOption) ([]model.AppInstall, error)
GetFirst(opts ...DBOption) (model.AppInstall, error) GetFirst(opts ...DBOption) (model.AppInstall, error)
Create(ctx context.Context, install *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 { func (a *AppInstallRepo) WithAppIdsIn(appIds []uint) DBOption {
return func(g *gorm.DB) *gorm.DB { return func(g *gorm.DB) *gorm.DB {
return g.Where("app_id in (?)", appIds) return g.Where("app_id in (?)", appIds)

View File

@ -296,6 +296,15 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
err = buserr.New(constant.ErrContainerName) err = buserr.New(constant.ErrContainerName)
return 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 req.Params[constant.ContainerName] = containerName
appInstall.ContainerName = containerName appInstall.ContainerName = containerName

View File

@ -267,7 +267,7 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
return err 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 return err
} }
composeByte, err := yaml.Marshal(composeMap) composeByte, err := yaml.Marshal(composeMap)
@ -279,7 +279,20 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
req.Params[constant.ContainerName] = installed.ContainerName req.Params[constant.ContainerName] = installed.ContainerName
} else { } else {
req.Params[constant.ContainerName] = req.ContainerName 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
}
} }
} }

View File

@ -35,6 +35,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker" composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
dockerTypes "github.com/docker/docker/api/types"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -542,6 +543,31 @@ func getServiceFromInstall(appInstall *model.AppInstall) (service *composeV2.Com
return 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) { func upApp(appInstall *model.AppInstall) {
upProject := func(appInstall *model.AppInstall) (err error) { upProject := func(appInstall *model.AppInstall) (err error) {
if err == nil { if err == nil {