feat: 创建、更新 node.js 环境增加容器名称校验 (#2461)

This commit is contained in:
zhengkunwang 2023-10-08 04:06:14 -05:00 committed by GitHub
parent 517bae6910
commit 5bb77d7176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

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

View File

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