motif64

m64_7796c3cb05e5 / warm_opencode.sh

37 lines, 1.8 KBraw
1#!/usr/bin/env bash2# Build-time only (see Dockerfile). On a project's first use, opencode installs npm3# packages (@opencode-ai/plugin and its deps) into $HOME/.config/opencode. At run time the4# capsule can reach only openrouter.ai, so do that here, where the build has network: start5# opencode once, open a session to trigger the install, and keep the resulting HOME.6#7# Every wait is bounded (curl --max-time, a deadline on each loop, kill -9 as a last8# resort): a step that hangs fails the build with a message instead of hanging it.9set -eu10export HOME=/opt/opencode-capsule OPENCODE_CONFIG=/etc/opencode/opencode.json11mkdir -p "$HOME" /tmp/warm12opencode serve --hostname 127.0.0.1 --port 4096 --pure >/tmp/warm.log 2>&1 &13pid=$!14stop() {15  kill "$pid" 2>/dev/null || true16  for _ in $(seq 1 10); do kill -0 "$pid" 2>/dev/null || return 0; sleep 1; done17  kill -9 "$pid" 2>/dev/null || true18}19fail() { echo "warm_opencode: $1" >&2; cat /tmp/warm.log >&2; stop; exit 1; }2021up=""22for _ in $(seq 1 60); do23  curl -sf --max-time 3 127.0.0.1:4096/global/health >/dev/null && { up=1; break; }24  sleep 125done26[ -n "$up" ] || fail "opencode did not answer /global/health within 60 s"27curl -sf --max-time 30 -X POST "127.0.0.1:4096/session?directory=/tmp/warm" \28  -H 'Content-Type: application/json' -d '{}' >/dev/null || fail "creating a session failed"29for _ in $(seq 1 120); do [ -f "$HOME/.config/opencode/package-lock.json" ] && break; sleep 1; done30sleep 531stop32test -d "$HOME/.config/opencode/node_modules/@opencode-ai/plugin" \33  || fail "opencode did not install @opencode-ai/plugin"34# Keep only the installed packages (opencode pins @opencode-ai/plugin to its own version,35# 1.18.23; package-lock.json records the rest). The npm cache, session DB and logs go.36rm -rf "$HOME/.local" "$HOME/.npm" /tmp/warm /tmp/warm.log37chmod -R a+rX "$HOME"