#!/usr/bin/env sh # Linux bootstrap: uv + Python + locked dependencies + local model + update/start. set -eu REPO_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) cd "$REPO_ROOT" ACTION=${1:-templates} case "$ACTION" in -h|--help|help) echo 'Usage: sh deploy.sh [setup|model|templates|dms|start|restart] [--host HOST] [--port PORT]' echo 'setup: install environment and model only; model: ensure model is ready' echo 'templates/dms: prepare environment and model, update, then start; start: prepare and start' echo 'Environment: AUTO_INSTALL_UV=0 disables uv installation; DEPLOY_PYTHON=3.11 selects Python' exit 0 ;; setup|model|templates|dms|start|restart) ;; *) echo "Unknown action: $ACTION" >&2; exit 2 ;; esac if [ "$#" -gt 0 ]; then shift; fi if [ "$ACTION" = setup ] || [ "$ACTION" = model ]; then if [ "$#" -gt 0 ]; then echo 'setup/model do not accept service arguments' >&2; exit 2; fi fi if ! command -v uv >/dev/null 2>&1; then if [ -x "$HOME/.local/bin/uv" ]; then PATH="$HOME/.local/bin:$PATH"; export PATH elif [ -x "$REPO_ROOT/.runtime/bin/uv" ]; then PATH="$REPO_ROOT/.runtime/bin:$PATH"; export PATH else if [ "${AUTO_INSTALL_UV:-1}" != 1 ]; then echo 'uv is missing; install uv or set AUTO_INSTALL_UV=1.' >&2; exit 127 fi installer=$(mktemp) trap 'rm -f "$installer"' EXIT HUP INT TERM if command -v curl >/dev/null 2>&1; then curl -fLsS https://astral.sh/uv/install.sh -o "$installer" elif command -v wget >/dev/null 2>&1; then wget -q https://astral.sh/uv/install.sh -O "$installer" else echo 'Installing uv requires curl or wget.' >&2; exit 127 fi UV_UNMANAGED_INSTALL="$REPO_ROOT/.runtime/bin" sh "$installer" rm -f "$installer" trap - EXIT HUP INT TERM PATH="$REPO_ROOT/.runtime/bin:$PATH"; export PATH fi fi # Never source .env as shell code; Python reads it via python-dotenv. if [ ! -f .env ]; then cp .env.example .env echo 'Created .env from .env.example; fill in real service credentials and paths.' if [ "$ACTION" != setup ] && [ "$ACTION" != model ]; then echo 'After editing .env, rerun this command. No business update was attempted.' >&2 exit 2 fi fi export UV_CACHE_DIR="${UV_CACHE_DIR:-$REPO_ROOT/.uv-cache}" echo '[1/3] Prepare Python and locked project environment' uv python install "${DEPLOY_PYTHON:-3.11}" uv sync --frozen --python "${DEPLOY_PYTHON:-3.11}" case "$ACTION" in templates|dms|start|restart) uv run --frozen --no-sync python scripts/start_service.py --check-config "$@" ;; esac echo '[2/3] Prepare local embedding model' uv run --frozen --no-sync python scripts/download_model.py case "$ACTION" in setup|model) echo 'Environment and model ready. Configure .env, then run sh deploy.sh templates.' ;; *) echo '[3/3] Update/start service' if [ "$ACTION" = templates ] || [ "$ACTION" = dms ]; then uv run --frozen --no-sync python scripts/update_data.py --mode "$ACTION" fi if [ "$ACTION" = restart ]; then set -- --restart "$@"; fi exec uv run --frozen --no-sync python scripts/start_service.py "$@" ;; esac