update.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/10/20 15:20
  4. * @Modify : 2021/10/20 15:20
  5. * @Note : 生成用于内网升级的.sh脚本
  6. */
  7. package other
  8. import (
  9. _ "embed"
  10. "github.com/gin-gonic/gin"
  11. "mtp2_if/logger"
  12. "net/http"
  13. "os"
  14. "os/exec"
  15. "path/filepath"
  16. "runtime"
  17. "strings"
  18. )
  19. //go:embed upmtpgo.sh
  20. var strShell string
  21. var IsUpdateing = false
  22. // MakeUpdateScript 生成脚本
  23. func MakeUpdateScript(c *gin.Context) {
  24. if IsUpdateing {
  25. c.String(http.StatusOK, "正在升级中...")
  26. return
  27. }
  28. if runtime.GOOS == "windows" {
  29. c.String(http.StatusOK, "windows不支持, 仅支持linux下执行")
  30. return
  31. }
  32. logger.GetLogger().Debug("*********************auto update*******************************")
  33. IsUpdateing = true
  34. defer func() {
  35. IsUpdateing = false
  36. }()
  37. if createsh() {
  38. c.String(http.StatusOK, "正在执行升级, 升级会保留原有的config.xml和cfg.json,且重启服务。稍后请在日志页面查看版本信息, 请勿刷新本页面。")
  39. _ = exec.Command("sh", "./update.sh").Run()
  40. } else {
  41. c.String(http.StatusBadRequest, "创建脚本失败, 可能是权限不足")
  42. }
  43. }
  44. // 生成update.sh文件
  45. func createsh() bool {
  46. dir := ""
  47. if str, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
  48. dir = strings.Replace(str, "\\", "/", -1)
  49. } else {
  50. return false
  51. }
  52. sh :=
  53. "# author : zyb\n" +
  54. "# note : 在程序运行目录执行此脚本, 升级go服务到最新构建的版本。升级完成自动重启go服务\n" +
  55. "# --------------------------------------------------------------------\n" +
  56. "\n" +
  57. "mkdir -p upmtpgo\n" +
  58. "cd upmtpgo\n" +
  59. "rm -f console\n" +
  60. "wget -c http://192.168.30.153:8080/view/mtp2.0_release/job/mtp2.0_QueryService_linux/label=192.168.31.56/lastBuild/console\n" +
  61. "filename=`grep -o -w -m1 mtp2.*zip console`\n" +
  62. "wget -c http://192.168.30.153/share/build/mtp2.0_release/$filename\n" +
  63. "rm -rf queryservice\n" +
  64. "unzip $filename -d queryservice\n" +
  65. "cp ../config/config.xml ./queryservice/config/config.xml\n" +
  66. "cp ../config/cfg.json ./queryservice/config/cfg.json\n" +
  67. "cp -rf ./queryservice/* ../\n" +
  68. "cd ..\n" +
  69. "rm -rf upmtpgo\n" +
  70. "pkill QueryService\n" +
  71. "nohup `pwd`/QueryService &" +
  72. "rm -f update.sh"
  73. // 创建文件
  74. if f, err := os.OpenFile(dir+"/update.sh", os.O_RDWR|os.O_CREATE, 0666); err == nil {
  75. _ = sh
  76. _, err = f.Write([]byte(strShell))
  77. f.Close()
  78. } else {
  79. return false
  80. }
  81. return true
  82. }