garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /deploy.sh

raw

#!/bin/bash
# Test midas, build it, and update the copies a workspace's operators run, from an
# agent container on the hub's machine (reached as `ssh host`).
#
# A hub with a deploy target for midas (docs/hub.md, "midas itself") does all of
# this after each landing. This is for a hub without one, its first setup of the
# self-update service, a node whose recipe changed, and a hub a bad build left
# unable to deploy the next.
#
#   ./deploy.sh                 local + hub + claude
#   ./deploy.sh <node>          also re-set-up a node that runs on the hub's machine
#
# local    the agent container: the binary on the cargo-target volume, and the
#          names it answers to (midas, cargo) in ~/.local/bin
# hub      `sudo midas hub setup` with the new binary: it installs itself,
#          updates the services and restarts the hub. Every node then takes
#          this build from the hub once nobody holds it (`midas node update`),
#          the Macs' too when zig could build theirs.
# claude   the shared skills and the session hooks, into ~/.claude (`midas
#          claude install`; every session start does it too). `hooks` is
#          still accepted for it.
# <node>   `sudo midas setup <node>` on the hub's machine, for a node that runs
#          there. Skipped while somebody holds it: it restarts the node. Only
#          needed when its recipe changed (`midas check` says so).
#
# A changed recipe on another machine is its owner's to apply: `sudo midas
# setup <node>` there, or `midas update` from their own machine (README.md).
set -euo pipefail
cd "$(dirname "$0")"

targets=("$@")
[ ${#targets[@]} -gt 0 ] || targets=(local hub claude)

export CARGO_TARGET_DIR=/home/agent/cargo-target/midas-target CARGO_TERM_COLOR=never
# The build this is, so `midas check` can tell a copy left behind.
export MIDAS_BUILD="$(git describe --always --dirty --abbrev=8)"
# The hub's web page, which the binary carries (build.rs): built first, so the
# binary below has it.
if ! out=$(cd web && bun install --frozen-lockfile 2>&1 && bunx tsc --noEmit 2>&1 && bun test 2>&1 && bun run build 2>&1); then
    printf "%s\n" "$out" | tail -20 >&2; echo "web page failed; not deploying" >&2; exit 1
fi
if ! out=$(MIDAS_LOCAL_BUILD=1 CARGO_BUILD_JOBS=3 cargo test --release --target x86_64-unknown-linux-musl --quiet 2>&1); then
    printf "%s\n" "$out" | grep -E "panicked|FAILED|^error" >&2; echo "tests failed; not deploying" >&2; exit 1
fi
printf "%s\n" "$out" | grep "^test result"
MIDAS_LOCAL_BUILD=1 CARGO_BUILD_JOBS=3 cargo build --release --target x86_64-unknown-linux-musl --quiet
B=$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/midas
D=/home/agent/cargo-target/midas/bin
V=/data/docker/volumes/agent_cargo-target/_data/midas/bin   # $D, as the hub's machine sees it
echo "build $MIDAS_BUILD"

# The Macs' nodes take their midas from the hub too (DESIGN.md §3), so build
# theirs beside it: cross-linked by zig, which needs no Apple SDK — the one
# framework the date library names is a link-time stub (node/macos/stubs); the
# real one is on the Mac. zig and cargo-zigbuild come from PATH or ~/opt.
for d in "$HOME"/opt/zig-*/ "$HOME/opt/cargo-zigbuild/bin"; do [ -d "$d" ] && PATH=$PATH:${d%/}; done
MAC=""
if command -v zig >/dev/null && command -v cargo-zigbuild >/dev/null; then
    CARGO_TARGET_AARCH64_APPLE_DARWIN_RUSTFLAGS="-C link-arg=-F$PWD/node/macos/stubs" MIDAS_LOCAL_BUILD=1 CARGO_BUILD_JOBS=3 \
        cargo zigbuild --release --target aarch64-apple-darwin --quiet 2> >(grep -v "xcrun" >&2)
    MAC=$CARGO_TARGET_DIR/aarch64-apple-darwin/release/midas
else
    echo "no zig and cargo-zigbuild: the Macs' nodes keep the midas they have" >&2
fi

# The hub's machine reads the agent's volume directly: no copy over ssh needed.
on_host() {
    cp "$B" "$D/midas.deploy"
    ssh host "sudo install -m 755 $V/midas.deploy /tmp/midas-deploy \
        && sudo /tmp/midas-deploy $*; s=\$?; sudo rm -f /tmp/midas-deploy; exit \$s"
    rm -f "$D/midas.deploy"
}

for t in "${targets[@]}"; do
    case "$t" in
        local)
            mkdir -p "$D" "$HOME/.local/bin"
            cp "$B" "$D/midas.new" && mv "$D/midas.new" "$D/midas"
            for name in midas cargo; do
                ln -sfn "$D/midas" "$HOME/.local/bin/$name"
            done
            echo "local: $D/midas, as midas and cargo" ;;
        hub)
            # Every node takes this build from the hub once free; the Macs' too
            # when it was built above.
            extra=""
            if [ -n "$MAC" ]; then
                cp "$MAC" "$D/midas-macos-aarch64.deploy"
                extra="--node-binary macos-aarch64=$V/midas-macos-aarch64.deploy"
            fi
            on_host hub setup $extra 2>&1 | sed 's/^/hub: /'
            rm -f "$D/midas-macos-aarch64.deploy" ;;
        claude|hooks)
            "$B" claude install 2>&1 | sed 's/^/claude: /' ;;
        *)
            if midas status --json | jq -e --arg s "$t" '.leases[$s]' >/dev/null; then
                echo "$t: held by $(midas status --json | jq -r --arg s "$t" '.leases[$s].owner') — not restarting it now" >&2
                continue
            fi
            midas drain "$t" --why "deploying midas $MIDAS_BUILD"
            on_host setup "$t" 2>&1 | tail -3 | sed "s/^/$t: /"
            midas undrain "$t" ;;
    esac
done