mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +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["isUpgrading"] = isUpgrading
 | 
				
			||||||
 | 
						this.Data["isUpgradingDB"] = isUpgradingDB
 | 
				
			||||||
	this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress * 100)
 | 
						this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress * 100)
 | 
				
			||||||
	if isUpgrading {
 | 
						if isUpgrading {
 | 
				
			||||||
		this.Data["doCheck"] = false
 | 
							this.Data["doCheck"] = false
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,9 +3,15 @@
 | 
				
			|||||||
package updates
 | 
					package updates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils"
 | 
						"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/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"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -13,6 +19,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var upgradeProgress float32
 | 
					var upgradeProgress float32
 | 
				
			||||||
var isUpgrading = false
 | 
					var isUpgrading = false
 | 
				
			||||||
 | 
					var isUpgradingDB = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type UpgradeAction struct {
 | 
					type UpgradeAction struct {
 | 
				
			||||||
	actionutils.ParentAction
 | 
						actionutils.ParentAction
 | 
				
			||||||
@@ -21,6 +28,7 @@ type UpgradeAction struct {
 | 
				
			|||||||
func (this *UpgradeAction) RunGet(params struct {
 | 
					func (this *UpgradeAction) RunGet(params struct {
 | 
				
			||||||
}) {
 | 
					}) {
 | 
				
			||||||
	this.Data["isUpgrading"] = isUpgrading
 | 
						this.Data["isUpgrading"] = isUpgrading
 | 
				
			||||||
 | 
						this.Data["isUpgradingDB"] = isUpgradingDB
 | 
				
			||||||
	this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress*100)
 | 
						this.Data["upgradeProgress"] = fmt.Sprintf("%.2f", upgradeProgress*100)
 | 
				
			||||||
	this.Success()
 | 
						this.Success()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -60,6 +68,22 @@ func (this *UpgradeAction) RunPost(params struct {
 | 
				
			|||||||
		return
 | 
							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
 | 
						// restart
 | 
				
			||||||
	exe, _ := os.Executable()
 | 
						exe, _ := os.Executable()
 | 
				
			||||||
	if len(exe) > 0 {
 | 
						if len(exe) > 0 {
 | 
				
			||||||
@@ -71,3 +95,69 @@ func (this *UpgradeAction) RunPost(params struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	this.Success()
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,11 +2,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
<div class="ui margin"></div>
 | 
					<div class="ui margin"></div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<div v-show="isUpgrading">
 | 
					<div v-show="isUpgrading || isUpgraded">
 | 
				
			||||||
    <p class="ui message warning">正在下载并升级到新版本,请耐心等待 {{upgradeProgress}}%...</p>
 | 
					    <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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<div v-show="!isUpgrading">
 | 
					<div v-show="!isUpgrading && !isUpgraded">
 | 
				
			||||||
    <form class="ui form">
 | 
					    <form class="ui form">
 | 
				
			||||||
        <table class="ui table definition selectable">
 | 
					        <table class="ui table definition selectable">
 | 
				
			||||||
            <tr>
 | 
					            <tr>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ Tea.context(function () {
 | 
				
			|||||||
	this.isStarted = false
 | 
						this.isStarted = false
 | 
				
			||||||
	this.isChecking = false
 | 
						this.isChecking = false
 | 
				
			||||||
	this.result = {isOk: false, message: "", hasNew: false, dlURL: ""}
 | 
						this.result = {isOk: false, message: "", hasNew: false, dlURL: ""}
 | 
				
			||||||
 | 
						this.isUpgraded = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.$delay(function () {
 | 
						this.$delay(function () {
 | 
				
			||||||
		if (this.doCheck) {
 | 
							if (this.doCheck) {
 | 
				
			||||||
@@ -64,9 +65,15 @@ Tea.context(function () {
 | 
				
			|||||||
			})
 | 
								})
 | 
				
			||||||
			.timeout(3600)
 | 
								.timeout(3600)
 | 
				
			||||||
			.success(function () {
 | 
								.success(function () {
 | 
				
			||||||
				teaweb.success("下载覆盖成功,系统将会尝试自动重启,请刷新页面查看重启状态。如果没能重启成功,请手动使用命令重启。", function () {
 | 
									this.$delay(function () {
 | 
				
			||||||
 | 
										let msg = "下载覆盖成功"
 | 
				
			||||||
 | 
										if (this.isUpgraded) {
 | 
				
			||||||
 | 
											msg = "升级成功"
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										teaweb.success(msg + ",当前管理系统将会尝试自动重启,请刷新页面查看重启状态。如果长时间没能重启成功,请使用命令手动重启。", function () {
 | 
				
			||||||
						teaweb.reload()
 | 
											teaweb.reload()
 | 
				
			||||||
					})
 | 
										})
 | 
				
			||||||
 | 
									}, 3000)
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.isUpgrading = true
 | 
							this.isUpgrading = true
 | 
				
			||||||
@@ -87,11 +94,15 @@ Tea.context(function () {
 | 
				
			|||||||
			.success(function (resp) {
 | 
								.success(function (resp) {
 | 
				
			||||||
				this.upgradeProgress = resp.data.upgradeProgress
 | 
									this.upgradeProgress = resp.data.upgradeProgress
 | 
				
			||||||
				this.isUpgrading = resp.data.isUpgrading
 | 
									this.isUpgrading = resp.data.isUpgrading
 | 
				
			||||||
 | 
									this.isUpgradingDB = resp.data.isUpgradingDB
 | 
				
			||||||
 | 
									if (resp.data.isUpgradingDB) {
 | 
				
			||||||
 | 
										this.isUpgraded = true
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
			.done(function () {
 | 
								.done(function () {
 | 
				
			||||||
				this.$delay(function () {
 | 
									this.$delay(function () {
 | 
				
			||||||
					this.updateUpgradeProgress()
 | 
										this.updateUpgradeProgress()
 | 
				
			||||||
				}, 3000)
 | 
									}, 2000)
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
		Reference in New Issue
	
	Block a user