Files
stack/scripts/common.sh
T

97 lines
4.2 KiB
Bash
Executable File

# Shared helpers for the Mosaic host scripts. Not a documented entry point.
POC_ROOT_MARKER=".mosaic-poc-root"
# Trusted first initialization plus a fresh processing check, before credentials/build/network work.
processing_guard() {
node packages/processing-control/src/container-cli.mjs --check
}
# Load and validate the Mosaic configuration (config.json), exporting
# MOSAIC_DATA_ROOT, MOSAIC_PROVIDER, and MOSAIC_MODEL.
#
# Fails closed: a missing or invalid configuration aborts the calling
# script before any container or filesystem mutation. Run paths never
# auto-bootstrap; use scripts/bootstrap.sh explicitly.
load_config() {
local config_env
if ! config_env="$(node scripts/mosaic-config.mjs env)"; then
echo "common: configuration load failed" >&2
exit 1
fi
eval "$config_env"
export MOSAIC_DATA_ROOT MOSAIC_PROVIDER MOSAIC_MODEL MOSAIC_ADAPTER
MOSAIC_DEV_DIR="$MOSAIC_DATA_ROOT"
}
# Resolve the release identity: RELEASE is the single source of the
# release version (stays 0.0.X until declared stable); the image tag
# derives from it plus the pi version package-lock.json resolves (#1873 S5:
# package.json gives pi a range, never a pin, and the tag never carries it).
# scripts/image-tag.mjs builds the tag and refuses a lock it can't read.
load_release() {
local release tag
release="$(tr -d '[:space:]' < RELEASE)"
if ! printf '%s' "$release" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "common: RELEASE must be a semver-ish version, got: '$release'" >&2
exit 1
fi
tag="$(node scripts/image-tag.mjs .)" || exit 1
export MOSAIC_RELEASE="$release"
export MOSAIC_IMAGE_TAG="$tag"
}
# Build the agent image (#1873 S5). The image fit step runs first: it runs the
# fit check on the pi package-lock.json resolves, as npm ci installed it here,
# and refuses with one line when that pi is unfit or the check can't tell, so a
# lock that resolves an unfit pi fails the build. scripts/pi-fit-image.mjs
# says why the step runs on the host and not inside the image build.
# compose.yaml has no build section, so this is the only way the scripts build
# the image; compose run refuses a missing one. Call load_release first.
build_image() {
if [ -z "${MOSAIC_IMAGE_TAG:-}" ]; then
echo "common: build_image needs MOSAIC_IMAGE_TAG from load_release" >&2
exit 1
fi
node scripts/pi-fit-image.mjs || exit 1
docker build -f Containerfile -t "$MOSAIC_IMAGE_TAG" .
}
# Ensure the configured runtime data directory exists and carries this
# project's ownership marker. The marker is what scripts/reset.sh requires
# before it will delete anything.
bootstrap_runtime_dir() {
if [ -d "$MOSAIC_DEV_DIR" ]; then
if [ ! -w "$MOSAIC_DEV_DIR" ] || [ ! -x "$MOSAIC_DEV_DIR" ]; then
echo "bootstrap: $MOSAIC_DEV_DIR exists but is not writable by $(id -un)" >&2
echo "bootstrap: a root-owned directory here is usually leftover from a" >&2
echo "bootstrap: Docker-created mount source; remove it and rerun." >&2
exit 1
fi
else
mkdir -p "$MOSAIC_DEV_DIR"
echo "bootstrap: created $MOSAIC_DEV_DIR"
fi
touch "$MOSAIC_DEV_DIR/$POC_ROOT_MARKER"
# Live user context layer (M14/M16): the directory is system-managed,
# but USER.md is NEVER auto-created. Onboarding (scripts/onboard.sh,
# auto-invoked by agent.sh) creates it from the template.
mkdir -p "$MOSAIC_DEV_DIR/user"
}
# M16 release self-determination: launchers call this after load_release.
# Cheap no-op when the active pointer matches the desired RELEASE; on drift
# it delegates to `release.sh ensure` (package if needed, health-gated
# activate). The health gate's own task run sets MOSAIC_ENSURE_SKIP so the
# ensure never re-enters itself.
ensure_release_aligned() {
[ -n "${MOSAIC_ENSURE_SKIP:-}" ] && return 0
local pointer="$MOSAIC_DEV_DIR/state/active.json"
local active=""
if [ -f "$pointer" ]; then
active="$(node -e 'try{const p=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"));process.stdout.write(p.release||"")}catch{}' "$pointer" 2>/dev/null)"
fi
[ "$active" = "$MOSAIC_RELEASE" ] && return 0
MOSAIC_ENSURE_SKIP=1 scripts/release.sh ensure
}