From bd3bfeff65fe376075fb5e37da426b4f7551ce48 Mon Sep 17 00:00:00 2001 From: vvvomit Date: Fri, 30 Oct 2026 01:08:54 +0330 Subject: [PATCH] Deploys can fetch everything from interzone's IPFS swarm With the IPFS source, the run first installs kubo on the server, joins the private swarm, finds the newest signed publish (by IPNS, or over HTTPS if the swarm is slow) and serves its OCI layout as a local registry. The playbook, roles, packages and images then all come through the server's gateway, an SSH forward reaching it from here. Seeding is a choice the setup asks: seeders keep the daemon running and listen on 4001, others stop it after the run. The publish's playbook commit is pinned, so the roles match the images it carries. Co-Authored-By: Claude Opus 5.5 --- internal/config/config.go | 11 + internal/config/missing_test.go | 14 + internal/config/ports.go | 3 + internal/deploy/bundle.go | 38 ++- internal/deploy/check.go | 20 +- internal/deploy/deploy.go | 21 +- internal/deploy/history.go | 6 +- internal/deploy/images.go | 6 +- internal/deploy/ipfs.go | 472 ++++++++++++++++++++++++++++++++ 9 files changed, 581 insertions(+), 10 deletions(-) create mode 100644 internal/deploy/ipfs.go diff --git a/internal/config/config.go b/internal/config/config.go index d21e7d0..e118b58 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -46,6 +46,14 @@ type Sources struct { APTMirror string // mirror for the server's distro packages PyPIIndex string // used when ansible has to be installed locally Blackout bool + // IPFS fetches everything a deploy needs from interzone's IPFS swarm + // instead of the sources above, which are kept but unused. + IPFS bool `json:",omitempty"` + // IPFSSeed is "yes" if the server keeps serving what it fetched to + // others, "no" if not, "" until asked. + IPFSSeed string `json:",omitempty"` + // IPFSVersion is the newest index a deploy used; older ones are refused. + IPFSVersion int64 `json:",omitempty"` } type SSL struct { @@ -338,6 +346,9 @@ func (c *Config) Missing() []string { out = append(out, p[0]+" can't contain {{, {% or {#") } } + if c.Sources.IPFS && c.Sources.IPFSSeed == "" { + out = append(out, "Server: whether to share what IPFS downloads") + } if c.SSL.Mode == "custom" && (c.SSL.CertPath == "" || c.SSL.KeyPath == "") { out = append(out, "Server: certificate and key paths") } diff --git a/internal/config/missing_test.go b/internal/config/missing_test.go index 4644ed6..7db75d6 100644 --- a/internal/config/missing_test.go +++ b/internal/config/missing_test.go @@ -34,3 +34,17 @@ func TestMissingFlagsPasswordProviderWithMAS(t *testing.T) { t.Fatalf("LDAP without MAS reported: %v", m) } } + +// Seeding over IPFS is asked during setup, never assumed. +func TestMissingIPFSSeedChoice(t *testing.T) { + c := Default() + c.Domain = "example.org" + c.Sources.IPFS = true + if got := strings.Join(c.Missing(), "\n"); !strings.Contains(got, "IPFS") { + t.Fatalf("unanswered seeding not reported: %q", got) + } + c.Sources.IPFSSeed = "no" + if m := c.Missing(); len(m) != 0 { + t.Fatalf("answered seeding reported: %v", m) + } +} diff --git a/internal/config/ports.go b/internal/config/ports.go index 5176c3c..7565029 100644 --- a/internal/config/ports.go +++ b/internal/config/ports.go @@ -64,6 +64,9 @@ func (c *Config) Ports() []Port { out = append(out, Port{For: "Postmoogle (receiving email)", TCP: []string{port("matrix_bridge_postmoogle_smtp_host_bind_port", "25"), port("matrix_bridge_postmoogle_submission_host_bind_port", "587")}}) } + if c.Sources.IPFS && c.Sources.IPFSSeed == "yes" { + out = append(out, Port{For: "IPFS (other servers fetch what this one seeds)", TCP: []string{"4001"}}) + } return out } diff --git a/internal/deploy/bundle.go b/internal/deploy/bundle.go index cfdb0d9..c016c7b 100644 --- a/internal/deploy/bundle.go +++ b/internal/deploy/bundle.go @@ -14,6 +14,8 @@ import ( "runtime" "strings" "time" + + "interzone/internal/ipfs" ) // An offline bundle is a .tar.gz holding everything a deploy downloads @@ -25,6 +27,7 @@ import ( // interzone this binary // playbook/ matrix-docker-ansible-deploy, roles in roles/galaxy // wheels/ ansible + passlib for `pip install --no-index` +// ipfs/ the IPFS daemon for both arches, with the IPFS source on const ( bundleRoot = "interzone-bundle" @@ -148,6 +151,11 @@ func (r *Runner) archive(ctx context.Context) error { return err } } + if r.cfg.Sources.IPFS { + if err := r.bundleKubo(ctx, tw); err != nil { + return err + } + } if err := tw.Close(); err != nil { return err } @@ -167,6 +175,27 @@ func (r *Runner) archive(ctx context.Context) error { return nil } +// bundleKubo adds the IPFS daemon for both arches, so a server can join +// the swarm without anything else from the internet. +func (r *Runner) bundleKubo(ctx context.Context, tw *tar.Writer) error { + idx, _ := indexFromWeb(ctx) + version := ipfs.KuboVersion + if idx != nil { + version = idx.Kubo + } + r.note(StepArchive, "adding the IPFS daemon "+version) + for _, arch := range []string{"amd64", "arm64"} { + if _, _, err := kuboBinary(ctx, arch, version, idx); err != nil { + r.note(StepArchive, "! no IPFS daemon for "+arch+": "+err.Error()) + r.emit(Event{Step: StepArchive, Warn: true}) + } + } + if d := filepath.Join(dataDir(), "ipfs", version); exists(d) { + return addTree(ctx, tw, d, bundleRoot+"/ipfs/"+version, nil) + } + return nil +} + const bundleReadme = `interzone offline bundle Copy this file to the machine you deploy from (it needs no internet), then: @@ -176,7 +205,8 @@ Copy this file to the machine you deploy from (it needs no internet), then: ./interzone-bundle/interzone Container images are not included: point interzone at a registry mirror -inside your network (Server step, "Container registry mirror"). +inside your network (Server step, "Container registry mirror"), or pick +IPFS as the network situation to fetch them from interzone's swarm. ` func addFile(tw *tar.Writer, src, name string) error { @@ -307,6 +337,12 @@ func Unbundle(src, playbookDir string, log func(string)) (*BundleInfo, error) { } log("Python packages installed in " + dst) } + if d := filepath.Join(root, "ipfs"); exists(d) { + if err := copyTree(d, filepath.Join(dataDirFor(playbookDir), "ipfs")); err != nil { + return nil, err + } + log("IPFS daemon kept for the IPFS source") + } return &info, nil } diff --git a/internal/deploy/check.go b/internal/deploy/check.go index 2bddfcf..203f8a1 100644 --- a/internal/deploy/check.go +++ b/internal/deploy/check.go @@ -10,6 +10,7 @@ import ( "time" "interzone/internal/config" + "interzone/internal/ipfs" ) type Check struct { @@ -44,12 +45,25 @@ func Preflight(c *config.Config) []Check { }) } s := c.Sources - add("Playbook git", s.PlaybookGit, func(ctx context.Context) (string, error) { return gitReachable(ctx, s.PlaybookGit) }) - if s.RolesGitBase != "" { + if s.IPFS { + add("IPFS publish", ipfs.IndexURL, func(ctx context.Context) (string, error) { + idx, err := indexFromWeb(ctx) + if err != nil { + return "", err + } + return "signed, from " + idx.Created, nil + }) + add("IPFS entry node", "infamous.g0th.ir", func(ctx context.Context) (string, error) { + return ipfsEntryReachable(ctx) + }) + } else { + add("Playbook git", s.PlaybookGit, func(ctx context.Context) (string, error) { return gitReachable(ctx, s.PlaybookGit) }) + } + if s.RolesGitBase != "" && !s.IPFS { u := strings.TrimRight(s.RolesGitBase, "/") + "/mother-of-all-self-hosting/ansible-role-aux" add("Roles git mirror", s.RolesGitBase, func(ctx context.Context) (string, error) { return gitReachable(ctx, u) }) } - if s.UseRegistry && s.RegistryPrefix != "" { + if s.UseRegistry && s.RegistryPrefix != "" && !s.IPFS { host := strings.SplitN(strings.TrimPrefix(strings.TrimPrefix(s.RegistryPrefix, "https://"), "http://"), "/", 2)[0] add("Container registry", s.RegistryPrefix, func(ctx context.Context) (string, error) { return httpReachable(ctx, "https://"+host+"/v2/") diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 0e1390b..f527e7f 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -52,6 +52,7 @@ const ( StepDryPlaybook StepDryRun StepBulkUsers + StepIPFS ) var stepNames = map[Step]string{ @@ -83,6 +84,7 @@ var stepNames = map[Step]string{ StepDryPlaybook: "Get the newest playbook, beside yours", StepDryRun: "Run it without changing anything", StepBulkUsers: "Create the users", + StepIPFS: "Join the IPFS swarm", } func (s Step) String() string { return stepNames[s] } @@ -272,14 +274,16 @@ type Runner struct { playbookUpdated bool onLine func(Event) // extra parsing for the running command + ipfs *ipfsRun // what an IPFS run started on the server + pinWhy string // why PinnedCommit holds the playbook, if not a rollback } // Start launches the run in the background. Events arrive on the returned // channel, which is closed when the run ends. func Start(ctx context.Context, cfg *config.Config, opts Options) <-chan Event { - // A migration and blackout mirror picks change the run's config; the - // caller applies those changes through events. - if opts.Action == ActionMigrate || opts.Action == ActionDryRun || cfg.Sources.Blackout { + // A migration, blackout mirror picks and IPFS sources change the run's + // config; the caller applies what it keeps through events. + if opts.Action == ActionMigrate || opts.Action == ActionDryRun || cfg.Sources.Blackout || cfg.Sources.IPFS { cfg = cfg.Clone() } if opts.Action == ActionDryRun { @@ -337,8 +341,13 @@ func (r *Runner) run(ctx context.Context) error { StepDryPlaybook: r.dryPlaybook, StepDryRun: r.dryRun, StepBulkUsers: r.bulkUsers, + StepIPFS: r.ipfsSetup, } plan := Plan(r.opts.Action) + if r.cfg.Sources.IPFS && usesDownloads(plan) { + plan = append([]Step{StepIPFS}, plan...) + defer r.ipfsDone() + } r.emit(Event{Plan: plan}) for _, st := range plan { if err := fns[st](ctx); err != nil { @@ -417,7 +426,11 @@ func (r *Runner) playbook(ctx context.Context) error { return err } r.playbookUpdated = true - if err := r.exec(ctx, StepPlaybook, "", "git", "clone", "--depth", "1", "--", r.cfg.Sources.PlaybookGit, dir); err != nil { + depth := []string{"--depth", "1"} + if r.cfg.Sources.IPFS { + depth = nil // dumb HTTP, as IPFS serves git, can't make shallow clones + } + if err := r.exec(ctx, StepPlaybook, "", "git", append(append([]string{"clone"}, depth...), "--", r.cfg.Sources.PlaybookGit, dir)...); err != nil { return err } if pin := r.cfg.PinnedCommit; pin != "" { diff --git a/internal/deploy/history.go b/internal/deploy/history.go index e13b01c..e191f5d 100644 --- a/internal/deploy/history.go +++ b/internal/deploy/history.go @@ -128,7 +128,11 @@ func defaultBranch(dir string) string { // checkout puts the playbook at commit, fetching it first if the shallow // clone doesn't have it. func (r *Runner) checkout(ctx context.Context, dir, commit string) error { - r.note(StepPlaybook, "holding the playbook at "+short(commit)+" (rollback)") + why := r.pinWhy + if why == "" { + why = "rollback" + } + r.note(StepPlaybook, "holding the playbook at "+short(commit)+" ("+why+")") if exec.Command("git", "-C", dir, "cat-file", "-e", commit+"^{commit}").Run() != nil { if err := r.exec(ctx, StepPlaybook, dir, "git", "fetch", "origin", commit); err != nil { if err := r.exec(ctx, StepPlaybook, dir, "git", "fetch", "--unshallow", "origin"); err != nil { diff --git a/internal/deploy/images.go b/internal/deploy/images.go index 979b829..151418b 100644 --- a/internal/deploy/images.go +++ b/internal/deploy/images.go @@ -224,7 +224,11 @@ var authParam = regexp.MustCompile(`(\w+)="([^"]*)"`) // has reports nil if the registry serves ref for arch. func (c *registryClient) has(ctx context.Context, ref, arch string) error { host, repo, tag := ParseRef(ref) - url := "https://" + host + "/v2/" + repo + "/manifests/" + tag + scheme := "https://" + if strings.HasPrefix(host, "127.0.0.1:") { // as Docker does: the IPFS deploy's serve-registry + scheme = "http://" + } + url := scheme + host + "/v2/" + repo + "/manifests/" + tag resp, err := c.get(ctx, url, "") if err != nil { return err diff --git a/internal/deploy/ipfs.go b/internal/deploy/ipfs.go new file mode 100644 index 0000000..9a7fd54 --- /dev/null +++ b/internal/deploy/ipfs.go @@ -0,0 +1,472 @@ +package deploy + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "context" + "crypto/sha256" + "crypto/sha512" + "encoding/hex" + "fmt" + "io" + "net" + "net/http" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "time" + + "interzone/internal/ipfs" +) + +// The IPFS source (IPFS.md in interzone-mirror): the server runs an IPFS +// daemon in interzone's private swarm and every download of the run comes +// through it. Packages and git go through its gateway, images through +// serve-registry. Where Ansible runs over SSH, an SSH forward reaches the +// gateway and the registry. + +const ( + ipfsBin = "/opt/interzone" + ipfsRepo = "/var/lib/interzone-ipfs" + ipfsAPI = "/ip4/127.0.0.1/tcp/45001" + ipfsGateway = "127.0.0.1:48080" + ipfsRegistry = "127.0.0.1:45050" + ipfsUser = "interzone-ipfs" +) + +// ipfsRun is what a run set up on the server and must undo. +type ipfsRun struct { + tunnel *exec.Cmd + seed bool +} + +// usesDownloads says whether a plan fetches anything the swarm provides. +func usesDownloads(plan []Step) bool { + for _, s := range plan { + switch s { + case StepPlaybook, StepRoles, StepMirrors, StepInstall: + return true + } + } + return false +} + +func (r *Runner) ipfsSetup(ctx context.Context) error { + r.begin(StepIPFS) + t := r.cfg.Target + facts, err := probe(ctx, r.cfg) + if err != nil { + return fmt.Errorf("can't log in to the server: %w", err) + } + arch := ociArch(facts["arch"]) + if arch != "amd64" && arch != "arm64" { + return fmt.Errorf("IPFS deploys need an amd64 or arm64 server, not %s", facts["arch"]) + } + seed := r.cfg.Sources.IPFSSeed == "yes" + r.ipfs = &ipfsRun{seed: seed} + + fromWeb, webErr := indexFromWeb(ctx) + want := ipfs.KuboVersion + if fromWeb != nil { + want = fromWeb.Kubo + } + have, _ := r.output(ctx, t, ipfsBin+"/ipfs version --number 2>/dev/null || true") + if have = strings.TrimSpace(have); "v"+have != want { + bin, from, err := kuboBinary(ctx, arch, want, fromWeb) + switch { + case err == nil: + r.note(StepIPFS, fmt.Sprintf("installing IPFS %s (from %s)", want, from)) + if err := r.upload(ctx, ipfsBin+"/ipfs", bin); err != nil { + return err + } + case have != "": + r.note(StepIPFS, "! couldn't get IPFS "+want+" ("+err.Error()+"); keeping "+have) + default: + return fmt.Errorf("couldn't get the IPFS daemon for the server: %w. An offline bundle carries it", err) + } + } + + r.note(StepIPFS, "starting the IPFS daemon in interzone's swarm"+map[bool]string{true: ", seeding for others", false: ""}[seed]) + if _, err := r.output(ctx, t, ipfsConfigScript(seed)); err != nil { + return fmt.Errorf("starting IPFS on the server: %w", err) + } + + idx := fromWeb + r.note(StepIPFS, "looking up the newest publish in the swarm…") + if fromSwarm, err := r.indexFromSwarm(ctx); err != nil { + r.note(StepIPFS, "! the swarm didn't answer: "+err.Error()) + } else if idx == nil || fromSwarm.Version > idx.Version { + idx = fromSwarm + } + if idx == nil { + return fmt.Errorf("neither the swarm nor %s could be reached (%v)", ipfs.IndexURL, webErr) + } + if idx.Version < r.cfg.Sources.IPFSVersion { + return fmt.Errorf("the newest publish found (%s) is older than one this server already used; refusing it", idx.Created) + } + r.note(StepIPFS, fmt.Sprintf("using the publish of %s: playbook %s, interzone %s", idx.Created, short(idx.Playbook.Commit), idx.Interzone)) + + if err := r.registryBinary(ctx, arch, idx); err != nil { + return err + } + if _, err := r.output(ctx, t, registryScript(idx.Parts.OCI)); err != nil { + return fmt.Errorf("starting the image registry on the server: %w", err) + } + + gateway := ipfsGateway + if t.Mode == "ssh" { + if gateway, err = r.ipfsTunnel(ctx); err != nil { + return err + } + } + saved := r.cfg.Sources + saved.IPFSVersion = idx.Version + onServer := "http://" + ipfsGateway + "/ipfs/" + here := "http://" + gateway + "/ipfs/" + s := &r.cfg.Sources + s.Blackout = false + s.APTMirror = onServer + idx.Parts.Packages + s.DockerPackageURL = onServer + idx.Parts.Packages + "/docker" + s.PyPIIndex = here + idx.Parts.Packages + "/pypi/simple" + s.PlaybookGit = here + idx.Parts.Git + "/" + idx.Playbook.Repo + ".git" + s.RolesGitBase = here + idx.Parts.Git + "/" + s.UseRegistry, s.RegistryPrefix = true, ipfsRegistry + if r.cfg.PinnedCommit == "" { + r.cfg.PinnedCommit, r.pinWhy = idx.Playbook.Commit, "the IPFS publish" + } + r.env = Env(r.cfg) + r.emit(Event{Step: StepIPFS, Sources: &saved}) + return nil +} + +// ipfsDone stops what the run started; with seeding on, the daemon keeps +// running. +func (r *Runner) ipfsDone() { + if r.ipfs == nil { + return + } + if r.ipfs.tunnel != nil && r.ipfs.tunnel.Process != nil { + _ = r.ipfs.tunnel.Process.Kill() + _ = r.ipfs.tunnel.Wait() + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + stop := "systemctl stop interzone-registry 2>/dev/null; true" + if !r.ipfs.seed { + stop += "; systemctl disable --now interzone-ipfs 2>/dev/null; true" + } + _, _ = r.output(ctx, r.cfg.Target, stop) +} + +// ipfsConfigScript installs the daemon as a service in the private swarm +// and waits until it answers. Only seeders listen for others. +func ipfsConfigScript(seed bool) string { + listen, routing, enable := `[]`, "dhtclient", "systemctl disable interzone-ipfs 2>/dev/null || true" + if seed { + listen, routing, enable = `["/ip4/0.0.0.0/tcp/4001", "/ip6/::/tcp/4001"]`, "dhtserver", "systemctl enable interzone-ipfs" + } + q := func(s string) string { return shellQuote(s) } + return `set -e +id -u ` + ipfsUser + ` >/dev/null 2>&1 || useradd --system --home-dir ` + ipfsRepo + ` --shell /usr/sbin/nologin ` + ipfsUser + ` +mkdir -p ` + ipfsRepo + ` && chown ` + ipfsUser + `: ` + ipfsRepo + ` && chmod 700 ` + ipfsRepo + ` +ipfs() { runuser -u ` + ipfsUser + ` -- env IPFS_PATH=` + ipfsRepo + ` ` + ipfsBin + `/ipfs "$@"; } +[ -f ` + ipfsRepo + `/config ] || ipfs init --empty-repo >/dev/null +printf '%s' ` + q(ipfs.SwarmKey) + ` > ` + ipfsRepo + `/swarm.key && chown ` + ipfsUser + `: ` + ipfsRepo + `/swarm.key +ipfs config --json Bootstrap ` + q(`["`+strings.Join(ipfs.Bootstrap, `","`)+`"]`) + ` +ipfs config --json AutoConf.Enabled false +ipfs config --json AutoTLS.Enabled false +ipfs config --json DNS.Resolvers '{}' +ipfs config --json Routing.DelegatedRouters '[]' +ipfs config --json Ipns.DelegatedPublishers '[]' +ipfs config Routing.Type ` + routing + ` +ipfs config --json Discovery.MDNS.Enabled false +ipfs config --json Addresses.Swarm ` + q(listen) + ` +ipfs config Addresses.API ` + ipfsAPI + ` +ipfs config Addresses.Gateway /ip4/` + strings.Replace(ipfsGateway, ":", "/tcp/", 1) + ` +cat > /etc/systemd/system/interzone-ipfs.service <<'EOF' +[Unit] +Description=IPFS daemon in interzone's swarm +After=network-online.target +Wants=network-online.target + +[Service] +User=` + ipfsUser + ` +Environment=IPFS_PATH=` + ipfsRepo + ` LIBP2P_FORCE_PNET=1 LIBP2P_TCP_MUX=false +ExecStart=` + ipfsBin + `/ipfs daemon +Restart=on-failure +NoNewPrivileges=yes +ProtectSystem=strict +ProtectHome=yes +PrivateTmp=yes +ReadWritePaths=` + ipfsRepo + ` + +[Install] +WantedBy=multi-user.target +EOF +systemctl daemon-reload +systemctl restart interzone-ipfs +` + enable + ` +i=0; until ipfs --api ` + ipfsAPI + ` id >/dev/null 2>&1; do i=$((i+1)); [ $i -lt 60 ] || { journalctl -u interzone-ipfs -n 5 --no-pager >&2; exit 1; }; sleep 1; done` +} + +// registryScript runs serve-registry for the run, as a throwaway user. +func registryScript(oci string) string { + return `set -e +systemctl stop interzone-registry 2>/dev/null || true +systemctl reset-failed interzone-registry 2>/dev/null || true +systemd-run --quiet --unit=interzone-registry --property=DynamicUser=yes ` + ipfsBin + `/interzone serve-registry -layout http://` + ipfsGateway + `/ipfs/` + oci + ` -listen ` + ipfsRegistry + ` +i=0; until ss -ltn | grep -q ' ` + ipfsRegistry + ` '; do + i=$((i+1)); systemctl is-active -q interzone-registry || { journalctl -u interzone-registry -n 5 --no-pager >&2; exit 1; } + [ $i -lt 300 ] || { echo "the image list didn't arrive from the swarm within 5 minutes" >&2; exit 1; }; sleep 1 +done` +} + +// ipfsEntryReachable dials the swarm's entry nodes from here, a hint of +// whether the server can. +func ipfsEntryReachable(ctx context.Context) (string, error) { + var d net.Dialer + var errs []string + for _, a := range ipfs.Bootstrap { + p := strings.Split(a, "/") // /dns4//tcp//… + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + c, err := d.DialContext(ctx, "tcp", net.JoinHostPort(p[2], p[4])) + cancel() + if err == nil { + c.Close() + return p[2] + ":" + p[4] + " answers", nil + } + errs = append(errs, err.Error()) + } + return "", fmt.Errorf("%s", strings.Join(errs, "; ")) +} + +// indexFromWeb reads the newest index over HTTPS. +func indexFromWeb(ctx context.Context) (*ipfs.Index, error) { + get := func(name string) ([]byte, error) { + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, ipfs.IndexURL+name, nil) + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%s: %s", name, res.Status) + } + return io.ReadAll(io.LimitReader(res.Body, 1<<20)) + } + idx, err := get("index.json") + if err != nil { + return nil, err + } + sig, err := get("index.json.sig") + if err != nil { + return nil, err + } + return ipfs.Verify(idx, sig) +} + +// indexFromSwarm resolves the IPNS name on the server's daemon. +func (r *Runner) indexFromSwarm(ctx context.Context) (*ipfs.Index, error) { + out, err := r.output(ctx, r.cfg.Target, `set -e +ipfs() { runuser -u `+ipfsUser+` -- env IPFS_PATH=`+ipfsRepo+` `+ipfsBin+`/ipfs --api `+ipfsAPI+` "$@"; } +root=$(ipfs name resolve --timeout=90s -r /ipns/`+ipfs.Name+`) +ipfs cat --timeout=60s "$root/index.json.sig" | head -c 4096 +echo "#" +ipfs cat --timeout=60s "$root/index.json" | head -c 1048576`) + if err != nil { + return nil, err + } + sig, idx, ok := strings.Cut(out, "\n#\n") + if !ok { + return nil, fmt.Errorf("unexpected answer from the daemon") + } + return ipfs.Verify([]byte(idx), []byte(sig)) +} + +// kuboBinary finds the IPFS daemon for arch: a copy kept from before, the +// mirror's (checked against the signed index), or the IPFS project's +// (checked against its published SHA-512). +func kuboBinary(ctx context.Context, arch, version string, idx *ipfs.Index) ([]byte, string, error) { + name := "ipfs-linux-" + arch + cache := filepath.Join(dataDir(), "ipfs", version, name) + if b, err := os.ReadFile(cache); err == nil { + return b, "this computer", nil + } + keep := func(b []byte) { + if os.MkdirAll(filepath.Dir(cache), 0o755) == nil { + _ = os.WriteFile(cache, b, 0o755) + } + } + var errs []string + if idx != nil && idx.Bin[name] != "" { + b, err := fetchURL(ctx, ipfs.IndexURL+"bin/"+name, 200<<20) + if err == nil && sha256Hex(b) == idx.Bin[name] { + keep(b) + return b, strings.TrimSuffix(ipfs.IndexURL, "/"), nil + } + errs = append(errs, fmt.Sprintf("mirror: %v", errOr(err, "checksum mismatch"))) + } + file := fmt.Sprintf("kubo_%s_linux-%s.tar.gz", version, arch) + for _, base := range []string{"https://github.com/ipfs/kubo/releases/download/" + version + "/", "https://dist.ipfs.tech/kubo/" + version + "/"} { + host := strings.Split(base, "/")[2] + b, err := func() ([]byte, error) { + tgz, err := fetchURL(ctx, base+file, 200<<20) + if err != nil { + return nil, err + } + sum, err := fetchURL(ctx, base+file+".sha512", 1024) + if err != nil || len(bytes.Fields(sum)) == 0 { + return nil, fmt.Errorf("no checksum: %v", err) + } + return untarIPFS(tgz, string(bytes.Fields(sum)[0])) + }() + if err == nil { + keep(b) + return b, host, nil + } + errs = append(errs, host+": "+err.Error()) + } + return nil, "", fmt.Errorf("%s", strings.Join(errs, "; ")) +} + +func errOr(err error, s string) string { + if err != nil { + return err.Error() + } + return s +} + +func sha256Hex(b []byte) string { + sum := sha256.Sum256(b) + return hex.EncodeToString(sum[:]) +} + +func fetchURL(ctx context.Context, url string, limit int64) ([]byte, error) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Minute) + defer cancel() + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%s", res.Status) + } + return io.ReadAll(io.LimitReader(res.Body, limit)) +} + +// registryBinary puts an interzone for the server's arch at +// /opt/interzone/interzone: this one if it fits, else the release in the +// swarm, checked against the signed index. +func (r *Runner) registryBinary(ctx context.Context, arch string, idx *ipfs.Index) error { + t := r.cfg.Target + cur, _ := r.output(ctx, t, "sha256sum "+ipfsBin+"/interzone 2>/dev/null || true") + if runtime.GOOS == "linux" && runtime.GOARCH == arch { + self, err := os.Executable() + if err != nil { + return err + } + b, err := os.ReadFile(self) + if err != nil { + return err + } + if strings.HasPrefix(cur, sha256Hex(b)) { + return nil + } + r.note(StepIPFS, "copying interzone to the server for its image registry") + return r.upload(ctx, ipfsBin+"/interzone", b) + } + name := "interzone-linux-" + arch + want := idx.Bin[name] + if want == "" { + return fmt.Errorf("the publish has no %s", name) + } + if strings.HasPrefix(cur, want) { + return nil + } + r.note(StepIPFS, "fetching interzone "+idx.Interzone+" for the server's image registry from the swarm") + _, err := r.output(ctx, t, `set -e +runuser -u `+ipfsUser+` -- env IPFS_PATH=`+ipfsRepo+` `+ipfsBin+`/ipfs --api `+ipfsAPI+` cat --timeout=10m /ipfs/`+idx.Parts.Bin+`/`+name+` > `+ipfsBin+`/interzone.new +echo "`+want+` `+ipfsBin+`/interzone.new" | sha256sum -c --quiet +chmod 755 `+ipfsBin+`/interzone.new && mv `+ipfsBin+`/interzone.new `+ipfsBin+`/interzone`) + return err +} + +// upload writes b to path on the server, as root, mode 0755. +func (r *Runner) upload(ctx context.Context, path string, b []byte) error { + t := r.cfg.Target + dir := filepath.Dir(path) + cmd := targetCmd(ctx, t, asRoot("sh -c "+shellQuote("mkdir -p "+dir+" && cat > "+path+".new && chmod 755 "+path+".new && mv "+path+".new "+path))) + cmd.Stdin = io.MultiReader(strings.NewReader(t.BecomePass+"\n"), bytes.NewReader(b)) + var stderr bytes.Buffer + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("copying %s to the server: %s", filepath.Base(path), lastLine(stderr.String(), err)) + } + return nil +} + +// ipfsTunnel forwards a local port to the server's gateway, and the +// registry's own port, which the image check reaches by the same name the +// server pulls by. +func (r *Runner) ipfsTunnel(ctx context.Context) (string, error) { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return "", err + } + local := l.Addr().String() + l.Close() + forwards := []string{"-L", local + ":" + ipfsGateway} + if l, err := net.Listen("tcp", ipfsRegistry); err == nil { + l.Close() + forwards = append(forwards, "-L", ipfsRegistry+":"+ipfsRegistry) + } else { + r.note(StepIPFS, "! "+ipfsRegistry+" is taken on this computer, so the image check can't reach the server's registry") + } + args := append([]string{"-N", "-o", "ExitOnForwardFailure=yes"}, forwards...) + cmd := exec.Command("ssh", append(args, sshArgs(r.cfg.Target)...)...) + if err := cmd.Start(); err != nil { + return "", err + } + r.ipfs.tunnel = cmd + for i := 0; i < 100; i++ { + if c, err := net.DialTimeout("tcp", local, time.Second); err == nil { + c.Close() + return local, nil + } + select { + case <-ctx.Done(): + return "", ctx.Err() + case <-time.After(200 * time.Millisecond): + } + } + return "", fmt.Errorf("the SSH forward to the server's IPFS gateway didn't come up") +} + +// untarIPFS takes the daemon out of kubo's release tarball after checking +// the tarball against its SHA-512. +func untarIPFS(tgz []byte, sha512hex string) ([]byte, error) { + if sum := sha512.Sum512(tgz); hex.EncodeToString(sum[:]) != sha512hex { + return nil, fmt.Errorf("checksum mismatch") + } + zr, err := gzip.NewReader(bytes.NewReader(tgz)) + if err != nil { + return nil, err + } + tr := tar.NewReader(zr) + for { + h, err := tr.Next() + if err != nil { + return nil, fmt.Errorf("no kubo/ipfs in the tarball") + } + if h.Name == "kubo/ipfs" { + return io.ReadAll(io.LimitReader(tr, 200<<20)) + } + } +}