|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +echo "==> Installing Python dependencies..." |
| 5 | +pip install -e ".[dev]" |
| 6 | + |
| 7 | +echo "==> Copying env.local.sample to env.local (if not present)..." |
| 8 | +if [ ! -f config/env.local ]; then |
| 9 | + cp config/env.local.sample config/env.local |
| 10 | +fi |
| 11 | + |
| 12 | +# Override hosts to point to docker-compose service names |
| 13 | +# (inside the devcontainer network, services are reachable by name) |
| 14 | +cat > config/env.devcontainer <<'EOF' |
| 15 | +#!/bin/sh |
| 16 | +version=0.0.0dev0 |
| 17 | +db_host=postgresql |
| 18 | +tests_db_host=postgresql |
| 19 | +elasticsearch_host=elasticsearch |
| 20 | +redis_url=redis://redis:6379/ |
| 21 | +EOF |
| 22 | + |
| 23 | +echo "==> Generating config files from templates..." |
| 24 | +./scripts/env_replace config/env.default config/env.dev config/env.devcontainer < alembic.ini.in > alembic.ini |
| 25 | +./scripts/env_replace config/env.default config/env.dev config/env.devcontainer < common.ini.in > common.ini |
| 26 | +./scripts/env_replace config/env.default config/env.dev config/env.devcontainer < development.ini.in > development.ini |
| 27 | +./scripts/env_replace config/env.default config/env.dev config/env.devcontainer < test.ini.in > test.ini |
| 28 | + |
| 29 | +echo "==> Waiting for PostgreSQL to be ready..." |
| 30 | +until pg_isready -h postgresql -U postgres; do |
| 31 | + echo " PostgreSQL not ready yet, retrying in 2s..." |
| 32 | + sleep 2 |
| 33 | +done |
| 34 | + |
| 35 | +echo "==> Initializing dev database..." |
| 36 | +export PGPASSWORD=test |
| 37 | +export PGUSER=postgres |
| 38 | +export PGHOST=postgresql |
| 39 | +export POSTGRES_USER=postgres |
| 40 | + |
| 41 | +# Create dev database (adapted from scripts/database/create_schema.sh) |
| 42 | +DBNAME="c2corg" |
| 43 | +if psql -tAc "SELECT 1 FROM pg_database WHERE datname='${DBNAME}'" | grep -q 1; then |
| 44 | + echo " Dev database already exists: ${DBNAME}" |
| 45 | +else |
| 46 | + echo " Creating dev database: ${DBNAME}" |
| 47 | + psql -v ON_ERROR_STOP=1 --username "$PGUSER" <<SQL |
| 48 | +CREATE DATABASE ${DBNAME} OWNER "postgres"; |
| 49 | +\c ${DBNAME} |
| 50 | +CREATE EXTENSION IF NOT EXISTS postgis; |
| 51 | +CREATE SCHEMA IF NOT EXISTS guidebook AUTHORIZATION "postgres"; |
| 52 | +CREATE SCHEMA IF NOT EXISTS users AUTHORIZATION "postgres"; |
| 53 | +CREATE SCHEMA IF NOT EXISTS sympa AUTHORIZATION "postgres"; |
| 54 | +CREATE SCHEMA IF NOT EXISTS alembic AUTHORIZATION "postgres"; |
| 55 | +SQL |
| 56 | +fi |
| 57 | +initialize_c2corg_api_db development.ini || true |
| 58 | + |
| 59 | +# Create test database (adapted from scripts/database/create_test_schema.sh) |
| 60 | +echo "==> Initializing test database..." |
| 61 | +DBNAME="c2corg_tests" |
| 62 | +if psql -tAc "SELECT 1 FROM pg_database WHERE datname='${DBNAME}'" | grep -q 1; then |
| 63 | + echo " Test database already exists: ${DBNAME}" |
| 64 | +else |
| 65 | + echo " Creating test database: ${DBNAME}" |
| 66 | + psql -v ON_ERROR_STOP=1 --username "$PGUSER" <<SQL |
| 67 | +CREATE DATABASE ${DBNAME} OWNER "postgres"; |
| 68 | +\c ${DBNAME} |
| 69 | +CREATE EXTENSION IF NOT EXISTS postgis; |
| 70 | +CREATE SCHEMA IF NOT EXISTS guidebook AUTHORIZATION "postgres"; |
| 71 | +CREATE SCHEMA IF NOT EXISTS users AUTHORIZATION "postgres"; |
| 72 | +CREATE SCHEMA IF NOT EXISTS sympa AUTHORIZATION "postgres"; |
| 73 | +CREATE SCHEMA IF NOT EXISTS alembic AUTHORIZATION "postgres"; |
| 74 | +SQL |
| 75 | +fi |
| 76 | + |
| 77 | +echo "==> Waiting for Elasticsearch to be ready..." |
| 78 | +until curl -s http://elasticsearch:9200 > /dev/null 2>&1; do |
| 79 | + echo " Elasticsearch not ready yet, retrying in 5s..." |
| 80 | + sleep 5 |
| 81 | +done |
| 82 | + |
| 83 | +echo "==> Initializing Elasticsearch indexes..." |
| 84 | +fill_es_index development.ini || true |
| 85 | + |
| 86 | +echo "" |
| 87 | +echo "=============================================" |
| 88 | +echo " Dev container setup complete!" |
| 89 | +echo "" |
| 90 | +echo " Run the API: pserve development.ini --reload" |
| 91 | +echo " Run tests: pytest" |
| 92 | +echo " Run linter: flake8 c2corg_api es_migration" |
| 93 | +echo " Or use make: make test / make serve / make lint" |
| 94 | +echo "=============================================" |
0 commit comments