HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.
- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
page from one process (stats, StartWeb, Wait). The default page address
is 127.0.0.1:3430. Any other argument is refused, where before it was
ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
`magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
key is made once. A reinstall with no flags keeps the page and its key
(read back from the file by daemon.Web), so the link survives restarts
and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
set, is used and checked like magpie web's. The page defaults to
loopback; --lan or a non-loopback --addr prints the network links and
magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
exits. Beside a gateway another magpie serves, it says so and points to
`magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
site's start page (en, zh, ja). The READMEs don't cover the daemon.
Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
reinstall without the page, the shell's key not carried, no key
refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
with its line reverted by hand: withKey, prevKey in webOptions, and the
status page line.
- Sandbox HOME with a fake launchctl:
- `daemon install --web --lan` wrote a 0600 plist with
`serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
the link, the network links and the warning.
- Reinstalling with no flags kept the key. `daemon status` showed the
link.
- `daemon web off` / `daemon web` / `daemon web on` behaved as above,
with a new key after off.
- `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
one process. The page answered 200 with the key and 401 without, and the
page and /v1/models both showed 3 models. `magpie web --no-open` beside
it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
windows; go test -tags nogui ./... in a sandbox HOME.
Not tried: a real systemd on Linux. The unit text is checked by test only.
293 lines
8.2 KiB
Go
293 lines
8.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/daemon"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/gui"
|
|
)
|
|
|
|
const daemonUsage = "usage: magpie daemon [install|uninstall|start|stop|restart|status] · magpie daemon install [--web] [--lan] [--addr host:port] [--no-web] · magpie daemon web [on|off] [--lan] [--addr host:port]"
|
|
|
|
// daemonWait is how long install, start and restart wait for the gateway
|
|
// to answer before saying where its log is; a var for the tests.
|
|
var daemonWait = 5 * time.Second
|
|
|
|
// daemonWeb is what a daemon command says of the page the service serves
|
|
// beside the gateway (`magpie serve --web`, HuanCheng on Discord).
|
|
type daemonWeb struct {
|
|
set bool // the command says whether it is served; not set, it stays as it is
|
|
on bool // served
|
|
addr string // where, "" for the default
|
|
}
|
|
|
|
// parseDaemon reads magpie daemon's arguments: the command, and what it
|
|
// says of the page.
|
|
func parseDaemon(args []string) (cmd string, w daemonWeb, err error) {
|
|
cmd = "status"
|
|
if len(args) > 0 {
|
|
cmd, args = args[0], args[1:]
|
|
}
|
|
if cmd == "web" && len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
switch args[0] {
|
|
case "on":
|
|
w.set, w.on = true, true
|
|
case "off":
|
|
w.set = true
|
|
case "status":
|
|
default:
|
|
return "", w, errors.New(daemonUsage)
|
|
}
|
|
args = args[1:]
|
|
}
|
|
if len(args) > 0 && cmd != "install" && cmd != "web" {
|
|
return "", w, errors.New(daemonUsage)
|
|
}
|
|
lan, off := false, false
|
|
for i := 0; i < len(args); i++ {
|
|
switch a := args[i]; {
|
|
case a == "--web" && cmd == "install":
|
|
w.set, w.on = true, true
|
|
case a == "--no-web" && cmd == "install":
|
|
off = true
|
|
case a == "--lan":
|
|
lan = true
|
|
case a == "--addr" && i+1 < len(args):
|
|
i++
|
|
w.addr = args[i]
|
|
case strings.HasPrefix(a, "--addr="):
|
|
w.addr = strings.TrimPrefix(a, "--addr=")
|
|
default:
|
|
return "", w, fmt.Errorf("magpie daemon %s: unknown %q · %s", cmd, a, daemonUsage)
|
|
}
|
|
}
|
|
if w.addr != "" {
|
|
if _, _, err := net.SplitHostPort(w.addr); err != nil {
|
|
return "", w, fmt.Errorf("magpie daemon %s: --addr %s isn't host:port", cmd, w.addr)
|
|
}
|
|
}
|
|
// --lan and --addr say where the page is: it is served
|
|
if lan || w.addr != "" {
|
|
if cmd == "web" && w.set && !w.on || off {
|
|
return "", w, fmt.Errorf("magpie daemon %s: --lan and --addr are for a page that is served", cmd)
|
|
}
|
|
w.set, w.on = true, true
|
|
}
|
|
if off {
|
|
if w.on {
|
|
return "", w, errors.New("magpie daemon install: --web and --no-web both")
|
|
}
|
|
w.set = true
|
|
}
|
|
if w.on && w.addr == "" {
|
|
w.addr = webAddr
|
|
if lan {
|
|
_, port, _ := net.SplitHostPort(webAddr)
|
|
w.addr = "0.0.0.0:" + port
|
|
}
|
|
}
|
|
return cmd, w, nil
|
|
}
|
|
|
|
// webOptions is what the service is to serve: what w asks, or what it
|
|
// serves now (prevAddr, prevKey) when w says nothing. The page keeps its
|
|
// key across installs, so a browser stays signed in: MAGPIE_WEB_KEY
|
|
// (envKey) when set, else the key the service has, else a new one.
|
|
func webOptions(w daemonWeb, prevAddr, prevKey, envKey string) (daemon.Options, error) {
|
|
if !w.set {
|
|
return daemon.Options{Web: prevAddr, Key: prevKey}, nil
|
|
}
|
|
if !w.on {
|
|
return daemon.Options{}, nil
|
|
}
|
|
key := envKey
|
|
if key == "" {
|
|
key = prevKey
|
|
}
|
|
if key == "" {
|
|
key = gui.NewWebKey()
|
|
}
|
|
if err := gui.CheckWebKey(key); err != nil {
|
|
return daemon.Options{}, err
|
|
}
|
|
return daemon.Options{Web: w.addr, Key: key}, nil
|
|
}
|
|
|
|
// magpie daemon …: `magpie serve` as a service of the user's, started at
|
|
// boot or login and again when it stops (systemd --user, launchd).
|
|
func daemonCmd(args []string) error {
|
|
cmd, web, err := parseDaemon(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch cmd {
|
|
case "status":
|
|
return daemonStatus()
|
|
case "install":
|
|
err = daemonInstall(web)
|
|
case "web":
|
|
if !web.set {
|
|
return daemonWebStatus()
|
|
}
|
|
if st, _ := daemon.Get(); !st.Installed && !web.on {
|
|
fmt.Println("the magpie service isn't installed ·", muted.Render("magpie daemon install"))
|
|
return nil
|
|
}
|
|
err = daemonInstall(web)
|
|
case "uninstall", "remove":
|
|
if err := daemon.Uninstall(); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "the magpie service is removed")
|
|
return nil
|
|
case "start":
|
|
err = daemon.Start()
|
|
case "stop":
|
|
if err := daemon.Stop(); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "the magpie service is stopped")
|
|
return nil
|
|
case "restart":
|
|
err = daemon.Restart()
|
|
default:
|
|
return errors.New(daemonUsage)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
st, _ := daemon.Get()
|
|
switch cmd {
|
|
case "install", "web":
|
|
fmt.Println(green.Render("✓"), "magpie serves the gateway as a service, from boot or login, started again when it stops")
|
|
fmt.Println(muted.Render(" file "), st.File)
|
|
if a, k := daemon.Web(); a != "" {
|
|
printDaemonWeb(a, k)
|
|
} else if cmd == "web" {
|
|
fmt.Println(muted.Render(" page not served"))
|
|
}
|
|
if a := daemon.Linger(); a != "" {
|
|
fmt.Println(amber.Render("!"), a)
|
|
}
|
|
if daemon.Node() == "" {
|
|
fmt.Println(amber.Render("!"), "no node on the service's PATH: plugins, and the MCP servers they start, need Node.js; install it, then run `magpie daemon install` again")
|
|
}
|
|
default:
|
|
fmt.Println(green.Render("✓"), "the magpie service is "+cmd+"ed")
|
|
}
|
|
deadline := time.Now().Add(daemonWait)
|
|
for {
|
|
if o := gateway.ServedBy(); o.Running {
|
|
fmt.Println(green.Render("●"), "the gateway answers on", bold.Render(gateway.URL()))
|
|
return nil
|
|
}
|
|
if !time.Now().Before(deadline) {
|
|
break
|
|
}
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
fmt.Println(amber.Render("!"), "the gateway doesn't answer on", gateway.URL(), "yet ·", st.Logs)
|
|
return nil
|
|
}
|
|
|
|
// daemonInstall installs the service, serving the page as w says.
|
|
func daemonInstall(w daemonWeb) error {
|
|
a, k := daemon.Web()
|
|
o, err := webOptions(w, a, k, os.Getenv("MAGPIE_WEB_KEY"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return daemon.Install(o)
|
|
}
|
|
|
|
// printDaemonWeb prints the service's page: its link, and on the network
|
|
// its links there and what that lets anyone with one do.
|
|
func printDaemonWeb(addr, key string) {
|
|
fmt.Println(muted.Render(" page "), webLink(addr, key))
|
|
host, port, _ := net.SplitHostPort(addr)
|
|
q := "/?k=" + key
|
|
if ip := net.ParseIP(host); host == "" || ip != nil && !ip.IsLoopback() {
|
|
for _, l := range gui.NetworkLinks(port, q) {
|
|
fmt.Println(muted.Render(" on the network"), l)
|
|
}
|
|
fmt.Println(amber.Render("!"), lanWarning)
|
|
} else if l := proxyLink(q); l != "" {
|
|
fmt.Println(muted.Render(" through your proxy"), l)
|
|
}
|
|
}
|
|
|
|
// daemonWebStatus: magpie daemon web — the page the service serves.
|
|
func daemonWebStatus() error {
|
|
st, err := daemon.Get()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !st.Installed {
|
|
fmt.Println("the magpie service isn't installed ·", muted.Render("magpie daemon install --web"))
|
|
return nil
|
|
}
|
|
a, k := daemon.Web()
|
|
if a == "" {
|
|
fmt.Println("the magpie service serves no page ·", muted.Render("magpie daemon web on"))
|
|
return nil
|
|
}
|
|
printDaemonWeb(a, k)
|
|
return nil
|
|
}
|
|
|
|
func daemonStatus() error {
|
|
st, err := daemon.Get()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
served := gateway.ServedBy()
|
|
if !st.Installed {
|
|
fmt.Println("the magpie service isn't installed ·", muted.Render("magpie daemon install"))
|
|
if served.Running {
|
|
fmt.Println(muted.Render(" another magpie (the app, magpie serve) answers on"), gateway.URL())
|
|
}
|
|
return nil
|
|
}
|
|
mark, word := amber.Render("○"), "not running"
|
|
if st.Running {
|
|
mark, word = green.Render("●"), "running"
|
|
}
|
|
line := "the magpie service is " + word
|
|
if st.PID > 0 {
|
|
line += " · pid " + strconv.Itoa(st.PID)
|
|
}
|
|
fmt.Println(mark, line)
|
|
if st.State != "" {
|
|
fmt.Println(muted.Render(" state "), st.State)
|
|
}
|
|
if st.LastExit != "" {
|
|
fmt.Println(muted.Render(" last exit"), st.LastExit)
|
|
}
|
|
start := "at boot or login"
|
|
if !st.Enabled {
|
|
start = "only when started"
|
|
}
|
|
fmt.Println(muted.Render(" start "), start)
|
|
fmt.Println(muted.Render(" file "), st.File)
|
|
fmt.Println(muted.Render(" logs "), st.Logs)
|
|
if a, k := daemon.Web(); a != "" {
|
|
printDaemonWeb(a, k)
|
|
}
|
|
if served.Running {
|
|
fmt.Println(muted.Render(" gateway"), "answers on", gateway.URL(), muted.Render(served.Version))
|
|
} else {
|
|
fmt.Println(muted.Render(" gateway"), "doesn't answer on", gateway.URL())
|
|
}
|
|
for _, a := range st.Advice {
|
|
fmt.Println(amber.Render("!"), a)
|
|
}
|
|
return nil
|
|
}
|