mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-02 20:00:26 +08:00
在线升级时自动执行本地安装的edge-api upgrade
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user