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,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 {
@ -77,7 +78,7 @@ func InitNode(env *model.Environment) (n *Node, err error) {
u, err := url.JoinPath(env.URL, "/api/node")
if err != nil {
return
return
}
t, err := transport.NewTransport()

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"