refactor: replace upgrader with version package for runtime and release information

This commit is contained in:
Jacky 2025-04-21 08:06:27 +00:00
parent 8e212ae79b
commit d0cf93d5e3
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
11 changed files with 182 additions and 121 deletions

View file

@ -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,
})

View file

@ -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)
}
}

View file

@ -3,25 +3,26 @@ package analytic
import (
"encoding/json"
"errors"
"github.com/0xJacky/Nginx-UI/internal/transport"
"github.com/0xJacky/Nginx-UI/internal/upgrader"
"github.com/0xJacky/Nginx-UI/model"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/net"
"github.com/uozi-tech/cosy/logger"
"io"
"net/http"
"net/url"
"sync"
"time"
"github.com/0xJacky/Nginx-UI/internal/transport"
"github.com/0xJacky/Nginx-UI/internal/version"
"github.com/0xJacky/Nginx-UI/model"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/net"
"github.com/uozi-tech/cosy/logger"
)
type NodeInfo struct {
NodeRuntimeInfo upgrader.RuntimeInfo `json:"node_runtime_info"`
Version string `json:"version"`
CPUNum int `json:"cpu_num"`
MemoryTotal string `json:"memory_total"`
DiskTotal string `json:"disk_total"`
NodeRuntimeInfo version.RuntimeInfo `json:"node_runtime_info"`
Version string `json:"version"`
CPUNum int `json:"cpu_num"`
MemoryTotal string `json:"memory_total"`
DiskTotal string `json:"disk_total"`
}
type NodeStat struct {

View file

@ -8,7 +8,7 @@ import (
"strings"
"time"
"github.com/0xJacky/Nginx-UI/internal/upgrader"
"github.com/0xJacky/Nginx-UI/internal/version"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
@ -58,7 +58,7 @@ func UpgradeStepOne(channel string) (err error) {
ctx := context.Background()
// 1. Get the tag of the latest release
release, err := upgrader.GetRelease(channel)
release, err := version.GetRelease(channel)
if err != nil {
return err
}

11
internal/helper/docker.go Normal file
View file

@ -0,0 +1,11 @@
package helper
import (
"os"
"github.com/spf13/cast"
)
func InNginxUIOfficialDocker() bool {
return cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER"))
}

View file

@ -0,0 +1,92 @@
package upgrader
import (
"os"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gorilla/websocket"
"github.com/uozi-tech/cosy/logger"
)
type Control struct {
DryRun bool `json:"dry_run"`
Channel string `json:"channel"`
}
func BinaryUpgrade(ws *websocket.Conn, control *Control) {
_ = ws.WriteJSON(CoreUpgradeResp{
Status: UpgradeStatusInfo,
Message: "Initialing core upgrader",
})
u, err := 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
}
}

View file

@ -0,0 +1,19 @@
package upgrader
import (
"github.com/0xJacky/Nginx-UI/internal/docker"
"github.com/gorilla/websocket"
"github.com/uozi-tech/cosy/logger"
)
func DockerUpgrade(ws *websocket.Conn, control *Control) {
err := docker.UpgradeStepOne(control.Channel)
if err != nil {
_ = ws.WriteJSON(CoreUpgradeResp{
Status: UpgradeStatusError,
Message: err.Error(),
})
logger.Error(err)
return
}
}

View file

@ -3,13 +3,6 @@ package upgrader
import (
"encoding/json"
"fmt"
_github "github.com/0xJacky/Nginx-UI/.github"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/jpillora/overseer"
"github.com/minio/selfupdate"
"github.com/pkg/errors"
"github.com/uozi-tech/cosy/logger"
"io"
"net/http"
"net/url"
@ -18,19 +11,40 @@ import (
"strconv"
"strings"
"sync/atomic"
_github "github.com/0xJacky/Nginx-UI/.github"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/version"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/jpillora/overseer"
"github.com/minio/selfupdate"
"github.com/pkg/errors"
"github.com/uozi-tech/cosy/logger"
)
const (
UpgradeStatusInfo = "info"
UpgradeStatusError = "error"
UpgradeStatusProgress = "progress"
)
type CoreUpgradeResp struct {
Status string `json:"status"`
Progress float64 `json:"progress"`
Message string `json:"message"`
}
type Upgrader struct {
Release TRelease
RuntimeInfo
Release version.TRelease
version.RuntimeInfo
}
func NewUpgrader(channel string) (u *Upgrader, err error) {
data, err := GetRelease(channel)
data, err := version.GetRelease(channel)
if err != nil {
return
}
runtimeInfo, err := GetRuntimeInfo()
runtimeInfo, err := version.GetRuntimeInfo()
if err != nil {
return
}

View file

@ -1,4 +1,4 @@
package upgrader
package version
import (
"os"

View file

@ -1,4 +1,4 @@
package upgrader
package version
import (
"encoding/json"

View file

@ -2,10 +2,10 @@ package settings
import (
"log"
"os"
"strings"
"time"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/caarlos0/env/v11"
"github.com/elliotchance/orderedmap/v3"
"github.com/spf13/cast"
@ -81,7 +81,7 @@ func Init(confPath string) {
// if in official docker, set the restart cmd of nginx to "nginx -s stop",
// then the supervisor of s6-overlay will start the nginx again.
if cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER")) {
if helper.InNginxUIOfficialDocker() {
NginxSettings.RestartCmd = "nginx -s stop"
}