""" Microsoft Foundry SDK - Part 12: From Notebook to Production Complete example demonstrating versioning, blue-green deployments, and promotion. This script shows: 1. Semantic versioning for agent releases 2. Draft vs. release version lifecycle 3. Blue-green deployment pattern 4. Environment promotion (dev → staging → prod) 5. Automatic rollback on issues 6. Production readiness checklist """ import time from datetime import datetime from azure.ai.projects import AIProjectClient from azure.ai.projects.models import HostedAgentDefinition from azure.identity import DefaultAzureCredential def create_agent_version(project_client, agent_name, version, is_draft=False): """Create a new agent version (draft or release).""" version_suffix = f"draft-{datetime.now().isoformat()}" if is_draft else version agent_def = HostedAgentDefinition( name=f"{agent_name}-{version_suffix}", instructions=f"Production-ready agent version {version}" ) print(f" ✓ Created version: {version_suffix}") return version_suffix def run_smoke_tests(agent_id, version_id): """Run smoke tests on a version.""" print(f" Running smoke tests...") tests = [ ("Health check", True), ("Basic query response", True), ("Tool invocation", True), ("Error handling", True), ] for test_name, passed in tests: status = "✓" if passed else "✗" print(f" {status} {test_name}") all_passed = all(result for _, result in tests) return all_passed def setup_blue_green(project_client, agent_id, blue_version, green_version): """Configure blue-green deployment.""" print(" Blue-Green Configuration:") print(f" Blue (Current): {blue_version} - 100% traffic") print(f" Green (Staging): {green_version} - 0% traffic") print() def switch_traffic(project_client, agent_id, from_version, to_version, percent=50): """Gradually switch traffic between versions.""" print(f" Switching traffic from {from_version} to {to_version}...") traffic_levels = [10, 25, 50, 75, 100] for traffic_pct in traffic_levels: print(f" → {traffic_pct}% traffic on {to_version}") time.sleep(0.3) # Simulate monitoring if traffic_pct == 50: print(f" ✓ Monitoring metrics: latency OK, error rate OK") print(f" ✓ Traffic successfully switched to {to_version}") def promote_environment(source_env, target_env, agent_version): """Promote agent version across environments.""" print(f"Promoting {agent_version} from {source_env} → {target_env}") print("-" * 40) steps = [ f"Validating {agent_version} in {source_env}", f"Running integration tests", f"Preparing {target_env} deployment", f"Deploying to {target_env}", f"Running smoke tests in {target_env}", f"Verifying endpoints in {target_env}" ] for i, step in enumerate(steps, 1): print(f" {i}. {step}") time.sleep(0.2) print(f" ✓ Promotion complete: {agent_version} is now in {target_env}") print() def production_checklist(): """Print production readiness checklist.""" checklist = { "Configuration": [ "Environment variables set for production", "Resource limits configured (rate limiting, tokens)", "Monitoring and alerts enabled" ], "Security": [ "Content safety guardrails enabled", "RBAC roles configured", "Audit logging enabled", "Secrets stored in Key Vault" ], "Performance": [ "Throughput targets defined", "Latency SLO set", "Cache strategy implemented", "Load testing completed" ], "Operations": [ "Runbook created", "On-call rotation established", "Blue-green deployment ready", "Automatic rollback configured", "Backup/recovery plan documented" ] } return checklist def main(): # Initialize Foundry client project_client = AIProjectClient.from_config( credential=DefaultAzureCredential() ) print(f"✓ Connected to project: {project_client.project_name}") print() agent_name = "production-agent" # ========================================================================= # Phase 1: Draft → Release Versioning # ========================================================================= print("=" * 50) print("PHASE 1: VERSIONING STRATEGY") print("=" * 50) print() print("Step 1: Create Draft Version") print("-" * 40) draft_version = create_agent_version(project_client, agent_name, "1.0.0", is_draft=True) print(" Testing draft in development environment...") if run_smoke_tests(agent_name, draft_version): print(" ✓ Draft passed all smoke tests") print() print("Step 2: Release Draft → Version 1.0.0") print("-" * 40) release_version = create_agent_version(project_client, agent_name, "1.0.0", is_draft=False) print() # ========================================================================= # Phase 2: Blue-Green Deployment # ========================================================================= print("=" * 50) print("PHASE 2: BLUE-GREEN DEPLOYMENT") print("=" * 50) print() print("Step 1: Setup Blue-Green") print("-" * 40) old_version = "0.9.5" setup_blue_green(project_client, agent_name, old_version, release_version) print("Step 2: Canary Rollout (Gradual Traffic Switch)") print("-" * 40) switch_traffic(project_client, agent_name, old_version, release_version) print() print("Step 3: Monitor & Verify") print("-" * 40) print(" Production Metrics (1 hour post-deployment):") print(" ✓ Latency: p50=150ms, p95=450ms (SLO: <500ms)") print(" ✓ Error Rate: 0.01% (SLO: <0.1%)") print(" ✓ Throughput: 5000 req/min (no throttling)") print() print("Step 4: Rollback (If Needed)") print("-" * 40) print(" If issues detected:") switch_traffic(project_client, agent_name, release_version, old_version) print(" ✓ Rollback complete - traffic restored to 0.9.5") print() # ========================================================================= # Phase 3: Environment Promotion # ========================================================================= print("=" * 50) print("PHASE 3: ENVIRONMENT PROMOTION") print("=" * 50) print() promote_environment("development", "staging", release_version) promote_environment("staging", "production", release_version) # ========================================================================= # Phase 4: Production Readiness # ========================================================================= print("=" * 50) print("PHASE 4: PRODUCTION READINESS CHECKLIST") print("=" * 50) print() checklist = production_checklist() for category, items in checklist.items(): print(f"{category}:") for item in items: print(f" ✓ {item}") print() # ========================================================================= # Phase 5: Operations # ========================================================================= print("=" * 50) print("PHASE 5: ONGOING OPERATIONS") print("=" * 50) print() print("Post-Deployment Actions:") print("-" * 40) print(" ✓ Version 1.0.0 tagged in production") print(" ✓ Metrics dashboard created") print(" ✓ Alerts configured") print(" ✓ Documentation updated") print(" ✓ Team notified of new version") print() print("Keeping Version 0.9.5 for 48 hours as safety net:") print(" → If critical bugs found in 1.0.0, rollback is < 30 seconds") print(" → After 48 hours, delete old version to free resources") if __name__ == "__main__": main()