mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-10 18:05: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 (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
|
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
|
||||||
"github.com/0xJacky/Nginx-UI/internal/version"
|
"github.com/0xJacky/Nginx-UI/internal/version"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -21,17 +20,17 @@ func GetCurrentNode(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeInfo, err := upgrader.GetRuntimeInfo()
|
runtimeInfo, err := version.GetRuntimeInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cosy.ErrHandler(c, err)
|
cosy.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cpuInfo, _ := cpu.Info()
|
cpuInfo, _ := cpu.Info()
|
||||||
memory, _ := analytic2.GetMemoryStat()
|
memory, _ := analytic.GetMemoryStat()
|
||||||
ver := version.GetVersionInfo()
|
ver := version.GetVersionInfo()
|
||||||
diskUsage, _ := disk.Usage(".")
|
diskUsage, _ := disk.Usage(".")
|
||||||
|
|
||||||
nodeInfo := analytic2.NodeInfo{
|
nodeInfo := analytic.NodeInfo{
|
||||||
NodeRuntimeInfo: runtimeInfo,
|
NodeRuntimeInfo: runtimeInfo,
|
||||||
CPUNum: len(cpuInfo),
|
CPUNum: len(cpuInfo),
|
||||||
MemoryTotal: memory.Total,
|
MemoryTotal: memory.Total,
|
||||||
|
@ -39,9 +38,9 @@ func GetCurrentNode(c *gin.Context) {
|
||||||
Version: ver.Version,
|
Version: ver.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
stat := analytic2.GetNodeStat()
|
stat := analytic.GetNodeStat()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, analytic2.Node{
|
c.JSON(http.StatusOK, analytic.Node{
|
||||||
NodeInfo: nodeInfo,
|
NodeInfo: nodeInfo,
|
||||||
NodeStat: stat,
|
NodeStat: stat,
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,11 +2,10 @@ package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
|
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/version"
|
"github.com/0xJacky/Nginx-UI/internal/version"
|
||||||
"github.com/0xJacky/Nginx-UI/settings"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/uozi-tech/cosy"
|
"github.com/uozi-tech/cosy"
|
||||||
|
@ -14,19 +13,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRelease(c *gin.Context) {
|
func GetRelease(c *gin.Context) {
|
||||||
data, err := upgrader.GetRelease(c.Query("channel"))
|
data, err := version.GetRelease(c.Query("channel"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cosy.ErrHandler(c, err)
|
cosy.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
runtimeInfo, err := upgrader.GetRuntimeInfo()
|
runtimeInfo, err := version.GetRuntimeInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cosy.ErrHandler(c, err)
|
cosy.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
type resp struct {
|
type resp struct {
|
||||||
upgrader.TRelease
|
version.TRelease
|
||||||
upgrader.RuntimeInfo
|
version.RuntimeInfo
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, resp{
|
c.JSON(http.StatusOK, resp{
|
||||||
data, runtimeInfo,
|
data, runtimeInfo,
|
||||||
|
@ -63,10 +62,7 @@ func PerformCoreUpgrade(c *gin.Context) {
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
var control struct {
|
var control upgrader.Control
|
||||||
DryRun bool `json:"dry_run"`
|
|
||||||
Channel string `json:"channel"`
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ws.ReadJSON(&control)
|
err = ws.ReadJSON(&control)
|
||||||
|
|
||||||
|
@ -74,80 +70,9 @@ func PerformCoreUpgrade(c *gin.Context) {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if helper.InNginxUIOfficialDocker() {
|
||||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
upgrader.DockerUpgrade(ws, &control)
|
||||||
Status: UpgradeStatusInfo,
|
} else {
|
||||||
Message: "Initialing core upgrader",
|
upgrader.BinaryUpgrade(ws, &control)
|
||||||
})
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,25 +3,26 @@ package analytic
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"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"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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 {
|
type NodeInfo struct {
|
||||||
NodeRuntimeInfo upgrader.RuntimeInfo `json:"node_runtime_info"`
|
NodeRuntimeInfo version.RuntimeInfo `json:"node_runtime_info"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
CPUNum int `json:"cpu_num"`
|
CPUNum int `json:"cpu_num"`
|
||||||
MemoryTotal string `json:"memory_total"`
|
MemoryTotal string `json:"memory_total"`
|
||||||
DiskTotal string `json:"disk_total"`
|
DiskTotal string `json:"disk_total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeStat struct {
|
type NodeStat struct {
|
||||||
|
@ -77,7 +78,7 @@ func InitNode(env *model.Environment) (n *Node, err error) {
|
||||||
|
|
||||||
u, err := url.JoinPath(env.URL, "/api/node")
|
u, err := url.JoinPath(env.URL, "/api/node")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := transport.NewTransport()
|
t, err := transport.NewTransport()
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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/container"
|
||||||
"github.com/docker/docker/api/types/image"
|
"github.com/docker/docker/api/types/image"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
@ -58,7 +58,7 @@ func UpgradeStepOne(channel string) (err error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// 1. Get the tag of the latest release
|
// 1. Get the tag of the latest release
|
||||||
release, err := upgrader.GetRelease(channel)
|
release, err := version.GetRelease(channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
11
internal/helper/docker.go
Normal file
11
internal/helper/docker.go
Normal 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"))
|
||||||
|
}
|
92
internal/upgrader/binary.go
Normal file
92
internal/upgrader/binary.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
19
internal/upgrader/docker.go
Normal file
19
internal/upgrader/docker.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,13 +3,6 @@ package upgrader
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"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"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -18,19 +11,40 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"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 {
|
type Upgrader struct {
|
||||||
Release TRelease
|
Release version.TRelease
|
||||||
RuntimeInfo
|
version.RuntimeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUpgrader(channel string) (u *Upgrader, err error) {
|
func NewUpgrader(channel string) (u *Upgrader, err error) {
|
||||||
data, err := GetRelease(channel)
|
data, err := version.GetRelease(channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
runtimeInfo, err := GetRuntimeInfo()
|
runtimeInfo, err := version.GetRuntimeInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package upgrader
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
|
@ -1,4 +1,4 @@
|
||||||
package upgrader
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
|
@ -2,10 +2,10 @@ package settings
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||||
"github.com/caarlos0/env/v11"
|
"github.com/caarlos0/env/v11"
|
||||||
"github.com/elliotchance/orderedmap/v3"
|
"github.com/elliotchance/orderedmap/v3"
|
||||||
"github.com/spf13/cast"
|
"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",
|
// 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.
|
// 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"
|
NginxSettings.RestartCmd = "nginx -s stop"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue