install-durable-prod-env.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # One-time (or after secret rotation): write durable env for safe-redeploy-service.sh
  3. #
  4. # Prefer dumping from a *healthy* API container so Dokploy-injected secrets
  5. # (Mittera, Polar, OAuth, …) are kept — not only the short bootstrap .env.
  6. #
  7. # On France:
  8. # bash scripts/install-durable-prod-env.sh
  9. # # or with an explicit container:
  10. # bash scripts/install-durable-prod-env.sh briven-brivenfrance-uilsk6-api-1
  11. set -euo pipefail
  12. OUT="${BRIVEN_DURABLE_ENV:-/etc/dokploy/compose/briven-brivenfrance-uilsk6/.env.prod}"
  13. CONTAINER="${1:-briven-brivenfrance-uilsk6-api-1}"
  14. BOOTSTRAP="${BRIVEN_BOOTSTRAP_ENV:-/opt/briven_deploy/infra/dokploy/.env}"
  15. mkdir -p "$(dirname "$OUT")"
  16. TMP="$(mktemp)"
  17. trap 'rm -f "$TMP"' EXIT
  18. if docker ps --format '{{.Names}}' | grep -qx "$CONTAINER"; then
  19. echo "dumping env from healthy container: $CONTAINER"
  20. docker inspect "$CONTAINER" --format '{{range .Config.Env}}{{println .}}{{end}}' >"$TMP"
  21. else
  22. echo "container $CONTAINER not running — using bootstrap only: $BOOTSTRAP"
  23. if [[ ! -f "$BOOTSTRAP" ]]; then
  24. echo "error: no container and no bootstrap env"
  25. exit 1
  26. fi
  27. # strip comments for env-file compatibility
  28. grep -v '^\s*#' "$BOOTSTRAP" | grep -v '^\s*$' >"$TMP" || true
  29. fi
  30. # Force domain origins
  31. {
  32. grep -vE '^(BRIVEN_WEB_ORIGIN|BRIVEN_API_ORIGIN|BRIVEN_DOMAIN)=' "$TMP" || true
  33. echo "BRIVEN_DOMAIN=briven.tech"
  34. echo "BRIVEN_WEB_ORIGIN=https://briven.tech"
  35. echo "BRIVEN_API_ORIGIN=https://api.briven.tech"
  36. } >"${TMP}.2"
  37. mv "${TMP}.2" "$TMP"
  38. # Overlay live doltgres password into DATABASE URLs if present
  39. if docker ps --format '{{.Names}}' | grep -qx 'briven-brivenfrance-uilsk6-doltgres-1'; then
  40. PW="$(
  41. docker inspect briven-brivenfrance-uilsk6-doltgres-1 \
  42. --format '{{range .Config.Env}}{{println .}}{{end}}' \
  43. | sed -n 's/^DOLTGRES_PASSWORD=//p'
  44. )"
  45. if [[ -n "$PW" ]]; then
  46. python3 - "$TMP" "$PW" <<'PY'
  47. import sys, re
  48. path, pw = sys.argv[1], sys.argv[2]
  49. lines = open(path).read().splitlines()
  50. out = []
  51. have = set()
  52. for line in lines:
  53. if "=" not in line or line.strip().startswith("#"):
  54. out.append(line)
  55. continue
  56. k, v = line.split("=", 1)
  57. have.add(k)
  58. if k == "BRIVEN_DOLTGRES_PASSWORD":
  59. v = pw
  60. if k in ("BRIVEN_DATABASE_URL", "BRIVEN_ENGINE_DATABASE_URL", "BRIVEN_DATA_PLANE_URL"):
  61. db = "briven_engine" if "ENGINE" in k else "briven_control"
  62. v = f"postgres://postgres:{pw}@doltgres:5432/{db}?sslmode=disable"
  63. out.append(f"{k}={v}")
  64. if "BRIVEN_DOLTGRES_PASSWORD" not in have:
  65. out.append(f"BRIVEN_DOLTGRES_PASSWORD={pw}")
  66. open(path, "w").write("\n".join(out) + "\n")
  67. PY
  68. fi
  69. fi
  70. install -m 600 "$TMP" "$OUT"
  71. echo "wrote $OUT (mode 600, $(wc -l <"$OUT") lines)"
  72. echo "use: scripts/safe-redeploy-service.sh api web docs"