Host shows interzone-mirror's IPFS node when this machine runs one
It's read through docker, without sudo, and pins only the root, so every part under a pinned root counts as kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca0da60b18
commit
a45220caa4
3 files changed
+75
-14
No files matched your search
@@ -2,8 +2,10 @@ package deploy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -23,6 +25,7 @@ type HostStats struct {
|
||||
NextCheck time.Time
|
||||
Parts []string // what it hosts, from PartNames
|
||||
Last string // ipfs-follow's last log line
|
||||
Mirror string // the container, when the node is interzone-mirror's entry node
|
||||
|
||||
Peers int
|
||||
RepoSize int64
|
||||
@@ -48,16 +51,37 @@ type HostPart struct {
|
||||
|
||||
// ReadHostStats asks the target's IPFS node for its numbers.
|
||||
func ReadHostStats(ctx context.Context, t config.Target) (*HostStats, error) {
|
||||
r := &Runner{cfg: &config.Config{Target: t}}
|
||||
out, err := r.output(ctx, t, hostStatsScript)
|
||||
// interzone-mirror's node is read through docker, which needs no sudo;
|
||||
// nothing hosted needs none either.
|
||||
cmd := targetCmd(ctx, t, mirrorStatsScript+`
|
||||
[ -f /etc/systemd/system/interzone-ipfs.service ] || { echo installed no; exit 0; }
|
||||
`+asRoot("sh -c "+shellQuote(hostStatsScript)))
|
||||
cmd.Stdin = strings.NewReader(t.BecomePass + "\n")
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("%s", lastLine(stderr.String(), err))
|
||||
}
|
||||
return parseHostStats(out), nil
|
||||
return parseHostStats(string(out)), nil
|
||||
}
|
||||
|
||||
const hostStatsScript = `[ -f /etc/systemd/system/interzone-ipfs.service ] || { echo installed no; exit 0; }
|
||||
// mirrorStatsScript finds interzone-mirror's compose service ipfs (see its
|
||||
// IPFS.md), which publishes every part, and reads it like a hosting node.
|
||||
const mirrorStatsScript = `c=$(docker ps -a --filter label=com.docker.compose.service=ipfs --format '{{.Names}} {{.Image}}' 2>/dev/null | awk '$2 ~ /kubo/ {print $1; exit}')
|
||||
if [ -n "$c" ]; then
|
||||
echo installed yes
|
||||
echo mirror $c
|
||||
echo active $(docker inspect -f '{{if .State.Running}}active{{end}}' $c)
|
||||
echo parts all
|
||||
echo free $(df -B1 --output=avail "$(docker inspect -f '{{range .Mounts}}{{if eq .Destination "/data/ipfs"}}{{.Source}}{{end}}{{end}}' $c)" | tail -1)
|
||||
docker inspect -f '{{.State.Running}}' $c | grep -q true || exit 0
|
||||
ipfs() { docker exec $c ipfs --timeout=10s "$@"; }
|
||||
` + ipfsStatsScript + `
|
||||
exit 0
|
||||
fi`
|
||||
|
||||
const hostStatsScript = `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)
|
||||
@@ -68,15 +92,20 @@ echo last $(journalctl -u interzone-ipfs-follow -n 1 -o cat --no-pager 2>/dev/nu
|
||||
echo free $(df -B1 --output=avail ` + ipfsRepo + ` | tail -1)
|
||||
systemctl is-active -q interzone-ipfs || exit 0
|
||||
ipfs() { runuser -u ` + ipfsUser + ` -- env IPFS_PATH=` + ipfsRepo + ` ` + ipfsBin + `/ipfs --api ` + ipfsAPI + ` --timeout=10s "$@"; }
|
||||
echo peers $(ipfs swarm peers | wc -l)
|
||||
` + ipfsStatsScript
|
||||
|
||||
// ipfsStatsScript reads a running node through the shell function ipfs.
|
||||
const ipfsStatsScript = `echo peers $(ipfs swarm peers | wc -l)
|
||||
echo repo $(ipfs repo stat --size-only --enc=json)
|
||||
echo bw $(ipfs stats bw)
|
||||
echo bitswap "{$(ipfs stats bitswap --enc=json | grep -oE '"(BlocksSent|DataSent|BlocksReceived|DataReceived)":[0-9]+' | paste -sd,)}"
|
||||
# The publish ipfs-follow works on: the newest root it pinned directly.
|
||||
root=$(for r in $(ipfs pin ls --type=direct --quiet); do
|
||||
# The newest publish: ipfs-follow pins its root directly, the mirror's
|
||||
# publish recursively.
|
||||
root=$(for r in $(ipfs pin ls --type=direct --quiet; ipfs pin ls --type=recursive --quiet); do
|
||||
echo "$(ipfs cat --offline "$r/index.json" 2>/dev/null | grep -o '"version": *[0-9]*' | grep -o '[0-9]*$') $r"
|
||||
done | sort -n | tail -1 | cut -d' ' -f2)
|
||||
[ -n "$root" ] || exit 0
|
||||
echo root $root
|
||||
echo index $(ipfs cat --offline "$root/index.json" | head -c 65536)
|
||||
echo pins $(ipfs pin ls --type=recursive --quiet)
|
||||
echo links $(ipfs dag get "$root" | head -c 65536)`
|
||||
@@ -86,7 +115,8 @@ var siUnits = map[string]float64{"B": 1, "kB": 1e3, "MB": 1e6, "GB": 1e9, "TB":
|
||||
|
||||
func parseHostStats(out string) *HostStats {
|
||||
s := &HostStats{}
|
||||
var pinned []string // printed before links
|
||||
var root string
|
||||
var pinned []string // both printed before links
|
||||
sc := bufio.NewScanner(strings.NewReader(out))
|
||||
sc.Buffer(make([]byte, 128<<10), 128<<10)
|
||||
num := func(v string) int64 { n, _ := strconv.ParseInt(strings.TrimSpace(v), 10, 64); return n }
|
||||
@@ -145,6 +175,10 @@ func parseHostStats(out string) *HostStats {
|
||||
var x struct{ Created string }
|
||||
_ = json.Unmarshal([]byte(v), &x)
|
||||
s.Published = x.Created
|
||||
case "mirror":
|
||||
s.Mirror = v
|
||||
case "root":
|
||||
root = v
|
||||
case "pins":
|
||||
pinned = strings.Fields(v)
|
||||
case "links": // the root's own block, so sizes are known before the parts arrive
|
||||
@@ -161,7 +195,7 @@ func parseHostStats(out string) *HostStats {
|
||||
for _, l := range x.Links {
|
||||
switch l.Name {
|
||||
case "packages", "oci", "git", "bin":
|
||||
s.PublishedParts = append(s.PublishedParts, HostPart{Name: l.Name, Size: l.Tsize, Pinned: slices.Contains(pinned, l.Hash.Link)})
|
||||
s.PublishedParts = append(s.PublishedParts, HostPart{Name: l.Name, Size: l.Tsize, Pinned: slices.Contains(pinned, l.Hash.Link) || slices.Contains(pinned, root)})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,18 @@ links {"Data":{"/":{"bytes":"CAE"}},"Links":[{"Hash":{"/":"bafypkg"},"Name":"pac
|
||||
if s.Wanted != 10000 || s.Received != 5000 || len(s.PublishedParts) != 3 || !s.PublishedParts[2].Pinned || s.PublishedParts[1].Pinned {
|
||||
t.Fatalf("wanted %d received %d parts %v", s.Wanted, s.Received, s.PublishedParts)
|
||||
}
|
||||
// interzone-mirror's node pins only the root: every part under it is kept.
|
||||
m := parseHostStats(`installed yes
|
||||
mirror interzone-mirror-ipfs-1
|
||||
active active
|
||||
parts all
|
||||
root bafyroot
|
||||
pins bafyroot
|
||||
links {"Links":[{"Hash":{"/":"bafypkg"},"Name":"packages","Tsize":3000},{"Hash":{"/":"bafyoci"},"Name":"oci","Tsize":10000}]}
|
||||
`)
|
||||
if m.Mirror != "interzone-mirror-ipfs-1" || len(m.Parts) != 4 || m.Wanted != 13000 || m.Received != 13000 {
|
||||
t.Fatalf("%+v", m)
|
||||
}
|
||||
if s := parseHostStats("installed no\n"); s.Installed || s.Active {
|
||||
t.Fatalf("%+v", s)
|
||||
}
|
||||
|
||||
+19
-4
@@ -129,14 +129,17 @@ func (s *hostScreen) target() config.Target {
|
||||
func targetKey(t config.Target) string { return t.Mode + " " + t.User + "@" + t.Host + ":" + t.Port }
|
||||
|
||||
// poll reads the node's numbers, unless a reading is on its way. This
|
||||
// machine is only asked if it hosts; an SSH server once its address is in.
|
||||
// machine is only asked if it hosts or runs Docker (interzone-mirror's
|
||||
// node); an SSH server once its address is in.
|
||||
func (s *hostScreen) poll() tea.Cmd {
|
||||
t := s.target()
|
||||
if s.polling || t.Mode == "ssh" && t.Host == "" {
|
||||
return nil
|
||||
}
|
||||
if t.Mode != "ssh" {
|
||||
if _, err := os.Stat("/etc/systemd/system/interzone-ipfs.service"); err != nil {
|
||||
_, unit := os.Stat("/etc/systemd/system/interzone-ipfs.service")
|
||||
_, docker := os.Stat("/var/run/docker.sock")
|
||||
if unit != nil && docker != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -294,12 +297,21 @@ func (s *hostScreen) statsView(w int) []string {
|
||||
return nil
|
||||
}
|
||||
w = min(w, maxFormWidth)
|
||||
rows := []string{"", sectionHeading("Hosting on "+map[bool]string{true: "this machine", false: s.target().Host}[s.target().Mode != "ssh"], w)}
|
||||
where := map[bool]string{true: "this machine", false: s.target().Host}[s.target().Mode != "ssh"]
|
||||
heading := "Hosting on " + where
|
||||
if st.Mirror != "" {
|
||||
heading = "interzone-mirror's node on " + where
|
||||
}
|
||||
rows := []string{"", sectionHeading(heading, w)}
|
||||
row := func(label, value string) {
|
||||
rows = append(rows, " "+lipgloss.NewStyle().Width(12).Render(sMuted.Render(label))+value)
|
||||
}
|
||||
if !st.Active {
|
||||
row("Status", sBad.Render("● stopped")+sFaint.Render(" enter starts it again"))
|
||||
again := " enter starts it again"
|
||||
if st.Mirror != "" {
|
||||
again = " docker start " + st.Mirror + " starts it again"
|
||||
}
|
||||
row("Status", sBad.Render("● stopped")+sFaint.Render(again))
|
||||
return rows
|
||||
}
|
||||
state := sGood.Render("● sharing") + sMuted.Render(fmt.Sprintf(" · %s · %s", plural(st.Peers, "peer"), strings.Join(partNames(st.Parts), ", ")))
|
||||
@@ -344,6 +356,9 @@ func (s *hostScreen) statsView(w int) []string {
|
||||
row("", sBad.Render("✗ not enough free space for the rest"))
|
||||
}
|
||||
update := sMuted.Render("waiting")
|
||||
if st.Mirror != "" {
|
||||
update = sMuted.Render("publishes after each images run")
|
||||
}
|
||||
switch {
|
||||
case st.Following:
|
||||
update = sAccent.Render("updating now")
|
||||
|
||||
Reference in new issue
Block a user