diff --git a/azure.yaml b/azure.yaml index 0a74a2d6..65c29aff 100644 --- a/azure.yaml +++ b/azure.yaml @@ -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 diff --git a/infra/main.bicep b/infra/main.bicep index d8b19355..e55cd5ec 100644 --- a/infra/main.bicep +++ b/infra/main.bicep @@ -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 : '') diff --git a/pyproject.toml b/pyproject.toml index 00ca09ac..02bff5ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,8 @@ filterwarnings = ["ignore::DeprecationWarning"] [[tool.mypy.overrides]] module = [ "pgvector.*", - "evaltools.*" + "evaltools.*", + "dotenv_azd.*", ] ignore_missing_imports = true diff --git a/requirements-dev.txt b/requirements-dev.txt index 1169d8a2..cc2180eb 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -14,3 +14,5 @@ pytest-snapshot locust git+https://github.com/Azure-Samples/ai-rag-chat-evaluator/@installable psycopg2 +azure-mgmt-cognitiveservices +dotenv-azd diff --git a/scripts/pre_down.ps1 b/scripts/pre_down.ps1 new file mode 100644 index 00000000..63a0604f --- /dev/null +++ b/scripts/pre_down.ps1 @@ -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" diff --git a/scripts/pre_down.py b/scripts/pre_down.py new file mode 100644 index 00000000..7080e539 --- /dev/null +++ b/scripts/pre_down.py @@ -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) diff --git a/scripts/pre_down.sh b/scripts/pre_down.sh new file mode 100755 index 00000000..8679e863 --- /dev/null +++ b/scripts/pre_down.sh @@ -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