aima_phone_catcher/script/launch.sh

62 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
ENV_FILE="${MOTOR_ENV_FILE:-${PROJECT_ROOT}/.env}"
cd "${PROJECT_ROOT}"
if ! command -v uv >/dev/null 2>&1; then
echo "error: uv is not installed or is not in PATH" >&2
exit 1
fi
if [[ ! -f "${ENV_FILE}" ]]; then
cp -- "${PROJECT_ROOT}/.env.example" "${ENV_FILE}"
echo "Created ${ENV_FILE} from .env.example." >&2
echo "Set MOTOR_HARDWARE_ENABLED=true after checking the hardware, then run again." >&2
exit 1
fi
set -a
# shellcheck disable=SC1090
source "${ENV_FILE}"
set +a
SERVICE_PORT="${MOTOR_HTTP_PORT:-8000}"
if command -v curl >/dev/null 2>&1; then
HEALTH_CODE="$(
curl --noproxy '*' --silent --output /dev/null --write-out '%{http_code}' \
--max-time 1 "http://127.0.0.1:${SERVICE_PORT}/healthz" || true
)"
if [[ "${HEALTH_CODE}" == "200" ]]; then
OPEN_ROUTE_CODE="$(
curl --noproxy '*' --silent --output /dev/null --write-out '%{http_code}' \
--max-time 1 "http://127.0.0.1:${SERVICE_PORT}/v1/motor/open" || true
)"
REMOVED_ENABLE_ROUTE_CODE="$(
curl --noproxy '*' --silent --output /dev/null --write-out '%{http_code}' \
--max-time 1 "http://127.0.0.1:${SERVICE_PORT}/v1/motor/enable" || true
)"
if [[ "${OPEN_ROUTE_CODE}" == "405" && "${REMOVED_ENABLE_ROUTE_CODE}" == "404" ]]; then
echo "Lingkong motor service is already running on port ${SERVICE_PORT}."
exit 0
fi
echo "error: an older or different service is already using port ${SERVICE_PORT}" >&2
echo "Stop that service before launching the new open/close API." >&2
exit 1
fi
fi
# Samba/NAS mounts may not support uv's virtual-environment symlinks reliably.
export UV_PROJECT_ENVIRONMENT="${UV_PROJECT_ENVIRONMENT:-/tmp/lingkong-motor-service-venv}"
echo "Synchronizing the locked environment in ${UV_PROJECT_ENVIRONMENT}..."
uv sync --locked
echo "Starting Lingkong motor service on ${MOTOR_HTTP_HOST:-0.0.0.0}:${SERVICE_PORT}..."
echo "Motors auto-enable at startup; Ctrl-C performs Stop followed by Disable."
exec uv run --locked lingkong-motor-service