-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathuninstall.sh
121 lines (110 loc) · 3.95 KB
/
uninstall.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
function show_help() {
# Display Help
echo "Run this script to delete resources used to simulate a Purdue Network and its assets, including IoT Edge devices created in IoT Hub."
echo
echo "Syntax: ./uninstall.sh [-flag=value]"
echo ""
echo "List of mandatory flags:"
echo "-hubrg Azure Resource Group with the Azure IoT Hub."
echo "-hubname Name of the Azure IoT Hub controlling the IoT Edge devices."
echo ""
echo "List of optional flags:"
echo "-h Print this help."
echo "-c Path to configuration file with IoT Edge VMs information. Default: ./config.txt."
echo "-s Azure subscription ID to use to deploy resources. Default: use current subscription of Azure CLI."
echo "-rg Prefix used for all Azure Resource Groups created by the installation script. Default: iotedge4iiot."
echo
}
#global variable
scriptFolder=$(dirname "$(readlink -f "$0")")
# Default settings / Initializing all option variables to avoid contamination by variables from the environment.
iotHubResourceGroup=""
iotHubName=""
configFilePath="./config.txt"
resourceGroupPrefix="iotedge4iiot"
while :; do
case $1 in
-h|-\?|--help)
show_help
exit;;
-c=?*)
configFilePath=${1#*=}
if [ ! -f "${configFilePath}" ]; then
echo "Configuration file not found. Exiting."
exit 1
fi;;
-c=)
echo "Missing configuration file path. Exiting."
exit;;
-hubrg=?*)
iotHubResourceGroup=${1#*=}
;;
-hubrg=)
echo "Missing IoT Hub resource group. Exiting."
exit;;
-hubname=?*)
iotHubName=${1#*=}
;;
-hubname=)
echo "Missing IoT Hub name. Exiting."
exit;;
-s=?*)
subscription=${1#*=}
;;
-s=)
echo "Missing subscription. Exiting."
exit;;
-rg=?*)
resourceGroupPrefix=${1#*=}
;;
-rg=)
echo "Missing resource group prefix. Exiting."
exit;;
--)
shift
break;;
*)
break
esac
shift
done
# Derived default settings
networkResourceGroupName="${resourceGroupPrefix}-RG-network"
iotedgeResourceGroupName="${resourceGroupPrefix}-RG-iotedge"
#Verifying that mandatory parameters are there
if [ -z $iotHubResourceGroup ]; then
echo "Missing IoT Hub resource group. Exiting."
exit 1
fi
if [ -z $iotHubName ]; then
echo "Missing IoT Hub name. Exiting."
exit 1
fi
echo "==========================================================="
echo "== Azure Subscription =="
echo "==========================================================="
echo ""
if [ ! -z $subscription ]; then
az account set --subscription $subscription
fi
subscriptionDetails=($(az account show --query '[id, name]' -o tsv))
subscription=${subscriptionDetails[0]}
subscriptionName=${subscriptionDetails[1]}
echo "Executing script with Azure Subscription: ${subscriptionName}"
subscription=$(az account show --query 'name' -o tsv)
# Parse the configuration file
source ${scriptFolder}/scripts/parseConfigFile.sh $configFilePath
echo "Deleting all IoT Edge devices listed in the configuration file from IoT Hub..."
for (( i=0; i<${#iotEdgeDevices[@]}; i++))
do
echo "...${iotEdgeDevices[i]}"
az iot hub device-identity delete --device-id ${iotEdgeDevices[i]} --hub-name $iotHubName
done
echo "done"
echo "Deleting all resource groups..."
az group list --out tsv --tag $resourceGroupPrefix --query 'reverse(sort_by([], &tags.CreationDate)[*].name)' | while read line; do echo "...$line"; az group delete --yes --name $line; done
echo "done"
echo "==========================================================="
echo "== All resources have been removed =="
echo "==========================================================="