mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
refactor: replace upgrader with version package for runtime and release information
This commit is contained in:
parent
8e212ae79b
commit
d0cf93d5e3
11 changed files with 182 additions and 121 deletions
|
@ -3,8 +3,7 @@ package cluster
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
||||
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/version"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -21,17 +20,17 @@ func GetCurrentNode(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
runtimeInfo, err := upgrader.GetRuntimeInfo()
|
||||
runtimeInfo, err := version.GetRuntimeInfo()
|
||||
if err != nil {
|
||||
cosy.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
cpuInfo, _ := cpu.Info()
|
||||
memory, _ := analytic2.GetMemoryStat()
|
||||
memory, _ := analytic.GetMemoryStat()
|
||||
ver := version.GetVersionInfo()
|
||||
diskUsage, _ := disk.Usage(".")
|
||||
|
||||
nodeInfo := analytic2.NodeInfo{
|
||||
nodeInfo := analytic.NodeInfo{
|
||||
NodeRuntimeInfo: runtimeInfo,
|
||||
CPUNum: len(cpuInfo),
|
||||
MemoryTotal: memory.Total,
|
||||
|
@ -39,9 +38,9 @@ func GetCurrentNode(c *gin.Context) {
|
|||
Version: ver.Version,
|
||||
}
|
||||
|
||||
stat := analytic2.GetNodeStat()
|
||||
stat := analytic.GetNodeStat()
|
||||
|
||||
c.JSON(http.StatusOK, analytic2.Node{
|
||||
c.JSON(http.StatusOK, analytic.Node{
|
||||
NodeInfo: nodeInfo,
|
||||
NodeStat: stat,
|
||||
})
|
||||
|
|
|
@ -2,11 +2,10 @@ package system
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
||||
"github.com/0xJacky/Nginx-UI/internal/version"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/uozi-tech/cosy"
|
||||
|
@ -14,19 +13,19 @@ import (
|
|||
)
|
||||
|
||||
func GetRelease(c *gin.Context) {
|
||||
data, err := upgrader.GetRelease(c.Query("channel"))
|
||||
data, err := version.GetRelease(c.Query("channel"))
|
||||
if err != nil {
|
||||
cosy.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
runtimeInfo, err := upgrader.GetRuntimeInfo()
|
||||
runtimeInfo, err := version.GetRuntimeInfo()
|
||||
if err != nil {
|
||||
cosy.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
type resp struct {
|
||||
upgrader.TRelease
|
||||
upgrader.RuntimeInfo
|
||||
version.TRelease
|
||||
version.RuntimeInfo
|
||||
}
|
||||
c.JSON(http.StatusOK, resp{
|
||||
data, runtimeInfo,
|
||||
|
@ -63,10 +62,7 @@ func PerformCoreUpgrade(c *gin.Context) {
|
|||
}
|
||||
defer ws.Close()
|
||||
|
||||
var control struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
Channel string `json:"channel"`
|
||||
}
|
||||
var control upgrader.Control
|
||||
|
||||
err = ws.ReadJSON(&control)
|
||||
|
||||
|
@ -74,80 +70,9 @@ func PerformCoreUpgrade(c *gin.Context) {
|
|||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Initialing core upgrader",
|
||||
})
|
||||
|
||||
u, err := upgrader.NewUpgrader(control.Channel)
|
||||
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Initial core upgrader error",
|
||||
})
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Downloading latest release",
|
||||
})
|
||||
progressChan := make(chan float64)
|
||||
defer close(progressChan)
|
||||
go func() {
|
||||
for progress := range progressChan {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusProgress,
|
||||
Progress: progress,
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
tarName, err := u.DownloadLatestRelease(progressChan)
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Download latest release error",
|
||||
})
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(tarName)
|
||||
_ = os.Remove(tarName + ".digest")
|
||||
}()
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Performing core upgrade",
|
||||
})
|
||||
// dry run
|
||||
if control.DryRun || settings.NodeSettings.Demo {
|
||||
return
|
||||
}
|
||||
|
||||
// bye, will restart nginx-ui in performCoreUpgrade
|
||||
err = u.PerformCoreUpgrade(tarName)
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Perform core upgrade error",
|
||||
})
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
if helper.InNginxUIOfficialDocker() {
|
||||
upgrader.DockerUpgrade(ws, &control)
|
||||
} else {
|
||||
upgrader.BinaryUpgrade(ws, &control)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue