Skip to content

Add pre_down scripts to delete GlobalStandard deployments #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ hooks:
run: ./scripts/setup_postgres_database.sh;./scripts/setup_postgres_azurerole.sh;./scripts/setup_postgres_seeddata.sh
interactive: true
continueOnError: false
predown:
windows:
shell: pwsh
run: ./scripts/pre_down.ps1
continueOnError: true
posix:
shell: sh
run: ./scripts/pre_down.sh
continueOnError: true
pipeline:
variables:
- DEPLOY_AZURE_OPENAI
Expand Down
2 changes: 2 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ output SERVICE_WEB_IMAGE_NAME string = web.outputs.SERVICE_WEB_IMAGE_NAME

output OPENAI_CHAT_HOST string = openAIChatHost
output OPENAI_EMBED_HOST string = openAIEmbedHost
output AZURE_OPENAI_SERVICE string = deployAzureOpenAI ? openAI.outputs.name : ''
output AZURE_OPENAI_RESOURCE_GROUP string = deployAzureOpenAI ? openAIResourceGroup.name : ''
output AZURE_OPENAI_ENDPOINT string = !empty(azureOpenAIEndpoint)
? azureOpenAIEndpoint
: (deployAzureOpenAI ? openAI.outputs.endpoint : '')
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ filterwarnings = ["ignore::DeprecationWarning"]
[[tool.mypy.overrides]]
module = [
"pgvector.*",
"evaltools.*"
"evaltools.*",
"dotenv_azd.*",
]
ignore_missing_imports = true

Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ pytest-snapshot
locust
git+https://github.com/Azure-Samples/ai-rag-chat-evaluator/@installable
psycopg2
azure-mgmt-cognitiveservices
dotenv-azd
5 changes: 5 additions & 0 deletions scripts/pre_down.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Get the directory of the current script
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition

# Run the Python script
python "$scriptDir/pre-down.py"
54 changes: 54 additions & 0 deletions scripts/pre_down.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging
import os

from azure.identity import AzureDeveloperCliCredential
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient
from dotenv_azd import load_azd_env

logger = logging.getLogger("ragapp")


def delete_deployments(resource_name: str, resource_group: str, subscription_id: str, tenant_id: str | None = None):
"""
Delete all deployments for an Azure OpenAI resource
"""
if tenant_id:
logger.info("Authenticating to Azure using Azure Developer CLI Credential for tenant %s", tenant_id)
azure_credential = AzureDeveloperCliCredential(tenant_id=tenant_id, process_timeout=60)
else:
logger.info("Authenticating to Azure using Azure Developer CLI Credential")
azure_credential = AzureDeveloperCliCredential(process_timeout=60)

# Initialize the Cognitive Services client
client = CognitiveServicesManagementClient(azure_credential, subscription_id=subscription_id)

# List all deployments
deployments = client.deployments.list(resource_group_name=resource_group, account_name=resource_name)

# Delete each deployment and wait for the operation to complete
for deployment in deployments:
deployment_name = deployment.name
if not deployment_name:
continue
poller = client.deployments.begin_delete(
resource_group_name=resource_group, account_name=resource_name, deployment_name=deployment_name
)
poller.result()
logger.info(f"Deployment {deployment_name} deleted successfully.")


if __name__ == "__main__":
logging.basicConfig(level=logging.WARNING)
logger.setLevel(logging.INFO)
load_azd_env()

try:
resource_name = os.environ["AZURE_OPENAI_SERVICE"]
resource_group = os.environ["AZURE_OPENAI_RESOURCE_GROUP"]
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
tenant_id = os.environ["AZURE_TENANT_ID"]
except KeyError as e:
logger.error("Missing azd environment variable %s", e)
exit(1)

delete_deployments(resource_name, resource_group, subscription_id, tenant_id)
9 changes: 9 additions & 0 deletions scripts/pre_down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

. ./scripts/load_python_env.sh

# Get the directory of the current script
script_dir=$(dirname "$0")

# Run the Python script with the retrieved values
.venv/bin/python "$script_dir/pre_down.py" --subscription-id $subscription_id --resource-name $resource_name --resource-group $resource_group --tenant-id $tenant_id
Loading