|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Create local secret env files (secrets/*.env) from their committed |
| 4 | +# *.env.sample templates. Idempotent: existing files are left untouched. |
| 5 | +# |
| 6 | +# Secrets are gitignored, so a fresh clone / CI runner has only the samples. |
| 7 | +# Running `make dev` (or build/test) without the real files fails with |
| 8 | +# "env file ... not found". This script bootstraps them for local dev. |
| 9 | +# |
| 10 | +# Coupling: MYSQL_ROOT_PASSWORD must be identical in ops_mysql_dev.env and |
| 11 | +# ops_app_celery.env or the app/celery containers cannot auth to MySQL. |
| 12 | +# We generate the password once and reuse it (and reuse an existing one if |
| 13 | +# only some files are present) to keep them in sync. |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | +SECRETS_DIR="$(cd "${SCRIPT_DIR}/../secrets" && pwd)" |
| 19 | + |
| 20 | +randstr() { LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c "$1"; } |
| 21 | + |
| 22 | +# Extract a KEY=value from a file, or empty string if absent. |
| 23 | +readval() { |
| 24 | + local key="$1" file="$2" |
| 25 | + [ -f "$file" ] && sed -n "s/^${key}=//p" "$file" | head -n1 || true |
| 26 | +} |
| 27 | + |
| 28 | +# Precedence: env override (CI/deterministic) > existing file (stay consistent |
| 29 | +# with an already-initialised MySQL volume) > freshly generated random value. |
| 30 | +PW="${MYSQL_ROOT_PASSWORD:-}" |
| 31 | +[ -n "$PW" ] || PW="$(readval MYSQL_ROOT_PASSWORD "${SECRETS_DIR}/ops_mysql_dev.env")" |
| 32 | +[ -n "$PW" ] || PW="$(readval MYSQL_ROOT_PASSWORD "${SECRETS_DIR}/ops_app_celery.env")" |
| 33 | +[ -n "$PW" ] || PW="$(randstr 24)" |
| 34 | + |
| 35 | +TOKEN="${APP_TOKEN:-}" |
| 36 | +[ -n "$TOKEN" ] || TOKEN="$(readval TOKEN "${SECRETS_DIR}/ops_app_dev.env")" |
| 37 | +[ -n "$TOKEN" ] || TOKEN="$(readval TOKEN "${SECRETS_DIR}/ops_app_test.env")" |
| 38 | +[ -n "$TOKEN" ] || TOKEN="$(randstr 32)" |
| 39 | + |
| 40 | +created=0 |
| 41 | +gen() { |
| 42 | + local out="$1"; shift |
| 43 | + if [ -f "${SECRETS_DIR}/${out}" ]; then |
| 44 | + return 0 |
| 45 | + fi |
| 46 | + # Remaining args: sed substitution expressions. |
| 47 | + local sed_args=() |
| 48 | + for expr in "$@"; do sed_args+=(-e "$expr"); done |
| 49 | + sed "${sed_args[@]}" "${SECRETS_DIR}/${out}.sample" > "${SECRETS_DIR}/${out}" |
| 50 | + echo " created secrets/${out}" |
| 51 | + created=1 |
| 52 | +} |
| 53 | + |
| 54 | +gen ops_mysql_dev.env "s|__CHANGE_ME__|${PW}|g" |
| 55 | +gen ops_app_celery.env "s|__CHANGE_ME__|${PW}|g" |
| 56 | +gen ops_app_dev.env "s|__CHANGE_ME__|${TOKEN}|g" |
| 57 | +gen ops_app_test.env "s|__CHANGE_ME__|${TOKEN}|g" |
| 58 | + |
| 59 | +if [ "$created" -eq 1 ]; then |
| 60 | + echo "Local secret env files ready. These are gitignored — edit values as needed." |
| 61 | +else |
| 62 | + echo "Secret env files already present — nothing to do." |
| 63 | +fi |
0 commit comments