diff --git a/agent/app/dto/request/runtime.go b/agent/app/dto/request/runtime.go index 88f07b1a4..ad0f2debf 100644 --- a/agent/app/dto/request/runtime.go +++ b/agent/app/dto/request/runtime.go @@ -66,14 +66,15 @@ type RuntimeDelete struct { } type RuntimeUpdate struct { - Name string `json:"name"` - ID uint `json:"id"` - Image string `json:"image"` - Version string `json:"version"` - Rebuild bool `json:"rebuild"` - Source string `json:"source"` - CodeDir string `json:"codeDir"` - Remark string `json:"remark"` + AppDetailID uint `json:"appDetailId"` + Name string `json:"name"` + ID uint `json:"id"` + Image string `json:"image"` + Version string `json:"version"` + Rebuild bool `json:"rebuild"` + Source string `json:"source"` + CodeDir string `json:"codeDir"` + Remark string `json:"remark"` Params map[string]interface{} `json:"params"` NodeConfig diff --git a/agent/app/service/runtime.go b/agent/app/service/runtime.go index 3d60040f0..3ea55cfaf 100644 --- a/agent/app/service/runtime.go +++ b/agent/app/service/runtime.go @@ -640,6 +640,12 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { runtime.Version = req.Version return runtimeRepo.Save(runtime) } + appDetail, err := getRuntimeUpdateAppDetail(runtime, req) + if err != nil { + return err + } + versionChanged := appDetail.ID != runtime.AppDetailID || appDetail.Version != runtime.Version + req.Version = appDetail.Version oldImage := runtime.Image oldEnv := runtime.Env var hostPorts []string @@ -659,23 +665,6 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { } } - appDetail, err := appDetailRepo.GetFirst(repo.WithByID(runtime.AppDetailID)) - if err != nil { - return err - } - app, err := appRepo.GetFirst(repo.WithByID(appDetail.AppId)) - if err != nil { - return err - } - fileOp := files.NewFileOp() - appVersionDir := path.Join(global.Dir.AppResourceDir, app.Resource, app.Key, appDetail.Version) - if !fileOp.Stat(appVersionDir) || appDetail.Update { - if err := downloadApp(app, appDetail, nil, nil); err != nil { - return err - } - _ = fileOp.Rename(path.Join(runtime.GetPath(), "run.sh"), path.Join(runtime.GetPath(), "run.sh.bak")) - _ = fileOp.CopyFile(path.Join(appVersionDir, "run.sh"), runtime.GetPath()) - } } if containerName, ok := req.Params["CONTAINER_NAME"]; ok && containerName != getRuntimeEnv(runtime.Env, "CONTAINER_NAME") { @@ -685,6 +674,36 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { runtime.ContainerName = containerName.(string) } + app, err := appRepo.GetFirst(repo.WithByID(appDetail.AppId)) + if err != nil { + return err + } + fileOp := files.NewFileOp() + appVersionDir := filepath.Join(app.GetAppResourcePath(), appDetail.Version) + refreshTemplate := !fileOp.Stat(appVersionDir) || appDetail.Update + if err = downloadApp(app, appDetail, nil, nil); err != nil { + return err + } + if versionChanged { + if err = updateRuntimeVersionFiles(runtime, appVersionDir); err != nil { + return err + } + } else if refreshTemplate && runtime.Type != constant.RuntimePHP { + if err = fileOp.CopyFile(filepath.Join(appVersionDir, "run.sh"), runtime.GetPath()); err != nil { + return err + } + } + if runtime.Type == constant.RuntimePHP { + composeContent, err := fileOp.GetContent(runtime.GetComposePath()) + if err != nil { + return err + } + req.Environments, err = getDockerComposeEnvironments(composeContent) + if err != nil { + return err + } + } + projectDir := path.Join(global.Dir.RuntimeDir, runtime.Type, runtime.Name) create := request.RuntimeCreate{ Image: req.Image, @@ -702,19 +721,21 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { ExtraHosts: req.ExtraHosts, }, } - composeContent, envContent, _, err := handleParams(create, projectDir) + composeContent, envContent, forms, err := handleParams(create, projectDir) if err != nil { return err } runtime.Remark = req.Remark + runtime.AppDetailID = appDetail.ID + runtime.Version = appDetail.Version runtime.Env = string(envContent) runtime.DockerCompose = string(composeContent) switch runtime.Type { case constant.RuntimePHP: runtime.Image = req.Image + runtime.Params = string(forms) runtime.Status = constant.StatusBuilding - _ = runtimeRepo.Save(runtime) client, err := docker.NewClient() if err != nil { return err @@ -724,14 +745,18 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { if err != nil { return err } - go buildRuntime(runtime, imageID, oldEnv, req.Rebuild) + if err = runtimeRepo.Save(runtime); err != nil { + return err + } + go buildRuntime(runtime, imageID, oldEnv, req.Rebuild || versionChanged) case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet: - runtime.Version = req.Version runtime.CodeDir = req.CodeDir runtime.Port = strings.Join(hostPorts, ",") runtime.Status = constant.StatusReCreating runtime.ContainerName = req.Params["CONTAINER_NAME"].(string) - _ = runtimeRepo.Save(runtime) + if err = runtimeRepo.Save(runtime); err != nil { + return err + } go reCreateRuntime(runtime) } return nil diff --git a/frontend/src/api/interface/runtime.ts b/frontend/src/api/interface/runtime.ts index 641399b11..e7d390fa4 100644 --- a/frontend/src/api/interface/runtime.ts +++ b/frontend/src/api/interface/runtime.ts @@ -92,7 +92,7 @@ export namespace Runtime { export interface RuntimeUpdate { name: string; - appDetailID: number; + appDetailID?: number; image: string; params: object; type: string; diff --git a/frontend/src/views/website/runtime/app/index.vue b/frontend/src/views/website/runtime/app/index.vue index c31f56d47..f160fc8f4 100644 --- a/frontend/src/views/website/runtime/app/index.vue +++ b/frontend/src/views/website/runtime/app/index.vue @@ -1,10 +1,10 @@