Skip to content

Commit 48742e4

Browse files
authored
add pre-down hooks to unblock azd down
1 parent 8d1fff0 commit 48742e4

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"forwardPorts": [
2424
50505
2525
],
26-
"postCreateCommand": "",
26+
"postCreateCommand": "python3 -m pip install -r requirements-dev.txt",
2727
"remoteUser": "vscode",
2828
"hostRequirements": {
2929
"memory": "8gb"

azure.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,12 @@ hooks:
132132
run: ./scripts/auth_update.sh;./scripts/prepdocs.sh
133133
interactive: true
134134
continueOnError: false
135+
predown:
136+
windows:
137+
shell: pwsh
138+
run: ./scripts/pre-down.ps1
139+
continueOnError: true
140+
posix:
141+
shell: sh
142+
run: ./scripts/pre-down.sh
143+
continueOnError: true

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-r app/backend/requirements.txt
2+
azure-mgmt-cognitiveservices
23
ruff
34
black
45
pytest

scripts/pre-down.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Get the directory of the current script
2+
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
3+
4+
# Load environment variables from azd env
5+
$subscriptionId = azd env get-value AZURE_SUBSCRIPTION_ID
6+
$resourceName = azd env get-value AZURE_OPENAI_SERVICE
7+
$resourceGroup = azd env get-value AZURE_OPENAI_RESOURCE_GROUP
8+
9+
# Run the Python script with the retrieved values
10+
python "$scriptDir/pre-down.py" --subscription-id $subscriptionId --resource-name $resourceName --resource-group $resourceGroup

scripts/pre-down.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import argparse
2+
3+
from azure.identity import DefaultAzureCredential
4+
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient
5+
6+
# Set up argument parsing
7+
parser = argparse.ArgumentParser(description="Delete an Azure OpenAI deployment.")
8+
parser.add_argument("--resource-name", required=True, help="The name of the Azure OpenAI resource.")
9+
parser.add_argument("--resource-group", required=True, help="The name of the Azure resource group.")
10+
parser.add_argument("--subscription-id", required=True, help="The Azure subscription ID.")
11+
12+
args = parser.parse_args()
13+
14+
# Authenticate using DefaultAzureCredential
15+
credential = DefaultAzureCredential()
16+
17+
# Initialize the Cognitive Services client
18+
client = CognitiveServicesManagementClient(credential, subscription_id=args.subscription_id)
19+
20+
# List all deployments
21+
deployments = client.deployments.list(
22+
resource_group_name=args.resource_group, account_name=args.resource_name
23+
)
24+
25+
# Delete each deployment and wait for the operation to complete
26+
for deployment in deployments:
27+
deployment_name = deployment.name
28+
poller = client.deployments.begin_delete(
29+
resource_group_name=args.resource_group, account_name=args.resource_name, deployment_name=deployment_name
30+
)
31+
poller.result()
32+
print(f"Deployment {deployment_name} deleted successfully.")

scripts/pre-down.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Get the directory of the current script
4+
script_dir=$(dirname "$0")
5+
6+
# Load environment variables from azd env
7+
subscription_id=$(azd env get-value AZURE_SUBSCRIPTION_ID)
8+
resource_name=$(azd env get-value AZURE_OPENAI_SERVICE)
9+
resource_group=$(azd env get-value AZURE_OPENAI_RESOURCE_GROUP)
10+
11+
# Run the Python script with the retrieved values
12+
python "$script_dir/pre-down.py" --subscription-id $subscription_id --resource-name $resource_name --resource-group $resource_group

0 commit comments

Comments
 (0)