diff --git a/internal/web/actions/default/settings/updates/index.go b/internal/web/actions/default/settings/updates/index.go index 3bf19af0..0aa4ce0c 100644 --- a/internal/web/actions/default/settings/updates/index.go +++ b/internal/web/actions/default/settings/updates/index.go @@ -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 diff --git a/internal/web/actions/default/settings/updates/upgrade.go b/internal/web/actions/default/settings/updates/upgrade.go index 6b0b50f1..327e8dcb 100644 --- a/internal/web/actions/default/settings/updates/upgrade.go +++ b/internal/web/actions/default/settings/updates/upgrade.go @@ -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 +} diff --git a/web/views/@default/settings/updates/index.html b/web/views/@default/settings/updates/index.html index a146fd12..9a48660a 100644 --- a/web/views/@default/settings/updates/index.html +++ b/web/views/@default/settings/updates/index.html @@ -2,11 +2,13 @@
-
-

正在下载并升级到新版本,请耐心等待 {{upgradeProgress}}%...

+
+

正在下载并升级到新版本,请耐心等待 {{upgradeProgress}}% ...

+

正在升级数据库 ...

+

升级完成!

-
+
diff --git a/web/views/@default/settings/updates/index.js b/web/views/@default/settings/updates/index.js index f3ecc9e0..385b3b10 100644 --- a/web/views/@default/settings/updates/index.js +++ b/web/views/@default/settings/updates/index.js @@ -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) }) } }) \ No newline at end of file