- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml - Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix - Per-stream WireGuard/SOCKS egress profiles + Settings UI - Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers - Docs: streamd-server-setup lifecycle and update flow
243 lines
5.5 KiB
Go
243 lines
5.5 KiB
Go
package servecmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type updateFlags struct {
|
|
Channel string // stable|beta or empty → config
|
|
Tag string
|
|
NoRestart bool
|
|
Keep int // -1 = use config
|
|
}
|
|
|
|
// errUpdateDeferred means the binary swap finishes after process exit (Windows self-update).
|
|
var errUpdateDeferred = fmt.Errorf("update deferred until process exit")
|
|
|
|
func defaultAssetName() string {
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
switch runtime.GOARCH {
|
|
case "amd64":
|
|
return "streamd-linux-amd64"
|
|
case "arm64":
|
|
return "streamd-linux-arm64"
|
|
}
|
|
case "darwin":
|
|
switch runtime.GOARCH {
|
|
case "amd64":
|
|
return "streamd-darwin-amd64"
|
|
case "arm64":
|
|
return "streamd-darwin-arm64"
|
|
}
|
|
case "windows":
|
|
switch runtime.GOARCH {
|
|
case "amd64":
|
|
return "streamd-windows-amd64.exe"
|
|
case "arm64":
|
|
return "streamd-windows-arm64.exe"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func updateURL(base, ref, asset string) string {
|
|
base = strings.TrimRight(base, "/")
|
|
return fmt.Sprintf("%s/%s/%s", base, ref, asset)
|
|
}
|
|
|
|
func updateCmd(opt ServeOptions, uf updateFlags) error {
|
|
channel := opt.Channel
|
|
if channel == "" {
|
|
channel = defaultChannel
|
|
}
|
|
if uf.Channel != "" {
|
|
channel = uf.Channel
|
|
}
|
|
ref := channel
|
|
if uf.Tag != "" {
|
|
ref = uf.Tag
|
|
}
|
|
asset := opt.Asset
|
|
if asset == "" {
|
|
asset = defaultAssetName()
|
|
}
|
|
if asset == "" {
|
|
return fmt.Errorf("unsupported platform %s/%s — set asset: in streamd.yaml", runtime.GOOS, runtime.GOARCH)
|
|
}
|
|
keep := opt.KeepOld
|
|
if uf.Keep >= 0 {
|
|
keep = uf.Keep
|
|
}
|
|
|
|
url := updateURL(releaseBaseURL, ref, asset)
|
|
fmt.Printf("streamd update: downloading %s\n", url)
|
|
|
|
tmp, err := downloadToTemp(opt.InstallRoot, url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmp)
|
|
|
|
if err := smokeTestBinary(tmp); err != nil {
|
|
return err
|
|
}
|
|
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = resolved
|
|
}
|
|
// Prefer replacing the install-root binary named streamd[.exe] when that is
|
|
// where operators keep the release asset.
|
|
target := exe
|
|
wantName := "streamd"
|
|
if runtime.GOOS == "windows" {
|
|
wantName = "streamd.exe"
|
|
}
|
|
installBin := filepath.Join(opt.InstallRoot, wantName)
|
|
if st, err := os.Stat(installBin); err == nil && !st.IsDir() {
|
|
target = installBin
|
|
}
|
|
|
|
if _, ok := runningPID(opt); ok {
|
|
fmt.Println("streamd update: stopping running serve…")
|
|
if err := stopCmd(opt); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
oldDir := filepath.Join(opt.InstallRoot, "old")
|
|
if err := os.MkdirAll(oldDir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
stamp := time.Now().UTC().Format("20060102T150405Z")
|
|
archive := filepath.Join(oldDir, "streamd."+stamp)
|
|
if runtime.GOOS == "windows" {
|
|
archive += ".exe"
|
|
}
|
|
if _, err := os.Stat(archive); err == nil {
|
|
archive += "." + fmt.Sprint(os.Getpid())
|
|
}
|
|
|
|
if uf.NoRestart {
|
|
_ = os.Setenv("STREAMD_UPDATE_RESTART", "0")
|
|
} else {
|
|
_ = os.Setenv("STREAMD_UPDATE_RESTART", "1")
|
|
}
|
|
if err := replaceBinary(target, tmp, archive); err != nil {
|
|
if err == errUpdateDeferred {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
fmt.Printf("streamd update: archived previous → %s\n", archive)
|
|
fmt.Printf("streamd update: installed %s (%s)\n", target, ref)
|
|
pruneOldBinaries(oldDir, keep)
|
|
|
|
if uf.NoRestart {
|
|
fmt.Println("streamd update: done (--no-restart). Start with: streamd serve start")
|
|
return nil
|
|
}
|
|
fmt.Println("streamd update: starting…")
|
|
return startCmd(opt)
|
|
}
|
|
|
|
func downloadToTemp(dir, url string) (string, error) {
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return "", err
|
|
}
|
|
f, err := os.CreateTemp(dir, ".streamd-download-*")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
path := f.Name()
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
_ = f.Close()
|
|
_ = os.Remove(path)
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
_ = f.Close()
|
|
_ = os.Remove(path)
|
|
return "", fmt.Errorf("download %s: HTTP %s", url, resp.Status)
|
|
}
|
|
n, err := io.Copy(f, resp.Body)
|
|
closeErr := f.Close()
|
|
if err != nil {
|
|
_ = os.Remove(path)
|
|
return "", err
|
|
}
|
|
if closeErr != nil {
|
|
_ = os.Remove(path)
|
|
return "", closeErr
|
|
}
|
|
if n < 1024 {
|
|
_ = os.Remove(path)
|
|
return "", fmt.Errorf("download too small (%d bytes) — check URL %s", n, url)
|
|
}
|
|
// Reject HTML error pages.
|
|
head, _ := os.ReadFile(path)
|
|
if len(head) > 32 {
|
|
h := strings.ToLower(string(head[:32]))
|
|
if strings.Contains(h, "<!doctype") || strings.Contains(h, "<html") {
|
|
_ = os.Remove(path)
|
|
return "", fmt.Errorf("download looks like HTML, not a binary: %s", url)
|
|
}
|
|
}
|
|
_ = os.Chmod(path, 0o755)
|
|
return path, nil
|
|
}
|
|
|
|
func smokeTestBinary(path string) error {
|
|
cmd := exec.Command(path, "help")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
// help may exit 0; some builds exit 2 — accept if output mentions serve.
|
|
if !strings.Contains(string(out), "serve") && !strings.Contains(string(out), "streamd") {
|
|
return fmt.Errorf("new binary failed smoke test: %w\n%s", err, out)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func pruneOldBinaries(oldDir string, keep int) {
|
|
if keep <= 0 {
|
|
return
|
|
}
|
|
ents, err := os.ReadDir(oldDir)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var names []string
|
|
for _, e := range ents {
|
|
if e.IsDir() {
|
|
continue
|
|
}
|
|
n := e.Name()
|
|
if strings.HasPrefix(n, "streamd.") {
|
|
names = append(names, n)
|
|
}
|
|
}
|
|
sort.Strings(names)
|
|
if len(names) <= keep {
|
|
return
|
|
}
|
|
for _, n := range names[:len(names)-keep] {
|
|
_ = os.Remove(filepath.Join(oldDir, n))
|
|
fmt.Printf("streamd update: pruned %s\n", filepath.Join(oldDir, n))
|
|
}
|
|
}
|