From 22b374364c6cf5a3ba1321a3326de149f006560c Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 18 Oct 2024 10:35:39 +0000 Subject: [PATCH 1/3] Add pre_down script --- azure.yaml | 9 ++++++++ infra/main.bicep | 2 ++ requirements-dev.txt | 2 ++ scripts/pre_down.ps1 | 5 ++++ scripts/pre_down.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ scripts/pre_down.sh | 12 ++++++++++ 6 files changed, 84 insertions(+) create mode 100644 scripts/pre_down.ps1 create mode 100644 scripts/pre_down.py create mode 100755 scripts/pre_down.sh 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/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..427e3ee6 --- /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): + """ + 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..7eb5deb6 --- /dev/null +++ b/scripts/pre_down.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status +set -e + +. ./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 From 051e790d3a7a8a2f75872e130232ee78d22b6ba1 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 18 Oct 2024 10:36:25 +0000 Subject: [PATCH 2/3] Remove set e --- scripts/pre_down.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/pre_down.sh b/scripts/pre_down.sh index 7eb5deb6..8679e863 100755 --- a/scripts/pre_down.sh +++ b/scripts/pre_down.sh @@ -1,8 +1,5 @@ #!/bin/bash -# Exit immediately if a command exits with a non-zero status -set -e - . ./scripts/load_python_env.sh # Get the directory of the current script From a24248a86c6bfc6549b0f97233e8c0238e91d657 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 18 Oct 2024 10:56:30 +0000 Subject: [PATCH 3/3] Update mypy config --- pyproject.toml | 3 ++- scripts/pre_down.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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/scripts/pre_down.py b/scripts/pre_down.py index 427e3ee6..7080e539 100644 --- a/scripts/pre_down.py +++ b/scripts/pre_down.py @@ -8,7 +8,7 @@ logger = logging.getLogger("ragapp") -def delete_deployments(resource_name: str, resource_group: str, subscription_id: str, tenant_id: str = None): +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 """