Two edges of v0.1.3687's gateway.json:
1. The first update from a magpie older than the record found no gateway.json,
asked 3425, said "isn't running" and left a service on MAGPIE_ADDR=0.0.0.0:10100
on the old version. With no record, no MAGPIE_ADDR and no answer at the usual
address, `magpie update` now tries the installed service's MAGPIE_ADDR (read
from its unit or launch agent, daemon.Addr), the port in Settings and 3425,
and takes the first that answers as magpie. --addr and MAGPIE_ADDR stay the
user's word.
2. A restart asked by `magpie update` waited on "N requests" with no end (Windows
hands nothing over, long streams never finish). The app and magpie serve now
wait at most 10 minutes for that ask, then restart; the output says so
("or in 10 min at the latest, which cuts what is still running"). `magpie
update --force` restarts at once (gateway.SetForceRestarter), and says if the
running magpie is too old to know --force. The app's own look at the disk
(followReplaced) and autonomous serve follow still never cut a request.
Run (mote refused SSH; win-run on xiaomi-gpu): go vet cli/gui/gateway/daemon
(windows), linux nogui vet; go test -run TestUpdate|TestServeFollow|
TestTerminalUpdate|TestRestart over cli, gui, gateway, daemon: ok except
TestUpdateThatWontStart, red on origin/main on the same box too (#6059).
Broken by hand: drain bound off, record fallback off, serve bound off: the new
tests TestTerminalUpdateWaitIsBoundedAndCanBeForced, TestUpdateFindsAMagpieThatWroteNoRecord,
TestServeFollowDrainIsBounded go red.
Not tried: a real systemd service upgrade; darwin cross vet (cgo) and the full
suite (started after the tag if mote is back).
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/yetone/magpie/internal/daemon"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// updateBase is where `magpie update` asks the running magpie to restart.
|
|
// A magpie from before gateway.json never wrote where it listens (ljlwlcz
|
|
// on X: a service with MAGPIE_ADDR=0.0.0.0:10100 was found nowhere by the
|
|
// first update, which said "isn't running" and left it on the old version
|
|
// until a cold start). So when the record is missing and nothing answers at
|
|
// the address the command would use, it looks where such a magpie runs:
|
|
// the address the installed service was written with, the port in Settings,
|
|
// the default port. The first that answers as magpie wins. --addr and
|
|
// MAGPIE_ADDR are the user's word and are never second-guessed.
|
|
func updateBase(explicit string) string {
|
|
if explicit != "" {
|
|
return explicit
|
|
}
|
|
base := gateway.ClientURL()
|
|
if _, ok := gateway.ReadListening(); ok || envAddr() {
|
|
return base
|
|
}
|
|
if updateProbe(base) {
|
|
return base
|
|
}
|
|
for _, u := range updateCandidates() {
|
|
if u != base && updateProbe(u) {
|
|
return u
|
|
}
|
|
}
|
|
return base
|
|
}
|
|
|
|
func envAddr() bool { return os.Getenv("MAGPIE_ADDR") != "" }
|
|
|
|
// updateProbe says whether a magpie answers at u; a var for the tests.
|
|
var updateProbe = func(u string) bool { return gateway.ServedAt(u).Running }
|
|
|
|
// updateCandidates are the places a magpie without a record may run; a var
|
|
// for the tests.
|
|
var updateCandidates = func() []string {
|
|
var out []string
|
|
if a := daemon.Addr(); a != "" {
|
|
if h, p, err := net.SplitHostPort(a); err == nil {
|
|
if ip := net.ParseIP(h); h == "" || ip != nil && ip.IsUnspecified() {
|
|
h = "127.0.0.1"
|
|
}
|
|
out = append(out, "http://"+net.JoinHostPort(h, p))
|
|
}
|
|
}
|
|
out = append(out, gateway.SavedURL(), "http://127.0.0.1:"+strconv.Itoa(settings.DefaultPort))
|
|
return out
|
|
}
|