-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathpost_deploy.py
More file actions
731 lines (602 loc) · 33.8 KB
/
Copy pathpost_deploy.py
File metadata and controls
731 lines (602 loc) · 33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env python3
"""
Post-Deployment Script for Content Generation Solution Accelerator.
This unified script handles all post-deployment tasks by calling admin APIs
that run inside the VNet (bypassing firewall restrictions):
1. Upload product images via /api/admin/upload-images
2. Load sample product data via /api/admin/load-sample-data
3. Create and populate Azure AI Search index via /api/admin/create-search-index
4. Run application health tests
The admin APIs run inside the ACI container which has private endpoint access
to Blob Storage, Cosmos DB, and Azure AI Search - eliminating the need to
modify firewall rules.
Usage:
python post_deploy.py [options]
Reads resource names from azd environment variables by default.
Can override with explicit arguments if needed.
Options:
--resource-group, -g Resource group name (or use RESOURCE_GROUP_NAME env var)
--app-name App Service name (or use APP_SERVICE_NAME env var)
--storage-account Storage account name (or use AZURE_BLOB_ACCOUNT_NAME env var)
--cosmos-account Cosmos DB account name (or use COSMOSDB_ACCOUNT_NAME env var)
--search-service AI Search service name (or use AI_SEARCH_SERVICE_NAME env var)
--api-key Admin API key (or use ADMIN_API_KEY env var)
--skip-images Skip uploading images
--skip-data Skip loading sample data
--skip-index Skip creating search index
--skip-tests Skip application tests
--dry-run Show what would be done without executing
"""
import argparse
import asyncio
import base64
import json
import os
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional
try:
import httpx
except ModuleNotFoundError as exc:
missing = getattr(exc, "name", "<unknown>")
print("\nERROR: Missing Python dependency: %s\n" % missing)
print("Install post-deploy dependencies first, e.g.:")
print(" python -m pip install httpx")
sys.exit(2)
@dataclass
class ResourceConfig:
"""Configuration for Azure resources."""
resource_group: str
app_service: str
app_url: str
api_key: str = ""
storage_account: str = "" # Only needed for reference, not direct access
cosmos_account: str = "" # Only needed for reference, not direct access
search_service: str = "" # Only needed for reference, not direct access
container_name: str = "product-images"
class Colors:
"""ANSI color codes for terminal output."""
GREEN = '\033[92m'
RED = '\033[91m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
CYAN = '\033[96m'
BOLD = '\033[1m'
END = '\033[0m'
def print_header(text: str):
"""Print a section header."""
print(f"\n{Colors.BOLD}{Colors.CYAN}{'=' * 70}{Colors.END}")
print(f"{Colors.BOLD}{Colors.CYAN}{text}{Colors.END}")
print(f"{Colors.BOLD}{Colors.CYAN}{'=' * 70}{Colors.END}\n")
def print_step(text: str):
"""Print a step indicator."""
print(f"{Colors.BLUE}→ {text}{Colors.END}")
def print_success(text: str):
"""Print a success message."""
print(f"{Colors.GREEN}✓ {text}{Colors.END}")
def print_error(text: str):
"""Print an error message."""
print(f"{Colors.RED}✗ {text}{Colors.END}")
def print_warning(text: str):
"""Print a warning message."""
print(f"{Colors.YELLOW}⚠ {text}{Colors.END}")
def get_values_from_az_deployment(resource_group: str) -> Optional[Dict[str, str]]:
"""Get deployment output values from Azure using az CLI."""
print_step("Getting values from Azure deployment outputs...")
try:
# First, try to get deployment name from resource group tags
result = subprocess.run(
["az", "group", "show", "--name", resource_group, "--query", "tags.DeploymentName", "-o", "tsv"],
capture_output=True,
text=True,
check=True,
shell=(os.name == "nt") # Required on Windows to find az.cmd
)
deployment_name = result.stdout.strip()
# If no tag found, try to find the most recent successful deployment
if not deployment_name or deployment_name == "None":
print_warning("No DeploymentName tag found on resource group")
print_step("Searching for most recent successful deployment...")
result = subprocess.run(
["az", "deployment", "group", "list",
"--resource-group", resource_group,
"--query", "[?properties.provisioningState=='Succeeded'] | sort_by(@, &properties.timestamp) | [-1].name",
"-o", "tsv"],
capture_output=True,
text=True,
check=True,
shell=(os.name == "nt") # Required on Windows to find az.cmd
)
deployment_name = result.stdout.strip()
if not deployment_name or deployment_name == "None":
print_warning("No successful deployments found in resource group")
return None
print(f" Found deployment: {deployment_name}")
else:
print(f" Deployment Name (from tag): {deployment_name}")
# Get deployment outputs
print_step("Fetching deployment outputs...")
result = subprocess.run(
["az", "deployment", "group", "show",
"--name", deployment_name,
"--resource-group", resource_group,
"--query", "properties.outputs",
"-o", "json"],
capture_output=True,
text=True,
check=True,
shell=(os.name == "nt") # Required on Windows to find az.cmd
)
outputs = json.loads(result.stdout)
# Build a case-insensitive lookup map for output keys.
# ARM/Bicep preserve output names as authored in the template; this map lets
# us perform lookups without worrying about casing in our code.
outputs_lower = {k.lower(): v for k, v in outputs.items()}
# Extract values from deployment outputs
# The outputs are in format: {"keyName": {"type": "String", "value": "actual-value"}}
def extract_value(key: str, fallback_key: str = "") -> str:
"""Extract value from deployment outputs, trying fallback key if primary not found."""
key_lower = key.lower()
if key_lower in outputs_lower and "value" in outputs_lower[key_lower]:
return outputs_lower[key_lower]["value"]
if fallback_key:
fb_lower = fallback_key.lower()
if fb_lower in outputs_lower and "value" in outputs_lower[fb_lower]:
return outputs_lower[fb_lower]["value"]
return ""
values = {
"app_service": extract_value("appServiceName", "APP_SERVICE_NAME"),
"storage_account": extract_value("azureBlobAccountName", "AZURE_BLOB_ACCOUNT_NAME"),
"cosmos_account": extract_value("cosmosDbAccountName", "COSMOSDB_ACCOUNT_NAME"),
"search_service": extract_value("aiSearchServiceName", "AI_SEARCH_SERVICE_NAME"),
}
# Filter out empty values
values = {k: v for k, v in values.items() if v}
if values:
print_success(f"Retrieved {len(values)} values from deployment outputs")
for k, v in values.items():
print(f" {k}: {v}")
else:
print_warning("No matching values found in deployment outputs")
return values
except subprocess.CalledProcessError as e:
print_warning(f"Failed to query Azure deployment: {e.stderr if e.stderr else str(e)}")
return None
except json.JSONDecodeError as e:
print_warning(f"Failed to parse deployment outputs: {e}")
return None
except Exception as e:
print_warning(f"Error getting deployment values: {e}")
return None
def discover_resources(resource_group: str, app_name: str, storage_account: str, cosmos_account: str, search_service: str, api_key: str = "") -> ResourceConfig:
"""Build resource configuration from provided values (no Azure CLI required)."""
print_step("Configuring Azure resources...")
# Build App URL from app name
app_url = f"https://{app_name}.azurewebsites.net"
config = ResourceConfig(
resource_group=resource_group,
app_service=app_name,
app_url=app_url,
api_key=api_key,
storage_account=storage_account,
cosmos_account=cosmos_account,
search_service=search_service
)
print(f" App Service: {config.app_service}")
print(f" App URL: {config.app_url}")
print(f" Storage Account: {config.storage_account}")
print(f" Cosmos DB: {config.cosmos_account}")
print(f" AI Search: {config.search_service}")
print(f" API Key: {'***' if config.api_key else '(not set - development mode)'}")
return config
def get_api_headers(config: ResourceConfig) -> Dict[str, str]:
"""Get headers for admin API requests."""
headers = {"Content-Type": "application/json"}
if config.api_key:
headers["X-Admin-API-Key"] = config.api_key
return headers
async def check_admin_api_health(config: ResourceConfig, max_retries: int = 8, retry_delay: int = 20) -> bool:
"""Check if the admin API is available with retry logic for cold starts and container restarts."""
print_step("Checking admin API health...")
async with httpx.AsyncClient(timeout=30.0) as client:
for attempt in range(1, max_retries + 1):
try:
response = await client.get(
f"{config.app_url}/api/admin/health",
headers=get_api_headers(config)
)
if response.status_code == 200:
# Validate the response is actually from the backend admin API,
# not the frontend catch-all serving index.html
try:
data = response.json()
if data.get("status") == "healthy":
print_success("Admin API is healthy")
return True
else:
print_warning(
f"Attempt {attempt}/{max_retries}: Admin API returned unexpected JSON: {data}"
)
except (ValueError, json.JSONDecodeError):
# Response is not JSON.
# This means the API proxy is not forwarding requests to the backend.
content_preview = response.text[:200]
is_html = "<html" in content_preview.lower() or "<!doctype" in content_preview.lower()
if is_html:
print_warning(
f"Attempt {attempt}/{max_retries}: Got HTML instead of JSON from /api/admin/health. "
"The frontend proxy may not be forwarding requests to the backend. "
"Check that BACKEND_URL is set in the App Service and that the backend container is running."
)
else:
print_warning(
f"Attempt {attempt}/{max_retries}: Admin API returned non-JSON response: {content_preview}"
)
else:
print_warning(f"Attempt {attempt}/{max_retries}: Admin API returned {response.status_code}")
except Exception as e:
print_warning(f"Attempt {attempt}/{max_retries}: Failed to reach admin API: {e}")
if attempt < max_retries:
print(f" Retrying in {retry_delay} seconds...")
await asyncio.sleep(retry_delay)
print_error(f"Admin API not available after {max_retries} attempts")
return False
async def upload_images(config: ResourceConfig, dry_run: bool = False) -> int:
"""Upload product images via admin API."""
print_header("Uploading Product Images via API")
images_folder = Path(__file__).parent / "images"
if not images_folder.exists():
print_error(f"Images folder not found: {images_folder}")
return 0
image_files = (
list(images_folder.glob("*.jpg")) +
list(images_folder.glob("*.JPG")) +
list(images_folder.glob("*.png")) +
list(images_folder.glob("*.PNG"))
)
if not image_files:
print_warning("No image files found")
return 0
print(f"Found {len(image_files)} image files")
if dry_run:
print_warning("DRY RUN: Would upload images via API")
for img in sorted(image_files):
print(f" - {img.name}")
return len(image_files)
# Upload images one at a time
uploaded_count = 0
async with httpx.AsyncClient(timeout=120.0) as client: # 2 minute timeout per image
for image_path in sorted(image_files):
content_type = "image/png" if image_path.suffix.lower() == ".png" else "image/jpeg"
with open(image_path, "rb") as f:
image_bytes = f.read()
image_data = {
"filename": image_path.name,
"content_type": content_type,
"data": base64.b64encode(image_bytes).decode("utf-8")
}
try:
response = await client.post(
f"{config.app_url}/api/admin/upload-images",
headers=get_api_headers(config),
json={"images": [image_data]}
)
if response.status_code == 401:
print_error("Unauthorized - check your ADMIN_API_KEY")
return uploaded_count
if response.status_code == 200:
uploaded_count += 1
else:
print_error(f"{image_path.name}: API returned {response.status_code}")
except Exception as e:
print_error(f"{image_path.name}: {e}")
print(f"\nUploaded {uploaded_count}/{len(image_files)} images")
return uploaded_count
async def load_sample_data(config: ResourceConfig, dry_run: bool = False) -> int:
"""Load sample product data via admin API."""
print_header("Loading Sample Product Data via API")
# Sample products (Contoso Paints) - image URLs use proxy path
sample_products = [
{"product_name": "Snow Veil", "description": "A crisp white with a hint of warmth — perfect for open, modern interiors.", "tags": "soft white, airy, minimal, fresh", "price": 59.95, "sku": "CP-0001", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/SnowVeil.png", "category": "Paint"},
{"product_name": "Porcelain Mist", "description": "A gentle off-white that softens spaces with a cozy, inviting glow.", "tags": "warm neutral, beige, cozy, calm", "price": 59.95, "sku": "CP-0002", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/PorcelainMist.png", "category": "Paint"},
{"product_name": "Stone Dusk", "description": "A balanced mix of gray and beige, ideal for grounding a room without heaviness.", "tags": "greige, muted, balanced, modern", "price": 59.95, "sku": "CP-0003", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/StoneDusk.png", "category": "Paint"},
{"product_name": "Fog Harbor", "description": "A moody gray with blue undertones that feels sleek and contemporary.", "tags": "cool gray, stormy, industrial, sleek", "price": 59.95, "sku": "CP-0004", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/FogHarbor.png", "category": "Paint"},
{"product_name": "Graphite Fade", "description": "A dark graphite shade that adds weight and sophistication to feature walls.", "tags": "charcoal, deep gray, moody, bold", "price": 59.95, "sku": "CP-0005", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/GraphiteFade.png", "category": "Paint"},
{"product_name": "Obsidian Pearl", "description": "A rich black that creates contrast and drama while staying refined.", "tags": "black, matte, dramatic, luxe", "price": 59.95, "sku": "CP-0006", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/ObsidianPearl.png", "category": "Paint"},
{"product_name": "Steel Sky", "description": "A mid-tone slate blue that feels steady, grounded, and architectural.", "tags": "slate, bluish gray, urban, cool", "price": 59.95, "sku": "CP-0007", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/SteelSky.png", "category": "Paint"},
{"product_name": "Blue Ash", "description": "A softened navy with gray undertones — stylish but not overpowering.", "tags": "midnight, muted navy, grounding, refined", "price": 59.95, "sku": "CP-0008", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/BlueAsh.png", "category": "Paint"},
{"product_name": "Cloud Drift", "description": "An ethereal off-white with subtle gray undertones that evokes soft, drifting clouds.", "tags": "cloud white, soft gray, peaceful, ethereal, airy", "price": 59.95, "sku": "CP-0009", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/CloudDrift.png", "category": "Paint"},
{"product_name": "Silver Shore", "description": "A frosty gray with subtle silver hints — sharp, bright, and clean.", "tags": "cool gray, icy, clean, modern", "price": 59.95, "sku": "CP-0010", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/SilverShore.png", "category": "Paint"},
{"product_name": "Seafoam Light", "description": "A soft seafoam tone that feels breezy and coastal without being too bold.", "tags": "pale green, misty, fresh, coastal", "price": 59.95, "sku": "CP-0011", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/SeafoamLight.png", "category": "Paint"},
{"product_name": "Quiet Moss", "description": "A soft moss green with sage undertones that adds organic calm to any interior palette.", "tags": "moss green, sage, organic, muted, calming", "price": 59.95, "sku": "CP-0012", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/QuietMoss.png", "category": "Paint"},
{"product_name": "Olive Stone", "description": "A grounded olive shade that pairs well with natural textures like wood and linen.", "tags": "earthy, muted green, natural, rustic", "price": 59.95, "sku": "CP-0013", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/OliveStone.png", "category": "Paint"},
{"product_name": "Verdant Haze", "description": "A muted teal that blends serenity with just enough depth for modern accents.", "tags": "soft teal, subdued, calming, serene", "price": 59.95, "sku": "CP-0014", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/VerdantHaze.png", "category": "Paint"},
{"product_name": "Glacier Tint", "description": "A barely-there aqua that brings a refreshing, clean lift to light spaces.", "tags": "pale aqua, refreshing, crisp, airy", "price": 59.95, "sku": "CP-0015", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/GlacierTint.png", "category": "Paint"},
{"product_name": "Pine Shadow", "description": "A deep forest green with pine undertones that anchors a room with natural richness.", "tags": "dark green, forest, pine, earthy, grounding, natural", "price": 59.95, "sku": "CP-0016", "image_url": f"https://{config.storage_account}.blob.core.windows.net/product-images/PineShadow.png", "category": "Paint"},
]
print(f"Sample products: {len(sample_products)} Contoso Paints items")
if dry_run:
print_warning("DRY RUN: Would load products via API")
for p in sample_products:
print(f" - {p['product_name']} ({p['sku']})")
return len(sample_products)
# Call admin API
print_step("Calling /api/admin/load-sample-data...")
async with httpx.AsyncClient(timeout=120.0) as client:
try:
response = await client.post(
f"{config.app_url}/api/admin/load-sample-data",
headers=get_api_headers(config),
json={
"products": sample_products,
"clear_existing": True
}
)
if response.status_code == 401:
print_error("Unauthorized - check your ADMIN_API_KEY")
return 0
if response.status_code != 200:
print_error(f"API returned {response.status_code}: {response.text[:500]}")
return 0
result = response.json()
loaded = result.get("loaded", 0)
failed = result.get("failed", 0)
deleted = result.get("deleted", 0)
if deleted > 0:
print(f" Deleted {deleted} existing products")
for r in result.get("results", []):
if r.get("status") == "loaded":
print_success(f"{r['product_name']} ({r['sku']})")
else:
print_error(f"{r['product_name']}: {r.get('error', 'Unknown error')}")
print(f"\nLoaded {loaded}/{len(sample_products)} products ({failed} failed)")
return loaded
except Exception as e:
print_error(f"API call failed: {e}")
return 0
async def create_search_index(config: ResourceConfig, dry_run: bool = False) -> int:
"""Create and populate the search index via admin API."""
print_header("Creating Search Index via API")
if dry_run:
print_warning("DRY RUN: Would create search index via API")
return 0
# Call admin API
print_step("Calling /api/admin/create-search-index...")
async with httpx.AsyncClient(timeout=180.0) as client: # 3 minute timeout
try:
response = await client.post(
f"{config.app_url}/api/admin/create-search-index",
headers=get_api_headers(config),
json={"reindex_all": True}
)
if response.status_code == 401:
print_error("Unauthorized - check your ADMIN_API_KEY")
return 0
if response.status_code != 200:
print_error(f"API returned {response.status_code}: {response.text[:500]}")
return 0
result = response.json()
indexed = result.get("indexed", 0)
failed = result.get("failed", 0)
index_name = result.get("index_name", "products")
print(f" Index name: {index_name}")
for r in result.get("results", []):
if r.get("status") == "indexed":
print_success(f"{r['product_name']} ({r['sku']})")
else:
print_error(f"{r['product_name']}: {r.get('error', 'Unknown error')}")
print(f"\nIndexed {indexed} products ({failed} failed)")
return indexed
except Exception as e:
print_error(f"API call failed: {e}")
return 0
async def run_application_tests(config: ResourceConfig, dry_run: bool = False) -> Dict[str, bool]:
"""Run application health tests."""
print_header("Running Application Tests")
if dry_run:
print_warning("DRY RUN: Would run application tests")
return {}
app_url = config.app_url
print(f"App URL: {app_url}")
print()
results = {}
async with httpx.AsyncClient(timeout=30.0) as client:
# Test 1: Frontend
print_step("Testing Frontend (GET /)")
try:
response = await client.get(f"{app_url}/")
if response.status_code == 200 and "<!DOCTYPE html>" in response.text:
print_success("Frontend serving HTML")
results["frontend"] = True
else:
print_error(f"Unexpected response: {response.status_code}")
results["frontend"] = False
except Exception as e:
print_error(f"Failed: {e}")
results["frontend"] = False
# Test 2: Health endpoint
print_step("Testing Health (GET /api/health)")
try:
response = await client.get(f"{app_url}/api/health")
if response.status_code == 200:
print_success(f"Health OK: {response.json()}")
results["health"] = True
else:
print_warning(f"Health returned {response.status_code}")
results["health"] = False
except Exception as e:
print_warning(f"Health check failed: {e}")
results["health"] = False
# Test 3: Chat API (POST /api/chat)
print_step("Testing Chat API (POST /api/chat)")
try:
response = await client.post(
f"{app_url}/api/chat",
json={"message": "Hello", "conversation_id": "test-post-deploy"},
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
print_success("Chat API responding")
results["chat_api"] = True
else:
print_error(f"Failed: {response.status_code}")
results["chat_api"] = False
except Exception as e:
print_error(f"Failed: {e}")
results["chat_api"] = False
# Test 4: Product Search (GET /api/products)
print_step("Testing Product Search (GET /api/products?search=blue)")
try:
response = await client.get(f"{app_url}/api/products?search=blue&limit=3")
if response.status_code == 200:
data = response.json()
count = data.get("count", 0)
products = data.get("products", [])
if count > 0:
print_success(f"Found {count} products: {[p['product_name'] for p in products]}")
results["product_search"] = True
else:
print_warning("No products found (search index may need time)")
results["product_search"] = False
else:
print_error(f"Failed: {response.status_code}")
results["product_search"] = False
except Exception as e:
print_error(f"Failed: {e}")
results["product_search"] = False
# Test 5: Product List (GET /api/products)
print_step("Testing Product List (GET /api/products)")
try:
response = await client.get(f"{app_url}/api/products?limit=5")
if response.status_code == 200:
data = response.json()
count = data.get("count", 0)
print_success(f"Listed {count} products")
results["product_list"] = True
else:
print_error(f"Failed: {response.status_code}")
results["product_list"] = False
except Exception as e:
print_error(f"Failed: {e}")
results["product_list"] = False
# Summary
print()
passed = sum(1 for v in results.values() if v)
total = len(results)
if passed == total:
print_success(f"All {total} tests passed!")
else:
print_warning(f"{passed}/{total} tests passed")
return results
def print_summary(
images_uploaded: int,
products_loaded: int,
products_indexed: int,
test_results: Dict[str, bool]
):
"""Print final summary."""
print_header("Post-Deployment Summary")
print(f" Images Uploaded: {images_uploaded}")
print(f" Products Loaded: {products_loaded}")
print(f" Products Indexed: {products_indexed}")
if test_results:
print()
print(" Application Tests:")
for test, passed in test_results.items():
status = f"{Colors.GREEN}PASS{Colors.END}" if passed else f"{Colors.RED}FAIL{Colors.END}"
print(f" {test}: {status}")
print()
async def main():
parser = argparse.ArgumentParser(
description="Post-deployment script for Content Generation Solution Accelerator",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("-g", "--resource-group", help="Azure resource group name (reads from RESOURCE_GROUP_NAME if not provided)")
parser.add_argument("--app-name", help="App Service name (reads from APP_SERVICE_NAME if not provided)")
parser.add_argument("--storage-account", help="Storage account name (reads from AZURE_BLOB_ACCOUNT_NAME if not provided)")
parser.add_argument("--cosmos-account", help="Cosmos DB account name (reads from COSMOSDB_ACCOUNT_NAME if not provided)")
parser.add_argument("--search-service", help="AI Search service name (reads from AI_SEARCH_SERVICE_NAME if not provided)")
parser.add_argument("--api-key", help="Admin API key (or set ADMIN_API_KEY env var)")
parser.add_argument("--skip-images", action="store_true", help="Skip uploading images")
parser.add_argument("--skip-data", action="store_true", help="Skip loading sample data")
parser.add_argument("--skip-index", action="store_true", help="Skip creating search index")
parser.add_argument("--skip-tests", action="store_true", help="Skip application tests")
parser.add_argument("--dry-run", action="store_true", help="Show what would be done")
args = parser.parse_args()
# Priority 1: Command line arguments
resource_group = args.resource_group or os.environ.get("RESOURCE_GROUP_NAME", "")
app_name = args.app_name or os.environ.get("APP_SERVICE_NAME", "")
storage_account = args.storage_account or os.environ.get("AZURE_BLOB_ACCOUNT_NAME", "")
cosmos_account = args.cosmos_account or os.environ.get("COSMOSDB_ACCOUNT_NAME", "")
search_service = args.search_service or os.environ.get("AI_SEARCH_SERVICE_NAME", "")
api_key = args.api_key or os.environ.get("ADMIN_API_KEY", "")
# Priority 2: If resource group is provided but other values are missing, try deployment outputs
if resource_group and (not app_name or not storage_account or not cosmos_account or not search_service):
print()
deployment_values = get_values_from_az_deployment(resource_group)
if deployment_values:
# Use deployment values only for missing parameters
app_name = app_name or deployment_values.get("app_service", "")
storage_account = storage_account or deployment_values.get("storage_account", "")
cosmos_account = cosmos_account or deployment_values.get("cosmos_account", "")
search_service = search_service or deployment_values.get("search_service", "")
else:
print_warning("Could not retrieve values from deployment outputs")
# Validate required values are present
if not resource_group:
print_error("Resource group is required. Provide via --resource-group argument or RESOURCE_GROUP_NAME environment variable.")
sys.exit(1)
if not app_name or not storage_account or not cosmos_account or not search_service:
print_error("Missing required resource names. Please provide via:")
print(" 1. Command line arguments (--app-name, --storage-account, --cosmos-account, --search-service)")
print(" 2. Environment variables (APP_SERVICE_NAME, AZURE_BLOB_ACCOUNT_NAME, COSMOSDB_ACCOUNT_NAME, AI_SEARCH_SERVICE_NAME)")
print(" 3. Azure deployment outputs (automatic if resource group has DeploymentName tag)")
print()
print("Missing values:")
if not app_name:
print(" - App Service name")
if not storage_account:
print(" - Storage Account name")
if not cosmos_account:
print(" - Cosmos DB Account name")
if not search_service:
print(" - AI Search Service name")
sys.exit(1)
print_header("Content Generation Solution Accelerator - Post Deployment")
print(f"Resource Group: {resource_group}")
print(f"Dry Run: {args.dry_run}")
print()
# Configure resources
config = discover_resources(resource_group, app_name, storage_account, cosmos_account, search_service, api_key)
# Check admin API health first
if not args.dry_run:
if not await check_admin_api_health(config):
print_error("Admin API not available. Make sure the app is deployed and running.")
print("You can check the app at: " + config.app_url)
sys.exit(1)
images_uploaded = 0
products_loaded = 0
products_indexed = 0
test_results = {}
try:
# Upload images via API
if not args.skip_images:
images_uploaded = await upload_images(config, dry_run=args.dry_run)
# Load sample data via API
if not args.skip_data:
products_loaded = await load_sample_data(config, dry_run=args.dry_run)
# Create search index via API
if not args.skip_index:
products_indexed = await create_search_index(config, dry_run=args.dry_run)
# Run application tests
if not args.skip_tests:
test_results = await run_application_tests(config, dry_run=args.dry_run)
except Exception as e:
print_error(f"Error during post-deployment: {e}")
raise
# Print summary
print_summary(images_uploaded, products_loaded, products_indexed, test_results)
if __name__ == "__main__":
asyncio.run(main())