#cloud-config
# Vibecarbon compose-deploy bootstrap.
#
# This user-data runs at VPS boot time on plain Ubuntu 26.04 on every
# provider. Each provider's renderer splices the one Docker install
# (src/lib/iac/docker-install.js) at the top of runcmd, so every server installs
# the latest stable Docker the same way. Boot refreshes only Docker's own apt
# source (no package_update: ufw and unattended-upgrades are already installed
# on the images we boot), with Ubuntu's post-update hooks silenced.
# By finishing UFW + unattended-upgrades before SSH is even reachable, the
# CLI's post-SSH setupServer becomes a ~2-second marker-file probe instead
# of a ~90-second serial apt-get chain.
#
# Writing the marker file `/var/lib/vibecarbon/ready` is the contract that
# tells the CLI it's safe to proceed. If user-data fails, the marker never
# appears; the CLI reports and dumps /var/log/cloud-init-output.log.

# ufw and unattended-upgrades install in runcmd, AFTER Docker is up and the
# boot image pull has started, so Docker (and the pull) start as early as the
# boot allows instead of waiting for these packages.

# Stable IPv6 flow label, set before anything downloads: bootcmd runs before
# runcmd, where the Docker install sits. Linux gives a TCP socket a new flow
# label when duplicate data arrives with a new one; something on the path to
# GitHub's package CDN (where ghcr.io serves every layer) picks the server by
# flow label, so our ACKs then reach a server without the connection and the
# pull is reset (`failed to copy: ... connection reset by peer`). Same setting
# and reason as carbon/cloud-init/k3s/_stable-flowlabel.sh; the sysctl.d file
# in write_files keeps it across reboots.
bootcmd:
  - [sh, -c, 'if [ -e /proc/sys/net/core/txrehash ]; then sysctl -q -w net.core.txrehash=0; fi']

write_files:
  - path: /etc/sysctl.d/99-vibecarbon-stable-flowlabel.conf
    permissions: '0644'
    content: |
      net.core.txrehash = 0

  # The boot image pull: the pinned core images (the db image, the traefik and
  # docker-socket-proxy mirrors; the CLI fills the list from src/lib/images.js,
  # the same refs the compose files use) pulled in the background while the
  # rest of boot runs, so the deploy's own `docker compose pull --policy
  # missing` finds them present. Never waited on and never retried: if a pull
  # fails, the deploy's pull does the work, loud on failure as always, and the
  # deploy log quotes what this one reported (status + log below).
  - path: /usr/local/lib/vibecarbon/boot-pull.sh
    permissions: '0755'
    content: |
      #!/bin/sh
      out=/var/lib/vibecarbon/boot-pull
      mkdir -p "$out"
      echo "b.begin $(date +%s.%N)">>/var/log/vcboot||true
      echo running > "$out/status"
      : > "$out/log"
      pids=""
      for ref in __VIBECARBON_BOOT_PULL_IMAGES__; do
        docker pull -q "$ref" >> "$out/log" 2>&1 &
        pids="$pids $!"
      done
      failed=0
      for pid in $pids; do
        wait "$pid" || failed=$((failed + 1))
      done
      if [ "$failed" -eq 0 ]; then
        echo ok > "$out/status"
      else
        echo "failed: $failed image(s), $(tail -n 1 "$out/log")" > "$out/status"
      fi
      echo "b.end $(date +%s.%N)">>/var/log/vcboot||true

  - path: /etc/apt/apt.conf.d/20auto-upgrades
    permissions: '0644'
    content: |
      APT::Periodic::Update-Package-Lists "1";
      APT::Periodic::Unattended-Upgrade "1";

  # The one Docker daemon config of every compose server (all five providers,
  # compose and compose-ha, and the server a compose scale builds).
  #
  # Logs: bound Docker's json-file logs at the DAEMON level so every container
  # the deploy later starts inherits the cap - no per-service logging block
  # needed. Without this, a chatty container grows
  # /var/lib/docker/containers/*/*.log without limit until the single-VPS root
  # disk fills and the whole stack wedges (slow disk-fill outage).
  # 10m x 3 files = 30MB/container ceiling.
  #
  # Image store: the classic one (overlay2), not the containerd image store
  # Docker 29 made the default on fresh installs. Only the classic store's pull
  # retries a failed layer download (max-download-attempts, default 5); the
  # containerd store's pull makes one attempt. Seven times from 2026-11-06 a
  # fresh server's first pull failed with `connection reset by peer` (six on
  # Linode, from ghcr.io over IPv6) and a rerun always worked, so Docker's own
  # per-layer retry was chosen (2026-11-09). The resets' cause, found
  # 2026-11-15, is the flow-label change bootcmd above turns off; the retry
  # stays for any other drop.
  # write_files runs before runcmd, where each provider splices the Docker
  # install, so dockerd starts on the classic store and no image ever lands in
  # the other one. A server made before this change keeps the store it has: a
  # live server never sees new user data, and switching stores hides every
  # image and container it holds.
  # tests/unit/iac/docker-daemon-config-census.test.ts holds every compose
  # server to this, and tests/integration/docker/server-image-store.test.ts
  # runs this file on a real Docker 29: a reset in the middle of a layer is
  # retried and the pull succeeds (on the containerd store the same reset
  # fails the pull with `failed to copy: ... connection reset by peer`).
  #
  # A fresh Ubuntu + Docker install ships no daemon.json, so this is a create,
  # not a merge; if that ever changes, fold these keys into the existing file.
  - path: /etc/docker/daemon.json
    permissions: '0644'
    content: |
      {
        "log-driver": "json-file",
        "log-opts": {
          "max-size": "10m",
          "max-file": "3"
        },
        "features": {
          "containerd-snapshotter": false
        }
      }

  # sshd connection-concurrency headroom. Ubuntu's default MaxStartups
  # 10:30:100 probabilistically DROPS unauthenticated connections beyond 10
  # concurrent - and a vibecarbon deploy legitimately fans builds (BuildKit
  # dials several ssh streams), image pulls, admin tunnels, and verification
  # probes at exactly that scale, concurrently, per node. On slow provider
  # links each connection sits unauthenticated longer, so the burst overlaps
  # harder: three subsystems on one linode leg died with
  # `kex_exchange_identification: read: Connection reset by peer`
  # (2026-08-23, runs 32640636398/32642715052) before this existed. These are
  # our nodes; provision them for our workload.
  - path: /etc/ssh/sshd_config.d/99-vibecarbon-concurrency.conf
    permissions: '0644'
    content: |
      MaxStartups 100:30:200
      MaxSessions 64

