What changed: a new internal/daemon package and `magpie daemon`, which run
`magpie serve` as the user's own service, with no window and no desktop
session.
- Linux: a systemd user unit at ~/.config/systemd/user/magpie.service
(Restart=on-failure), driven by systemctl --user. status reads
`systemctl --user show`, and says `sudo loginctl enable-linger $USER`
when the user doesn't linger. su/sudo without the user's bus, no
systemd, and WSL without systemd each get an error that says what to
do.
- macOS: a launch agent, com.yetone.magpie.serve (RunAtLoad, KeepAlive on
failure), loaded with launchctl bootstrap gui/$UID. Its log is
~/Library/Logs/magpie/serve.log.
- Windows and Android: refused, pointing to `magpie autostart on` (or to
WSL).
- The service carries PATH, the proxy variables, MAGPIE_*, the XDG folders
and the agents' folder variables from the install shell. The file is
0600 because a proxy can hold a password. It runs magpie by its PATH
name when that is the same file, so Homebrew's link survives updates.
It refuses a go-build binary and an AppTranslocation copy.
- Install is refused while `magpie autostart` is on: the tray app serves
the gateway too, and the two would crash-loop on the port.
- Docs: a reference.md section, and one sentence on the site's start page
(en, zh, ja).
Why: users on headless Linux boxes asked for a supported way to keep
`magpie serve` up across reboots instead of hand-written units. The
existing `magpie autostart` needs a desktop session (an XDG autostart
entry).
What was run:
- gofmt -l; go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test ./internal/daemon ./internal/cli on macOS, at origin/main
5b9530b56.
- The daemon tests cross-built for linux/arm64 and run in a Debian
container.
- The tests use fake systemctl, launchctl and loginctl on PATH in a test
home.
- Each guarantee was broken by hand and its test went red: the % escape,
the autostart refusal, the 0600 chmod, the bootout before re-bootstrap,
the go-build refusal, the bus advice, the last-exit filter and the
PATH link.
- Real use on Linux: a Debian bookworm container running systemd as PID 1,
with the linux/arm64 build, as an unprivileged user.
- install: the gateway answered on :3425, and the unit was 0600.
- kill -9: systemd restarted it 5s later.
- Container restart with linger on: it came back by itself.
- restart, stop, start and uninstall worked, and the unit and its wants
link were gone afterwards.
- Under sudo without XDG_RUNTIME_DIR it printed the bus advice.
- macOS: the darwin build ran `magpie daemon install` under a sandbox HOME
with a fake launchctl. plutil -lint passed on the plist, and the calls
were print and then bootstrap.
What wasn't: no real launch agent was loaded on a Mac (deliberately: never
on this machine), so launchd's own acceptance of the plist is checked
only by plutil. The linger advice was checked by the unit test, not seen
from a real SSH login. Windows was not run beyond vet.
48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// magpie daemon: status before an install says how to install; start
|
|
// says to install first; a word it doesn't take prints its usage; and a
|
|
// `go test` or `go run` build, which Go deletes, is never installed as the
|
|
// service. None of them runs the system's service tool.
|
|
func TestDaemonCmd(t *testing.T) {
|
|
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
|
|
t.Skip("systemd and launchd only")
|
|
}
|
|
helpHome(t)
|
|
dir := t.TempDir()
|
|
calls := filepath.Join(dir, "calls")
|
|
for _, name := range []string{"systemctl", "launchctl", "loginctl"} {
|
|
testenv.Program(t, filepath.Join(dir, name), "#!/bin/sh\necho \""+name+" $*\" >> '"+calls+"'\n")
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
prev := daemonWait
|
|
daemonWait = 0
|
|
t.Cleanup(func() { daemonWait = prev })
|
|
out, err := runOut(t, "daemon")
|
|
if err != nil || !strings.Contains(out, "isn't installed") || !strings.Contains(out, "magpie daemon install") {
|
|
t.Errorf("status: %q, %v", out, err)
|
|
}
|
|
if _, err := runOut(t, "daemon", "start"); err == nil || !strings.Contains(err.Error(), "isn't installed") {
|
|
t.Errorf("start: %v", err)
|
|
}
|
|
if _, err := runOut(t, "daemon", "bogus"); err == nil || !strings.Contains(err.Error(), "usage: magpie daemon") {
|
|
t.Errorf("bogus: %v", err)
|
|
}
|
|
if _, err := runOut(t, "daemon", "install"); err == nil || !strings.Contains(err.Error(), "go run") {
|
|
t.Errorf("install from a test build: %v", err)
|
|
}
|
|
if b, _ := os.ReadFile(calls); len(b) > 0 {
|
|
t.Errorf("ran %s", b)
|
|
}
|
|
}
|