Skip to content

Commit

Permalink
Revert Azure deletion to delete the whole resource group (#1584)
Browse files Browse the repository at this point in the history
* Removing column

* Moving disks deletion after VM deletion

* Fixing lint

* Add check for disk_ids

* Refactor and delete the whole resource group
  • Loading branch information
jeniawhite authored Nov 26, 2023
1 parent 3df384b commit b16472d
Showing 1 changed file with 9 additions and 63 deletions.
72 changes: 9 additions & 63 deletions deploy/test-environments/delete_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,72 +195,20 @@ printf "%s\n" "${DELETED_DEPLOYMENTS[@]}"
echo "Failed to delete GCP deployments (${#FAILED_DEPLOYMENTS[@]}):"
printf "%s\n" "${FAILED_DEPLOYMENTS[@]}"

# Delete Azure deployments
FAILED_AZURE_DEPLOYMENTS=()
FAILED_AZURE_RESOURCES=()
# Delete Azure groups
FAILED_AZURE_GROUPS=()
deployment_names=('cloudbeat-vm-deployment' 'elastic-agent-deployment')
groups=$(az group list --query "[?starts_with(name, '$ENV_PREFIX')].name" -o tsv | awk -v prefix="$IGNORE_PREFIX" '{if (prefix == "") print $0; else if (!($0 ~ "^" prefix)) print $0}')
for group in $groups; do
for dep_name in "${deployment_names[@]}"; do
resource_ids=$(az deployment group show --name "$dep_name" --resource-group "$group" --query "properties.outputResources[].id" --output tsv)
if [ -z "$resource_ids" ]; then
echo "No resources found for deployment $dep_name."
else
echo "Deleting resources deployed by $dep_name:"
for resource_id in $resource_ids; do
# Skip deleting the MSI extension resource because it will be deleted with the VM
# Removing this condition will cause failure when VM is deleted prior to the extension
if [[ $resource_id == *"extensions/EnableMSIExtension"* ]]; then
continue
elif [[ $resource_id == *"Microsoft.Compute/virtualMachines"* ]]; then
disk_ids=$(az disk list --resource-group "$group" --query "[?managedBy=='$resource_id')].id" --output tsv)
for disk_id in $disk_ids; do
az resource delete --ids "$disk_id" || {
echo "Failed to delete resource: $disk_id"
FAILED_AZURE_RESOURCES+=("$disk_id")
continue
}
done
fi
az resource delete --ids "$resource_id" || {
echo "Failed to delete resource: $resource_id"
FAILED_AZURE_RESOURCES+=("$resource_id")
continue
}
done
az deployment group delete --name "$dep_name" --resource-group "$group" || {
echo "Failed to delete deployment: $dep_name"
FAILED_AZURE_DEPLOYMENTS+=("$dep_name")
continue
}
echo "Resources deployed by $dep_name have been deleted."
fi
done

if ! res_group=$(az resource list --resource-group "$group" --query '[].id' -o tsv); then
echo "Failed to check resource group $group for pre-existing resources."
az group delete --name "$group" --yes || {
echo "Failed to delete resource group: $group"
FAILED_AZURE_GROUPS+=("$group")
elif [[ $res_group ]]; then
echo "Pre-existing resources found in resource group $group skipping delete."
else
az group delete --name "$group" --yes || {
echo "Failed to delete resource group: $group"
FAILED_AZURE_GROUPS+=("$group")
continue
}
echo "Resource group $group has been deleted."
fi
continue
}
echo "Resource group $group has been deleted."
done

# Summary of azure deployments, resources and groups deletions
FAILED_AZURE_CLEANUP=false
if [ ${#FAILED_AZURE_DEPLOYMENTS[@]} -gt 0 ] || [ ${#FAILED_AZURE_RESOURCES[@]} -gt 0 ] || [ ${#FAILED_AZURE_GROUPS[@]} -gt 0 ]; then
FAILED_AZURE_CLEANUP=true
fi

# Final check to exit with an error if any deletions of environments, stacks, or deployments failed
if [ ${#FAILED_ENVS[@]} -gt 0 ] || [ ${#FAILED_STACKS[@]} -gt 0 ] || [ ${#FAILED_DEPLOYMENTS[@]} -gt 0 ] || [ $FAILED_AZURE_CLEANUP != false ]; then
if [ ${#FAILED_ENVS[@]} -gt 0 ] || [ ${#FAILED_STACKS[@]} -gt 0 ] || [ ${#FAILED_DEPLOYMENTS[@]} -gt 0 ] || [ ${#FAILED_AZURE_GROUPS[@]} -gt 0 ]; then
echo "There were errors deleting one or more resources."
# Optionally, provide more details about which resources failed to delete
if [ ${#FAILED_ENVS[@]} -gt 0 ]; then
Expand All @@ -275,10 +223,8 @@ if [ ${#FAILED_ENVS[@]} -gt 0 ] || [ ${#FAILED_STACKS[@]} -gt 0 ] || [ ${#FAILED
echo "Failed to delete the following GCP deployments:"
printf "%s\n" "${FAILED_DEPLOYMENTS[@]}"
fi
if [ $FAILED_AZURE_CLEANUP != false ]; then
echo "Failed to delete the following Azure entities:"
printf "%s\n" "${FAILED_AZURE_DEPLOYMENTS[@]}"
printf "%s\n" "${FAILED_AZURE_RESOURCES[@]}"
if [ ${#FAILED_AZURE_GROUPS[@]} -gt 0 ]; then
echo "Failed to delete the following Azure groups:"
printf "%s\n" "${FAILED_AZURE_GROUPS[@]}"
fi
exit 1
Expand Down

0 comments on commit b16472d

Please sign in to comment.