实现自动下载升级版本

This commit is contained in:
GoEdgeLab
2023-06-13 20:52:37 +08:00
parent 05100d0ac4
commit 62d6e5bc17
13 changed files with 380 additions and 40 deletions

View File

@@ -119,7 +119,7 @@ func (this *CheckUpdatesTask) Loop() error {
var vMap = maps.NewMap(version) var vMap = maps.NewMap(version)
if vMap.GetString("code") == "admin" { if vMap.GetString("code") == "admin" {
var latestVersion = vMap.GetString("version") var latestVersion = vMap.GetString("version")
if stringutil.VersionCompare(teaconst.Version, latestVersion) < 0 { if stringutil.VersionCompare(teaconst.Version, latestVersion) < 0 && (len(config.IgnoredVersion) == 0 || stringutil.VersionCompare(latestVersion, config.IgnoredVersion) > 0) {
teaconst.NewVersionCode = latestVersion teaconst.NewVersionCode = latestVersion
teaconst.NewVersionDownloadURL = dlHost + vMap.GetString("url") teaconst.NewVersionDownloadURL = dlHost + vMap.GetString("url")
return nil return nil

View File

@@ -0,0 +1,61 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package updates
import (
"encoding/json"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
)
type IgnoreVersionAction struct {
actionutils.ParentAction
}
func (this *IgnoreVersionAction) RunPost(params struct {
Version string
}) {
defer this.CreateLogInfo("忽略升级版本 %s", params.Version)
if len(params.Version) == 0 {
this.Fail("请输入要忽略的版本号")
return
}
valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates})
if err != nil {
this.ErrorPage(err)
return
}
var valueJSON = valueResp.ValueJSON
var config = systemconfigs.NewCheckUpdatesConfig()
if len(valueJSON) > 0 {
err = json.Unmarshal(valueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
config.IgnoredVersion = params.Version
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeCheckUpdates,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
// 清除状态
teaconst.NewVersionCode = ""
this.Success()
}

View File

@@ -4,12 +4,12 @@ package updates
import ( import (
"encoding/json" "encoding/json"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
"io" "io"
"net/http" "net/http"
@@ -25,8 +25,18 @@ func (this *IndexAction) Init() {
this.Nav("", "updates", "") this.Nav("", "updates", "")
} }
func (this *IndexAction) RunGet(params struct{}) { func (this *IndexAction) RunGet(params struct {
DoCheck bool
}) {
this.Data["version"] = teaconst.Version this.Data["version"] = teaconst.Version
this.Data["doCheck"] = params.DoCheck
// 是否正在升级
this.Data["isUpgrading"] = isUpgrading
this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress * 100)
if isUpgrading {
this.Data["doCheck"] = false
}
valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates}) valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates})
if err != nil { if err != nil {
@@ -34,7 +44,7 @@ func (this *IndexAction) RunGet(params struct{}) {
return return
} }
var valueJSON = valueResp.ValueJSON var valueJSON = valueResp.ValueJSON
var config = &systemconfigs.CheckUpdatesConfig{AutoCheck: false} var config = systemconfigs.NewCheckUpdatesConfig()
if len(valueJSON) > 0 { if len(valueJSON) > 0 {
err = json.Unmarshal(valueJSON, config) err = json.Unmarshal(valueJSON, config)
if err != nil { if err != nil {
@@ -49,6 +59,21 @@ func (this *IndexAction) RunGet(params struct{}) {
func (this *IndexAction) RunPost(params struct { func (this *IndexAction) RunPost(params struct {
}) { }) {
valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates})
if err != nil {
this.ErrorPage(err)
return
}
var valueJSON = valueResp.ValueJSON
var config = systemconfigs.NewCheckUpdatesConfig()
if len(valueJSON) > 0 {
err = json.Unmarshal(valueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
type Response struct { type Response struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
@@ -66,6 +91,7 @@ func (this *IndexAction) RunPost(params struct {
"message": "读取更新信息失败:" + err.Error(), "message": "读取更新信息失败:" + err.Error(),
} }
this.Success() this.Success()
return
} }
defer func() { defer func() {
@@ -78,6 +104,7 @@ func (this *IndexAction) RunPost(params struct {
"message": "读取更新信息失败:" + err.Error(), "message": "读取更新信息失败:" + err.Error(),
} }
this.Success() this.Success()
return
} }
var apiResponse = &Response{} var apiResponse = &Response{}
@@ -88,6 +115,7 @@ func (this *IndexAction) RunPost(params struct {
"message": "解析更新信息失败:" + err.Error(), "message": "解析更新信息失败:" + err.Error(),
} }
this.Success() this.Success()
return
} }
if apiResponse.Code != 200 { if apiResponse.Code != 200 {
@@ -96,6 +124,7 @@ func (this *IndexAction) RunPost(params struct {
"message": "解析更新信息失败:" + apiResponse.Message, "message": "解析更新信息失败:" + apiResponse.Message,
} }
this.Success() this.Success()
return
} }
var m = maps.NewMap(apiResponse.Data) var m = maps.NewMap(apiResponse.Data)
@@ -107,19 +136,30 @@ func (this *IndexAction) RunPost(params struct {
if vMap.GetString("code") == "admin" { if vMap.GetString("code") == "admin" {
var latestVersion = vMap.GetString("version") var latestVersion = vMap.GetString("version")
if stringutil.VersionCompare(teaconst.Version, latestVersion) < 0 { if stringutil.VersionCompare(teaconst.Version, latestVersion) < 0 {
// 是否已忽略
if len(config.IgnoredVersion) > 0 && stringutil.VersionCompare(config.IgnoredVersion, latestVersion) >= 0 {
continue
}
this.Data["result"] = maps.Map{ this.Data["result"] = maps.Map{
"isOk": true, "isOk": true,
"message": "有最新的版本v" + types.String(latestVersion) + "可以更新", "version": latestVersion,
"message": "有最新的版本 v" + latestVersion + " 可以更新",
"hasNew": true, "hasNew": true,
"dlURL": dlHost + vMap.GetString("url"), "dlURL": dlHost + vMap.GetString("url"),
"day": vMap.GetString("day"),
"description": vMap.GetString("description"),
"docURL": vMap.GetString("docURL"),
} }
this.Success() this.Success()
return
} else { } else {
this.Data["result"] = maps.Map{ this.Data["result"] = maps.Map{
"isOk": true, "isOk": true,
"message": "你已安装最新版本,无需更新", "message": "你已安装最新版本,无需更新",
} }
this.Success() this.Success()
return
} }
} }
} }
@@ -127,7 +167,7 @@ func (this *IndexAction) RunPost(params struct {
this.Data["result"] = maps.Map{ this.Data["result"] = maps.Map{
"isOk": false, "isOk": false,
"message": "找不到更新信息", "message": "没有发现更新的版本",
} }
this.Success() this.Success()

View File

@@ -15,6 +15,9 @@ func init() {
Prefix("/settings/updates"). Prefix("/settings/updates").
GetPost("", new(IndexAction)). GetPost("", new(IndexAction)).
Post("/update", new(UpdateAction)). Post("/update", new(UpdateAction)).
Post("/ignoreVersion", new(IgnoreVersionAction)).
Post("/resetIgnoredVersion", new(ResetIgnoredVersionAction)).
GetPost("/upgrade", new(UpgradeAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -0,0 +1,50 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package updates
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
)
type ResetIgnoredVersionAction struct {
actionutils.ParentAction
}
func (this *ResetIgnoredVersionAction) RunPost(params struct{}) {
defer this.CreateLogInfo("重置忽略升级版本")
valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates})
if err != nil {
this.ErrorPage(err)
return
}
var valueJSON = valueResp.ValueJSON
var config = systemconfigs.NewCheckUpdatesConfig()
if len(valueJSON) > 0 {
err = json.Unmarshal(valueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
config.IgnoredVersion = ""
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeCheckUpdates,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -17,13 +17,16 @@ type UpdateAction struct {
func (this *UpdateAction) RunPost(params struct { func (this *UpdateAction) RunPost(params struct {
AutoCheck bool AutoCheck bool
}) { }) {
defer this.CreateLogInfo("修改检查更新设置")
// 读取当前设置
valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates}) valueResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeCheckUpdates})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
return return
} }
var valueJSON = valueResp.ValueJSON var valueJSON = valueResp.ValueJSON
var config = &systemconfigs.CheckUpdatesConfig{AutoCheck: false} var config = systemconfigs.NewCheckUpdatesConfig()
if len(valueJSON) > 0 { if len(valueJSON) > 0 {
err = json.Unmarshal(valueJSON, config) err = json.Unmarshal(valueJSON, config)
if err != nil { if err != nil {
@@ -40,6 +43,7 @@ func (this *UpdateAction) RunPost(params struct {
return return
} }
// 修改设置
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{ _, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeCheckUpdates, Code: systemconfigs.SettingCodeCheckUpdates,
ValueJSON: configJSON, ValueJSON: configJSON,

View File

@@ -0,0 +1,73 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package updates
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"os"
"os/exec"
"time"
)
var upgradeProgress float32
var isUpgrading = false
type UpgradeAction struct {
actionutils.ParentAction
}
func (this *UpgradeAction) RunGet(params struct {
}) {
this.Data["isUpgrading"] = isUpgrading
this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress*100)
this.Success()
}
func (this *UpgradeAction) RunPost(params struct {
Url string
}) {
if isUpgrading {
this.Success()
return
}
isUpgrading = true
upgradeProgress = 0
defer func() {
isUpgrading = false
}()
var manager = utils.NewUpgradeManager("admin", params.Url)
var ticker = time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
if manager.IsDownloading() {
var progress = manager.Progress()
if progress >= 0 {
upgradeProgress = progress
}
} else {
return
}
}
}()
err := manager.Start()
if err != nil {
this.Fail("下载失败:" + err.Error())
return
}
// restart
exe, _ := os.Executable()
if len(exe) > 0 {
go func() {
var cmd = exec.Command(exe, "restart")
_ = cmd.Run()
}()
}
this.Success()
}

View File

@@ -18,7 +18,7 @@
<div class="ui icon message error" v-if="!isLoading && newVersionCode.length > 0"> <div class="ui icon message error" v-if="!isLoading && newVersionCode.length > 0">
<i class="icon warning circle"></i> <i class="icon warning circle"></i>
升级提醒有新版本管理系统可以更新v{{currentVersionCode}} -&gt; v{{newVersionCode}} &nbsp; &nbsp; 升级提醒有新版本管理系统可以更新v{{currentVersionCode}} -&gt; v{{newVersionCode}} &nbsp; &nbsp;
<a href="https://goedge.cn/docs/Releases/Index.md?nav=1" target="_blank">[去官网查看]</a> &nbsp; &nbsp; <a :href="newVersionDownloadURL" target="_blank">[直接下载]</a> <a href="/settings/updates?doCheck=1">[查看详情]</a>
<a href="" title="关闭" @click.prevent="closeMessage"><i class="ui icon remove small"></i></a> <a href="" title="关闭" @click.prevent="closeMessage"><i class="ui icon remove small"></i></a>
</div> </div>

View File

@@ -0,0 +1,5 @@
.version-box {
line-height: 1.8;
color: #21ba45;
}
/*# sourceMappingURL=index.css.map */

View File

@@ -0,0 +1 @@
{"version":3,"sources":["index.less"],"names":[],"mappings":"AAAA;EACC,gBAAA;EACA,cAAA","file":"index.css"}

View File

@@ -1,12 +1,24 @@
{$layout} {$layout}
<div class="ui margin"></div> <div class="ui margin"></div>
<div v-show="isUpgrading">
<p class="ui message warning">正在下载升级到新版本,请耐心等待 {{upgradeProgress}}%...</p>
</div>
<div v-show="!isUpgrading">
<form class="ui form"> <form class="ui form">
<table class="ui table definition selectable"> <table class="ui table definition selectable">
<tr> <tr>
<td class="title">当前已安装版本</td> <td class="title">当前已安装版本</td>
<td>v{{version}}</td> <td>v{{version}}</td>
</tr> </tr>
<tr v-if="config.ignoredVersion.length > 0">
<td>已忽略版本</td>
<td>
v{{config.ignoredVersion}} &nbsp; <a href="" style="font-size: 0.8em" @click.prevent="resetIgnoredVersion()">[重置]</a>
</td>
</tr>
<tr> <tr>
<td>自动检查</td> <td>自动检查</td>
<td> <td>
@@ -21,12 +33,36 @@
<span class="blue">正在连接服务器检查更新...</span> <span class="blue">正在连接服务器检查更新...</span>
</div> </div>
<div v-if="!isChecking"> <div v-if="!isChecking">
<span class="green" v-if="result.isOk">{{result.message}}<span v-if="result.hasNew"><br/><a :href="result.dlURL">下载地址:{{result.dlURL}}</a> </span></span> <div class="version-box" v-if="result.isOk">
<span class="green">{{result.message}}
<span v-if="result.hasNew"><br/><a :href="result.dlURL">下载地址:{{result.dlURL}}</a> </span>
</span>
<div v-if="result.hasNew">
<div class="ui divider"></div>
<!-- 操作按钮 -->
<button class="ui button tiny" type="button" @click.prevent="install(result.dlURL)">安装此版本</button>
&nbsp; &nbsp;
<button class="ui button tiny basic" type="button" @click.prevent="ignoreVersion(result.version)">忽略此版本</button>
<div class="ui divider"></div>
<div v-if="result.day != null && result.day.length > 0">发布日期:{{result.day}}</div>
<div v-if="result.description != null && result.description.length > 0">
版本介绍:<pre>{{result.description}}</pre>
<div v-if="result.docURL != null && result.docURL.length > 0">
<div class="ui divider"></div>
<div>完整变更说明:<a :href="result.docURL" target="_blank">{{result.docURL}}</a> </div>
</div>
</div>
</div>
</div>
<span class="red" v-if="!result.isOk">{{result.message}}</span> <span class="red" v-if="!result.isOk">{{result.message}}</span>
</div> </div>
</td> </td>
</tr> </tr>
</table> </table>
<button class="ui button primary" type="button" @click.prevent="start">开始检查</button> <button class="ui button primary" type="button" @click.prevent="start" v-show="!isChecking">开始检查</button>
<button class="ui button disabled" type="button" v-show="isChecking">正在检查...</button>
</form> </form>
</div>

View File

@@ -1,8 +1,14 @@
Tea.context(function () { Tea.context(function () {
this.isStarted = false this.isStarted = false
this.isChecking = true this.isChecking = false
this.result = {isOk: false, message: "", hasNew: false, dlURL: ""} this.result = {isOk: false, message: "", hasNew: false, dlURL: ""}
this.$delay(function () {
if (this.doCheck) {
this.start()
}
})
this.start = function () { this.start = function () {
this.isStarted = true this.isStarted = true
this.isChecking = true this.isChecking = true
@@ -28,4 +34,61 @@ Tea.context(function () {
autoCheck: this.config.autoCheck ? 1 : 0 autoCheck: this.config.autoCheck ? 1 : 0
}) })
} }
this.ignoreVersion = function (version) {
teaweb.confirm("确定要忽略版本 v" + version + " 版本更新吗?", function () {
this.$post(".ignoreVersion")
.params({version: version})
.success(function () {
teaweb.reload()
})
})
}
this.resetIgnoredVersion = function (version) {
teaweb.confirm("确定要重置已忽略版本吗?", function () {
this.$post(".resetIgnoredVersion")
.success(function () {
teaweb.reload()
})
})
}
this.install = function (dlURL) {
this.$post(".upgrade")
.params({
url: dlURL
})
.timeout(3600)
.success(function () {
teaweb.success("下载覆盖成功,系统将会尝试自动重启,请刷新页面查看重启状态。如果没能重启成功,请手动使用命令重启。", function () {
teaweb.reload()
})
})
this.isUpgrading = true
this.updateUpgradeProgress()
}
if (this.isUpgrading) {
this.$delay(function () {
this.updateUpgradeProgress()
})
}
this.updateUpgradeProgress = function () {
if (!this.isUpgrading) {
return
}
this.$get(".upgrade")
.success(function (resp) {
this.upgradeProgress = resp.data.upgradeProgress
this.isUpgrading = resp.data.isUpgrading
})
.done(function () {
this.$delay(function () {
this.updateUpgradeProgress()
}, 3000)
})
}
}) })

View File

@@ -0,0 +1,4 @@
.version-box {
line-height: 1.8;
color: #21ba45;
}