-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_fsxn_svm
executable file
·104 lines (98 loc) · 3.91 KB
/
create_fsxn_svm
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
#!/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 create an FSxN virtual storage machine under the
# specified FSxN "filesystem".
################################################################################
################################################################################
# This function just outputs the usage information and exits.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage: $(basename $0) -n svmName -f fileSystemName [-r region] [-i fileSystemId]
Where:
svmName: Is the name you want to assign the storage virtual machine.
fileSystemName: Is the name of the FSxN file system where you want the SVM created. This option is mutually exclusive with the -i option.
region: Is the AWS region where the FSxN file system resides.
fileSystemID: Is the file system ID where you want to create the SVM on. This option is mutually exclusive with the -f option.
EOF
exit 1
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/create_svm.$$
trap 'rm -f $tmpout' exit
if which aws jq > /dev/null 2>&1; then
:
else
echo "Error, both the 'aws' and 'jq' commands are required to run this script." 1>&2
exit 1
fi
#
# Set some defaults.
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
#
# Process command line arguments.
while getopts "hn:r:f:i:" option; do
case $option in
n) svmName=$OPTARG
;;
r) region=$OPTARG
;;
f) fileSystemName=$OPTARG
;;
i) fsid=$OPTARG
;;
*) usage
;;
esac
done
if [ ! -z "$fileSystemName" -a ! -z "$fsid" ]; then
echo "Error, you can only specify the -i OR the -f option. Not both." 1>&2
usage
fi
#
# Ensure all the required parameters have been provided.
if [ -z "$svmName" -o -z "$fileSystemName" -a -z "$fsid" ]; then
echo "Error, missing required arguments." 1>&2
usage
fi
#
# Get the file system id from the file system name.
if [ -z "$fsid" ]; then
fsid=$(aws fsx describe-file-systems --output=json 2> /dev/null | jq -r ".FileSystems[] | if((.Tags[] | select(.Key == \"Name\") .Value) == \"${fileSystemName}\") then .FileSystemId else empty end" 2> /dev/null)
fi
if [ -z "$fsid" ]; then
echo "Error, could not find the file system with name '$fileSystemName}' in region $region." 1>&2
exit 1
fi
#
# Create the SVM
aws fsx create-storage-virtual-machine --name $svmName --region=$region --file-system-id "$fsid" --output=json > $tmpout 2>&1
if [ $? != "0" ]; then
echo "Failed to create storage virtual machine." 1>&2
cat $tmpout 1>&2
exit 1
else
status=$(jq -r .StorageVirtualMachine.Lifecycle $tmpout 2> /dev/null)
if [ "$status" == "CREATING" -o "$status" == "PENDING" ]; then
echo "Stroage Virtaul Machine '$svmName'($(jq -r '.StorageVirtualMachine.StorageVirtualMachineId' $tmpout)) is being created."
exit 0
else
echo "Unknown status '$status'. Complete output returned from the AWS api:" 1>&2
cat $tmpout 1>&2
exit 1
fi
fi