[Chore] Improve auto update mechanism (#1009)

This commit is contained in:
Sven van Ginkel
2025-08-25 23:30:42 +02:00
committed by GitHub
parent 4ce491fe48
commit ff854d481d
3 changed files with 205 additions and 82 deletions

View File

@@ -4,6 +4,7 @@ import (
"beszel"
"fmt"
"os"
"os/exec"
"strings"
"github.com/blang/semver"
@@ -54,4 +55,45 @@ func Update(_ *cobra.Command, _ []string) {
os.Exit(1)
}
fmt.Printf("Successfully updated to %s\n\n%s\n", latest.Version, strings.TrimSpace(latest.ReleaseNotes))
// Try to restart the service if it's running
restartService()
}
// restartService attempts to restart the beszel service
func restartService() {
// Check if we're running as a service by looking for systemd
if _, err := exec.LookPath("systemctl"); err == nil {
// Check if beszel service exists and is active
cmd := exec.Command("systemctl", "is-active", "beszel.service")
if err := cmd.Run(); err == nil {
fmt.Println("Restarting beszel service...")
restartCmd := exec.Command("systemctl", "restart", "beszel.service")
if err := restartCmd.Run(); err != nil {
fmt.Printf("Warning: Failed to restart service: %v\n", err)
fmt.Println("Please restart the service manually: sudo systemctl restart beszel")
} else {
fmt.Println("Service restarted successfully")
}
return
}
}
// Check for OpenRC (Alpine Linux)
if _, err := exec.LookPath("rc-service"); err == nil {
cmd := exec.Command("rc-service", "beszel", "status")
if err := cmd.Run(); err == nil {
fmt.Println("Restarting beszel service...")
restartCmd := exec.Command("rc-service", "beszel", "restart")
if err := restartCmd.Run(); err != nil {
fmt.Printf("Warning: Failed to restart service: %v\n", err)
fmt.Println("Please restart the service manually: sudo rc-service beszel restart")
} else {
fmt.Println("Service restarted successfully")
}
return
}
}
fmt.Println("Note: Service restart not attempted. If running as a service, restart manually.")
}