Cotton code
88 lines · 4.3 KB · ShellRaw
#!/usr/bin/env bash# Refuse to start a build on a filesystem that is nearly full.##   scripts/ci/require-free-disk.sh <path> [min-gb]## WHY BEFORE THE BUILD AND NOT DURING IT. On 2026-09-09 the CI host filled to# 100% (seven per-branch cargo target directories of 70–85 GB, five of them for# merged branches) and run 2400 died on `No space left on device` inside cargo# and `ENOSPC` in the checks job. A cargo step killed mid-link does not leave# nothing behind: it leaves a half-written artifact at a path with a fresh# mtime, which the NEXT run judges fresh and links against — the same class of# corruption the procedure “Work with the forge — tokens, the pnpm forge surface, workflows and runs” spends a chapter on. A red bar that names# the disk before a byte is compiled is the correct shape; a red bar seventeen# minutes in that names a linker is not.## It reads the filesystem holding <path> — the nearest existing ancestor if the# path itself has not been created yet — with `df -Pk`, which is POSIX output on# both the Linux runner and the build Mac. A `df` that answers something other# than a number is a FAILURE, not a pass: this step cannot be allowed to succeed# by not understanding the answer.## Env: COTTON_CI_MIN_FREE_GB (default 60 — one cold build of this workspace is# tens of GB, and the host is shared with Forgejo, its database and the relay).set -euo pipefailTARGET="${1-}"if [ "$(uname -s)" = Darwin ]; then  exec bash "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/mac-build-policy.sh" --path "${TARGET:-$PWD}" --checkfiMIN_GB="${2:-${COTTON_CI_MIN_FREE_GB:-60}}"if [ -z "$TARGET" ]; then  echo "require-free-disk: no path given — refusing to guess which filesystem to check" >&2  echo "usage: require-free-disk.sh <path> [min-gb]" >&2  exit 2ficase "$MIN_GB" in  ''|*[!0-9]*)    echo "require-free-disk: the minimum wants a whole number of GB, got '$MIN_GB'" >&2    exit 2    ;;esacprobe="$TARGET"while [ ! -e "$probe" ]; do  parent="$(dirname "$probe")"  [ "$parent" = "$probe" ] && break  probe="$parent"done# `|| true` because `pipefail` is on: a `df` that fails would otherwise end this# script with `set -e` and no message at all, which is the one outcome a guard# must never have. The empty answer falls into the loud refusal below.avail_kb="$(df -Pk "$probe" 2>/dev/null | awk 'NR == 2 { print $4 }' || true)"case "$avail_kb" in  ''|*[!0-9]*)    echo "require-free-disk: could not read the free space on the filesystem holding '$probe' — df said:" >&2    df -Pk "$probe" >&2 || true    exit 1    ;;esacavail_gb=$(( avail_kb / 1024 / 1024 ))if [ "$avail_gb" -ge "$MIN_GB" ]; then  echo "[free-disk] ${avail_gb} GB free on the filesystem holding $probe (need ${MIN_GB} GB) — building."  exit 0fi{  echo "[free-disk] REFUSING TO BUILD — only ${avail_gb} GB free on the filesystem holding $probe, and this job needs ${MIN_GB} GB."  echo "[free-disk] A cargo step that dies mid-link on ENOSPC leaves a half-written artifact the NEXT run judges fresh."  echo "[free-disk] Reclaim on this host, by hand:"  echo "[free-disk]   bash scripts/ci/build-slot.sh list                          # the Linux lanes' build slots and what each target holds"  echo "[free-disk]   rm -rf \"\$HOME\"/.cache/cottondb/rust/slots/<k>/target        # a FREE slot's target — sccache re-links what it held"  echo "[free-disk]   bash scripts/ci/sweep-cargo-targets.sh --dry-run --max 1   # a Mac's per-branch targets: what would go"  echo "[free-disk]   bash scripts/ci/sweep-cargo-targets.sh --max 1             # take it"  echo "[free-disk]   du -sh \"\$HOME\"/.cache/cottondb/rust/targets/*             # what is left"  echo "[free-disk] The other large thing under that root is the sccache compile cache:"  echo "[free-disk]   du -sh \"\$HOME\"/.cache/cottondb/rust/sccache               # bounded by SCCACHE_CACHE_SIZE (100G here, 15G on a Mac), LRU"  echo "[free-disk] The sweep does NOT touch it and must not: sccache evicts its own oldest"  echo "[free-disk] objects and never grows past that cap. Wipe it only if you SUSPECT it —"  echo "[free-disk] it is content-addressed, so there is no other reason to."  echo "[free-disk] the procedure “Work with the forge — tokens, the pnpm forge surface, workflows and runs” — 'The shared cargo cache after a cancelled run'."} >&2exit 1