update.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "strings"
  15. )
  16. // MakeUpdateScript 生成脚本
  17. func MakeUpdateScript(c *gin.Context) {
  18. if createsh() {
  19. c.String(http.StatusOK, "创建升级脚本成功, 请手动执行升级脚本update.sh(在程序所在目录), 升级会保留原有的config.xml,且重启服务.")
  20. } else {
  21. c.String(http.StatusBadRequest, "创建脚本失败, 可能是权限不足")
  22. }
  23. }
  24. // 生成update.sh文件
  25. func createsh() bool {
  26. dir := ""
  27. if str, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
  28. dir = strings.Replace(str, "\\", "/", -1)
  29. fmt.Println("currentDir:", dir)
  30. } else {
  31. return false
  32. }
  33. sh :=
  34. "# author : zyb\n" +
  35. "# note : 在程序运行目录执行此脚本, 升级go服务到最新构建的版本。升级完成自动重启go服务\n" +
  36. "# --------------------------------------------------------------------\n" +
  37. "\n" +
  38. "mkdir -p upmtpgo\n" +
  39. "cd upmtpgo\n" +
  40. "rm -f console\n" +
  41. "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" +
  42. "filename=`grep -o -w -m1 mtp2.*zip console`\n" +
  43. "wget -c http://192.168.30.153/share/build/mtp2.0_release/$filename\n" +
  44. "rm -rf queryservice\n" +
  45. "unzip $filename -d queryservice\n" +
  46. "cp ../config/config.xml ./queryservice/config/config.xml\n" +
  47. "cp -rf ./queryservice/* ../\n" +
  48. "cd ..\n" +
  49. "rm -rf upmtpgo\n" +
  50. "pkill QueryService\n" +
  51. "nohup `pwd`/QueryService &"
  52. // 创建文件
  53. if f, err := os.Create(dir + "/update.sh"); err == nil {
  54. _, err = f.Write([]byte(sh))
  55. f.Chmod(777)
  56. f.Close()
  57. } else {
  58. return false
  59. }
  60. return true
  61. }