"""Container-side steps for the operator-run CORM PostgreSQL deployment.""" import argparse import json import os import sys from contextlib import closing from pathlib import Path import psycopg2 from psycopg2 import sql from dotenv import set_key from history_migrate import load_backup, read_source from history_store import PostgresHistoryStore, validate_bundle from settings_migrate import load_snapshot, read_google_rows, save_snapshot from settings_store import PostgresSettingsStore, SettingsNotInitialized, settings_db_config def ensure_database(): config = settings_db_config() if config["dbname"] != "corm_db": raise ValueError("배포 대상은 corm_db여야 합니다.") admin_config = {**config, "dbname": "postgres"} with closing(psycopg2.connect(**admin_config, connect_timeout=3)) as connection: connection.autocommit = True with connection.cursor() as cursor: cursor.execute("SELECT 1 FROM pg_database WHERE datname = %s", (config["dbname"],)) if cursor.fetchone() is None: cursor.execute(sql.SQL("CREATE DATABASE {} OWNER {}") .format(sql.Identifier(config["dbname"]), sql.Identifier(config["user"]))) print("corm_db 생성 완료.") else: print("기존 corm_db 사용. 기존 데이터는 덮어쓰지 않습니다.") def snapshot(directory, settings_source="google", history_source="google"): directory = Path(directory) if settings_source == "postgres": rows, _ = PostgresSettingsStore(settings_db_config()).read() source = "postgres:corm_db:settings" else: rows, source = read_google_rows() save_snapshot(directory / "settings-source.json", rows, source) if history_source == "postgres": bundle = PostgresHistoryStore(settings_db_config()).export_all() source = "postgres:corm_db:histories" else: sms = directory / "legacy-sms.dat" manual = directory / "legacy-manual.dat" bundle, source = read_source(str(sms) if sms.exists() else None, str(manual) if manual.exists() else None) save_snapshot(directory / "history-source.json", bundle, source) # Container-created backups should remain readable by the server operator. owner = directory.stat() for name in ("settings-source.json", "history-source.json"): os.chown(directory / name, owner.st_uid, owner.st_gid) print(f"원본 백업 완료: 설정 {len(rows)}행 / 문자 {len(bundle['sms'])}건 / 수동발주 {len(bundle['manual'])}건.") def migrate(directory): directory = Path(directory) rows, source = load_snapshot(directory / "settings-source.json") bundle, history_source = load_backup(directory / "history-source.json") settings = PostgresSettingsStore(settings_db_config()) histories = PostgresHistoryStore(settings_db_config()) try: settings.read() except SettingsNotInitialized: settings.import_rows(rows, source) if settings.read()[0] != rows: raise RuntimeError("기존 DB 설정과 원본이 다릅니다. 덮어쓰지 않고 중단합니다.") try: histories.check_ready() except (SettingsNotInitialized, psycopg2.errors.UndefinedTable): histories.import_all(bundle, history_source) if validate_bundle(histories.export_all()) != validate_bundle(bundle): raise RuntimeError("기존 DB 내역과 원본이 다릅니다. 덮어쓰지 않고 중단합니다.") print("설정·문자내역·수동발주내역 전체 데이터 검증 완료.") def switch_environment(path): target = Path(path) before = target.stat() for key, value in (("SETTINGS_DB", "corm_db"), ("SETTINGS_BACKEND", "postgres"), ("HISTORY_BACKEND", "postgres")): set_key(target, key, value, quote_mode="never") os.chmod(target, 0o600) os.chown(target, before.st_uid, before.st_gid) print("설정·내역 저장소를 corm_db로 전환했습니다.") def probe(): import requests if os.getenv("SETTINGS_BACKEND") != "postgres" or os.getenv("HISTORY_BACKEND") != "postgres": raise RuntimeError("앱 저장소 환경이 아직 postgres가 아닙니다.") settings = PostgresSettingsStore(settings_db_config()) settings.read() PostgresHistoryStore(settings_db_config()).check_ready() token = os.getenv("INTERNAL_SERVICE_TOKEN", "") if not token: raise RuntimeError("API 검증용 기존 INTERNAL_SERVICE_TOKEN이 없습니다.") headers = {"X-Service-Token": token} for path in ("/api/config", "/api/sms/history", "/api/manual/history"): response = requests.get("http://127.0.0.1:8002" + path, headers=headers, timeout=5) response.raise_for_status() data = response.json() if path == "/api/config": if response.headers.get("X-Settings-Backend") != "postgres" or not isinstance(data, dict): raise RuntimeError("설정 API 저장소 검증 실패.") elif not isinstance(data, list): raise RuntimeError("내역 API 검증 실패.") print("DB 및 설정·문자내역·수동발주내역 API 조회 정상.") def main(argv=None): parser = argparse.ArgumentParser(description="CORM 운영자 배포 스크립트 내부 단계") parser.add_argument("step", choices=("snapshot", "database", "migrate", "environment", "probe")) parser.add_argument("--directory", default="/backups") parser.add_argument("--env-path", default="/deploy/.env") parser.add_argument("--settings-source", choices=("google", "postgres"), default="google") parser.add_argument("--history-source", choices=("google", "postgres"), default="google") args = parser.parse_args(argv) try: if args.step == "snapshot": snapshot(args.directory, args.settings_source, args.history_source) elif args.step == "database": ensure_database() elif args.step == "migrate": migrate(args.directory) elif args.step == "environment": switch_environment(args.env_path) else: probe() return 0 except Exception as exc: # Do not print credentials, customer records or request headers. print(f"배포 단계 실패: {args.step} ({type(exc).__name__}). 원본/접속/DB 권한을 확인해주세요.", file=sys.stderr) return 1 if __name__ == "__main__": raise SystemExit(main())