2023-06-13 20:52:37 +08:00
|
|
|
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
package updates
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-03 11:04:19 +08:00
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2023-06-13 20:52:37 +08:00
|
|
|
"fmt"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
2024-05-03 11:04:19 +08:00
|
|
|
executils "github.com/TeaOSLab/EdgeAdmin/internal/utils/exec"
|
2023-06-13 20:52:37 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2024-05-03 11:04:19 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2023-06-13 20:52:37 +08:00
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var upgradeProgress float32
|
|
|
|
|
var isUpgrading = false
|
2024-05-03 11:04:19 +08:00
|
|
|
var isUpgradingDB = false
|
2023-06-13 20:52:37 +08:00
|
|
|
|
|
|
|
|
type UpgradeAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *UpgradeAction) RunGet(params struct {
|
|
|
|
|
}) {
|
|
|
|
|
this.Data["isUpgrading"] = isUpgrading
|
2024-05-03 11:04:19 +08:00
|
|
|
this.Data["isUpgradingDB"] = isUpgradingDB
|
2023-06-13 20:52:37 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 11:04:19 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 20:52:37 +08:00
|
|
|
// restart
|
|
|
|
|
exe, _ := os.Executable()
|
|
|
|
|
if len(exe) > 0 {
|
|
|
|
|
go func() {
|
|
|
|
|
var cmd = exec.Command(exe, "restart")
|
|
|
|
|
_ = cmd.Run()
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|
2024-05-03 11:04:19 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|