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
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue