|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to delete an Azure ML capability host |
| 4 | + |
| 5 | +# Prompt for required information |
| 6 | +read -p "Enter Subscription ID: " subscription_id |
| 7 | +read -p "Enter Resource Group name: " resource_group |
| 8 | +read -p "Enter Hub or Project name: " workspace_name |
| 9 | +read -p "Enter CapabilityHost name: " caphost_name |
| 10 | + |
| 11 | +# Get Azure access token |
| 12 | +echo "Getting Azure access token..." |
| 13 | +access_token=$(az account get-access-token --query accessToken -o tsv) |
| 14 | + |
| 15 | +if [ -z "$access_token" ]; then |
| 16 | + echo "Error: Failed to get access token. Please make sure you're logged in with 'az login'" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +# Construct the API URL |
| 21 | +api_url="https://management.azure.com/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.MachineLearningServices/workspaces/${workspace_name}/capabilityHosts/${caphost_name}?api-version=2024-10-01-preview" |
| 22 | + |
| 23 | +echo "Deleting capability host: ${caphost_name}" |
| 24 | +echo "API URL: ${api_url}" |
| 25 | + |
| 26 | +# Send DELETE request and capture headers |
| 27 | +echo "Sending DELETE request..." |
| 28 | +response_headers=$(mktemp) |
| 29 | +curl -X DELETE \ |
| 30 | + -H "Authorization: Bearer ${access_token}" \ |
| 31 | + -H "Content-Type: application/json" \ |
| 32 | + -D "${response_headers}" \ |
| 33 | + -s \ |
| 34 | + "${api_url}" |
| 35 | + |
| 36 | +# Check if the curl command was successful |
| 37 | +if [ $? -ne 0 ]; then |
| 38 | + echo -e "\nError: Failed to send deletion request." |
| 39 | + rm -f "${response_headers}" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Extract the Azure-AsyncOperation URL from the response headers |
| 44 | +operation_url=$(grep -i "Azure-AsyncOperation" "${response_headers}" | cut -d' ' -f2 | tr -d '\r') |
| 45 | + |
| 46 | +if [ -z "${operation_url}" ]; then |
| 47 | + echo -e "\nError: Could not find operation URL in the response." |
| 48 | + cat "${response_headers}" |
| 49 | + rm -f "${response_headers}" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +rm -f "${response_headers}" |
| 54 | + |
| 55 | +echo -e "\nCapability host deletion request initiated." |
| 56 | +echo "Monitoring operation: ${operation_url}" |
| 57 | + |
| 58 | +# Poll the operation URL until the operation completes |
| 59 | +status="InProgress" |
| 60 | +while [ "${status}" = "InProgress" ]; do |
| 61 | + echo "Checking operation status..." |
| 62 | + |
| 63 | + # Get the operation status |
| 64 | + operation_response=$(curl -s \ |
| 65 | + -H "Authorization: Bearer ${access_token}" \ |
| 66 | + -H "Content-Type: application/json" \ |
| 67 | + "${operation_url}") |
| 68 | + |
| 69 | + # Extract the status from the response |
| 70 | + status=$(echo "${operation_response}" | grep -o '"status":"[^"]*"' | cut -d'"' -f4) |
| 71 | + |
| 72 | + if [ -z "${status}" ]; then |
| 73 | + echo "Error: Could not determine operation status." |
| 74 | + echo "Response: ${operation_response}" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + echo "Current status: ${status}" |
| 79 | + |
| 80 | + if [ "${status}" = "InProgress" ]; then |
| 81 | + echo "Operation still in progress. Waiting 10 seconds before checking again..." |
| 82 | + sleep 10 |
| 83 | + fi |
| 84 | +done |
| 85 | + |
| 86 | +# Check the final status |
| 87 | +if [ "${status}" = "Succeeded" ]; then |
| 88 | + echo -e "\nCapability host deletion completed successfully." |
| 89 | +else |
| 90 | + echo -e "\nCapability host deletion failed with status: ${status}" |
| 91 | + echo "Response: ${operation_response}" |
| 92 | + exit 1 |
| 93 | +fi |
0 commit comments