nnlrl_75403 on Discord: a magpie used as another's Remote magpie stayed on
its old version after the local one updated, and had to be updated by
hand on the other computer. Now nothing needs setting up:
- provider.KeepRemotesUpdated (started by the app's updater and by
magpie serve) runs UpdateRemotes 30 s after start and hourly. A
released build with automatic updates on reads each Remote magpie's
version (GET /v1) and, when it is older, asks it through
POST /v1/magpie/update?version=<v> with the provider's key, at most
once in six hours per version. Off here, or a dev build: nothing asked.
- The remote updates as its own automatic update would: the app
downloads and restarts once idle (updater.updateAsked → download,
waitIdle); magpie serve puts the latest in where it runs from, one at
a time, never elevated, and serveFollow restarts into it. Automatic
updates or downloads off there: it answers "off" and stays.
- A copy that can't replace itself says why: a container (docker pull
ghcr.io/yetone/magpie:latest), a package manager's (its command), a
folder it can't write, a build from source. A magpie from before the
route answers 404 ("too old"), a read-only or held key is "refused".
The provider's row on the Providers page adds a short status to its
line ("updating to v…" for 30 min, or "can't update itself" etc.),
with the reason and the command in its tooltip. en/zh/zh-TW/ja/de.
- The route replaces the program, so it takes the Codex reset's
admission: another machine only while shared, never a browser page,
an enabled key with full access from loopback too (401 without one),
read-only 403 gateway_key_read_only, held 403 gateway_key_held. It is
in readOnlyRefused and the CORS route list.
App.js stays under TestCoreScriptBudget by cutting two comments; the
budget is not raised.
Run (on mote, at the tree committed here):
- gofmt -l internal/: clean. go vet of gateway, gui, cli, provider for
darwin, GOOS=linux and GOOS=windows: ok.
- go test -count=1 ./internal/gui ./internal/cli ./internal/provider
./internal/gateway: gui and cli ok but for #5644's two settings tests
(red on main since 6b7ee4a98, owned); provider and gateway were
still running at the push, and go in the full run after it.
- Rebased on origin/main c7c05dcf2, again: gofmt, vet, the new tests,
ReadOnlyKeyEveryRoute, CORS, TestCoreScriptBudget (not raised) and
both GUI files 32 of 32: ok.
- New: TestRemoteUpdateNeedsAFullKey (no key from loopback, Origin null,
an app scheme, *.localhost, a rebound Host, a foreign page with and
without the full key, another machine with none or a wrong key,
read-only and held keys, then not shared; the updater is never
called until a full key asks, from LAN and loopback),
TestRemoteMagpieAskedToFollow (UpdateRemotes against a real gateway:
dev build, same and newer remote, NoAutoUpdate here, asked once per
version, container answer kept, read-only key refused, a 404 magpie
"old"), TestUpdateAskedByAnotherMagpie, TestRemoteUpdateNote,
TestServeUpdateAsked.
- GUI: remote-update.test.cjs (new) and remote-self.test.cjs, Chromium
and WebKit, 1100 and 420 wide, en/zh/ja/de: 32 of 32 ok, with every
key checked in zh, zh-TW, ja and de. i18n-en-no-cjk ok. i18n-keys'
"a plugin's words are its own" is red, the same on origin/main
f8525323d (#5327/#5332, #5426, #5482); its other cases ok.
Broken by hand, each red, then put back: the handler's key check
(loopback without a key got 200 updating), its held-key check, the
route itself (404 = the old code), asking a remote that is current,
ignoring NoAutoUpdate here, asking again for the same version, the
app ignoring automatic updates off, the app never waiting to restart,
serve's single flight (2 downloads), serve's container answer.
The handler's own Origin and not-shared checks stay green when removed:
corsGuard and lanGuard refuse those first; they are kept as the Codex
reset keeps them.
Not tried: a real update of a real remote (a Docker magpie and an app
on another Mac) end to end; Windows and Linux runs of the new tests
(vet only); the full suite (run after the push).
Reported-by: nnlrl_75403
207 lines
6.6 KiB
Go
207 lines
6.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// A Remote magpie follows this one's version (nnlrl_75403 on Discord):
|
|
// once magpie here has updated, every Remote magpie it uses that is on an
|
|
// older release is asked to update itself too, as that magpie's own
|
|
// automatic update would, so the two don't drift apart and nobody has to
|
|
// go to the other computer. Nothing is set up for it: the provider's key
|
|
// is the owner's, and the remote takes the ask only with a gateway key of
|
|
// full access (gateway.remoteUpdate). A remote that can't update itself
|
|
// (Docker, a package manager's copy, a folder it can't write) says why,
|
|
// and the provider's row here shows it.
|
|
|
|
// RemoteUpdatePath has the gateway update this magpie to its channel's
|
|
// latest release, once it is newer than ?version= says the asker runs.
|
|
const RemoteUpdatePath = "/v1/magpie/update"
|
|
|
|
// RemoteUpdate is RemoteUpdatePath's answer, and what is kept here of a
|
|
// remote's last ask.
|
|
type RemoteUpdate struct {
|
|
// updating: it is being put in, and magpie restarts into it once
|
|
// idle; current: nothing newer to put in; off: automatic updates are
|
|
// turned off there; unsupported: it can't update itself (Why);
|
|
// old: a magpie from before RemoteUpdatePath; refused: the key isn't
|
|
// one of full access there
|
|
State string `json:"state"`
|
|
// Version is what the remote runs (as answered), or is updating to
|
|
// (kept here)
|
|
Version string `json:"version,omitempty"`
|
|
// Why it can't: container, package, not-writable, source
|
|
Why string `json:"why,omitempty"`
|
|
// Manager and Command, a package manager's copy: its name and what
|
|
// updates it
|
|
Manager string `json:"manager,omitempty"`
|
|
Command string `json:"command,omitempty"`
|
|
}
|
|
|
|
// remoteUpdateWait is how long a remote is waited for to take the ask:
|
|
// it answers once the download has started, not when it is done.
|
|
const remoteUpdateWait = 20 * time.Second
|
|
|
|
// AskRemoteUpdate asks the Remote magpie p to update itself, this magpie
|
|
// running version.
|
|
func AskRemoteUpdate(ctx context.Context, p Provider, version string) RemoteUpdate {
|
|
b, err := remoteBody(ctx, p, http.MethodPost, RemoteUpdatePath+"?"+url.Values{"version": {version}}.Encode(), remoteUpdateWait)
|
|
switch {
|
|
case err == nil:
|
|
var out RemoteUpdate
|
|
if json.Unmarshal(b, &out) != nil || out.State == "" {
|
|
return RemoteUpdate{State: "unknown"}
|
|
}
|
|
return out
|
|
case err.Error() == errRemoteNoShare:
|
|
return RemoteUpdate{State: "old"}
|
|
case errors.Is(err, errKeyHeld), err.Error() == errRemoteReadOnly, err.Error() == errRemoteKey, err.Error() == errRemoteNotShared:
|
|
return RemoteUpdate{State: "refused"}
|
|
}
|
|
return RemoteUpdate{State: "unknown"}
|
|
}
|
|
|
|
// RemoteVersion is the version the Remote magpie p answers GET /v1 with,
|
|
// "" when it can't be told.
|
|
func RemoteVersion(ctx context.Context, p Provider) string {
|
|
b, err := remoteBody(ctx, p, http.MethodGet, "/v1", RemoteHubWait)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
var info struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
}
|
|
if json.Unmarshal(b, &info) != nil || info.Name != "magpie" {
|
|
return ""
|
|
}
|
|
return info.Version
|
|
}
|
|
|
|
// remoteUpdates is the last ask of each remote, by provider id.
|
|
var remoteUpdates struct {
|
|
sync.Mutex
|
|
m map[string]remoteUpdateAsked
|
|
}
|
|
|
|
type remoteUpdateAsked struct {
|
|
at time.Time
|
|
from string // the version this magpie ran when it asked
|
|
out RemoteUpdate
|
|
}
|
|
|
|
// remoteUpdateAgain is how long a remote that was asked isn't asked again
|
|
// for the same version; remoteUpdateShown, how long "updating" is shown.
|
|
const (
|
|
remoteUpdateAgain = 6 * time.Hour
|
|
remoteUpdateShown = 30 * time.Minute
|
|
)
|
|
|
|
// RemoteUpdateOf is what the Remote magpie id last answered an ask to
|
|
// update, when the row should say something of it: updating (for a
|
|
// while), or why it didn't.
|
|
func RemoteUpdateOf(id string, now time.Time) (RemoteUpdate, bool) {
|
|
remoteUpdates.Lock()
|
|
a, ok := remoteUpdates.m[id]
|
|
remoteUpdates.Unlock()
|
|
switch {
|
|
case !ok, a.out.State == "current", a.out.State == "unknown":
|
|
return RemoteUpdate{}, false
|
|
case a.out.State == "updating" && now.Sub(a.at) > remoteUpdateShown:
|
|
return RemoteUpdate{}, false
|
|
}
|
|
return a.out, true
|
|
}
|
|
|
|
// ForgetRemoteUpdatesForTest drops what remotes answered.
|
|
func ForgetRemoteUpdatesForTest() {
|
|
remoteUpdates.Lock()
|
|
remoteUpdates.m = nil
|
|
remoteUpdates.Unlock()
|
|
}
|
|
|
|
// SetRemoteUpdateForTest keeps out as id's last answer.
|
|
func SetRemoteUpdateForTest(id string, out RemoteUpdate) {
|
|
noteRemoteUpdate(id, "", out, time.Now())
|
|
}
|
|
|
|
func noteRemoteUpdate(id, from string, out RemoteUpdate, now time.Time) {
|
|
remoteUpdates.Lock()
|
|
if remoteUpdates.m == nil {
|
|
remoteUpdates.m = map[string]remoteUpdateAsked{}
|
|
}
|
|
remoteUpdates.m[id] = remoteUpdateAsked{at: now, from: from, out: out}
|
|
remoteUpdates.Unlock()
|
|
}
|
|
|
|
// UpdateRemotes asks every Remote magpie in use (on, not this one) on an
|
|
// older release than version to update itself, each at most once in
|
|
// remoteUpdateAgain for a version. Nothing is asked from a build that
|
|
// isn't a release, nor while automatic updates are off here: the user
|
|
// keeps this magpie where it is, and its remotes with it.
|
|
func UpdateRemotes(ctx context.Context, version string) {
|
|
if !update.Released(version) {
|
|
return
|
|
}
|
|
if s, err := settings.LoadChecked(); err != nil || s.NoAutoUpdate {
|
|
return
|
|
}
|
|
var wg sync.WaitGroup
|
|
for _, p := range remoteMagpies() {
|
|
if p.SelfAnswered() {
|
|
continue
|
|
}
|
|
remoteUpdates.Lock()
|
|
a, asked := remoteUpdates.m[p.ID]
|
|
remoteUpdates.Unlock()
|
|
if asked && a.from == version && time.Since(a.at) < remoteUpdateAgain {
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
have := RemoteVersion(ctx, p)
|
|
if have == "" || !update.Released(have) {
|
|
return // unknown, or a build from source: its own to keep
|
|
}
|
|
if !update.Newer(version, have) {
|
|
noteRemoteUpdate(p.ID, version, RemoteUpdate{State: "current", Version: have}, time.Now())
|
|
return
|
|
}
|
|
out := AskRemoteUpdate(ctx, p, version)
|
|
if out.State == "updating" {
|
|
out.Version = version
|
|
}
|
|
log.Printf("remote magpie %s on %s, asked to update to %s: %s %s", remoteName(p), have, version, out.State, out.Why)
|
|
noteRemoteUpdate(p.ID, version, out, time.Now())
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
// KeepRemotesUpdated runs UpdateRemotes a little after start (this magpie
|
|
// may just have updated) and every hour after, until ctx ends.
|
|
func KeepRemotesUpdated(ctx context.Context, version string) {
|
|
t := time.NewTimer(30 * time.Second)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
}
|
|
UpdateRemotes(ctx, version)
|
|
t.Reset(time.Hour)
|
|
}
|
|
}
|