stack/scripts/apply-profile.sh

71 lines
2.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Apply a hardware profile into .env (merges profile keys over existing .env).
#
# ./scripts/apply-profile.sh mini
# ./scripts/apply-profile.sh mid
# ./scripts/apply-profile.sh gaming
#
# Then: docker compose build bonsai && docker compose up -d
# Or: ./scripts/bootstrap-stack.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
STACK_DIR="${STACK_DIR:-$(cd "$SCRIPT_DIR/.." && pwd)}"
PROFILE="${1:-}"
PROFILE_DIR="$STACK_DIR/profiles"
usage() {
echo "Usage: $0 <mini|mid|gaming>"
echo " mini — Beelink-class: CPU, Ternary-Bonsai-27B"
echo " mid — 8GB AMD GPU / 16GB RAM: Vulkan, Qwen3.5-9B Q4_K_M abliterated"
echo " gaming — 16GB AMD GPU / 64GB RAM: Vulkan, Ternary-Bonsai-27B (+ Qwen download)"
exit 1
}
[[ -n "$PROFILE" ]] || usage
[[ -f "$PROFILE_DIR/${PROFILE}.env" ]] || { echo "Unknown profile: $PROFILE"; usage; }
ENV_FILE="$STACK_DIR/.env"
if [[ ! -f "$ENV_FILE" ]]; then
if [[ -f "$STACK_DIR/.env.example" ]]; then
cp "$STACK_DIR/.env.example" "$ENV_FILE"
echo "[apply-profile] created .env from .env.example"
else
touch "$ENV_FILE"
fi
fi
# Merge: for each KEY= in profile, replace or append in .env
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
[[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || continue
key="${line%%=*}"
if grep -qE "^${key}=" "$ENV_FILE"; then
# portable in-place replace
if sed --version >/dev/null 2>&1; then
sed -i "s|^${key}=.*|${line}|" "$ENV_FILE"
else
sed -i '' "s|^${key}=.*|${line}|" "$ENV_FILE"
fi
else
echo "$line" >>"$ENV_FILE"
fi
done <"$PROFILE_DIR/${PROFILE}.env"
# Stamp active profile
if grep -qE '^STACK_PROFILE=' "$ENV_FILE"; then
if sed --version >/dev/null 2>&1; then
sed -i "s|^STACK_PROFILE=.*|STACK_PROFILE=${PROFILE}|" "$ENV_FILE"
else
sed -i '' "s|^STACK_PROFILE=.*|STACK_PROFILE=${PROFILE}|" "$ENV_FILE"
fi
else
echo "STACK_PROFILE=${PROFILE}" >>"$ENV_FILE"
fi
echo "[apply-profile] applied profile '${PROFILE}' → $ENV_FILE"
echo "[apply-profile] next:"
echo " docker compose build bonsai"
echo " docker compose up -d"
echo " # or: ./scripts/bootstrap-stack.sh"