#!/usr/bin/env python3 """Provision only this generated project's local Compose development account.""" import json import os import subprocess import sys from pathlib import Path project = os.environ.get("BORTSTACK_COMPOSE_PROJECT", "") compose_file = Path(os.environ.get("BORTSTACK_COMPOSE_FILE", "")) secret_file = Path(os.environ.get("BORTSTACK_CREDENTIALS_FILE", "")) if not project.startswith("bortstack-") or not compose_file.is_file() or not secret_file.is_file(): raise SystemExit("local Compose project and temporary credentials are required") if os.environ.get("APP_ENV", "development") != "development": raise SystemExit("refusing account provisioning outside development") secret = json.loads(secret_file.read_text(encoding="utf-8")) if not secret.get("BORTSTACK_TEST_EMAIL") or not secret.get("BORTSTACK_TEST_PASSWORD"): raise SystemExit("missing local test credentials") base = ["docker", "compose", "--project-name", project, "--file", str(compose_file)] if subprocess.run(base + ["exec", "-T", "api", "flask", "--app", "api.py", "db", "upgrade"], check=False).returncode: raise SystemExit("local development migration failed") code = ''' import json, sys from datetime import datetime from sqlalchemy import func from settings import app, db from models.user import User, normalize_email values = json.load(sys.stdin) email = normalize_email(values["BORTSTACK_TEST_EMAIL"]) password = values["BORTSTACK_TEST_PASSWORD"] if len(password) < 8: raise SystemExit("test password must have at least eight characters") with app.app_context(): existing = User.query.filter(func.lower(User.email) == email).first() if existing: if not existing.check_password(password) or existing.email_verified_at is None: raise SystemExit("test account exists with different credentials or is unverified; no account was changed") else: user = User(email=email, username="Local Test User", email_verified_at=datetime.utcnow()) user.set_password(password) db.session.add(user) db.session.commit() ''' result = subprocess.run(base + ["exec", "-T", "api", "python", "-c", code], input=json.dumps(secret), text=True, check=False) sys.exit(result.returncode)