streamd: serve start/stop/update, streamd.yaml, egress, module panel fixes
- 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
This commit is contained in:
parent
da8e1123ec
commit
8fc7e4c0b8
57 changed files with 4031 additions and 257 deletions
200
apps/streamd/servecmd/daemon.go
Normal file
200
apps/streamd/servecmd/daemon.go
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
package servecmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func readPID(pidFile string) (int, error) {
|
||||
b, err := os.ReadFile(pidFile)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
s := strings.TrimSpace(string(b))
|
||||
if s == "" {
|
||||
return 0, fmt.Errorf("empty pid file")
|
||||
}
|
||||
return strconv.Atoi(s)
|
||||
}
|
||||
|
||||
func writePID(pidFile string, pid int) error {
|
||||
if err := os.MkdirAll(filepath.Dir(pidFile), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(pidFile, []byte(strconv.Itoa(pid)+"\n"), 0o644)
|
||||
}
|
||||
|
||||
func removePID(pidFile string) {
|
||||
_ = os.Remove(pidFile)
|
||||
}
|
||||
|
||||
func processAlive(pid int) bool {
|
||||
if pid <= 0 {
|
||||
return false
|
||||
}
|
||||
return signalAlive(pid)
|
||||
}
|
||||
|
||||
func runningPID(opt ServeOptions) (pid int, ok bool) {
|
||||
pid, err := readPID(opt.PIDFile)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
if !processAlive(pid) {
|
||||
removePID(opt.PIDFile)
|
||||
return pid, false
|
||||
}
|
||||
return pid, true
|
||||
}
|
||||
|
||||
func statusCmd(opt ServeOptions) error {
|
||||
fmt.Printf("config: %s\n", opt.ConfigPath)
|
||||
fmt.Printf("bind: %s\n", opt.Bind)
|
||||
fmt.Printf("data: %s\n", opt.Data)
|
||||
fmt.Printf("log: %s\n", opt.LogFile)
|
||||
fmt.Printf("pid: %s\n", opt.PIDFile)
|
||||
if pid, ok := runningPID(opt); ok {
|
||||
fmt.Printf("streamd: running pid=%d\n", pid)
|
||||
return nil
|
||||
}
|
||||
fmt.Println("streamd: stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopCmd(opt ServeOptions) error {
|
||||
pid, err := readPID(opt.PIDFile)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("streamd is not running")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if !processAlive(pid) {
|
||||
removePID(opt.PIDFile)
|
||||
fmt.Println("streamd is not running (stale pid file removed)")
|
||||
return nil
|
||||
}
|
||||
if err := signalTerminate(pid); err != nil {
|
||||
return fmt.Errorf("stop pid %d: %w", pid, err)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if !processAlive(pid) {
|
||||
removePID(opt.PIDFile)
|
||||
fmt.Printf("streamd stopped (was pid %d)\n", pid)
|
||||
return nil
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
_ = signalKill(pid)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
removePID(opt.PIDFile)
|
||||
fmt.Printf("streamd killed (pid %d)\n", pid)
|
||||
return nil
|
||||
}
|
||||
|
||||
func startCmd(opt ServeOptions) error {
|
||||
if pid, ok := runningPID(opt); ok {
|
||||
fmt.Printf("streamd already running (pid %d)\n", pid)
|
||||
return nil
|
||||
}
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
||||
exe = resolved
|
||||
}
|
||||
args := buildDaemonArgs(opt)
|
||||
cmd := exec.Command(exe, args...)
|
||||
if err := os.MkdirAll(filepath.Dir(opt.LogFile), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
logF, err := os.OpenFile(opt.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Stdout = logF
|
||||
cmd.Stderr = logF
|
||||
cmd.Stdin = nil
|
||||
detachProcess(cmd)
|
||||
if err := cmd.Start(); err != nil {
|
||||
_ = logF.Close()
|
||||
return fmt.Errorf("start daemon: %w", err)
|
||||
}
|
||||
pid := cmd.Process.Pid
|
||||
// Release so the child is not tied to this process on Unix.
|
||||
_ = cmd.Process.Release()
|
||||
_ = logF.Close()
|
||||
if err := writePID(opt.PIDFile, pid); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
if !processAlive(pid) {
|
||||
removePID(opt.PIDFile)
|
||||
return fmt.Errorf("daemon exited immediately — see %s", opt.LogFile)
|
||||
}
|
||||
fmt.Printf("streamd started pid=%d bind=%s data=%s\n", pid, opt.Bind, opt.Data)
|
||||
fmt.Printf("logs: %s\n", opt.LogFile)
|
||||
if opt.WroteConfig {
|
||||
fmt.Printf("wrote config %s\n", opt.ConfigPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restartCmd(opt ServeOptions) error {
|
||||
_ = stopCmd(opt)
|
||||
return startCmd(opt)
|
||||
}
|
||||
|
||||
func buildDaemonArgs(opt ServeOptions) []string {
|
||||
args := []string{"serve", "run", "--daemon-child"}
|
||||
if opt.ConfigPath != "" {
|
||||
args = append(args, "--config", opt.ConfigPath)
|
||||
}
|
||||
args = append(args,
|
||||
"--bind", opt.Bind,
|
||||
"--data", opt.Data,
|
||||
"--log-level", opt.LogLevel,
|
||||
)
|
||||
if opt.BinDir != "" {
|
||||
args = append(args, "--bin-dir", opt.BinDir)
|
||||
}
|
||||
if opt.NRE != "" {
|
||||
args = append(args, "--nre", opt.NRE)
|
||||
}
|
||||
if opt.FFmpeg != "" {
|
||||
args = append(args, "--ffmpeg", opt.FFmpeg)
|
||||
}
|
||||
if opt.MP4Decrypt != "" {
|
||||
args = append(args, "--mp4decrypt", opt.MP4Decrypt)
|
||||
}
|
||||
if opt.SkipDeps {
|
||||
args = append(args, "--skip-deps")
|
||||
}
|
||||
if opt.TrustedProxies != "" {
|
||||
args = append(args, "--trusted-proxies", opt.TrustedProxies)
|
||||
}
|
||||
if opt.AllowedOrigins != "" {
|
||||
args = append(args, "--allowed-origins", opt.AllowedOrigins)
|
||||
}
|
||||
if opt.CookieSecure {
|
||||
args = append(args, "--cookie-secure")
|
||||
}
|
||||
if opt.Token != "" {
|
||||
args = append(args, "--token", opt.Token)
|
||||
}
|
||||
if opt.AdminUser != "" {
|
||||
args = append(args, "--admin-user", opt.AdminUser)
|
||||
}
|
||||
if opt.AdminPass != "" {
|
||||
args = append(args, "--admin-pass", opt.AdminPass)
|
||||
}
|
||||
return args
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue