-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelete_fsxn_filesystem
executable file
·248 lines (239 loc) · 9.8 KB
/
delete_fsxn_filesystem
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
################################################################################
# THIS SOFTWARE IS PROVIDED BY NETAPP "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR'
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
#
# This script is used to delete a FSxN file system. It does that by first
# displaying all the SVM and volumes associated with the file system and then
# asking the user if they are sure they want to delete the file system. If the
# user responds with 'yes', then the script will delete all the SVMs associated
# with the file system. The 'delete_fsxn_svm' script will delete all the volumes
# associated with the SVM. Once all the SVMs and volumes have been deleted, the
# script will then delete the file system itself.
################################################################################
################################################################################
# This function just outputs the usage information and forces the script to
# exit.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage: $(basename $0) [-f fileSystemName] [-r region] [-i fileSystemID] [-b] [-w]
Where:
fileSystemName: Is the name of the file system you want to delete. This option is mutually exclusive to the -i option.
fileSystemID: Is the file system id of the file system you want to delete. This option is mutually exclusive to the -f option.
region: Is the AWS region where the FSxN filesystem resides.
-b: enable final backup. Otherwise, it is skipped.
-w: Wait for the file system to be deleted before returning.
EOF
exit 1
}
################################################################################
# This function is used to display the contents of a FSxN file system based on
# the file system ID passed as the first and only argument. It assumes that
# the svmsFile and volumesFile files have already been created.
################################################################################
displayFileSystemContents() {
fsId=$1
jq -r '.StorageVirtualMachines[] | if(.FileSystemId == "'$fsId'") then .StorageVirtualMachineId + " " + .Name else empty end' $svmsFile | while read svmId svmName; do
printf "Storage Virtual Machine: $svmId - '$svmName' with the following volumes:\n"
jq -r '.Volumes[] | if(.FileSystemId == "'$fsId'" and .OntapConfiguration.StorageVirtualMachineId == "'$svmId'") then .VolumeId + " " + .Name else empty end' $volumesFile | while read volumeId volumeName; do
printf "\t$volumeId - '$volumeName'\n"
done
done
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/fsx_fs_delete.$$
svmsFile=/tmp/fsx_fs_delete_svms.$$
volumesFile=/tmp/fsx_fs_delete_volumes.$$
trap 'rm -f $tmpout $svmsFile $volumesFile' exit
#
# Set the maximum number of times to check that will be made to see if a
# file system has been deleted. Multiple it by the SleepTime set below to
# the total amount of time allowed.
MaxIterations=120
#
# Set the number of seconds to wait between checks that the file system
# has been deleted.
SleepTime=5
#
# Check that the required commands are available.
for cmd in jq aws delete_fsxn_svm delete_fsxn_volume; do
if which $cmd > /dev/null 2>&1; then
:
else
echo "Error, command '$cmd' is required to run this script." 1>&2
exit 1
fi
done
#
# Get the default region.
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
enableBackup=false
waitForCompletion=false
#
# Process command line arguments.
while getopts "hwbr:f:i:" option; do
case $option in
f) fileSystemName=$OPTARG
;;
r) region=$OPTARG
;;
i) fsid=$OPTARG
;;
b) enableBackup=true
;;
w) waitForCompletion=true
;;
*) usage
;;
esac
done
if [ ! -z "$fsid" -a ! -z "$fileSystemName" ]; then
echo "Error, you can only specify the -i OR the -f option, not both." 1>&2
usage # implied exit
fi
#
# Ensure all the required parameters have been provided.
if [ -z "$fileSystemName" -a -z "$fsid" ]; then
echo "Error, you must provide either the filesystem name or the file system id that you want to delete." 1>&2
usage # implied exit
fi
if [ $enableBackup == "true" ]; then
enableBackup="-b" # Turn it into a flag passed to the delete_fsxn_svm script.
else
enableBackup=""
fi
#
# Get the file system id based on the name.
if [ -z "$fsid" ]; then
fsid=($(aws fsx describe-file-systems --region=$region --output=json 2> $tmpout | jq -r '.FileSystems[] | if((.Tags[] | select(.Key == "Name") .Value) == "'"${fileSystemName}"'") then .FileSystemId else empty end' 2> /dev/null))
if [ ${#fsid[*]} -gt 1 ]; then
echo "Error, more than one file system matched the file system name '$fileSystemName'." 1>&2
echo "Please use the -i option to specify the exact file system you want to delete." 1>&2
exit 1
fi
if [ -z "$fsid" ]; then
echo "Error, could not find the file system with name '$fileSystemName'." 1>&2
cat $tmpout 1>&2
exit 1
fi
else
#
# Get the file system name based on the fsid.
aws fsx describe-file-systems --file-system-ids $fsid --region=$region --output=json > $tmpout 2>&1
if [ $? -ne 0 ]; then
echo "Error, failed to get the file system name based on the ID ($fsid)." 1>&2
cat $tmpout 1>&2
exit 1
fi
fileSystemName=$(jq -r '.FileSystems[0].Tags[] | select(.Key == "Name") .Value' $tmpout)
#
# Since it isn't required to have a name (Name tag), set it to "Not Set" if it doesn't exist.k
if [ -z "$fileSystemName" ]; then
fileSystemName="Not Set"
fi
fi
#
# Create a JSON file with all the SVMs associated with the file system.
aws fsx describe-storage-virtual-machines --region=$region --output=json --filters Name=file-system-id,Values=$fsid > $svmsFile 2>&1
if [ $? -ne 0 ]; then
echo "Error, failed to get the list of SVMs." 1>&2
cat $svmsFile 1>&2
exit 1
fi
#
# Create a JSON file with all the FSXN volumes associated with the file system.
aws fsx describe-volumes --region=$region --output=json --filters Name=file-system-id,Values=$fsid > $volumesFile 2>&1
if [ $? -ne 0 ]; then
echo "Error, failed to get the list of volumes." 1>&2
cat $volumesFile 1>&2
exit 1
fi
numVolumes=$(jq '.Volumes | length' $volumesFile)
numSvms=$(jq '.StorageVirtualMachines | length' $svmsFile)
#
# Make sure the user really wants to delete the file system.
if [ $numVolumes -gt 0 -o $numSvms -gt 0 ]; then
echo "Here are the current contents of the '$fileSystemName'($fsid) file system you have indicated you want to delete:"
displayFileSystemContents $fsid
read -p "Are you sure you want to delete this file system, with all the above volumes (yes/no)? " response
if [ "$response" != "yes" ]; then
echo "Aborted."
exit 1
fi
fi
#
# Create a list of all the SVMs associated with the file system.
svms=($(jq -r '.StorageVirtualMachines[] | select(.FileSystemId == "'$fsId'") | .StorageVirtualMachineId' $svmsFile))
#
# First delete all the SVMs. The 'delete_fsxn_svm' script will delete all the volumes associated with the SVM.
for svmId in ${svms[*]}; do
delete_fsxn_svm -n -w $enableBackup -i $svmId -r $region
if [ $? -ne 0 ]; then
echo "Error, failed to delete the SVM with SVM ID '$svmId'." 1>&2
exit 1
fi
done
#
# Now that all the volumes and all the SVMs have been deleted, we can delete the filesystem.
aws fsx delete-file-system --file-system-id $fsid --output=json --region=$region > $tmpout 2>&1
if [ $? != "0" ]; then
printf "\nError, failed to delete file system.\n" 1>&2
cat $tmpout 1>&2
exit 1
else
status=$(jq -r .Lifecycle $tmpout)
if [ "$status" == "DELETING" -o "$status" == "PENDING" ]; then
printf "\nFile system '$fileSystemName'($fsid) is being deleted."
if [ $waitForCompletion == "true" ]; then
i=0
while [ $i -lt $MaxIterations ]; do
aws fsx describe-file-systems --file-system-ids $fsid --output=json --region=$region > $tmpout 2>&1
if [ $? -eq 0 ]; then
status=$(jq -r '.FileSystems[0].Lifecycle' $tmpout 2> /dev/null)
if [ "$status" != "DELETING" -a "$status" != "PENDING" ]; then
printf "\nError, failed to delete file system with file system ID '$fsid'. Status = $status\n" 1>&2
reason="$(jq -r '.FileSystems[0].LifecycleTransitionReason.Message' $tmpout 2> /dev/null)"
if [ ! -z "$reason" ]; then
printf "Reason: $reason\n" 1>&2
else
cat $tmpout 1>&2
fi
exit 1
else
printf "."
fi
else
# Assume if it failed, it is because the filesystem was deleted and therefore doesn't exist anymore.
printf "\nFile system '$fileSystemName'($fsid) has been deleted.\n"
break
fi
sleep $SleepTime
let i+=1
done
if [ $i -ge $MaxIterations ]; then
printf "\nFailed to delete file system with filesystem ID of '$fsid'. Taking too long.\n" 1>&2
exit 1
fi
exit 0
else
echo
fi
exit 0
else
printf "\nUnknown status '$status'. Complete output returned from the AWS api:\n" 1>&2
cat $tmpout 1>&2
exit 1
fi
fi