update.go 2.2 KB

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