-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete_fsxn_volume
executable file
·141 lines (134 loc) · 4.6 KB
/
delete_fsxn_volume
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
#!/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 an FSxN volume.
################################################################################
################################################################################
# This function just outputs the usage information and exits.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage: $(basename $0) -i volumeID [-r region] [-b] [-w]
where
volumeID: is the ID of the volume to delete.
region: is the AWS region where the FSxN filesystem resides.
-b: enable final backup. Otherwise it is skipped.
-w: wait for the volume to be deleted before returning.
EOF
exit 1
}
################################################################################
# This function waits for the volume to be deleted.
################################################################################
waitForVolumeDelete () {
local volumeId=$1
local MaxIterations=60
local SleepTime=5
#
# Wait for the volume to be deleted.
i=0
while [ $i -lt $MaxIterations ]; do
aws fsx describe-volumes --volume-ids $volumeId --output=json --region=$region > $tmpout 2>&1
if [ $? -eq 0 ]; then
status=$(jq -r .Volumes[0].Lifecycle $tmpout 2> /dev/null)
if [ "$status" != "DELETING" -a "$status" != "PENDING" ]; then
printf "\nError, failed to delete volume with volume ID '$volumeId'. Status = ${status}.\n" 1>&2
reason="$(jq -r '.Volumes[0].LifecycleTransitionReason.Message' $tmpout 2> /dev/null)"
if [ ! -z "$reason" ]; then
echo "Reason: $reason" 1>&2
else
cat $tmpout 1>&2
fi
return 1
fi
else
# Assume if it failed, it is because the volume was deleted and doesn't exist anymore.
break
fi
[ $quiet != "true" ] && printf "."
sleep $SleepTime
let i+=1
done
if [ $i -ge $MaxIterations ]; then
printf "\nFailed to delete volume with volume ID of '$volumeId'. Taking too long.\n" 1>&2
return 1
fi
return 0
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/delete-volume.$$
trap 'rm -f $tmpout' exit
#
# Set some defaults.
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
skipBackup=true
waitForCompletion=false
quiet=false
#
# Process command line arguments.
while getopts "qhwbi:r:" option; do
case $option in
r) region="$OPTARG"
;;
i) volumeId="$OPTARG"
;;
b) skipBackup=false
;;
w) waitForCompletion=true
;;
q) quiet=true
;;
*) usage
;;
esac
done
if which jq aws > /dev/null 2>&1; then
:
else
echo "Error, both 'jq' and 'aws' are required to run this script." 1>&2
exit 1
fi
#
# Ensure all the required parameters have been provided.
if [ -z "$volumeId" ]; then
echo "Error, missing required arguments." 1>&2
usage
fi
aws fsx delete-volume --volume-id $volumeId --region=$region --output=json --ontap-configuration '{"SkipFinalBackup": '$skipBackup'}' > $tmpout 2>&1
if [ $? != "0" ]; then
echo "Failed to delete volume." 1>&2
cat $tmpout 1>&2
exit 1
else
status=$(jq -r .Lifecycle $tmpout 2> /dev/null)
if [ "$status" == "DELETING" -o "$status" == "PENDING" ]; then
[ $quiet != "true" ] && printf "Volume '$volumeId' is being deleted."
if [ "$waitForCompletion" == "true" ]; then
waitForVolumeDelete $volumeId
if [ $? -ne 0 ]; then
exit 1
fi
[ $quiet != "true" ] && printf "\n"
else
[ $quiet != "true" ] && printf "\n"
fi
else
echo "Unknown status '$status'. Complete output returned from the AWS api:" 1>&2
cat $tmpout 1>&2
exit 1
fi
fi
exit 0