From d7f17372fda2147c51799d03f2aba73a6b2827e0 Mon Sep 17 00:00:00 2001 From: vvvomit Date: Sat, 31 Oct 2026 01:57:50 +0330 Subject: [PATCH] Host stops sharing with ctrl+x and removes the node with ctrl+d Someone who only hosts for the IPFS swarm had no way to stop but a systemctl line on the server. ctrl+x disables the node and its hourly update; ctrl+d, after asking, also deletes them, everything they downloaded and their user. interzone-mirror's own node is docker's, so neither is offered there. Co-Authored-By: Claude Opus 5.5 --- docs/features.md | 5 +- internal/deploy/ipfs.go | 2 +- internal/deploy/ipfs_stats.go | 30 +++++++ internal/deploy/reach_test.go | 10 +++ internal/ui/host.go | 89 ++++++++++++++++++++- internal/ui/swarmmap_test.go | 40 +++++++++ internal/ui/testdata/layout/host-120x36.txt | 2 +- 7 files changed, 171 insertions(+), 7 deletions(-) diff --git a/docs/features.md b/docs/features.md index 94ceeba..975fe4f 100644 --- a/docs/features.md +++ b/docs/features.md @@ -89,7 +89,10 @@ The short list is in the [README](../README.md). running. While it hosts, the Host screen shows its peers, download progress per part, traffic, what it gave the swarm, disk use and updates. It listens on TCP 4001 and shows the swarm that it holds Matrix software, like a sharing server. - To stop: `systemctl disable --now interzone-ipfs-follow.timer interzone-ipfs`. + On the Host screen, with the machine's details in the form, `ctrl+x` + stops sharing (it stays off after a reboot; enter starts it again) and + `ctrl+d` removes the node and everything it downloaded. By hand: + `systemctl disable --now interzone-ipfs-follow.timer interzone-ipfs`. Below the numbers, when the window is tall enough, a map of Iran shows the node and each peer it's connected to, placed by IP address without diff --git a/internal/deploy/ipfs.go b/internal/deploy/ipfs.go index eab89b3..76ea4a7 100644 --- a/internal/deploy/ipfs.go +++ b/internal/deploy/ipfs.go @@ -225,7 +225,7 @@ func (r *Runner) ipfsHost(ctx context.Context) error { } r.note(StepHost, fmt.Sprintf("the server is downloading about %.1f GB in the background", gb)+" and shares it as it arrives; "+ "it checks for a new publish every hour. Progress: journalctl -u interzone-ipfs-follow") - r.note(StepHost, "to stop hosting: systemctl disable --now interzone-ipfs-follow.timer interzone-ipfs") + r.note(StepHost, "to stop hosting: ctrl+x on this screen, or systemctl disable --now interzone-ipfs-follow.timer interzone-ipfs") return nil } diff --git a/internal/deploy/ipfs_stats.go b/internal/deploy/ipfs_stats.go index 353fdce..041768d 100644 --- a/internal/deploy/ipfs_stats.go +++ b/internal/deploy/ipfs_stats.go @@ -100,6 +100,36 @@ func ReadHostStats(ctx context.Context, t config.Target) (*HostStats, error) { return s, nil } +// StopHosting stops the target's IPFS node and its hourly update, for +// good: they stay off after a reboot. With remove it also deletes them and +// everything they downloaded; a later IPFS deploy or Host run installs +// them again. +func StopHosting(ctx context.Context, t config.Target, remove bool) error { + cmd := targetCmd(ctx, t, asRoot("sh -c "+shellQuote(stopHostingScript(remove)))) + cmd.Stdin = strings.NewReader(t.BecomePass + "\n") + var stderr bytes.Buffer + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("%s", lastLine(stderr.String(), err)) + } + return nil +} + +func stopHostingScript(remove bool) string { + // One unit at a time: disable gives up on all of them if one is missing. + s := `for u in interzone-ipfs-follow.timer interzone-ipfs-follow.service interzone-ipfs.service; do + systemctl disable --now $u 2>/dev/null || true +done` + if remove { + s += ` +rm -f /etc/systemd/system/interzone-ipfs.service /etc/systemd/system/interzone-ipfs-follow.service /etc/systemd/system/interzone-ipfs-follow.timer +systemctl daemon-reload +rm -rf ` + ipfsRepo + ` ` + ipfsBin + ` /var/lib/interzone-ipfs-follow /var/lib/private/interzone-ipfs-follow +userdel ` + ipfsUser + ` 2>/dev/null || true` + } + return s +} + // findMirrorNode sets c to interzone-mirror's IPFS container, if the // machine has one, running or not. const findMirrorNode = `c=$(docker ps -a --filter label=com.docker.compose.service=ipfs --format '{{.Names}} {{.Image}}' 2>/dev/null | awk '$2 ~ /kubo/ {print $1; exit}')` diff --git a/internal/deploy/reach_test.go b/internal/deploy/reach_test.go index c10d2f3..1bfac56 100644 --- a/internal/deploy/reach_test.go +++ b/internal/deploy/reach_test.go @@ -180,3 +180,13 @@ func TestServerName(t *testing.T) { } } } + +func TestStopHostingScript(t *testing.T) { + stop, remove := stopHostingScript(false), stopHostingScript(true) + if !strings.Contains(stop, "disable --now $u") || strings.Contains(stop, "rm ") { + t.Fatalf("stop:\n%s", stop) + } + if !strings.HasPrefix(remove, stop) || !strings.Contains(remove, "rm -rf /var/lib/interzone-ipfs ") { + t.Fatalf("remove:\n%s", remove) + } +} diff --git a/internal/ui/host.go b/internal/ui/host.go index 5220e4e..d7d6805 100644 --- a/internal/ui/host.go +++ b/internal/ui/host.go @@ -50,7 +50,15 @@ func (hostFrameMsg) owner() string { return "Host" } const hostFrame = 200 * time.Millisecond // five frames a second: each redraws most of the screen func (hostStatsMsg) owner() string { return "Host" } -func (hostTickMsg) owner() string { return "Host" } + +// hostStoppedMsg is the end of stopping or removing the node. +type hostStoppedMsg struct { + removed bool + err error +} + +func (hostStoppedMsg) owner() string { return "Host" } +func (hostTickMsg) owner() string { return "Host" } const hostHistory = 60 // readings kept for the traffic sparklines @@ -87,6 +95,10 @@ type hostScreen struct { placing bool // the user is picking home placeIn textinput.Model placeSel int + + confirmRemove bool // ctrl+d asked to remove the node; y does it + stopping bool // stopping or removing it + note string // how that went } // hostComing is a peer joining or leaving the swarm. @@ -101,7 +113,7 @@ type hostComing struct { func (s *hostScreen) mapMode() bool { return !s.showForm && localMirror.Load() } func (s *hostScreen) handlesEsc() bool { - return s.placing || s.showForm && localMirror.Load() && !s.running + return s.placing || s.confirmRemove || s.showForm && localMirror.Load() && !s.running } // mirrorHere says the form's target runs interzone-mirror's node, which @@ -110,6 +122,12 @@ func (s *hostScreen) mirrorHere() bool { return s.stats != nil && s.stats.Mirror != "" && s.statsFor == targetKey(s.target()) } +// hosting says the form's target runs a node interzone set up, which +// ctrl+x can stop and ctrl+d remove. +func (s *hostScreen) hosting() bool { + return s.stats != nil && s.stats.Installed && s.stats.Mirror == "" && s.statsFor == targetKey(s.target()) +} + // moving says whether data is moving with any peer, so the map animates. func (s *hostScreen) moving() bool { for _, r := range s.rates { @@ -214,7 +232,17 @@ func (s *hostScreen) keys() [][2]string { if s.mirrorHere() { return append([][2]string{{"↑↓", "move"}}, back...) } - return append([][2]string{{"enter", "start hosting"}, {"↑↓", "move"}}, back...) + if s.confirmRemove { + return [][2]string{{"y", "remove it"}, {"esc", "cancel"}} + } + keys := [][2]string{{"enter", "start hosting"}, {"↑↓", "move"}} + if s.hosting() && s.stats.Active { + keys = append(keys, [2]string{"ctrl+x", "stop sharing"}) + } + if s.hosting() { + keys = append(keys, [2]string{"ctrl+d", "remove"}) + } + return append(keys, back...) } // enter offers the active server's SSH details, the likeliest machine, @@ -357,9 +385,30 @@ func (s *hostScreen) update(msg tea.Msg) tea.Cmd { return s.poll() } return nil + case hostStoppedMsg: + s.stopping = false + switch { + case m.err != nil: + s.err = m.err + case m.removed: + s.note = "✓ removed, with everything it downloaded. enter sets hosting up again." + default: + s.note = "✓ stopped sharing; it stays off after a reboot." + } + return s.tick(0) case spinner.TickMsg: - return spinning(s.running, &s.spin, m) + return spinning(s.running || s.stopping, &s.spin, m) case tea.KeyMsg: + if s.stopping { + return nil + } + if s.confirmRemove { + s.confirmRemove = false + if m.String() == "y" { + return s.stop(true) + } + return nil + } if s.running { switch m.String() { case "ctrl+x": @@ -390,7 +439,15 @@ func (s *hostScreen) update(msg tea.Msg) tea.Cmd { s.showForm, s.err = false, nil return tea.Batch(s.tick(0), s.frame()) } + if m.String() == "ctrl+x" && s.hosting() && s.stats.Active { + return s.stop(false) + } + if m.String() == "ctrl+d" && s.hosting() { + s.confirmRemove, s.err, s.note = true, nil, "" + return nil + } if m.String() == "enter" { + s.note = "" if s.mirrorHere() { s.err = fmt.Errorf("%s already hosts everything here: interzone-mirror's node, below", map[bool]string{true: "this machine", false: s.target().Host}[s.target().Mode != "ssh"]) return nil @@ -409,6 +466,17 @@ func (s *hostScreen) update(msg tea.Msg) tea.Cmd { return cmd } +// stop stops the node, or removes it, on the form's target. +func (s *hostScreen) stop(remove bool) tea.Cmd { + t := s.target() + s.stopping, s.err, s.note = true, nil, "" + return tea.Batch(s.spin.Tick, func() tea.Msg { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + return hostStoppedMsg{remove, deploy.StopHosting(ctx, t, remove)} + }) +} + // tick asks for a reading after d, replacing any tick asked for before. func (s *hostScreen) tick(d time.Duration) tea.Cmd { s.ticks++ @@ -427,8 +495,21 @@ func (s *hostScreen) view(w, h int) string { switch { case s.running: status = append(status, s.spin.View()+sMuted.Render(" setting up… ctrl+x stops")) + case s.stopping: + status = append(status, s.spin.View()+sMuted.Render(" stopping the node…")) + case s.confirmRemove: + size := "" + if s.stats != nil && s.stats.RepoSize > 0 { + size = " (" + humanBytes(s.stats.RepoSize) + ")" + } + status = append(status, strings.Split(confirmView(min(w, maxFormWidth), + sWarn.Render("Remove hosting from "+map[bool]string{true: "this machine", false: s.target().Host}[s.target().Mode != "ssh"]+"?"), + sSub.Render("Stops the IPFS node and deletes it with everything it downloaded"+size+". A Matrix server installed there keeps running; a later deploy in IPFS mode installs the node again."), + "", confirmKeys("y", "remove it")), "\n")...) case s.err != nil: status = append(status, sBad.Render("✗ "+s.err.Error())) + case s.note != "": + status = append(status, sGood.Render(s.note)) case s.done: status = append(status, sGood.Render("✓ hosting. It keeps going without interzone; enter sets it up again.")) } diff --git a/internal/ui/swarmmap_test.go b/internal/ui/swarmmap_test.go index 453b526..a316110 100644 --- a/internal/ui/swarmmap_test.go +++ b/internal/ui/swarmmap_test.go @@ -78,3 +78,43 @@ func TestHostPlace(t *testing.T) { t.Fatal("not cleared") } } + +func TestHostStop(t *testing.T) { + s := newHostScreen() + s.form.get("mode").choice = 1 + s.form.get("host").input.SetValue("192.0.2.1") + s.stats, s.statsFor = &deploy.HostStats{Installed: true, Active: true, RepoSize: 6 << 30}, targetKey(s.target()) + has := func(key string) bool { + for _, k := range s.keys() { + if k[0] == key { + return true + } + } + return false + } + if !has("ctrl+x") || !has("ctrl+d") { + t.Fatalf("keys %v", s.keys()) + } + s.update(tea.KeyMsg{Type: tea.KeyCtrlD}) + if !s.confirmRemove || !strings.Contains(s.view(100, 40), "Remove hosting from 192.0.2.1?") { + t.Fatal("ctrl+d should ask first") + } + s.update(tea.KeyMsg{Type: tea.KeyEsc}) + if s.confirmRemove || s.stopping { + t.Fatal("esc should keep it") + } + s.update(tea.KeyMsg{Type: tea.KeyCtrlD}) + if cmd := s.update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("y")}); cmd == nil || !s.stopping { + t.Fatal("y should remove it") // the command isn't run: it would reach the target + } + s.update(hostStoppedMsg{removed: true}) + if s.stopping || !strings.Contains(s.view(100, 40), "removed") { + t.Fatal("no word of the removal") + } + + // interzone-mirror's node is docker's to stop, not interzone's. + s.stats.Mirror = "mirror-ipfs-1" + if has("ctrl+x") || has("ctrl+d") { + t.Fatalf("keys %v on the mirror's node", s.keys()) + } +} diff --git a/internal/ui/testdata/layout/host-120x36.txt b/internal/ui/testdata/layout/host-120x36.txt index 1499c07..01cb7c1 100644 --- a/internal/ui/testdata/layout/host-120x36.txt +++ b/internal/ui/testdata/layout/host-120x36.txt @@ -33,4 +33,4 @@ │ ◐ interzone ctrl+y ││ │ │ ◂ fold ctrl+b menu ││ │ ╰────────────────────────╯╰────────────────────────────────────────────────────────────────────────────────────────────╯ - enter start hosting · ↑↓ move · esc back · ctrl+c quit \ No newline at end of file + enter start hosting · ↑↓ move · ctrl+x stop sharing · ctrl+d remove · esc back · ctrl+c quit \ No newline at end of file