deploy.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env sh
  2. # Linux bootstrap: uv + Python + locked dependencies + local model + update/start.
  3. set -eu
  4. REPO_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
  5. cd "$REPO_ROOT"
  6. ACTION=${1:-templates}
  7. case "$ACTION" in
  8. -h|--help|help)
  9. echo 'Usage: sh deploy.sh [setup|model|templates|dms|start|restart] [--host HOST] [--port PORT]'
  10. echo 'setup: install environment and model only; model: ensure model is ready'
  11. echo 'templates/dms: prepare environment and model, update, then start; start: prepare and start'
  12. echo 'Environment: AUTO_INSTALL_UV=0 disables uv installation; DEPLOY_PYTHON overrides .python-version (default: 3.14.5)'
  13. exit 0 ;;
  14. setup|model|templates|dms|start|restart) ;;
  15. *) echo "Unknown action: $ACTION" >&2; exit 2 ;;
  16. esac
  17. if [ "$#" -gt 0 ]; then shift; fi
  18. if [ "$ACTION" = setup ] || [ "$ACTION" = model ]; then
  19. if [ "$#" -gt 0 ]; then echo 'setup/model do not accept service arguments' >&2; exit 2; fi
  20. fi
  21. if ! command -v uv >/dev/null 2>&1; then
  22. if [ -x "$HOME/.local/bin/uv" ]; then
  23. PATH="$HOME/.local/bin:$PATH"; export PATH
  24. elif [ -x "$REPO_ROOT/.runtime/bin/uv" ]; then
  25. PATH="$REPO_ROOT/.runtime/bin:$PATH"; export PATH
  26. else
  27. if [ "${AUTO_INSTALL_UV:-1}" != 1 ]; then
  28. echo 'uv is missing; install uv or set AUTO_INSTALL_UV=1.' >&2; exit 127
  29. fi
  30. installer=$(mktemp)
  31. trap 'rm -f "$installer"' EXIT HUP INT TERM
  32. if command -v curl >/dev/null 2>&1; then
  33. curl -fLsS https://astral.sh/uv/install.sh -o "$installer"
  34. elif command -v wget >/dev/null 2>&1; then
  35. wget -q https://astral.sh/uv/install.sh -O "$installer"
  36. else
  37. echo 'Installing uv requires curl or wget.' >&2; exit 127
  38. fi
  39. UV_UNMANAGED_INSTALL="$REPO_ROOT/.runtime/bin" sh "$installer"
  40. rm -f "$installer"
  41. trap - EXIT HUP INT TERM
  42. PATH="$REPO_ROOT/.runtime/bin:$PATH"; export PATH
  43. fi
  44. fi
  45. # Never source .env as shell code; Python reads it via python-dotenv.
  46. if [ ! -f .env ]; then
  47. cp .env.example .env
  48. echo 'Created .env from .env.example; fill in real service credentials and paths.'
  49. if [ "$ACTION" != setup ] && [ "$ACTION" != model ]; then
  50. echo 'After editing .env, rerun this command. No business update was attempted.' >&2
  51. exit 2
  52. fi
  53. fi
  54. export UV_CACHE_DIR="${UV_CACHE_DIR:-$REPO_ROOT/.uv-cache}"
  55. echo '[1/3] Prepare Python and locked project environment'
  56. PYTHON_VERSION=${DEPLOY_PYTHON:-$(cat "$REPO_ROOT/.python-version")}
  57. echo "Using Python $PYTHON_VERSION"
  58. uv python install "$PYTHON_VERSION"
  59. uv sync --frozen --python "$PYTHON_VERSION"
  60. case "$ACTION" in
  61. templates|dms|start|restart) uv run --frozen --no-sync --python "$PYTHON_VERSION" python scripts/start_service.py --check-config "$@" ;;
  62. esac
  63. echo '[2/3] Prepare local embedding model'
  64. uv run --frozen --no-sync --python "$PYTHON_VERSION" python scripts/download_model.py
  65. case "$ACTION" in
  66. setup|model) echo 'Environment and model ready. Configure .env, then run sh deploy.sh templates.' ;;
  67. *)
  68. echo '[3/3] Update/start service'
  69. if [ "$ACTION" = templates ] || [ "$ACTION" = dms ]; then
  70. uv run --frozen --no-sync --python "$PYTHON_VERSION" python scripts/update_data.py --mode "$ACTION"
  71. fi
  72. if [ "$ACTION" = restart ]; then set -- --restart "$@"; fi
  73. exec uv run --frozen --no-sync --python "$PYTHON_VERSION" python scripts/start_service.py "$@"
  74. ;;
  75. esac