feat: 删除旧的发布工作流,添加新的发布工作流,重构更新逻辑以支持跨平台重启

This commit is contained in:
ryan
2026-03-10 23:02:51 +08:00
parent fbc27e9d5d
commit 6fe9ad4af6
5 changed files with 189 additions and 114 deletions
@@ -0,0 +1,27 @@
//go:build !windows
package updater
import (
"fmt"
"os"
"syscall"
)
func replaceAndRestart(execPath string, tmpPath string) error {
backupPath := execPath + ".bak"
os.Remove(backupPath)
if err := os.Rename(execPath, backupPath); err != nil {
os.Remove(tmpPath)
return fmt.Errorf("backup current binary: %w", err)
}
if err := os.Rename(tmpPath, execPath); err != nil {
os.Rename(backupPath, execPath)
return fmt.Errorf("replace binary: %w", err)
}
os.Remove(backupPath)
if err := syscall.Exec(execPath, os.Args, os.Environ()); err != nil {
return fmt.Errorf("exec restart: %w", err)
}
return fmt.Errorf("unreachable after exec")
}
@@ -0,0 +1,53 @@
//go:build windows
package updater
import (
"fmt"
"os"
"os/exec"
"strings"
)
func replaceAndRestart(execPath string, tmpPath string) error {
backupPath := execPath + ".bak"
scriptPath := execPath + ".update.cmd"
script := fmt.Sprintf(`@echo off
setlocal
:waitloop
move /Y "%s" "%s" >nul 2>nul
if errorlevel 1 (
ping 127.0.0.1 -n 2 >nul
goto waitloop
)
move /Y "%s" "%s" >nul 2>nul
if errorlevel 1 exit /b 1
start "" %s
del /Q "%s" >nul 2>nul
del /Q "%%~f0" >nul 2>nul
`, execPath, backupPath, tmpPath, execPath, buildWindowsCommandLine(execPath, os.Args[1:]), backupPath)
if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil {
os.Remove(tmpPath)
return fmt.Errorf("write restart script: %w", err)
}
cmd := exec.Command("cmd", "/C", "start", "", scriptPath)
if err := cmd.Start(); err != nil {
os.Remove(scriptPath)
os.Remove(tmpPath)
return fmt.Errorf("schedule restart: %w", err)
}
os.Exit(0)
return nil
}
func buildWindowsCommandLine(execPath string, args []string) string {
parts := []string{quoteWindowsArg(execPath)}
for _, arg := range args {
parts = append(parts, quoteWindowsArg(arg))
}
return strings.Join(parts, " ")
}
func quoteWindowsArg(value string) string {
return `"` + strings.ReplaceAll(value, `"`, `""`) + `"`
}
+15 -27
View File
@@ -3,7 +3,6 @@ package updater
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
@@ -11,7 +10,6 @@ import (
"os"
"runtime"
"strings"
"syscall"
"time"
"atsflare-agent/internal/config"
@@ -59,7 +57,7 @@ func (s *Service) CheckAndUpdate(ctx context.Context, repo string) error {
}
log.Printf("agent update available: %s -> %s", localVersion, remoteVersion)
assetName := fmt.Sprintf("atsflare-agent-%s-%s", runtime.GOOS, runtime.GOARCH)
assetName := assetNameForGOOSGOARCH(runtime.GOOS, runtime.GOARCH)
var downloadURL string
for _, asset := range release.Assets {
@@ -77,12 +75,10 @@ func (s *Service) CheckAndUpdate(ctx context.Context, repo string) error {
if err != nil {
return fmt.Errorf("get executable path: %w", err)
}
if err = s.downloadAndReplace(ctx, downloadURL, execPath); err != nil {
return fmt.Errorf("download and replace: %w", err)
if err = s.downloadAndRestart(ctx, downloadURL, execPath); err != nil {
return fmt.Errorf("download and restart: %w", err)
}
log.Printf("agent binary updated, restarting...")
return s.restart(execPath)
return nil
}
func (s *Service) getLatestRelease(ctx context.Context, repo string) (*githubRelease, error) {
@@ -113,7 +109,7 @@ func (s *Service) getLatestRelease(ctx context.Context, repo string) (*githubRel
return &release, nil
}
func (s *Service) downloadAndReplace(ctx context.Context, url string, targetPath string) error {
func (s *Service) downloadAndRestart(ctx context.Context, url string, targetPath string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
@@ -129,6 +125,9 @@ func (s *Service) downloadAndReplace(ctx context.Context, url string, targetPath
}
tmpPath := targetPath + ".update"
if runtime.GOOS == "windows" && !strings.HasSuffix(strings.ToLower(tmpPath), ".exe") {
tmpPath += ".exe"
}
tmpFile, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755)
if err != nil {
return err
@@ -140,27 +139,16 @@ func (s *Service) downloadAndReplace(ctx context.Context, url string, targetPath
}
tmpFile.Close()
backupPath := targetPath + ".bak"
os.Remove(backupPath)
if err = os.Rename(targetPath, backupPath); err != nil {
os.Remove(tmpPath)
return fmt.Errorf("backup current binary: %w", err)
}
if err = os.Rename(tmpPath, targetPath); err != nil {
// Attempt to restore backup
os.Rename(backupPath, targetPath)
return fmt.Errorf("replace binary: %w", err)
}
os.Remove(backupPath)
return nil
log.Printf("agent binary updated, restarting...")
return replaceAndRestart(targetPath, tmpPath)
}
func (s *Service) restart(execPath string) error {
argv := os.Args
if err := syscall.Exec(execPath, argv, os.Environ()); err != nil {
return fmt.Errorf("exec restart: %w", err)
func assetNameForGOOSGOARCH(goos string, goarch string) string {
name := fmt.Sprintf("atsflare-agent-%s-%s", goos, goarch)
if goos == "windows" {
return name + ".exe"
}
return errors.New("unreachable after exec")
return name
}
func normalizeVersion(v string) string {