deploy.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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=3.11 selects Python'
  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. uv python install "${DEPLOY_PYTHON:-3.11}"
  57. uv sync --frozen --python "${DEPLOY_PYTHON:-3.11}"
  58. case "$ACTION" in
  59. templates|dms|start|restart) uv run --frozen --no-sync python scripts/start_service.py --check-config "$@" ;;
  60. esac
  61. echo '[2/3] Prepare local embedding model'
  62. uv run --frozen --no-sync python scripts/download_model.py
  63. case "$ACTION" in
  64. setup|model) echo 'Environment and model ready. Configure .env, then run sh deploy.sh templates.' ;;
  65. *)
  66. echo '[3/3] Update/start service'
  67. if [ "$ACTION" = templates ] || [ "$ACTION" = dms ]; then
  68. uv run --frozen --no-sync python scripts/update_data.py --mode "$ACTION"
  69. fi
  70. if [ "$ACTION" = restart ]; then set -- --restart "$@"; fi
  71. exec uv run --frozen --no-sync python scripts/start_service.py "$@"
  72. ;;
  73. esac