|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Deploy the OHM **frontend** container to Azure Container Apps. |
| 3 | +
|
| 4 | +Updates the frontend container app's image and applies its non-secret |
| 5 | +per-environment config (``API_UPSTREAM_URL``, ``PORT``) from the |
| 6 | +``[frontend]`` table of ``config/environments/<environment>.toml`` via |
| 7 | +``--set-env-vars`` (additive). The frontend has no secrets. |
| 8 | +
|
| 9 | +The container app itself (ingress, target port 8080) is provisioned once |
| 10 | +out-of-band; this script only updates an existing app, mirroring |
| 11 | +``deploy_azure.py`` for the backend. |
| 12 | +""" |
| 13 | + |
| 14 | +import argparse |
| 15 | +import logging |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +# Add project root to path |
| 20 | +project_root = Path(__file__).parent.parent.parent |
| 21 | +sys.path.insert(0, str(project_root)) |
| 22 | + |
| 23 | +from deploy.providers.azure import ( |
| 24 | + AzureContainerAppsDeployer, |
| 25 | + AzureDeploymentConfig, |
| 26 | + DeploymentError, |
| 27 | +) |
| 28 | +from src.config.schema import frontend_deploy_env_vars |
| 29 | + |
| 30 | +logging.basicConfig( |
| 31 | + level=logging.INFO, |
| 32 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 33 | +) |
| 34 | +logger = logging.getLogger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + """Deploy the frontend to Azure Container Apps.""" |
| 39 | + parser = argparse.ArgumentParser(description="Deploy the OHM frontend to Azure") |
| 40 | + parser.add_argument( |
| 41 | + "--resource-group", |
| 42 | + default="project_data_rg", |
| 43 | + help="Azure resource group (default: project_data_rg)", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--subscription-id", |
| 47 | + default=None, |
| 48 | + help="Azure subscription ID (or set AZURE_SUBSCRIPTION_ID)", |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--container-app-name", |
| 52 | + default="openhardwaremanager-frontend", |
| 53 | + help="Frontend Container App name (default: openhardwaremanager-frontend)", |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--region", |
| 57 | + default="eastus", |
| 58 | + help="Azure region, only used if the resource group needs creating", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--image", |
| 62 | + required=True, |
| 63 | + help="Frontend image to deploy (e.g. touchthesun/openhardwaremanager-frontend:0.8.8)", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "--environment", |
| 67 | + default="production", |
| 68 | + help=( |
| 69 | + "Target environment; selects config/environments/<env>.toml [frontend] " |
| 70 | + "(default: production). Deliberately NOT read from the ENVIRONMENT env " |
| 71 | + "var — importing src.config loads .env, so inferring the deploy target " |
| 72 | + "from it would let a local .env redirect a prod deploy. Pass explicitly " |
| 73 | + "for anything but production." |
| 74 | + ), |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + "--memory", default="1Gi", help="Memory allocation (default: 1Gi)" |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--cpu", type=float, default=0.5, help="CPU allocation (default: 0.5)" |
| 81 | + ) |
| 82 | + parser.add_argument("--min-instances", type=int, default=1) |
| 83 | + parser.add_argument("--max-instances", type=int, default=3) |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + import os |
| 87 | + |
| 88 | + subscription_id = args.subscription_id or os.getenv("AZURE_SUBSCRIPTION_ID") |
| 89 | + if not subscription_id: |
| 90 | + print("❌ Error: --subscription-id is required or set AZURE_SUBSCRIPTION_ID") |
| 91 | + return 1 |
| 92 | + |
| 93 | + # Non-secret frontend config from the centralized config surface. The |
| 94 | + # frontend has no secrets; API_UPSTREAM_URL / PORT are the whole surface. |
| 95 | + environment_vars = frontend_deploy_env_vars(args.environment) |
| 96 | + if not environment_vars.get("API_UPSTREAM_URL"): |
| 97 | + print( |
| 98 | + f"❌ Error: no [frontend].api_upstream_url for environment " |
| 99 | + f"{args.environment!r} in config/environments/{args.environment}.toml" |
| 100 | + ) |
| 101 | + return 1 |
| 102 | + |
| 103 | + print("=" * 80) |
| 104 | + print("Azure Container Apps Deployment — FRONTEND") |
| 105 | + print("=" * 80) |
| 106 | + print(f"Resource Group: {args.resource_group}") |
| 107 | + print(f"Container App: {args.container_app_name}") |
| 108 | + print(f"Image: {args.image}") |
| 109 | + print(f"Environment: {args.environment}") |
| 110 | + print("Applying frontend config from the config surface:") |
| 111 | + for key, value in environment_vars.items(): |
| 112 | + print(f" {key}={value}") |
| 113 | + print("=" * 80) |
| 114 | + |
| 115 | + try: |
| 116 | + config = AzureDeploymentConfig.from_dict( |
| 117 | + { |
| 118 | + "provider": "azure", |
| 119 | + "environment": args.environment, |
| 120 | + "region": args.region, |
| 121 | + "service": { |
| 122 | + "name": args.container_app_name, |
| 123 | + "image": args.image, |
| 124 | + "memory": args.memory, |
| 125 | + "cpu": args.cpu, |
| 126 | + "min_instances": args.min_instances, |
| 127 | + "max_instances": args.max_instances, |
| 128 | + "environment_vars": environment_vars, |
| 129 | + }, |
| 130 | + "providers": { |
| 131 | + "azure": { |
| 132 | + "resource_group": args.resource_group, |
| 133 | + "subscription_id": subscription_id, |
| 134 | + } |
| 135 | + }, |
| 136 | + } |
| 137 | + ) |
| 138 | + |
| 139 | + deployer = AzureContainerAppsDeployer(config) |
| 140 | + |
| 141 | + print("\n🚀 Starting frontend deployment...") |
| 142 | + service_url = deployer.deploy() |
| 143 | + |
| 144 | + print("\n" + "=" * 80) |
| 145 | + print("✅ Frontend Deployment Successful!") |
| 146 | + print("=" * 80) |
| 147 | + print(f"Service URL: {service_url}") |
| 148 | + print("=" * 80) |
| 149 | + return 0 |
| 150 | + |
| 151 | + except DeploymentError as e: |
| 152 | + print(f"\n❌ Deployment failed: {e}") |
| 153 | + return 1 |
| 154 | + except Exception as e: |
| 155 | + print(f"\n❌ Unexpected error: {e}") |
| 156 | + import traceback |
| 157 | + |
| 158 | + traceback.print_exc() |
| 159 | + return 1 |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + sys.exit(main()) |
0 commit comments