MEGAINFRA-WEBAPP/scripts/pb-enable-batch-api.py

74 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Enable PocketBase's transactional batch API.
The staged delivery-note pipeline verifies its batch lease and commits the
protected business write inside one transaction, which requires this endpoint.
With it disabled the runner fails closed with
``dn_pipeline_transactions_required``.
The default 3s transaction timeout is raised because a staged batch commits
page images alongside its metadata.
Dry run unless ``--apply`` is supplied, and ``--apply`` additionally requires
``--confirm-host`` matching the configured PocketBase host exactly.
"""
import sys
from pb_env import PB, http, superuser_token
WANT_TIMEOUT = 30
APPLY = "--apply" in sys.argv
def confirmation_host():
for index, argument in enumerate(sys.argv):
if argument.startswith("--confirm-host="):
return argument.split("=", 1)[1]
if argument == "--confirm-host" and index + 1 < len(sys.argv):
return sys.argv[index + 1]
return None
def main():
if APPLY and confirmation_host() != PB:
sys.exit(
f"REFUSING --apply: pass --confirm-host with the exact configured "
f"host ({PB})."
)
token = superuser_token()
print(f"auth ok on {PB}")
print(f"target: {PB} mode: {'APPLY' if APPLY else 'DRY RUN'}")
code, settings = http("GET", "/api/settings", token=token)
if code != 200:
sys.exit(f"could not read settings ({code})")
batch = dict(settings.get("batch") or {})
print(f" current: enabled={batch.get('enabled')} timeout={batch.get('timeout')} maxRequests={batch.get('maxRequests')}")
desired = dict(batch)
desired["enabled"] = True
if int(batch.get("timeout") or 0) < WANT_TIMEOUT:
desired["timeout"] = WANT_TIMEOUT
if desired == batch:
print(" already configured; nothing to do")
return
if not APPLY:
print(f" would set: enabled={desired['enabled']} timeout={desired['timeout']} maxRequests={desired.get('maxRequests')}")
print("\nDRY RUN. No settings written.")
return
code, response = http("PATCH", "/api/settings", {"batch": desired}, token=token)
if code != 200:
sys.exit(f" PATCH failed {code}: {response}")
applied = (response or {}).get("batch", desired)
print(f" applied: enabled={applied.get('enabled')} timeout={applied.get('timeout')} maxRequests={applied.get('maxRequests')}")
print("\nAPPLIED.")
main()