-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdelete-vm.sh
executable file
·52 lines (45 loc) · 1.73 KB
/
delete-vm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
. $SCRIPT_DIR/../../handle-paths.sh
if [ -f $SCRIPT_DIR/secrets-export.sh ]; then
echo "Sourcing secrets"
source $SCRIPT_DIR/secrets-export.sh
fi
# Delete an Azure VM. `az` is expected to be logged in.
if [ -z "${AZUREKMS_RESOURCEGROUP:-}" ] || \
[ -z "${AZUREKMS_VMNAME:-}" ]; then
echo "Please set the following required environment variables"
echo " AZUREKMS_RESOURCEGROUP"
echo " AZUREKMS_VMNAME"
exit 1
fi
if [ -n "${AZUREKMS_SCOPE:-}" ]; then
echo "Deleting the role from the Virtual Machine $AZUREKMS_VMNAME ... begin"
PRINCIPAL_ID=$(az vm show --show-details --resource-group "$AZUREKMS_RESOURCEGROUP" --name "$AZUREKMS_VMNAME" --query identity.principalId -o tsv)
az role assignment delete \
--assignee "$PRINCIPAL_ID" \
--role "Key Vault Crypto User" \
--scope "$AZUREKMS_SCOPE" \
-y \
>/dev/null
echo "Deleting the role from the Virtual Machine $AZUREKMS_VMNAME ... end"
fi
echo "Deleting Virtual Machine $AZUREKMS_VMNAME ... begin"
az vm delete \
--resource-group "$AZUREKMS_RESOURCEGROUP" \
--name "$AZUREKMS_VMNAME" \
--yes
echo "Deleting Virtual Machine $AZUREKMS_VMNAME ... end"
echo "Delete public IP $AZUREKMS_VMNAME-PUBLIC-IP ... begin"
az network public-ip delete \
--resource-group "$AZUREKMS_RESOURCEGROUP" \
-n "$AZUREKMS_VMNAME-PUBLIC-IP"
echo "Delete public IP $AZUREKMS_VMNAME-PUBLIC-IP ... end"
echo "Delete Network Security Group $AZUREKMS_VMNAME-NSG ... begin"
az network nsg delete \
--resource-group "$AZUREKMS_RESOURCEGROUP" \
-n "$AZUREKMS_VMNAME-NSG"
echo "Delete Network Security Group $AZUREKMS_VMNAME-NSG ... end"