| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- /**
- * @Author: zou.yingbin
- * @Create : 2021/10/20 15:20
- * @Modify : 2021/10/20 15:20
- * @Note : 生成用于内网升级的.sh脚本
- */
- package other
- import (
- _ "embed"
- "fmt"
- "github.com/gin-gonic/gin"
- "mtp2_if/logger"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "regexp"
- "runtime"
- "strings"
- )
- //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.xml和cfg.json,且重启服务。稍后请在日志页面查看版本信息, 请勿刷新本页面。")
- rx := regexp.MustCompile(`mtp2_queryservice_r[\d_]*\.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 {
- _, err = f.Write([]byte(strShell))
- f.Close()
- } else {
- return false
- }
- return true
- }
|