mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-10 18:05:48 +02:00
feat(upgrader): add test commit and restart functionality
This commit is contained in:
parent
9f6f9088c4
commit
5c2621aaa6
5 changed files with 120 additions and 8 deletions
|
@ -9,8 +9,9 @@ import (
|
|||
)
|
||||
|
||||
type Control struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
Channel string `json:"channel"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
TestCommitAndRestart bool `json:"test_commit_and_restart"`
|
||||
Channel string `json:"channel"`
|
||||
}
|
||||
|
||||
// BinaryUpgrade Upgrade the binary
|
||||
|
@ -48,6 +49,16 @@ func BinaryUpgrade(ws *websocket.Conn, control *Control) {
|
|||
}
|
||||
}()
|
||||
|
||||
if control.TestCommitAndRestart {
|
||||
err = u.TestCommitAndRestart()
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Test commit and restart error",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
tarName, err := u.DownloadLatestRelease(progressChan)
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
|
@ -71,7 +82,7 @@ func BinaryUpgrade(ws *websocket.Conn, control *Control) {
|
|||
Message: "Performing core upgrade",
|
||||
})
|
||||
|
||||
if settings.NodeSettings.Demo {
|
||||
if control.DryRun || settings.NodeSettings.Demo {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
98
internal/upgrader/test_commit_restart.go
Normal file
98
internal/upgrader/test_commit_restart.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package upgrader
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"code.pfad.fr/risefront"
|
||||
"github.com/minio/selfupdate"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (u *Upgrader) TestCommitAndRestart() error {
|
||||
// Get the directory of the current executable
|
||||
exDir := filepath.Dir(u.ExPath)
|
||||
testBinaryPath := filepath.Join(exDir, "nginx-ui.test")
|
||||
|
||||
// Create temporary old file path
|
||||
oldExe := filepath.Join(exDir, ".nginx-ui.old."+strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
// Setup update options
|
||||
opts := selfupdate.Options{
|
||||
OldSavePath: oldExe,
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
if err := opts.CheckPermissions(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Copy current executable to test file
|
||||
srcFile, err := os.Open(u.ExPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to open source executable")
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
// Create destination file
|
||||
destFile, err := os.Create(testBinaryPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create test executable")
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
// Copy file content
|
||||
_, err = io.Copy(destFile, srcFile)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to copy executable content")
|
||||
}
|
||||
|
||||
// Set executable permissions
|
||||
if err = destFile.Chmod(0755); err != nil {
|
||||
return errors.Wrap(err, "failed to set executable permission")
|
||||
}
|
||||
|
||||
// Reopen file for selfupdate
|
||||
srcFile.Close()
|
||||
srcFile, err = os.Open(testBinaryPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to open test executable for update")
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
// Prepare and check binary
|
||||
if err = selfupdate.PrepareAndCheckBinary(srcFile, opts); err != nil {
|
||||
var pathErr *os.PathError
|
||||
if errors.As(err, &pathErr) {
|
||||
return pathErr.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Commit binary update
|
||||
if err = selfupdate.CommitBinary(opts); err != nil {
|
||||
if rerr := selfupdate.RollbackError(err); rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
var pathErr *os.PathError
|
||||
if errors.As(err, &pathErr) {
|
||||
return pathErr.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
_ = os.Remove(oldExe)
|
||||
}
|
||||
|
||||
// Wait for file to be written
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// Gracefully restart
|
||||
risefront.Restart()
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue