|
| 1 | +""" |
| 2 | +Update Modal version registries after deployment. |
| 3 | +
|
| 4 | +Each deployment creates a versioned app (e.g., policyengine-v2-us1-592-4-uk2-75-1). |
| 5 | +This script updates the version dicts to map package versions to app names. |
| 6 | +
|
| 7 | +The dicts allow Cloud Run to route requests for specific versions to the |
| 8 | +correct versioned Modal app. Multiple versions coexist — old deployments |
| 9 | +remain accessible via their version numbers. |
| 10 | +
|
| 11 | +Usage: |
| 12 | + uv run python scripts/update_version_registry.py \ |
| 13 | + --app-name policyengine-v2-us1-592-4-uk2-75-1 \ |
| 14 | + --us-version 1.592.4 \ |
| 15 | + --uk-version 2.75.1 \ |
| 16 | + --environment staging |
| 17 | +""" |
| 18 | + |
| 19 | +import argparse |
| 20 | + |
| 21 | +import modal |
| 22 | + |
| 23 | + |
| 24 | +def _upsert_entry( |
| 25 | + version_dict: modal.Dict, |
| 26 | + dict_name: str, |
| 27 | + key: str, |
| 28 | + value: str, |
| 29 | +) -> None: |
| 30 | + """Insert or update a single Dict entry, logging the change.""" |
| 31 | + try: |
| 32 | + previous = version_dict[key] |
| 33 | + if previous != value: |
| 34 | + print(f" {dict_name}[{key}]: {previous} -> {value}") |
| 35 | + else: |
| 36 | + print(f" {dict_name}[{key}]: {value} (unchanged)") |
| 37 | + except KeyError: |
| 38 | + print(f" {dict_name}[{key}]: (new) -> {value}") |
| 39 | + |
| 40 | + version_dict[key] = value |
| 41 | + |
| 42 | + |
| 43 | +def _update_latest( |
| 44 | + version_dict: modal.Dict, |
| 45 | + dict_name: str, |
| 46 | + version: str, |
| 47 | +) -> None: |
| 48 | + """Update the 'latest' pointer, logging the change.""" |
| 49 | + _upsert_entry(version_dict, dict_name, "latest", version) |
| 50 | + |
| 51 | + |
| 52 | +def update_version_dict( |
| 53 | + dict_name: str, |
| 54 | + environment: str, |
| 55 | + version: str, |
| 56 | + app_name: str, |
| 57 | +) -> None: |
| 58 | + """Update a version dict: set version → app_name and latest → version. |
| 59 | +
|
| 60 | + Args: |
| 61 | + dict_name: Name of the Modal Dict (e.g., "simulation-api-us-versions") |
| 62 | + environment: Modal environment (staging or main) |
| 63 | + version: Package version (e.g., "1.592.4") |
| 64 | + app_name: App name to map this version to |
| 65 | + """ |
| 66 | + version_dict = modal.Dict.from_name( |
| 67 | + dict_name, |
| 68 | + environment_name=environment, |
| 69 | + create_if_missing=True, |
| 70 | + ) |
| 71 | + |
| 72 | + _upsert_entry(version_dict, dict_name, version, app_name) |
| 73 | + _update_latest(version_dict, dict_name, version) |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + parser = argparse.ArgumentParser( |
| 78 | + description="Update version registries after Modal deployment" |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--app-name", |
| 82 | + required=True, |
| 83 | + help="Versioned app name (e.g., policyengine-v2-us1-592-4-uk2-75-1)", |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--us-version", |
| 87 | + required=True, |
| 88 | + help="US package version (e.g., 1.592.4)", |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "--uk-version", |
| 92 | + required=True, |
| 93 | + help="UK package version (e.g., 2.75.1)", |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + "--environment", |
| 97 | + required=True, |
| 98 | + help="Modal environment (staging or main)", |
| 99 | + ) |
| 100 | + args = parser.parse_args() |
| 101 | + |
| 102 | + print(f"Updating version registries in Modal environment: {args.environment}") |
| 103 | + print(f" App name: {args.app_name}") |
| 104 | + print(f" US version: {args.us_version}") |
| 105 | + print(f" UK version: {args.uk_version}") |
| 106 | + print() |
| 107 | + |
| 108 | + print("US version registry:") |
| 109 | + update_version_dict( |
| 110 | + "simulation-api-us-versions", |
| 111 | + args.environment, |
| 112 | + args.us_version, |
| 113 | + args.app_name, |
| 114 | + ) |
| 115 | + print() |
| 116 | + |
| 117 | + print("UK version registry:") |
| 118 | + update_version_dict( |
| 119 | + "simulation-api-uk-versions", |
| 120 | + args.environment, |
| 121 | + args.uk_version, |
| 122 | + args.app_name, |
| 123 | + ) |
| 124 | + print() |
| 125 | + |
| 126 | + print("Version registries updated successfully.") |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments