From daa40a2e92d5c089488b0ca7f4c3fe287739b666 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Fri, 3 May 2024 11:20:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8edge-admin=20upgrade=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=A7=E8=A1=8C=E6=9C=AC=E5=9C=B0=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E7=9A=84edge-api=20upgrade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-admin/main.go | 15 ++++ internal/const/const.go | 6 +- .../settings/updates/updateutils/utils.go | 81 +++++++++++++++++++ .../default/settings/updates/upgrade.go | 74 +---------------- 4 files changed, 100 insertions(+), 76 deletions(-) create mode 100644 internal/web/actions/default/settings/updates/updateutils/utils.go diff --git a/cmd/edge-admin/main.go b/cmd/edge-admin/main.go index 20bcaf60..377e8ea7 100644 --- a/cmd/edge-admin/main.go +++ b/cmd/edge-admin/main.go @@ -9,8 +9,11 @@ import ( teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" "github.com/TeaOSLab/EdgeAdmin/internal/gen" "github.com/TeaOSLab/EdgeAdmin/internal/nodes" + "github.com/TeaOSLab/EdgeAdmin/internal/rpc" "github.com/TeaOSLab/EdgeAdmin/internal/utils" + executils "github.com/TeaOSLab/EdgeAdmin/internal/utils/exec" _ "github.com/TeaOSLab/EdgeAdmin/internal/web" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/updates/updateutils" _ "github.com/TeaOSLab/EdgeCommon/pkg/langs/messages" "github.com/iwind/TeaGo/Tea" _ "github.com/iwind/TeaGo/bootstrap" @@ -176,6 +179,18 @@ func main() { log.Println("upgrade failed: " + err.Error()) return } + + // try to exec local 'edge-api upgrade' + rpcClient, err := rpc.SharedRPC() + if err == nil { + exePath, ok := updateutils.CheckLocalAPINode(rpcClient, rpcClient.Context(0)) + if ok && len(exePath) > 0 { + log.Println("upgrading database ...") + var cmd = executils.NewCmd(exePath, "upgrade") + _ = cmd.Run() + } + } + log.Println("finished!") log.Println("restarting ...") app.RunRestart() diff --git a/internal/const/const.go b/internal/const/const.go index f21549c3..8ee9ff60 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -5,13 +5,11 @@ const ( APINodeVersion = "1.3.8" - ProductName = "Edge Admin" - ProcessName = "edge-admin" - ProductNameZH = "Edge" + ProductName = "Edge Admin" + ProcessName = "edge-admin" Role = "admin" - EncryptKey = "8f983f4d69b83aaa0d74b21a212f6967" EncryptMethod = "aes-256-cfb" CookieSID = "geadsid" diff --git a/internal/web/actions/default/settings/updates/updateutils/utils.go b/internal/web/actions/default/settings/updates/updateutils/utils.go new file mode 100644 index 00000000..b66549fc --- /dev/null +++ b/internal/web/actions/default/settings/updates/updateutils/utils.go @@ -0,0 +1,81 @@ +// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package updateutils + +import ( + "bytes" + "context" + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/rpc" + "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/maps" + "os" + "os/exec" +) + +func CheckLocalAPINode(rpcClient *rpc.RPCClient, ctx context.Context) (exePath string, ok bool) { + resp, err := rpcClient.APINodeRPC().FindCurrentAPINode(ctx, &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/internal/web/actions/default/settings/updates/upgrade.go b/internal/web/actions/default/settings/updates/upgrade.go index 327e8dcb..c14b3aa3 100644 --- a/internal/web/actions/default/settings/updates/upgrade.go +++ b/internal/web/actions/default/settings/updates/upgrade.go @@ -3,15 +3,11 @@ 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" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/updates/updateutils" "os" "os/exec" "time" @@ -69,7 +65,7 @@ func (this *UpgradeAction) RunPost(params struct { } // try to exec local 'edge-api upgrade' - exePath, ok := this.checkLocalAPINode() + exePath, ok := updateutils.CheckLocalAPINode(this.RPC(), this.AdminContext()) if ok && len(exePath) > 0 { isUpgradingDB = true var before = time.Now() @@ -95,69 +91,3 @@ 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 -}