From 68a457ae899a4f234f160ab4dd509c12f0ab42fb Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 21 Mar 2023 15:16:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=87=E7=BA=A7=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E8=B0=83=E6=95=B4=20(#341)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 升级逻辑调整 --- backend/app/api/v1/upgrade.go | 22 ++++ backend/app/dto/setting.go | 6 +- backend/app/service/upgrade.go | 102 ++++++++++++++---- backend/router/ro_setting.go | 1 + cmd/server/docs/docs.go | 26 +++-- cmd/server/docs/swagger.json | 26 +++-- cmd/server/docs/swagger.yaml | 19 +++- frontend/src/api/interface/setting.ts | 1 + frontend/src/api/modules/setting.ts | 3 + .../src/components/system-upgrade/index.vue | 33 ++++-- frontend/src/lang/modules/en.ts | 7 +- frontend/src/lang/modules/zh.ts | 6 +- 12 files changed, 201 insertions(+), 51 deletions(-) diff --git a/backend/app/api/v1/upgrade.go b/backend/app/api/v1/upgrade.go index 997342542..7cd7567a3 100644 --- a/backend/app/api/v1/upgrade.go +++ b/backend/app/api/v1/upgrade.go @@ -22,6 +22,28 @@ func (b *BaseApi) GetUpgradeInfo(c *gin.Context) { helper.SuccessWithData(c, info) } +// @Tags System Setting +// @Summary Load release notes by version +// @Description 获取版本 release notes +// @Accept json +// @Param request body dto.Upgrade true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /settings/upgrade [get] +func (b *BaseApi) GetNotesByVersion(c *gin.Context) { + var req dto.Upgrade + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + notes, err := upgradeService.LoadNotes(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, notes) +} + // @Tags System Setting // @Summary Upgrade // @Description 系统更新 diff --git a/backend/app/dto/setting.go b/backend/app/dto/setting.go index 6290f3203..e6beda926 100644 --- a/backend/app/dto/setting.go +++ b/backend/app/dto/setting.go @@ -82,9 +82,11 @@ type SnapshotInfo struct { } type UpgradeInfo struct { - NewVersion string `json:"newVersion"` - ReleaseNote string `json:"releaseNote"` + NewVersion string `json:"newVersion"` + LatestVersion string `json:"latestVersion"` + ReleaseNote string `json:"releaseNote"` } + type Upgrade struct { Version string `json:"version"` } diff --git a/backend/app/service/upgrade.go b/backend/app/service/upgrade.go index 4d9bec447..bf25fcf9c 100644 --- a/backend/app/service/upgrade.go +++ b/backend/app/service/upgrade.go @@ -1,6 +1,8 @@ package service import ( + "encoding/json" + "errors" "fmt" "io/ioutil" "net/http" @@ -20,6 +22,7 @@ type UpgradeService struct{} type IUpgradeService interface { Upgrade(req dto.Upgrade) error + LoadNotes(req dto.Upgrade) (string, error) SearchUpgrade() (*dto.UpgradeInfo, error) } @@ -34,36 +37,45 @@ func (u *UpgradeService) SearchUpgrade() (*dto.UpgradeInfo, error) { return nil, err } - versionRes, err := http.Get(fmt.Sprintf("%s/%s/latest", global.CONF.System.RepoUrl, global.CONF.System.Mode)) + latestVersion, err := u.loadVersion(true, currentVersion.Value) if err != nil { + global.LOG.Infof("load latest version failed, err: %v", err) return nil, err } - defer versionRes.Body.Close() - version, err := ioutil.ReadAll(versionRes.Body) + if !common.CompareVersion(string(latestVersion), currentVersion.Value) { + return nil, err + } + upgrade.LatestVersion = latestVersion + if latestVersion[0:4] == currentVersion.Value[0:4] { + upgrade.NewVersion = "" + } else { + newerVersion, err := u.loadVersion(false, currentVersion.Value) + if err != nil { + global.LOG.Infof("load newer version failed, err: %v", err) + return nil, err + } + if newerVersion == currentVersion.Value { + upgrade.NewVersion = "" + } else { + upgrade.NewVersion = newerVersion + } + } + notes, err := u.loadReleaseNotes(fmt.Sprintf("%s/%s/%s/release/1panel-%s-release-notes", global.CONF.System.RepoUrl, global.CONF.System.Mode, upgrade.LatestVersion, upgrade.LatestVersion)) if err != nil { - return nil, err + return nil, fmt.Errorf("load relase-notes of version %s failed, err: %v", latestVersion, err) } - isNew := common.CompareVersion(string(version), currentVersion.Value) - if !isNew { - return nil, err - } - - upgrade.NewVersion = string(version) - - releaseNotes, err := http.Get(fmt.Sprintf("%s/%s/%s/release/1panel-%s-release-notes", global.CONF.System.RepoUrl, global.CONF.System.Mode, upgrade.NewVersion, upgrade.NewVersion)) - if err != nil { - return nil, err - } - defer releaseNotes.Body.Close() - release, err := ioutil.ReadAll(releaseNotes.Body) - if err != nil { - return nil, err - } - upgrade.ReleaseNote = string(release) - + upgrade.ReleaseNote = notes return &upgrade, nil } +func (u *UpgradeService) LoadNotes(req dto.Upgrade) (string, error) { + notes, err := u.loadReleaseNotes(fmt.Sprintf("%s/%s/%s/release/1panel-%s-release-notes", global.CONF.System.RepoUrl, global.CONF.System.Mode, req.Version, req.Version)) + if err != nil { + return "", fmt.Errorf("load relase-notes of version %s failed, err: %v", req.Version, err) + } + return notes, nil +} + func (u *UpgradeService) Upgrade(req dto.Upgrade) error { global.LOG.Info("start to upgrade now...") fileOp := files.NewFileOp() @@ -174,5 +186,49 @@ func (u *UpgradeService) handleRollback(fileOp files.FileOp, originalDir string, if err := cpBinary(originalDir+"/1panel.service", "/etc/systemd/system/1panel.service"); err != nil { global.LOG.Errorf("rollback 1panel failed, err: %v", err) } - +} + +func (u *UpgradeService) loadVersion(isLatest bool, currentVersion string) (string, error) { + path := fmt.Sprintf("%s/%s/latest", global.CONF.System.RepoUrl, global.CONF.System.Mode) + if !isLatest { + path = fmt.Sprintf("%s/%s/latest.current", global.CONF.System.RepoUrl, global.CONF.System.Mode) + } + latestVersionRes, err := http.Get(path) + if err != nil { + return "", err + } + defer latestVersionRes.Body.Close() + version, err := ioutil.ReadAll(latestVersionRes.Body) + if err != nil { + return "", err + } + if isLatest { + return string(version), nil + } + + versionMap := make(map[string]string) + if err := json.Unmarshal(version, &versionMap); err != nil { + return "", fmt.Errorf("load version map failed, err: %v", err) + } + + if len(currentVersion) < 4 { + return "", fmt.Errorf("current version is error format: %s", currentVersion) + } + if version, ok := versionMap[currentVersion[0:4]]; ok { + return version, nil + } + return "", errors.New("load version failed in latest.current") +} + +func (u *UpgradeService) loadReleaseNotes(path string) (string, error) { + releaseNotes, err := http.Get(path) + if err != nil { + return "", err + } + defer releaseNotes.Body.Close() + release, err := ioutil.ReadAll(releaseNotes.Body) + if err != nil { + return "", err + } + return string(release), nil } diff --git a/backend/router/ro_setting.go b/backend/router/ro_setting.go index 544e21b1f..1493acca1 100644 --- a/backend/router/ro_setting.go +++ b/backend/router/ro_setting.go @@ -51,6 +51,7 @@ func (s *SettingRouter) InitSettingRouter(Router *gin.RouterGroup) { settingRouter.POST("/backup/record/del", baseApi.DeleteBackupRecord) settingRouter.POST("/upgrade", baseApi.Upgrade) + settingRouter.POST("/upgrade/notes", baseApi.GetNotesByVersion) settingRouter.GET("/upgrade", baseApi.GetUpgradeInfo) settingRouter.GET("/basedir", baseApi.LoadBaseDir) } diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 8629d7668..332eb766c 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -6853,17 +6853,28 @@ const docTemplate = `{ "ApiKeyAuth": [] } ], - "description": "系统更新信息", + "description": "获取版本 release notes", + "consumes": [ + "application/json" + ], "tags": [ "System Setting" ], - "summary": "Load upgrade info", + "summary": "Load release notes by version", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.Upgrade" + } + } + ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.UpgradeInfo" - } + "description": "" } } }, @@ -10711,6 +10722,9 @@ const docTemplate = `{ "dto.UpgradeInfo": { "type": "object", "properties": { + "latestVersion": { + "type": "string" + }, "newVersion": { "type": "string" }, diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 9856175d6..86c00a5b0 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -6846,17 +6846,28 @@ "ApiKeyAuth": [] } ], - "description": "系统更新信息", + "description": "获取版本 release notes", + "consumes": [ + "application/json" + ], "tags": [ "System Setting" ], - "summary": "Load upgrade info", + "summary": "Load release notes by version", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.Upgrade" + } + } + ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.UpgradeInfo" - } + "description": "" } } }, @@ -10704,6 +10715,9 @@ "dto.UpgradeInfo": { "type": "object", "properties": { + "latestVersion": { + "type": "string" + }, "newVersion": { "type": "string" }, diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 571488aac..4203c329f 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -1401,6 +1401,8 @@ definitions: type: object dto.UpgradeInfo: properties: + latestVersion: + type: string newVersion: type: string releaseNote: @@ -6983,15 +6985,22 @@ paths: paramKeys: [] /settings/upgrade: get: - description: 系统更新信息 + consumes: + - application/json + description: 获取版本 release notes + parameters: + - description: request + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.Upgrade' responses: "200": - description: OK - schema: - $ref: '#/definitions/dto.UpgradeInfo' + description: "" security: - ApiKeyAuth: [] - summary: Load upgrade info + summary: Load release notes by version tags: - System Setting post: diff --git a/frontend/src/api/interface/setting.ts b/frontend/src/api/interface/setting.ts index 20a1f0aa4..b411c34c3 100644 --- a/frontend/src/api/interface/setting.ts +++ b/frontend/src/api/interface/setting.ts @@ -82,6 +82,7 @@ export namespace Setting { } export interface UpgradeInfo { newVersion: string; + latestVersion: string; releaseNote: string; } } diff --git a/frontend/src/api/modules/setting.ts b/frontend/src/api/modules/setting.ts index a2c2b321f..7f902eb81 100644 --- a/frontend/src/api/modules/setting.ts +++ b/frontend/src/api/modules/setting.ts @@ -139,6 +139,9 @@ export const searchSnapshotPage = (param: SearchWithPage) => { export const loadUpgradeInfo = () => { return http.get(`/settings/upgrade`); }; +export const loadReleaseNotes = (version: string) => { + return http.post(`/settings/upgrade/notes`, { version: version }); +}; export const upgrade = (version: string) => { return http.post(`/settings/upgrade`, { version: version }); }; diff --git a/frontend/src/components/system-upgrade/index.vue b/frontend/src/components/system-upgrade/index.vue index 52f2fad90..a0ca16e7b 100644 --- a/frontend/src/components/system-upgrade/index.vue +++ b/frontend/src/components/system-upgrade/index.vue @@ -1,7 +1,7 @@
+ + {{ $t('setting.versionHelper') }} +
  • {{ $t('setting.versionHelper1') }}
  • +
  • {{ $t('setting.versionHelper2') }}
  • +
    -

    {{ $t('setting.newVersion') }}

    - {{ upgradeInfo.newVersion }} +

    {{ $t('app.version') }}

    + + + {{ upgradeInfo.newVersion }} {{ $t('setting.newVersion') }} + + + {{ upgradeInfo.latestVersion }} {{ $t('setting.latestVersion') }} + +