2022-10-19 07:10:15 -04:00
|
|
|
#!/usr/bin/env bash
|
2022-11-10 04:28:35 -05:00
|
|
|
|
2022-10-19 07:10:15 -04:00
|
|
|
set -euo pipefail
|
2022-11-10 04:28:35 -05:00
|
|
|
shopt -s inherit_errexit
|
2022-10-19 07:10:15 -04:00
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
if [[ -z ${CONFIG_FILE-} ]] && [[ -f ${CONFIG_FILE-} ]]; then
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
. "${CONFIG_FILE}"
|
2022-10-19 07:10:15 -04:00
|
|
|
fi
|
|
|
|
POSITIONAL_ARGS=()
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
2022-11-10 08:17:04 -05:00
|
|
|
-n | --name)
|
|
|
|
AZURE_VM_NAME="$2"
|
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2022-10-19 07:10:15 -04:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
|
|
|
|
|
|
|
AZ_VM_INFO=$(az vm show --name "${AZURE_VM_NAME}" --resource-group "${AZURE_RESOURCE_GROUP_NAME}" -o json)
|
|
|
|
NIC=$(echo "${AZ_VM_INFO}" | jq -r '.networkProfile.networkInterfaces[0].id')
|
|
|
|
NIC_INFO=$(az network nic show --ids "${NIC}" -o json)
|
|
|
|
PUBIP=$(echo "${NIC_INFO}" | jq -r '.ipConfigurations[0].publicIpAddress.id')
|
|
|
|
NSG=$(echo "${NIC_INFO}" | jq -r '.networkSecurityGroup.id')
|
|
|
|
SUBNET=$(echo "${NIC_INFO}" | jq -r '.ipConfigurations[0].subnet.id')
|
2022-11-10 04:28:35 -05:00
|
|
|
VNET=${SUBNET//\/subnets\/.*/}
|
2022-10-19 07:10:15 -04:00
|
|
|
DISK=$(echo "${AZ_VM_INFO}" | jq -r '.storageProfile.osDisk.managedDisk.id')
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_vm() {
|
|
|
|
az vm delete -y --name "${AZURE_VM_NAME}" \
|
|
|
|
--resource-group "${AZURE_RESOURCE_GROUP_NAME}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_vnet() {
|
|
|
|
az network vnet delete --ids "${VNET}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_subnet() {
|
|
|
|
az network vnet subnet delete --ids "${SUBNET}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_nsg() {
|
|
|
|
az network nsg delete --ids "${NSG}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_pubip() {
|
|
|
|
az network public-ip delete --ids "${PUBIP}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_disk() {
|
|
|
|
az disk delete -y --ids "${DISK}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
delete_nic() {
|
|
|
|
az network nic delete --ids "${NIC}" || true
|
2022-10-19 07:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete_vm
|
|
|
|
delete_disk
|
|
|
|
delete_nic
|
|
|
|
delete_nsg
|
|
|
|
delete_subnet
|
|
|
|
delete_vnet
|
|
|
|
delete_pubip
|