mirror of https://github.com/1Panel-dev/1Panel
feat: 修改应用容器名称增加校验 (#1251)
parent
3a17f4f29f
commit
05e5f06cf1
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue