LifeMate — Personal, Family & Learning Companion

feat/standalone-weekly-planner
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
sh 77 lines 4.04 KB
Raw
1 #!/usr/bin/env bash
2 # Local Docker only. This script never SSHs, modifies DNS or creates accounts.
3 set -euo pipefail
4 cd "$(dirname "${BASH_SOURCE[0]}")/.."
5 umask 077
6 prebuilt_web=no
7 if [[ "${1:-}" == '--prebuilt-web' ]]; then
8 prebuilt_web=yes
9 shift
10 fi
11 if [[ "${1:-}" == '--init' ]]; then
12 origin="${2:?Usage: scripts/local-deploy.sh --init https://LAN-IP}"
13 [[ "$origin" =~ ^https://([A-Za-z0-9.-]+|\[[0-9A-Fa-f:]+\])(:[0-9]+)?$ ]] || { echo 'Use one HTTPS origin without a path or credentials' >&2; exit 1; }
14 https_port=443
15 if [[ "$origin" =~ :([0-9]+)$ ]]; then https_port="${BASH_REMATCH[1]}"; fi
16 [[ "$https_port" -ge 1 && "$https_port" -le 65535 ]] || { echo 'Invalid HTTPS port' >&2; exit 1; }
17 if [[ -f .env ]]; then
18 echo '.env already exists; preserving it. Edit it deliberately to change settings.'
19 else
20 command -v openssl >/dev/null || { echo 'openssl is required to generate secrets' >&2; exit 1; }
21 db_secret="$(openssl rand -hex 32)"
22 jwt_secret="$(openssl rand -hex 48)"
23 sed -e "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=$db_secret|" \
24 -e "s|^JWT_SECRET=.*|JWT_SECRET=$jwt_secret|" \
25 -e "s|^PUBLIC_APP_URL=.*|PUBLIC_APP_URL=$origin|" \
26 -e "s|^CORS_ORIGINS=.*|CORS_ORIGINS=$origin|" \
27 -e "s|^HTTPS_PORT=.*|HTTPS_PORT=$https_port|" .env.example >.env
28 chmod 600 .env
29 unset db_secret jwt_secret
30 echo 'Created private .env with random local secrets.'
31 fi
32 fi
33 [[ -f .env ]] || { echo 'Create .env first, or use --init https://LAN-IP' >&2; exit 1; }
34 command -v node >/dev/null || { echo 'Node >=22 required to prepare/check independent database role settings' >&2; exit 1; }
35 node scripts/prepare-db-role-env.mjs --write
36 if grep -Eq '^((POSTGRES_PASSWORD|API_DB_PASSWORD|MIGRATOR_DB_PASSWORD|BACKUP_DB_PASSWORD|JWT_SECRET)=CHANGE_ME|JWT_SECRET=.{0,31}$)' .env; then
37 echo 'Replace placeholder/short secrets in .env' >&2; exit 1
38 fi
39 if [[ -n "${DOCKER_CONTEXT:-}" ]]; then
40 endpoint="$(docker context inspect "$DOCKER_CONTEXT" --format '{{.Endpoints.docker.Host}}')"
41 else
42 endpoint="${DOCKER_HOST:-$(docker context inspect --format '{{.Endpoints.docker.Host}}')}"
43 fi
44 case "$endpoint" in unix://*|npipe://*) ;; *) echo 'Refusing a remote Docker endpoint; this command is local only.' >&2; exit 1;; esac
45 docker info >/dev/null
46 docker compose config --quiet
47 cert_file="$(sed -n 's/^TLS_CERT_PATH=//p' .env)"
48 key_file="$(sed -n 's/^TLS_KEY_PATH=//p' .env)"
49 [[ -f "$cert_file" && -f "$key_file" ]] || { echo 'Create the configured TLS certificate/key first' >&2; exit 1; }
50 # These bind-mounted files contain public client configuration/downloads. Keep
51 # private .env and TLS keys unchanged; nginx workers must read only this data.
52 chmod 755 deployment deployment/downloads
53 chmod 644 deployment/runtime-config.json
54 find deployment/downloads -maxdepth 1 -type f -name '*.apk' -exec chmod 644 {} +
55 # nginx validates the certificate/key pair on startup.
56 compose_build=(-f docker-compose.yml)
57 if [[ "$prebuilt_web" == yes ]]; then
58 command -v node >/dev/null || { echo 'Node >=22 required to validate the prebuilt web artifact' >&2; exit 1; }
59 node scripts/prebuilt-web-check.mjs apps/lifemate
60 compose_build+=(-f docker-compose.prebuilt-web.yml)
61 fi
62 if [[ -n "${BUILD_CA_FILE:-}" ]]; then
63 [[ -f "$BUILD_CA_FILE" ]] || { echo 'BUILD_CA_FILE must be a public CA certificate file' >&2; exit 1; }
64 if grep -q 'PRIVATE KEY' "$BUILD_CA_FILE"; then echo 'Private key forbidden in build CA input' >&2; exit 1; fi
65 compose_build+=(-f docker-compose.build-proxy.yml)
66 docker compose "${compose_build[@]}" build api gateway backup
67 else
68 docker compose "${compose_build[@]}" build --build-arg HTTP_PROXY --build-arg HTTPS_PROXY api gateway backup
69 fi
70 docker compose up -d --wait --wait-timeout 120 postgres
71 docker compose run --rm --no-deps db-provision
72 docker compose run --rm --no-deps migrate
73 docker compose run --rm --no-deps catalog-import
74 docker compose run --rm --no-deps db-grants
75 docker compose up -d --wait --wait-timeout 180 api gateway backup
76 docker compose ps
77 echo 'Local stack started. Run the HTTPS smoke test; health alone does not prove family CRUD.'