mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-18 22:22:59 +08:00
feat: 创建、更新 node.js 环境增加容器名称校验 (#2461)
This commit is contained in:
parent
517bae6910
commit
5bb77d7176
@ -78,6 +78,11 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) {
|
||||
if err = checkPortExist(create.Port); err != nil {
|
||||
return err
|
||||
}
|
||||
if containerName, ok := create.Params["CONTAINER_NAME"]; ok {
|
||||
if err := checkContainerName(containerName.(string)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(create.AppDetailID))
|
||||
@ -309,6 +314,18 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
|
||||
}
|
||||
runtime.Port = req.Port
|
||||
}
|
||||
if containerName, ok := req.Params["CONTAINER_NAME"]; ok {
|
||||
envs, err := gotenv.Unmarshal(runtime.Env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldContainerName := envs["CONTAINER_NAME"]
|
||||
if containerName != oldContainerName {
|
||||
if err := checkContainerName(containerName.(string)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
projectDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
|
||||
|
@ -328,3 +328,18 @@ func handleParams(create request.RuntimeCreate, projectDir string) (composeConte
|
||||
envContent = []byte(envStr)
|
||||
return
|
||||
}
|
||||
|
||||
func checkContainerName(name string) error {
|
||||
dockerCli, err := docker.NewClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
names, err := dockerCli.ListContainersByName([]string{name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(names) > 0 {
|
||||
return buserr.New(constant.ErrContainerName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user