From bc8a2511d185b35a4211ae5fb3a20dbbda4271af Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 28 Aug 2024 17:48:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20OpenResty=20=E5=AE=89=E8=A3=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=BD=91=E7=AB=99=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=20(#6277)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/app/model/app_install.go | 13 +++++ agent/app/service/app.go | 11 ++++ agent/app/service/app_utils.go | 57 +++++++++++++++++-- agent/i18n/lang/en.yaml | 4 ++ agent/i18n/lang/zh-Hant.yaml | 4 ++ agent/i18n/lang/zh.yaml | 6 +- agent/init/migration/migrations/init.go | 2 +- agent/utils/docker/compose.go | 30 ++++++++++ .../src/components/container-log/index.vue | 4 +- .../app-store/installed/delete/index.vue | 4 +- .../app-store/installed/upgrade/index.vue | 1 - .../config/basic/proxy/create/index.vue | 3 - .../src/views/website/website/nginx/index.vue | 2 +- 13 files changed, 124 insertions(+), 17 deletions(-) diff --git a/agent/app/model/app_install.go b/agent/app/model/app_install.go index d753003b0..846e06885 100644 --- a/agent/app/model/app_install.go +++ b/agent/app/model/app_install.go @@ -31,6 +31,19 @@ func (i *AppInstall) GetPath() string { return path.Join(i.GetAppPath(), i.Name) } +//func (i *AppInstall) GetSiteDir() string { +// var data map[string]interface{} +// err := json.Unmarshal([]byte(i.Env), &data) +// if err != nil { +// return path.Join(i.GetAppPath(), i.Name, "www") +// } +// websiteDir, ok := data["WEBSITE_DIR"].(string) +// if !ok || websiteDir == "" { +// return path.Join(i.GetAppPath(), i.Name, "www") +// } +// return websiteDir +//} + func (i *AppInstall) GetComposePath() string { return path.Join(i.GetAppPath(), i.Name, "docker-compose.yml") } diff --git a/agent/app/service/app.go b/agent/app/service/app.go index 48743e0fa..57d217aef 100644 --- a/agent/app/service/app.go +++ b/agent/app/service/app.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "os" + "path" "path/filepath" "reflect" "strconv" @@ -337,6 +338,16 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App return } } + if app.Key == "openresty" && app.Resource == "remote" && common.CompareVersion(appDetail.Version, "1.21.4.3-3-3") { + if dir, ok := req.Params["WEBSITE_DIR"]; ok { + siteDir := dir.(string) + if siteDir == "" || !strings.HasPrefix(siteDir, "/") { + siteDir = path.Join(constant.DataDir, dir.(string)) + } + req.Params["WEBSITE_DIR"] = siteDir + _ = settingRepo.Create("WEBSITE_DIR", siteDir) + } + } for key := range req.Params { if !strings.Contains(key, "PANEL_APP_PORT") { continue diff --git a/agent/app/service/app_utils.go b/agent/app/service/app_utils.go index f96d29b34..a877b3582 100644 --- a/agent/app/service/app_utils.go +++ b/agent/app/service/app_utils.go @@ -5,6 +5,8 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/1Panel-dev/1Panel/agent/utils/nginx" + "github.com/1Panel-dev/1Panel/agent/utils/nginx/parser" "log" "math" "net/http" @@ -346,12 +348,11 @@ func deleteAppInstall(deleteReq request.AppInstallDelete) error { } if deleteReq.DeleteImage { delImageStr := i18n.GetMsgByKey("TaskDelete") + i18n.GetMsgByKey("Image") - projectName := strings.ToLower(install.Name) content, err := op.GetContent(install.GetEnvPath()) if err != nil { return err } - images, err := composeV2.GetDockerComposeImages(projectName, content, []byte(install.DockerCompose)) + images, err := composeV2.GetDockerComposeImagesV2(content, []byte(install.DockerCompose)) if err != nil { return err } @@ -448,7 +449,7 @@ func deleteAppInstall(deleteReq request.AppInstallDelete) error { } uninstallTask.AddSubTask(task.GetTaskName(install.Name, task.TaskUninstall, task.TaskScopeApp), uninstall, nil) go func() { - if err := uninstallTask.Execute(); err != nil { + if err := uninstallTask.Execute(); err != nil && !deleteReq.ForceDelete { install.Status = constant.Error _ = appInstallRepo.Save(context.Background(), &install) } @@ -615,8 +616,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error { return err } if req.PullImage { - projectName := strings.ToLower(install.Name) - images, err := composeV2.GetDockerComposeImages(projectName, content, []byte(detail.DockerCompose)) + images, err := composeV2.GetDockerComposeImagesV2(content, []byte(detail.DockerCompose)) if err != nil { return err } @@ -674,6 +674,51 @@ func upgradeInstall(req request.AppInstallUpgrade) error { } envParams := make(map[string]string, len(envs)) handleMap(envs, envParams) + if install.App.Key == "openresty" && install.App.Resource == "remote" && !common.CompareVersion(install.Version, "1.25.3.2-0-1") { + t.Log(i18n.GetMsgByKey("MoveSiteDir")) + siteDir := path.Join(constant.DataDir, "www") + envParams["WEBSITE_DIR"] = siteDir + oldSiteDir := path.Join(install.GetPath(), "www") + t.Log(i18n.GetWithName("MoveSiteToDir", siteDir)) + if err := fileOp.CopyDir(oldSiteDir, constant.DataDir); err != nil { + t.Log(i18n.GetMsgByKey("ErrMoveSiteDir")) + return err + } + newConfDir := path.Join(constant.DataDir, "www", "conf.d") + _ = fileOp.CreateDir(newConfDir, 0644) + oldConfDir := path.Join(install.GetPath(), "conf/conf.d") + items, err := os.ReadDir(oldConfDir) + if err != nil { + return err + } + for _, item := range items { + itemPath := path.Join(oldConfDir, item.Name()) + if item.IsDir() { + _ = fileOp.Mv(itemPath, newConfDir) + } + if item.Name() != "default.conf" && item.Name() != "00.default.conf" { + _ = fileOp.Mv(itemPath, newConfDir) + } + } + nginxConfPath := path.Join(install.GetPath(), "conf", "nginx.conf") + parse, err := parser.NewParser(nginxConfPath) + if err != nil { + return err + } + config, err := parse.Parse() + if err != nil { + return err + } + config.FilePath = nginxConfPath + httpDirective := config.FindHttp() + httpDirective.UpdateDirective("include", []string{"/usr/local/openresty/nginx/conf/default/*.conf"}) + + if err = nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { + return buserr.WithErr(constant.ErrUpdateBuWebsite, err) + } + t.Log(i18n.GetMsgByKey("MoveSiteDirSuccess")) + } + if err = env.Write(envParams, install.GetEnvPath()); err != nil { return err } @@ -719,7 +764,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error { existInstall, _ := appInstallRepo.GetFirst(commonRepo.WithByID(req.InstallID)) if existInstall.ID > 0 && existInstall.Status != constant.Running { existInstall.Status = constant.UpgradeErr - existInstall.Message = upErr.Error() + existInstall.Message = err.Error() _ = appInstallRepo.Save(context.Background(), &existInstall) } } diff --git a/agent/i18n/lang/en.yaml b/agent/i18n/lang/en.yaml index 38ff5d2a3..ce4b2098b 100644 --- a/agent/i18n/lang/en.yaml +++ b/agent/i18n/lang/en.yaml @@ -66,6 +66,10 @@ AppStoreIsLastVersion: "The app store is up to date" AppStoreSyncSuccess: "App store synchronized successfully" SyncAppDetail: "Synchronize app configuration" AppVersionNotMatch: "{{ .name }} app requires an update to 1Panel, skipping synchronization" +MoveSiteDir: "The current upgrade requires migrating the OpenResty website directory" +MoveSiteToDir: "Migrate the website directory to {{ .name }}" +ErrMoveSiteDir: "Failed to migrate the website directory" +MoveSiteDirSuccess: "Successfully migrated the website directory" #file diff --git a/agent/i18n/lang/zh-Hant.yaml b/agent/i18n/lang/zh-Hant.yaml index b7823c120..165694457 100644 --- a/agent/i18n/lang/zh-Hant.yaml +++ b/agent/i18n/lang/zh-Hant.yaml @@ -67,6 +67,10 @@ AppStoreIsLastVersion: "應用商店已經是最新版本" AppStoreSyncSuccess: "應用商店同步成功" SyncAppDetail: "同步應用配置" AppVersionNotMatch: "{{ .name }} 應用需要更新 1Panel,跳過同步" +MoveSiteDir: "當前升級需要遷移 OpenResty 網站目錄" +MoveSiteToDir: "遷移網站目錄到 {{ .name }}" +ErrMoveSiteDir: "遷移網站目錄失敗" +MoveSiteDirSuccess: "遷移網站目錄成功" #file diff --git a/agent/i18n/lang/zh.yaml b/agent/i18n/lang/zh.yaml index 8bc86ed02..6ed9adaf1 100644 --- a/agent/i18n/lang/zh.yaml +++ b/agent/i18n/lang/zh.yaml @@ -65,7 +65,11 @@ UpgradeAppSuccess: "应用 {{ .name }} 升级成功" AppStoreIsLastVersion: "应用商店已经是最新版本" AppStoreSyncSuccess: "应用商店同步成功" SyncAppDetail: "同步应用配置" -AppVersionNotMatch: "{{ .name }} 应用需要更新 1Panel,跳过同步" +AppVersionNotMatch: "{{ .name }} 应用需要更高的 1Panel 版本,跳过同步" +MoveSiteDir: "当前升级需要迁移 OpenResty 网站目录" +MoveSiteToDir: "迁移网站目录到 {{ .name }}" +ErrMoveSiteDir: "迁移网站目录失败" +MoveSiteDirSuccess: "迁移网站目录成功" #file ErrFileCanNotRead: "此文件不支持预览" diff --git a/agent/init/migration/migrations/init.go b/agent/init/migration/migrations/init.go index 8e09ccc82..2d7d27dd7 100644 --- a/agent/init/migration/migrations/init.go +++ b/agent/init/migration/migrations/init.go @@ -16,7 +16,7 @@ import ( ) var AddTable = &gormigrate.Migration{ - ID: "20240722-add-table", + ID: "20240826-add-table", Migrate: func(tx *gorm.DB) error { return tx.AutoMigrate( &model.AppDetail{}, diff --git a/agent/utils/docker/compose.go b/agent/utils/docker/compose.go index 988ca46a8..f11152153 100644 --- a/agent/utils/docker/compose.go +++ b/agent/utils/docker/compose.go @@ -2,10 +2,12 @@ package docker import ( "context" + "fmt" "github.com/compose-spec/compose-go/v2/loader" "github.com/compose-spec/compose-go/v2/types" "github.com/docker/compose/v2/pkg/api" "github.com/joho/godotenv" + "gopkg.in/yaml.v3" "path" "regexp" "strings" @@ -55,6 +57,34 @@ type Service struct { Image string `yaml:"image"` } +func replaceEnvVariables(input string, envVars map[string]string) string { + for key, value := range envVars { + placeholder := fmt.Sprintf("${%s}", key) + input = strings.ReplaceAll(input, placeholder, value) + } + return input +} +func GetDockerComposeImagesV2(env, yml []byte) ([]string, error) { + var ( + compose ComposeProject + err error + images []string + ) + err = yaml.Unmarshal(yml, &compose) + if err != nil { + return nil, err + } + envMap, err := godotenv.UnmarshalBytes(env) + if err != nil { + return nil, err + } + for _, service := range compose.Services { + image := replaceEnvVariables(service.Image, envMap) + images = append(images, image) + } + return images, nil +} + func GetDockerComposeImages(projectName string, env, yml []byte) ([]string, error) { var ( configFiles []types.ConfigFile diff --git a/frontend/src/components/container-log/index.vue b/frontend/src/components/container-log/index.vue index daff35ee1..54fa363b4 100644 --- a/frontend/src/components/container-log/index.vue +++ b/frontend/src/components/container-log/index.vue @@ -25,7 +25,7 @@ {{ $t('commons.button.clean') }} - + @@ -80,7 +80,7 @@ const searchLogs = async () => { const protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss'; const host = href.split('//')[1].split('/')[0]; terminalSocket.value = new WebSocket( - `${protocol}://${host}/api/v1/containers/search/log?container=${logSearch.containerID}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}`, + `${protocol}://${host}/api/v2/containers/search/log?container=${logSearch.containerID}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}`, ); terminalSocket.value.onmessage = (event) => { logInfo.value += event.data; diff --git a/frontend/src/views/app-store/installed/delete/index.vue b/frontend/src/views/app-store/installed/delete/index.vue index 513e60916..5b1195ce9 100644 --- a/frontend/src/views/app-store/installed/delete/index.vue +++ b/frontend/src/views/app-store/installed/delete/index.vue @@ -65,7 +65,7 @@ const deleteReq = ref({ deleteBackup: false, forceDelete: false, deleteDB: true, - deleteImage: true, + deleteImage: false, taskID: '', }); const open = ref(false); @@ -91,7 +91,7 @@ const acceptParams = async (app: App.AppInstallDto) => { deleteBackup: false, forceDelete: false, deleteDB: true, - deleteImage: true, + deleteImage: false, taskID: uuidv4(), }; deleteInfo.value = ''; diff --git a/frontend/src/views/app-store/installed/upgrade/index.vue b/frontend/src/views/app-store/installed/upgrade/index.vue index 88db880b0..4ce4b087e 100644 --- a/frontend/src/views/app-store/installed/upgrade/index.vue +++ b/frontend/src/views/app-store/installed/upgrade/index.vue @@ -204,7 +204,6 @@ const operate = async () => { operateReq.taskID = taskID; await InstalledOp(operateReq) .then(() => { - MsgSuccess(i18n.global.t('app.upgradeStart')); bus.emit('upgrade', true); handleClose(); openTaskLog(taskID); diff --git a/frontend/src/views/website/website/config/basic/proxy/create/index.vue b/frontend/src/views/website/website/config/basic/proxy/create/index.vue index 0100be0b6..b32a98dd3 100644 --- a/frontend/src/views/website/website/config/basic/proxy/create/index.vue +++ b/frontend/src/views/website/website/config/basic/proxy/create/index.vue @@ -102,9 +102,6 @@ {{ $t('website.addReplace') }} -
- {{ $t('website.replacementHelper') }} -
diff --git a/frontend/src/views/website/website/nginx/index.vue b/frontend/src/views/website/website/nginx/index.vue index 6da5d2b4d..7fbfb5a2a 100644 --- a/frontend/src/views/website/website/nginx/index.vue +++ b/frontend/src/views/website/website/nginx/index.vue @@ -16,7 +16,7 @@