Browse Source

支持快速更新功能

zhou.xiaoning 3 năm trước cách đây
mục cha
commit
84e2eeab52
4 tập tin đã thay đổi với 136 bổ sung0 xóa
  1. 22 0
      api/console/console.go
  2. 74 0
      api/console/update.go
  3. 36 0
      api/console/upmtpgo.sh
  4. 4 0
      initialize/router.go

+ 22 - 0
api/console/console.go

@@ -0,0 +1,22 @@
+package console
+
+import (
+	"html/template"
+
+	"github.com/gin-gonic/gin"
+)
+
+func Console(c *gin.Context) {
+	const templateText = `
+<h1>Go接入服务信息</h1>
+<a href="{{.host}}/debug/update" title="点击后从自动构建服务拉取版本升级并重启GO服务, 请谨慎点击" target="_blank">点击升级到最新版本</a>
+<br><br>
+<a href="{{.host}}/swagger/index.html" target="_blank">点击打开swagger</a>
+<br>
+`
+	if tmpl, err := template.New("index").Parse(templateText); err == nil {
+		_ = tmpl.Execute(c.Writer, nil)
+	} else {
+		c.String(400, "index err.")
+	}
+}

+ 74 - 0
api/console/update.go

@@ -0,0 +1,74 @@
+package console
+
+import (
+	_ "embed"
+	"fmt"
+	"net/http"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"regexp"
+	"runtime"
+	"strings"
+
+	"github.com/gin-gonic/gin"
+)
+
+//go:embed upmtpgo.sh
+var strShell string
+
+var IsUpdateing = false
+
+// MakeUpdateScript 生成脚本
+func MakeUpdateScript(c *gin.Context) {
+	if IsUpdateing {
+		c.String(http.StatusOK, "正在升级中...")
+		return
+	}
+
+	if runtime.GOOS == "windows" {
+		c.String(http.StatusOK, "windows不支持, 仅支持linux下执行")
+		return
+	}
+	// logger.GetLogger().Debug("*********************auto update*******************************")
+	IsUpdateing = true
+	defer func() {
+		IsUpdateing = false
+	}()
+
+	filename := c.DefaultQuery("filename", "")
+	if createsh() {
+		c.String(http.StatusOK, "正在执行升级, 升级会保留原有的config.yaml,且重启服务。注意:请勿刷新本页面。")
+		rx := regexp.MustCompile(`mtp20_access_*\.zip`) // mtp20_access_20221009092417.zip
+		names := rx.FindStringSubmatch(filename)
+		// 正则表达式检查文件名是否符合规范
+		if len(names) > 0 && names[0] == filename {
+			fmt.Println("filename:", filename)
+			cmd := exec.Command("sh", "./update.sh", filename)
+			fmt.Println("cmd param:", cmd.Args)
+			cmd.Run()
+		} else {
+			_ = exec.Command("sh", "./update.sh").Run()
+		}
+	} else {
+		c.String(http.StatusBadRequest, "创建脚本失败, 可能是权限不足")
+	}
+}
+
+// 生成update.sh文件
+func createsh() bool {
+	dir := ""
+	if str, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
+		dir = strings.Replace(str, "\\", "/", -1)
+	} else {
+		return false
+	}
+	// 创建文件
+	if f, err := os.OpenFile(dir+"/update.sh", os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666); err == nil {
+		f.Write([]byte(strShell))
+		f.Close()
+	} else {
+		return false
+	}
+	return true
+}

+ 36 - 0
api/console/upmtpgo.sh

@@ -0,0 +1,36 @@
+# note   : 在程序运行目录执行此脚本, 升级go服务到最新构建的版本。升级完成自动重启go服务
+# --------------------------------------------------------------------
+
+function excute() {
+    local arg=$1
+    if [[ "$arg" == mtp2_queryservice_*.zip ]]; then
+      filename=$1
+      updateSpecialVer $filename
+    else
+      # 获取最新版本的文件名
+      rm -f console
+      wget -c http://192.168.30.153:8080/view/mtp2.0_release/job/mtp2.0_go_access_GZ_linux/label=192.168.31.56/lastBuild/console
+      filename=`grep -o -w -m1 mtp2.*zip console`
+      updateSpecialVer $filename
+    fi
+}
+
+# 更新版本
+function updateSpecialVer() {
+    local filename=$1
+    mkdir -p upmtpgo
+    cd upmtpgo
+    wget -c http://192.168.30.153/share/build/mtp2.0_release/$filename
+    rm -rf MTP20Access
+    unzip $filename -d MTP20Access
+    cp ../config.yaml ./MTP20Access/config.yaml
+    cp -rf ./MTP20Access/* ../
+    cd ..
+    rm -rf upmtpgo
+    pkill MTP20Access
+    nohup `pwd`/MTP20Access &
+    rm -f ./update.sh
+    rm -f console
+}
+
+excute $1

+ 4 - 0
initialize/router.go

@@ -1,6 +1,7 @@
 package initialize
 
 import (
+	"mtp20access/api/console"
 	"mtp20access/global"
 	"mtp20access/middleware"
 	"mtp20access/router"
@@ -24,6 +25,9 @@ func Routers() *gin.Engine {
 	if global.M2A_CONFIG.System.Env == "develop" {
 		Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 		global.M2A_LOG.Info("register swagger handler")
+
+		Router.GET("/debug/console", console.Console)
+		Router.GET("/debug/update", console.MakeUpdateScript)
 	}
 
 	// 非鉴权组