LifeMate — Personal, Family & Learning Companion

test/personal-recovery-acceptance
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
ps1 93 lines 5.12 KB
Raw
1 param(
2 [Parameter(Mandatory = $true)][string]$Origin,
3 [string]$Certificate = 'deployment/tls/server.pem',
4 [string]$PrivateKey = 'deployment/tls/server-key.pem',
5 [switch]$PrebuiltWeb
6 )
7 $ErrorActionPreference = 'Stop'
8 Set-Location (Split-Path $PSScriptRoot -Parent)
9 $uri = [Uri]$Origin
10 if ($uri.Scheme -ne 'https' -or $uri.AbsolutePath -ne '/' -or $uri.Query -or $uri.Fragment -or $uri.UserInfo) {
11 throw 'Origin must be one HTTPS origin without path, credentials, query or fragment.'
12 }
13 $Origin = $uri.GetLeftPart([UriPartial]::Authority)
14 if (!(Test-Path $Certificate -PathType Leaf) -or !(Test-Path $PrivateKey -PathType Leaf)) {
15 throw 'Create TLS certificate/key with mkcert first. No automatic certificate service is contacted.'
16 }
17 if (!(Test-Path '.env')) {
18 $rng = [Security.Cryptography.RandomNumberGenerator]::Create()
19 try {
20 $dbBytes = New-Object byte[] 32
21 $jwtBytes = New-Object byte[] 48
22 $rng.GetBytes($dbBytes)
23 $rng.GetBytes($jwtBytes)
24 $dbSecret = ([BitConverter]::ToString($dbBytes)).Replace('-', '').ToLowerInvariant()
25 $jwtSecret = ([BitConverter]::ToString($jwtBytes)).Replace('-', '').ToLowerInvariant()
26 $contents = [IO.File]::ReadAllText((Join-Path (Get-Location) '.env.example'))
27 $contents = $contents -replace '(?m)^POSTGRES_PASSWORD=.*$', "POSTGRES_PASSWORD=$dbSecret"
28 $contents = $contents -replace '(?m)^JWT_SECRET=.*$', "JWT_SECRET=$jwtSecret"
29 $contents = $contents -replace '(?m)^PUBLIC_APP_URL=.*$', "PUBLIC_APP_URL=$Origin"
30 $contents = $contents -replace '(?m)^CORS_ORIGINS=.*$', "CORS_ORIGINS=$Origin"
31 $contents = $contents -replace '(?m)^HTTPS_PORT=.*$', "HTTPS_PORT=$($uri.Port)"
32 $certPath = (Resolve-Path $Certificate).Path.Replace('\', '/')
33 $keyPath = (Resolve-Path $PrivateKey).Path.Replace('\', '/')
34 $contents = $contents -replace '(?m)^TLS_CERT_PATH=.*$', "TLS_CERT_PATH=$certPath"
35 $contents = $contents -replace '(?m)^TLS_KEY_PATH=.*$', "TLS_KEY_PATH=$keyPath"
36 [IO.File]::WriteAllText((Join-Path (Get-Location) '.env'), $contents, (New-Object Text.UTF8Encoding $false))
37 } finally {
38 $rng.Dispose()
39 $dbSecret = $null
40 $jwtSecret = $null
41 }
42 Write-Host 'Created local .env with random secrets. Restrict this file to your Windows account.'
43 } else {
44 Write-Host '.env exists and is preserved; Origin/certificate changes require a deliberate .env edit.'
45 }
46 & node scripts/prepare-db-role-env.mjs --write
47 if ($LASTEXITCODE -ne 0) { throw 'Independent database role environment preparation failed.' }
48 if ((Get-Content '.env' -Raw) -match '(?m)^(POSTGRES_PASSWORD|API_DB_PASSWORD|MIGRATOR_DB_PASSWORD|BACKUP_DB_PASSWORD|JWT_SECRET)=CHANGE_ME') {
49 throw 'Replace placeholder secrets in .env.'
50 }
51 $endpoint = $null
52 $dockerContextExit = 0
53 if ($env:DOCKER_CONTEXT) {
54 $endpoint = docker context inspect $env:DOCKER_CONTEXT --format '{{.Endpoints.docker.Host}}'
55 $dockerContextExit = $LASTEXITCODE
56 } elseif ($env:DOCKER_HOST) {
57 $endpoint = $env:DOCKER_HOST
58 } else {
59 $endpoint = docker context inspect --format '{{.Endpoints.docker.Host}}'
60 $dockerContextExit = $LASTEXITCODE
61 }
62 if ($dockerContextExit -ne 0 -or $endpoint -notmatch '^(unix|npipe)://') {
63 throw 'Refusing a remote Docker endpoint; use local Docker Desktop.'
64 }
65 function Invoke-CheckedDocker {
66 param([string[]]$Arguments)
67 & docker @Arguments
68 if ($LASTEXITCODE -ne 0) { throw "Docker command failed with exit code $LASTEXITCODE" }
69 }
70 Invoke-CheckedDocker -Arguments @('info', '--format', '{{.ServerVersion}}')
71 Invoke-CheckedDocker -Arguments @('compose', 'config', '--quiet')
72 $composeBuild = @('compose', '-f', 'docker-compose.yml')
73 if ($PrebuiltWeb) {
74 & node scripts/prebuilt-web-check.mjs apps/lifemate
75 if ($LASTEXITCODE -ne 0) { throw 'Prebuilt web artifact is missing, stale or has external resources.' }
76 $composeBuild += @('-f', 'docker-compose.prebuilt-web.yml')
77 }
78 if ($env:BUILD_CA_FILE) {
79 if (!(Test-Path $env:BUILD_CA_FILE -PathType Leaf) -or ((Get-Content $env:BUILD_CA_FILE -Raw) -match 'PRIVATE KEY')) {
80 throw 'BUILD_CA_FILE must contain a public CA certificate, never a private key.'
81 }
82 Invoke-CheckedDocker -Arguments ($composeBuild + @('-f', 'docker-compose.build-proxy.yml', 'build', 'api', 'gateway', 'backup'))
83 } else {
84 Invoke-CheckedDocker -Arguments ($composeBuild + @('build', '--build-arg', 'HTTP_PROXY', '--build-arg', 'HTTPS_PROXY', 'api', 'gateway', 'backup'))
85 }
86 Invoke-CheckedDocker -Arguments @('compose', 'up', '-d', '--wait', '--wait-timeout', '120', 'postgres')
87 Invoke-CheckedDocker -Arguments @('compose', 'run', '--rm', '--no-deps', 'db-provision')
88 Invoke-CheckedDocker -Arguments @('compose', 'run', '--rm', '--no-deps', 'migrate')
89 Invoke-CheckedDocker -Arguments @('compose', 'run', '--rm', '--no-deps', 'catalog-import')
90 Invoke-CheckedDocker -Arguments @('compose', 'run', '--rm', '--no-deps', 'db-grants')
91 Invoke-CheckedDocker -Arguments @('compose', 'up', '-d', '--wait', '--wait-timeout', '180', 'api', 'gateway', 'backup')
92 Invoke-CheckedDocker -Arguments @('compose', 'ps')
93 Write-Host 'Local stack started. Run the HTTPS smoke test with synthetic accounts.'