diff --git a/README.md b/README.md index 921024f..eb30d6f 100644 --- a/README.md +++ b/README.md @@ -159,10 +159,13 @@ or the [Telegram group](https://t.me/+6UwIu9F4gZQxNjM0). **Hosting without a homeserver.** Anyone can help the swarm: **Host** in the menu sets up this machine or a server over SSH (Linux with systemd, - x86 or ARM) to keep the newest publish and share it, everything (about - 19 GB) or only the container images (about 15 GB). An hourly timer on - that machine fetches each new signed publish and lets the old one go; - interzone doesn't need to keep running. It listens on TCP 4001 and + x86 or ARM) to keep the newest publish and share it: any mix of the + container images (about 16 GB), the OS, Docker and Python packages + (3.7 GB), the playbook and roles in git (50 MB) and interzone and IPFS + themselves (230 MB). An hourly timer on that machine fetches each new + signed publish and lets the old one go; interzone doesn't need to keep + 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`. diff --git a/internal/cli/ipfs.go b/internal/cli/ipfs.go index 773479b..2e3bec8 100644 --- a/internal/cli/ipfs.go +++ b/internal/cli/ipfs.go @@ -37,7 +37,7 @@ func ServeRegistry(args []string) error { func IPFSFollow(args []string) error { fs := flag.NewFlagSet("ipfs-follow", flag.ExitOnError) api := fs.String("api", "http://127.0.0.1:45001", "the IPFS daemon's RPC API") - parts := fs.String("parts", "all", "what to host: all, or images") + parts := fs.String("parts", "all", "what to host: all, images, or some of oci,packages,git,bin") state := fs.String("state", filepath.Join(os.Getenv("STATE_DIRECTORY"), "follow.json"), "where to remember what is pinned") parse(fs, args) return ipfs.Follow(context.Background(), *api, *parts, *state, func(s string) { fmt.Println(s) }) diff --git a/internal/deploy/ipfs.go b/internal/deploy/ipfs.go index 59e5422..bdb4f94 100644 --- a/internal/deploy/ipfs.go +++ b/internal/deploy/ipfs.go @@ -152,14 +152,19 @@ func (r *Runner) ipfsJoin(ctx context.Context, step Step, seed bool) (*ipfs.Inde return idx, arch, nil } -// ipfsHost makes the target a seeder that keeps the newest publish (all of -// it, or the images) without a homeserver: a timer runs ipfs-follow, which +// ipfsHost makes the target a seeder that keeps the newest publish (the +// parts in Options.HostParts) without a homeserver: a timer runs ipfs-follow, which // pins each new signed publish and lets the last one go. func (r *Runner) ipfsHost(ctx context.Context) error { r.begin(StepHost) parts := r.opts.HostParts - if parts != "all" && parts != "images" { - return fmt.Errorf("choose what to host: all or images") + names, err := ipfs.Parts(parts) + if err != nil { + return fmt.Errorf("choose what to host: %w", err) + } + var gb float64 + for _, n := range names { + gb += PartGB[n] } idx, arch, err := r.ipfsJoin(ctx, StepHost, true) if err != nil { @@ -171,14 +176,16 @@ func (r *Runner) ipfsHost(ctx context.Context) error { if _, err := r.output(ctx, r.cfg.Target, followScript(parts)); err != nil { return fmt.Errorf("setting up the hourly update: %w", err) } - size := map[string]string{"all": "about 19 GB", "images": "about 15 GB"}[parts] - r.note(StepHost, "the server is downloading "+size+" in the background and shares it as it arrives; "+ + 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") return nil } // followScript installs and starts ipfs-follow's service and hourly timer. +// PartGB is roughly how big each part of a publish is, in GB. +var PartGB = map[string]float64{"oci": 16, "packages": 3.7, "git": 0.05, "bin": 0.23} + func followScript(parts string) string { return `set -e cat > /etc/systemd/system/interzone-ipfs-follow.service <<'EOF' diff --git a/internal/deploy/ipfs_stats.go b/internal/deploy/ipfs_stats.go index 4617cf2..1046f6c 100644 --- a/internal/deploy/ipfs_stats.go +++ b/internal/deploy/ipfs_stats.go @@ -10,6 +10,7 @@ import ( "time" "interzone/internal/config" + "interzone/internal/ipfs" ) // HostStats is what a hosting node (ActionHost) is doing, read from the @@ -20,8 +21,8 @@ type HostStats struct { Following bool // ipfs-follow is fetching a publish right now Failed bool // its last run failed NextCheck time.Time - Parts string // all or images - Last string // ipfs-follow's last log line + Parts []string // what it hosts, from PartNames + Last string // ipfs-follow's last log line Peers int RepoSize int64 @@ -60,9 +61,9 @@ echo installed yes echo active $(systemctl is-active interzone-ipfs) echo follow $(systemctl is-active interzone-ipfs-follow.service) echo result $(systemctl show interzone-ipfs-follow.service -p Result --value) -n=$(systemctl show interzone-ipfs-follow.timer -p NextElapseUSecRealtime --value) +n=$(systemctl list-timers interzone-ipfs-follow.timer --no-pager --no-legend | awk '{print $2, $3, $4}') [ -n "$n" ] && echo next $(date -d "$n" +%s 2>/dev/null) -echo parts $(grep -o -- '-parts [a-z]*' /etc/systemd/system/interzone-ipfs-follow.service | cut -d' ' -f2) +echo parts $(grep -o -- '-parts [a-z,]*' /etc/systemd/system/interzone-ipfs-follow.service | cut -d' ' -f2) echo last $(journalctl -u interzone-ipfs-follow -n 1 -o cat --no-pager 2>/dev/null | tail -c 300) echo free $(df -B1 --output=avail ` + ipfsRepo + ` | tail -1) systemctl is-active -q interzone-ipfs || exit 0 @@ -105,7 +106,7 @@ func parseHostStats(out string) *HostStats { s.NextCheck = time.Unix(n, 0) } case "parts": - s.Parts = v + s.Parts, _ = ipfs.Parts(v) case "last": s.Last = strings.TrimSpace(v) case "free": @@ -166,7 +167,7 @@ func parseHostStats(out string) *HostStats { } } for _, p := range s.PublishedParts { - if s.Parts == "all" || p.Name == "oci" { + if slices.Contains(s.Parts, p.Name) { s.Wanted += p.Size if p.Pinned { s.Received += p.Size diff --git a/internal/ui/host.go b/internal/ui/host.go index 77105b0..335d0b4 100644 --- a/internal/ui/host.go +++ b/internal/ui/host.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "slices" "strings" "time" @@ -13,6 +14,7 @@ import ( "interzone/internal/config" "interzone/internal/deploy" + "interzone/internal/ipfs" ) // ── Host ─────────────────────────────────────────────────────────── @@ -65,10 +67,12 @@ type hostScreen struct { func newHostScreen() *hostScreen { f := newForm( - choiceField("parts", "What to host", []string{"all", "images"}, - []string{"Everything (about 19 GB)", "Container images only (about 15 GB)"}, "all", - "Everything lets other servers set up from scratch through yours: the playbook, OS, Docker and Python packages, and every image. "+ - "New publishes replace the old one every hour."), + toggleField("part:oci", "Host the container images (about 16 GB)", true, + "Host any mix of parts. All four let other servers set up from scratch through yours; "+ + "new publishes replace the old one every hour."), + toggleField("part:packages", "Host the OS, Docker and Python packages (about 3.7 GB)", true, ""), + toggleField("part:git", "Host the playbook and its roles in git (about 50 MB)", true, ""), + toggleField("part:bin", "Host interzone and IPFS themselves (about 230 MB)", true, ""), choiceField("mode", "Host where?", []string{"local", "ssh"}, []string{"This machine", "A server over SSH"}, "local", "A Linux machine with systemd, x86 or ARM. It needs no homeserver."), @@ -149,15 +153,24 @@ func (s *hostScreen) poll() tea.Cmd { func (s *hostScreen) start() tea.Cmd { cfg := config.Default() cfg.Target = s.target() - f := s.form if cfg.Target.Mode == "ssh" && cfg.Target.Host == "" { s.err = fmt.Errorf("type the server's address first") return nil } + var parts []string + for _, n := range ipfs.PartNames { + if s.form.get("part:" + n).on { + parts = append(parts, n) + } + } + if len(parts) == 0 { + s.err = fmt.Errorf("pick at least one thing to host") + return nil + } ctx, cancel := context.WithCancel(context.Background()) s.gen++ s.cancel, s.running, s.done, s.err, s.log, s.scroll = cancel, true, false, nil, nil, 0 - s.events = deploy.Start(ctx, cfg, deploy.Options{Action: deploy.ActionHost, HostParts: f.value("parts")}) + s.events = deploy.Start(ctx, cfg, deploy.Options{Action: deploy.ActionHost, HostParts: strings.Join(parts, ",")}) return tea.Batch(s.spin.Tick, s.wait()) } @@ -289,7 +302,7 @@ func (s *hostScreen) statsView(w int) []string { row("Status", sBad.Render("● stopped")+sFaint.Render(" enter starts it again")) return rows } - state := sGood.Render("● sharing") + sMuted.Render(fmt.Sprintf(" · %s · %s", plural(st.Peers, "peer"), map[string]string{"all": "everything", "images": "images only"}[st.Parts])) + state := sGood.Render("● sharing") + sMuted.Render(fmt.Sprintf(" · %s · %s", plural(st.Peers, "peer"), strings.Join(partNames(st.Parts), ", "))) row("Status", state) if st.Wanted > 0 { frac := float64(st.Received) / float64(st.Wanted) @@ -300,14 +313,14 @@ func (s *hostScreen) statsView(w int) []string { row("Copy", meter(frac, min(24, w/4))+sMuted.Render(fmt.Sprintf(" %s · %s of %s", note, humanBytes(st.Received), humanBytes(st.Wanted)))) var parts []string for _, p := range st.PublishedParts { - if st.Parts != "all" && p.Name != "oci" { + if !slices.Contains(st.Parts, p.Name) { continue } mark := sFaint.Render("…") if p.Pinned { mark = sGood.Render("✓") } - parts = append(parts, mark+" "+map[string]string{"oci": "images", "packages": "packages", "git": "git", "bin": "programs"}[p.Name]+" "+sFaint.Render(humanBytes(p.Size))) + parts = append(parts, mark+" "+partNames([]string{p.Name})[0]+" "+sFaint.Render(humanBytes(p.Size))) } row("Parts", strings.Join(parts, " ")) } @@ -338,7 +351,11 @@ func (s *hostScreen) statsView(w int) []string { update = sBad.Render("the last update failed") } if !st.NextCheck.IsZero() && !st.Following { - update += sFaint.Render(" · next check in " + max(0, time.Until(st.NextCheck)).Round(time.Minute).String()) + in := "under a minute" + if d := time.Until(st.NextCheck); d >= time.Minute { + in = strings.TrimSuffix(d.Round(time.Minute).String(), "0s") // "1h0m", "45m" + } + update += sFaint.Render(" · next check in " + in) } if st.Published != "" { update += sFaint.Render(" · the publish of " + strings.NewReplacer("T", " ", "Z", " UTC").Replace(st.Published)) @@ -349,3 +366,12 @@ func (s *hostScreen) statsView(w int) []string { } return rows } + +// partNames says what parts of a publish are, the way the form does. +func partNames(parts []string) []string { + names := make([]string, len(parts)) + for i, p := range parts { + names[i] = map[string]string{"oci": "images", "packages": "packages", "git": "git", "bin": "programs"}[p] + } + return names +} diff --git a/internal/ui/layout_test.go b/internal/ui/layout_test.go index 2c9b16e..6ccd899 100644 --- a/internal/ui/layout_test.go +++ b/internal/ui/layout_test.go @@ -106,7 +106,7 @@ func TestLayout(t *testing.T) { "the server is downloading about 19 GB in the background and shares it as it arrives"} hs.statsFor = targetKey(hs.target()) hs.in, hs.out = []float64{0, 4e6, 9e6, 7e6, 10e6}, []float64{0, 0, 1e5, 3e5, 2e5} - hs.stats = &deploy.HostStats{Installed: true, Active: true, Following: true, Parts: "all", Peers: 3, + hs.stats = &deploy.HostStats{Installed: true, Active: true, Following: true, Parts: []string{"oci", "packages", "git", "bin"}, Peers: 3, RepoSize: 6 << 30, DiskFree: 80 << 30, RateIn: 10e6, RateOut: 2e5, TotalIn: 6 << 30, TotalOut: 300 << 20, BlocksSent: 1200, DataSent: 290 << 20, BlocksReceived: 25000, DataReceived: 6 << 30, Published: "2026-10-29T03:00:00Z", Wanted: 19 << 30, Received: 6 << 30, diff --git a/internal/ui/testdata/layout/host-120x36.txt b/internal/ui/testdata/layout/host-120x36.txt index 91f6f27..d4dc4a7 100644 --- a/internal/ui/testdata/layout/host-120x36.txt +++ b/internal/ui/testdata/layout/host-120x36.txt @@ -8,11 +8,11 @@ │ ││ on TCP port 4001 (open it in the firewall), and the swarm's members can see that it │ │ ▸ Test ││ holds Matrix software. │ │ ││ │ -│ ▾ Host ││ ▌ What to host │ -│ ▌ ✓ Host for IPFS ││ ▌ ◉ Everything (about 19 GB) ○ Container images only (about 15 GB) │ -│ ││ ▌ Everything lets other servers set up from scratch through yours: the playbook, │ -│ ▾ Servers ││ ▌ OS, Docker and Python packages, and every image. New publishes replace the old │ -│ ● example.org ││ ▌ one every hour. │ +│ ▾ Host ││ ▌ ● Host the container images (about 16 GB) on │ +│ ▌ ✓ Host for IPFS ││ ▌ Host any mix of parts. All four let other servers set up from scratch through │ +│ ││ ▌ yours; new publishes replace the old one every hour. │ +│ ▾ Servers ││ │ +│ ● example.org ││ ● Host the OS, Docker and Python packages (about 3.7 GB) on │ │ ││ ▼ more below │ │ ││ │ │ ││ ✓ hosting. It keeps going without interzone; enter sets it up again. │ @@ -20,7 +20,7 @@ │ ││ the server is downloading about 19 GB in the background and shares it as it arrives │ │ ││ │ │ ││ ◆ Hosting on this machine ──────────────────────────────────────────────────────────── │ -│ ││ Status ● sharing · 3 peers · everything │ +│ ││ Status ● sharing · 3 peers · images, packages, git, programs │ │ ││ Copy ███████░░░░░░░░░░░░░░ 32% · 6.0 GB of 19.0 GB │ │ ││ Parts … packages 3.0 GB … images 15.0 GB ✓ git 200 MB ✓ programs 300 MB │ │ ││ Download ▁▄▇▆█ 9.5 MB/s · 6.0 GB in all │ diff --git a/internal/ui/testdata/layout/host-46x40.txt b/internal/ui/testdata/layout/host-46x40.txt index 9399795..6b82698 100644 --- a/internal/ui/testdata/layout/host-46x40.txt +++ b/internal/ui/testdata/layout/host-46x40.txt @@ -9,11 +9,11 @@ firewall), and the swarm's members can see that it holds Matrix software. - ▌ What to host - ▌ ◉ Everything (about 19 GB) - ▌ ○ Container images only (about 15 GB) - ▌ Everything lets other servers set up - ▌ from scratch through yours: the + ▌ ● Host the container images (about 16 + ▌ GB) + ▌ on + ▌ Host any mix of parts. All four let + ▌ other servers set up from scratch ▼ more below ✓ hosting. It keeps going without interzone; @@ -23,7 +23,7 @@ the background and shares it as it arrives ◆ Hosting on this machine ────────────────── - Status ● sharing · 3 peers · everythi + Status ● sharing · 3 peers · images, Copy ███░░░░░░░░ 32% · 6.0 GB of 1 Parts … packages 3.0 GB … images 15 Download ▁▄▇▆█ 9.5 MB/s diff --git a/internal/ui/testdata/layout/host-48x20.txt b/internal/ui/testdata/layout/host-48x20.txt index 738a54e..3e26921 100644 --- a/internal/ui/testdata/layout/host-48x20.txt +++ b/internal/ui/testdata/layout/host-48x20.txt @@ -8,11 +8,11 @@ swarm's members can see that it holds Matrix software. - ▌ What to host - ▌ ◉ Everything (about 19 GB) - ▌ ○ Container images only (about 15 GB) - ▌ Everything lets other servers set up from - ▌ scratch through yours: the playbook, OS, + ▌ ● Host the container images (about 16 GB) + ▌ on + ▌ Host any mix of parts. All four let other + ▌ servers set up from scratch through + ▌ yours; new publishes replace the old one ▼ more below ✓ hosting. It keeps going without interzone; e