|
#!/usr/bin/env bash |
|
set -euo pipefail |
|
|
|
# create_pi_sandbox.sh |
|
# Generates a custom Docker sandbox containing the pi installation, then creates |
|
# a named sandbox from that template with host config files copied in. |
|
# Requires: sbx (Docker Sandboxes CLI) and host ~/.pi/agent configs. |
|
|
|
SANDBOX_NAME="pi-sandbox" |
|
BUILDER_NAME="pi-builder" |
|
TEMPLATE_TAG="pi-template:latest" |
|
HOST_PI_DIR="${HOME}/.pi/agent" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# Pre-flight checks |
|
# ------------------------------------------------------------------------------ |
|
if ! command -v sbx &> /dev/null; then |
|
echo "Error: sbx is not installed or not in PATH" >&2 |
|
exit 1 |
|
fi |
|
|
|
for f in "${HOST_PI_DIR}/models.json" "${HOST_PI_DIR}/settings.json"; do |
|
if [[ ! -f "$f" ]]; then |
|
echo "Error: Missing host file: $f" >&2 |
|
exit 1 |
|
fi |
|
done |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 1. Clean up any stale builder / target sandbox |
|
# ------------------------------------------------------------------------------ |
|
if sbx ls --quiet | grep -qx "${BUILDER_NAME}"; then |
|
echo "==> Removing existing builder sandbox '${BUILDER_NAME}'..." |
|
sbx rm -f "${BUILDER_NAME}" |
|
fi |
|
|
|
if sbx ls --quiet | grep -qx "${SANDBOX_NAME}"; then |
|
echo "==> Removing existing sandbox '${SANDBOX_NAME}'..." |
|
sbx rm -f "${SANDBOX_NAME}" |
|
fi |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 2. Create a shell sandbox (builder) |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Creating builder shell sandbox '${BUILDER_NAME}'..." |
|
sbx create shell . --name "${BUILDER_NAME}" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 3. Install pi inside the builder sandbox |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Installing pi inside builder..." |
|
sbx exec "${BUILDER_NAME}" -- npm install -g @mariozechner/pi-coding-agent |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 4. Ensure ~/.pi/agent directory exists inside the builder |
|
# ------------------------------------------------------------------------------ |
|
SANDBOX_HOME=$(sbx exec "${BUILDER_NAME}" -- sh -c 'echo "$HOME"') |
|
echo "==> Creating pi agent directory (${SANDBOX_HOME}/.pi/agent)..." |
|
sbx exec "${BUILDER_NAME}" -- mkdir -p "${SANDBOX_HOME}/.pi/agent" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 5. Stop the builder so it can be snapshotted cleanly |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Stopping builder sandbox..." |
|
sbx stop "${BUILDER_NAME}" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 6. Save builder as a template |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Saving builder as template '${TEMPLATE_TAG}'..." |
|
sbx template save "${BUILDER_NAME}" "${TEMPLATE_TAG}" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 7. Create the new pi-sandbox from the template |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Creating sandbox '${SANDBOX_NAME}' from template..." |
|
sbx create shell . --name "${SANDBOX_NAME}" --template "${TEMPLATE_TAG}" |
|
|
|
# Re-detect home dir for the new sandbox (same image, but kept for safety) |
|
SANDBOX_HOME=$(sbx exec "${SANDBOX_NAME}" -- sh -c 'echo "$HOME"') |
|
sbx exec "${SANDBOX_NAME}" -- mkdir -p "${SANDBOX_HOME}/.pi/agent" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 8. Copy host pi configuration files into the new sandbox |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Copying host configuration into '${SANDBOX_NAME}'..." |
|
sbx cp "${HOST_PI_DIR}/models.json" "${SANDBOX_NAME}:${SANDBOX_HOME}/.pi/agent/models.json" |
|
sbx cp "${HOST_PI_DIR}/settings.json" "${SANDBOX_NAME}:${SANDBOX_HOME}/.pi/agent/settings.json" |
|
|
|
# ------------------------------------------------------------------------------ |
|
# 9. Verify pi is configured correctly |
|
# ------------------------------------------------------------------------------ |
|
echo "==> Verifying pi installation and configuration..." |
|
sbx exec "${SANDBOX_NAME}" -- pi --version |
|
|
|
echo "==> Done. Sandbox '${SANDBOX_NAME}' is ready to use." |
|
echo " Open it: sbx run ${SANDBOX_NAME}" |
|
echo " Or exec: sbx exec -it ${SANDBOX_NAME} bash" |