runcmd:
  # Docker first (every provider splices the one install above this line).
  # Ensure it is running and restart it, so the daemon the boot pull and every
  # deploy pull talk to runs /etc/docker/daemon.json above (the log cap and
  # the classic image store). write_files runs before runcmd, so the daemon the
  # package starts on install already reads the file; the restart makes that
  # hold even for a daemon started before the file existed (an image with
  # Docker preinstalled). It is cheap here - no containers exist yet at boot.
  # Nothing below restarts Docker again, so the boot pull started next never
  # races a restart, and the ready marker the deploy waits on comes after it.
  #
  # Boot timing: one `<id> <epoch seconds.nanoseconds>` line per major phase (and a
  # begin/end pair around the background boot pull), read back by the deploy
  # after its ready wait (src/lib/deploy/boot-timing.js). Timing only: nothing
  # here depends on these lines. Anything spliced above this mark (a provider's
  # docker install) is timed as the part of the user script before it.
  - [sh, -c, 'echo "d $(date +%s.%N)">>/var/log/vcboot||true']
  - [systemctl, enable, --now, docker]
  - [systemctl, restart, docker]
  # The boot image pull, detached so it overlaps the rest of boot and never
  # holds the ready marker.
  - [sh, -c, 'setsid nohup /usr/local/lib/vibecarbon/boot-pull.sh > /dev/null 2>&1 < /dev/null &']

  # Pick up the sshd drop-in written above (write_files runs first; ssh is
  # already up serving cloud-init's own channel, so reload - never restart,
  # which would sever the session provisioning us).
  - [systemctl, reload, ssh]
  # UFW and unattended-upgrades, now that Docker and the pull are going.
  # Lock timeout, not bare apt-get: unattended-upgrades may hold dpkg's lock
  # on first boot (src/lib/deploy/apt.js).
  - [sh, -c, 'echo "a $(date +%s.%N)">>/var/log/vcboot||true']
  # Spelled out because shell templates cannot import src/lib/deploy/apt.js;
  # same shape as aptInstallOrRefresh: install, and only if that fails refresh
  # the lists and retry. EnableSrvRecords=false because DigitalOcean's resolver
  # takes 31 s to answer the SRV query apt makes first; nothing here uses SRV.
  - [sh, -c, 'apt-get -o DPkg::Lock::Timeout=300 -o Acquire::EnableSrvRecords=false install -y ufw unattended-upgrades || { apt-get -o DPkg::Lock::Timeout=300 -o Acquire::EnableSrvRecords=false update && apt-get -o DPkg::Lock::Timeout=300 -o Acquire::EnableSrvRecords=false install -y ufw unattended-upgrades; }']
  - [sh, -c, 'echo "u $(date +%s.%N)">>/var/log/vcboot||true']
  # UFW defaults + the three ports Traefik needs. Ordering matters: we enable
  # last so the allow rules are in place before the first policy flip.
  - [ufw, default, deny, incoming]
  - [ufw, default, allow, outgoing]
  - [ufw, allow, '22/tcp']
  - [ufw, allow, '80/tcp']
  - [ufw, allow, '443/tcp']
  - [sh, -c, 'echo y | ufw enable']

  # History: an earlier boot pull (iter-perfwave4b 2026-05-06) ran in the
  # foreground against Docker Hub and was reverted: Hetzner DC IPs hit Docker
  # Hub's per-IP rate limit, and the marker waited for it (cloudInitReady
  # +61s). Today's differs on both counts: the core images come from the
  # public ghcr mirror (src/lib/images.js MIRRORED_IMAGES), and the pull is
  # detached, so it never holds the marker.

  # Marker file - the CLI's setupServer polls for this. The parent dir may
  # not exist yet on a first boot; mkdir -p is safe either way.
  - [mkdir, -p, /var/lib/vibecarbon]
  - [sh, -c, 'printf "%s\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > /var/lib/vibecarbon/ready']
  - [sh, -c, 'echo "e $(date +%s.%N)">>/var/log/vcboot||true']

final_message: "vibecarbon cloud-init complete after $UPTIME seconds"
