upgrade.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package system
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/0xJacky/Nginx-UI/internal/upgrader"
  6. "github.com/0xJacky/Nginx-UI/internal/version"
  7. "github.com/0xJacky/Nginx-UI/settings"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gorilla/websocket"
  10. "github.com/uozi-tech/cosy"
  11. "github.com/uozi-tech/cosy/logger"
  12. )
  13. func GetRelease(c *gin.Context) {
  14. data, err := upgrader.GetRelease(c.Query("channel"))
  15. if err != nil {
  16. cosy.ErrHandler(c, err)
  17. return
  18. }
  19. runtimeInfo, err := upgrader.GetRuntimeInfo()
  20. if err != nil {
  21. cosy.ErrHandler(c, err)
  22. return
  23. }
  24. type resp struct {
  25. upgrader.TRelease
  26. upgrader.RuntimeInfo
  27. }
  28. c.JSON(http.StatusOK, resp{
  29. data, runtimeInfo,
  30. })
  31. }
  32. func GetCurrentVersion(c *gin.Context) {
  33. c.JSON(http.StatusOK, version.GetVersionInfo())
  34. }
  35. const (
  36. UpgradeStatusInfo = "info"
  37. UpgradeStatusError = "error"
  38. UpgradeStatusProgress = "progress"
  39. )
  40. type CoreUpgradeResp struct {
  41. Status string `json:"status"`
  42. Progress float64 `json:"progress"`
  43. Message string `json:"message"`
  44. }
  45. func PerformCoreUpgrade(c *gin.Context) {
  46. var upGrader = websocket.Upgrader{
  47. CheckOrigin: func(r *http.Request) bool {
  48. return true
  49. },
  50. }
  51. // upgrade http to websocket
  52. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  53. if err != nil {
  54. logger.Error(err)
  55. return
  56. }
  57. defer ws.Close()
  58. var control struct {
  59. DryRun bool `json:"dry_run"`
  60. Channel string `json:"channel"`
  61. }
  62. err = ws.ReadJSON(&control)
  63. if err != nil {
  64. logger.Error(err)
  65. return
  66. }
  67. _ = ws.WriteJSON(CoreUpgradeResp{
  68. Status: UpgradeStatusInfo,
  69. Message: "Initialing core upgrader",
  70. })
  71. u, err := upgrader.NewUpgrader(control.Channel)
  72. if err != nil {
  73. _ = ws.WriteJSON(CoreUpgradeResp{
  74. Status: UpgradeStatusError,
  75. Message: "Initial core upgrader error",
  76. })
  77. _ = ws.WriteJSON(CoreUpgradeResp{
  78. Status: UpgradeStatusError,
  79. Message: err.Error(),
  80. })
  81. logger.Error(err)
  82. return
  83. }
  84. _ = ws.WriteJSON(CoreUpgradeResp{
  85. Status: UpgradeStatusInfo,
  86. Message: "Downloading latest release",
  87. })
  88. progressChan := make(chan float64)
  89. defer close(progressChan)
  90. go func() {
  91. for progress := range progressChan {
  92. _ = ws.WriteJSON(CoreUpgradeResp{
  93. Status: UpgradeStatusProgress,
  94. Progress: progress,
  95. })
  96. }
  97. }()
  98. tarName, err := u.DownloadLatestRelease(progressChan)
  99. if err != nil {
  100. _ = ws.WriteJSON(CoreUpgradeResp{
  101. Status: UpgradeStatusError,
  102. Message: "Download latest release error",
  103. })
  104. _ = ws.WriteJSON(CoreUpgradeResp{
  105. Status: UpgradeStatusError,
  106. Message: err.Error(),
  107. })
  108. logger.Error(err)
  109. return
  110. }
  111. defer func() {
  112. _ = os.Remove(tarName)
  113. _ = os.Remove(tarName + ".digest")
  114. }()
  115. _ = ws.WriteJSON(CoreUpgradeResp{
  116. Status: UpgradeStatusInfo,
  117. Message: "Performing core upgrade",
  118. })
  119. // dry run
  120. if control.DryRun || settings.NodeSettings.Demo {
  121. return
  122. }
  123. // bye, will restart nginx-ui in performCoreUpgrade
  124. err = u.PerformCoreUpgrade(tarName)
  125. if err != nil {
  126. _ = ws.WriteJSON(CoreUpgradeResp{
  127. Status: UpgradeStatusError,
  128. Message: "Perform core upgrade error",
  129. })
  130. _ = ws.WriteJSON(CoreUpgradeResp{
  131. Status: UpgradeStatusError,
  132. Message: err.Error(),
  133. })
  134. logger.Error(err)
  135. return
  136. }
  137. }