Spaces:
Paused

File size: 5,652 Bytes
383d839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# YAZ Care Platform — Deployment Guide

## Prerequisites

- Python 3.12+
- Podman (rootless) or Docker 24+
- Docker Compose v2 or podman-compose
- PostgreSQL 16+ (or managed PG)
- Redis 7+
- `uv` (dependency manager): `curl -LsSf https://astral.sh/uv/install.sh | sh`

---

## Quick Start (Development)

```bash
# 1. Clone and enter
git clone https://github.com/Elanass/yaz && cd yaz

# 2. Create virtual env + install deps (uv)
uv venv && source .venv/bin/activate
uv pip install -r requirements.txt

# 3. Copy and fill env vars
cp .env.example .env
# Edit .env: DATABASE_URL, REDIS_URL, JWT_SECRET_KEY, PII_VAULT_KEY

# 4. Apply migrations
export PYTHONPATH=src
alembic upgrade head

# 5. Start dev server (hot-reload)
uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload --reload-dir src

# 6. Start arq worker (separate terminal)
python -m workers.worker
```

---

## Environment Variables

All required vars must be present at startup; missing required vars cause `RuntimeError` on boot.

| Variable | Required | Default | Description |
|---|---|---|---|
| `DATABASE_URL` | ✅ | `sqlite:///./data/databases/yaz_care.db` | PostgreSQL or SQLite URL |
| `REDIS_URL` | ✅ | `redis://localhost:6379/0` | Redis connection URL |
| `JWT_SECRET_KEY` | ✅ prod | `dev-jwt-*` | HMAC secret (HS256 dev) |
| `JWT_ALGORITHM` | — | `HS256` | `RS256` in production |
| `JWT_PRIVATE_KEY` | ✅ RS256 | — | PEM RSA private key |
| `JWT_PUBLIC_KEY` | ✅ RS256 | — | PEM RSA public key |
| `PII_VAULT_KEY` | ✅ prod | derived | Base64-encoded 32-byte AES key |
| `YAZ_ENV` | — | `development` | `development\|testing\|production` |
| `CORS_ORIGINS` | — | localhost | Comma-separated allowed origins |
| `SMTP_HOST` | — | — | SMTP server for email jobs |
| `SMTP_PORT` | — | `587` | SMTP port |
| `SMTP_USER` | — | — | SMTP username |
| `SMTP_PASSWORD` | — | — | SMTP password |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | — | — | OTLP HTTP endpoint |
| `ARGON2_TIME_COST` | — | `3` | Argon2id time parameter |
| `ARGON2_MEMORY_COST` | — | `65536` | Argon2id memory (KB) |
| `ACCESS_TOKEN_TTL` | — | `900` | Access token seconds (15 min) |
| `REFRESH_TOKEN_TTL` | — | `604800` | Refresh token seconds (7 days) |

See `.env.example` for the complete annotated list.

---

## Container Deployment

### Build
```bash
# Podman (rootless)
podman build --target production -t ghcr.io/elanass/yaz-care:latest -f Containerfile .

# Docker
docker build --target production -t ghcr.io/elanass/yaz-care:latest -f Containerfile .
```

### Run (compose)
```bash
# Development (no nginx)
podman-compose -f infra/compose.yaml up -d

# With nginx + OTEL collector
podman-compose -f infra/compose.yaml --profile production --profile observability up -d
```

### Production compose
```bash
podman-compose -f infra/compose.yaml -f docker-compose.production.yml up -d
```

---

## Database Migrations

```bash
# Apply all migrations
alembic upgrade head

# Create new migration (after model changes)
alembic revision --autogenerate -m "describe the change"

# Downgrade one step
alembic downgrade -1

# Check for uncommitted changes
alembic check
```

**Expand/Contract pattern** — see [migration runbook](runbooks/migration.md) and [ADR-007](adr/ADR-007-expand-contract.md).

---

## Makefile Commands

```bash
make install       # uv pip install -r requirements.txt
make dev           # uvicorn with hot-reload
make worker        # arq worker process
make test          # full test suite with containers
make test-unit     # unit tests only (no DB required)
make lint          # ruff check + format
make typecheck     # mypy
make migrate       # alembic upgrade head
make migration     # alembic revision --autogenerate -m "..."
make health        # curl /api/v1/health/ready
make shell         # python shell with app context
```

---

## Production Hardening Checklist

- [ ] `YAZ_ENV=production`
- [ ] `DEBUG=false`  
- [ ] `JWT_ALGORITHM=RS256` with RSA key pair
- [ ] `PII_VAULT_KEY` set (or KMS/Vault configured)
- [ ] `CORS_ORIGINS` contains only `https://` domains
- [ ] PostgreSQL RLS enabled on all org-scoped tables
- [ ] Redis password set (`REDIS_PASSWORD`)
- [ ] TLS enforced (nginx or cloud LB)
- [ ] HSTS header enabled (automatic when `YAZ_ENV=production`)
- [ ] Secrets from Vault or SOPS (no `.env` with real secrets in git)
- [ ] `alembic check` passes (no uncommitted migrations)
- [ ] Container image scanned by Trivy (0 CRITICAL/HIGH)
- [ ] Backups configured (see [restore runbook](runbooks/restore.md))
- [ ] Alerting rules deployed (see [runbooks/](runbooks/))
- [ ] MFA enforced for admin and surgeon roles

---

## Health Checks

```bash
# Liveness
curl http://localhost:8000/api/v1/health

# Readiness (DB + Redis)
curl http://localhost:8000/api/v1/health/ready
# → { "db": "ok", "redis": "ok", "worker": "ok" }

# Prometheus metrics
curl http://localhost:9090/metrics
```

---

## Scaling

- **Horizontal API scaling**: `WORKERS=N` in compose; all instances stateless
- **Worker scaling**: run multiple `python -m workers.worker` processes  
  (set `YAZ_WORKER_QUEUE` to target high/default/low queue)
- **Read replicas**: set `ANALYTICS_DATABASE_URL` for report queries
- **Redis**: use Redis Sentinel or Cluster for HA

---

## CI/CD

Pipeline jobs (in order):
1. **lint** — ruff
2. **typecheck** — mypy
3. **secret-scan** — gitleaks (blocks on detected secrets)
4. **test** — pytest + real PG/Redis
5. **migration-check** — alembic check
6. **build** — buildah → GHCR (`main`/`master` only)
7. **trivy-scan** — CRITICAL/HIGH → fail

See [.github/workflows/ci.yml](../.github/workflows/ci.yml).