在线升级时自动执行本地安装的edge-api upgrade

This commit is contained in:
GoEdgeLab
2024-05-03 11:04:19 +08:00
parent 89d70ce121
commit 7aa43dd485
4 changed files with 111 additions and 7 deletions

View File

@@ -33,6 +33,7 @@ func (this *IndexAction) RunGet(params struct {
// 是否正在升级
this.Data["isUpgrading"] = isUpgrading
this.Data["isUpgradingDB"] = isUpgradingDB
this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress * 100)
if isUpgrading {
this.Data["doCheck"] = false

View File

@@ -3,9 +3,15 @@
package updates
import (
"bytes"
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
executils "github.com/TeaOSLab/EdgeAdmin/internal/utils/exec"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"os"
"os/exec"
"time"
@@ -13,6 +19,7 @@ import (
var upgradeProgress float32
var isUpgrading = false
var isUpgradingDB = false
type UpgradeAction struct {
actionutils.ParentAction
@@ -21,6 +28,7 @@ type UpgradeAction struct {
func (this *UpgradeAction) RunGet(params struct {
}) {
this.Data["isUpgrading"] = isUpgrading
this.Data["isUpgradingDB"] = isUpgradingDB
this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress*100)
this.Success()
}
@@ -60,6 +68,22 @@ func (this *UpgradeAction) RunPost(params struct {
return
}
// try to exec local 'edge-api upgrade'
exePath, ok := this.checkLocalAPINode()
if ok && len(exePath) > 0 {
isUpgradingDB = true
var before = time.Now()
var cmd = executils.NewCmd(exePath, "upgrade")
_ = cmd.Run()
var costSeconds = time.Since(before).Seconds()
// sleep to show upgrading status
if costSeconds < 3 {
time.Sleep(3 * time.Second)
}
isUpgradingDB = false
}
// restart
exe, _ := os.Executable()
if len(exe) > 0 {
@@ -71,3 +95,69 @@ func (this *UpgradeAction) RunPost(params struct {
this.Success()
}
func (this *UpgradeAction) checkLocalAPINode() (exePath string, ok bool) {
resp, err := this.RPC().APINodeRPC().FindCurrentAPINode(this.AdminContext(), &pb.FindCurrentAPINodeRequest{})
if err != nil {
return
}
if resp.ApiNode == nil {
return
}
var instanceCode = resp.ApiNode.InstanceCode
if len(instanceCode) == 0 {
return
}
var statusJSON = resp.ApiNode.StatusJSON
if len(statusJSON) == 0 {
return
}
var status = &nodeconfigs.NodeStatus{}
err = json.Unmarshal(statusJSON, status)
if err != nil {
return
}
exePath = status.ExePath
if len(exePath) == 0 {
return
}
stat, err := os.Stat(exePath)
if err != nil {
return
}
if stat.IsDir() {
return
}
// 实例信息
{
var outputBuffer = &bytes.Buffer{}
var cmd = exec.Command(exePath, "instance")
cmd.Stdout = outputBuffer
err = cmd.Run()
if err != nil {
return
}
var outputBytes = outputBuffer.Bytes()
if len(outputBytes) == 0 {
return
}
var instanceMap = maps.Map{}
err = json.Unmarshal(bytes.TrimSpace(outputBytes), &instanceMap)
if err != nil {
return
}
if instanceMap.GetString("code") != instanceCode {
return
}
}
ok = true
return
}

View File

@@ -2,11 +2,13 @@
<div class="ui margin"></div>
<div v-show="isUpgrading">
<p class="ui message warning">正在下载并升级到新版本,请耐心等待 {{upgradeProgress}}%...</p>
<div v-show="isUpgrading || isUpgraded">
<p class="ui message warning" v-if="!isUpgradingDB && !isUpgraded">正在下载并升级到新版本,请耐心等待 {{upgradeProgress}}% ...</p>
<p class="ui message warning" v-if="isUpgradingDB">正在升级数据库 ...</p>
<p class="ui message warning" v-if="!isUpgradingDB && isUpgraded">升级完成!</p>
</div>
<div v-show="!isUpgrading">
<div v-show="!isUpgrading && !isUpgraded">
<form class="ui form">
<table class="ui table definition selectable">
<tr>

View File

@@ -2,6 +2,7 @@ Tea.context(function () {
this.isStarted = false
this.isChecking = false
this.result = {isOk: false, message: "", hasNew: false, dlURL: ""}
this.isUpgraded = false
this.$delay(function () {
if (this.doCheck) {
@@ -64,9 +65,15 @@ Tea.context(function () {
})
.timeout(3600)
.success(function () {
teaweb.success("下载覆盖成功,系统将会尝试自动重启,请刷新页面查看重启状态。如果没能重启成功,请手动使用命令重启。", function () {
teaweb.reload()
})
this.$delay(function () {
let msg = "下载覆盖成功"
if (this.isUpgraded) {
msg = "升级成功"
}
teaweb.success(msg + ",当前管理系统将会尝试自动重启,请刷新页面查看重启状态。如果长时间没能重启成功,请使用命令手动重启。", function () {
teaweb.reload()
})
}, 3000)
})
this.isUpgrading = true
@@ -87,11 +94,15 @@ Tea.context(function () {
.success(function (resp) {
this.upgradeProgress = resp.data.upgradeProgress
this.isUpgrading = resp.data.isUpgrading
this.isUpgradingDB = resp.data.isUpgradingDB
if (resp.data.isUpgradingDB) {
this.isUpgraded = true
}
})
.done(function () {
this.$delay(function () {
this.updateUpgradeProgress()
}, 3000)
}, 2000)
})
}
})