11#! /bin/bash
22
33# user data
4- # Secret name has it been saved in AWS secret manager
4+ # Set the secret name and region
55SECRET_NAME=
66AWS_REGION=
7- # Fsx admin ip, e.g. 172.25.45.32
7+ # Set the FSx admin IP
88FSXN_ADMIN_IP=
9- # FSxN Volume name , e.g. iscsiVol
9+ # Volume name
1010VOLUME_NAME=
11- # Volume size in GB e.g 100
11+ # Volume size in GB
1212VOLUME_SIZE=
13- # Default value is fsx, but you can change it to any other value according to yours FSx for ONTAP SVM name
13+ # SVM name (default: fsx)
1414SVM_NAME=fsx
15- # Default value is fsxadmin, but you can change it to any other value according to yours FSx for ONTAP admin user name
15+ # ONTAP admin user (default: fsxadmin)
1616ONTAP_USER=fsxadmin
1717# end - user data
18-
1918SECRET_NAME=" ${SECRET_NAME:= $1 } "
2019AWS_REGION=" ${AWS_REGION:= $2 } "
2120FSXN_ADMIN_IP=" ${FSXN_ADMIN_IP:= $3 } "
@@ -25,14 +24,12 @@ VOLUME_SIZE="${VOLUME_SIZE:=$5}"
2524min=100
2625max=999
2726LUN_NAME=${VOLUME_NAME} _$(( $RANDOM % ($max - $min + 1 )+ $min ))
28-
2927# defaults
30- # The script will create a log file in the ec2-user home directory
28+ # Log file in ec2-user home
3129LOG_FILE=/home/ec2-user/install.log
3230TIMEOUT=5
3331
3432LUN_SIZE=$( bc -l <<< " 0.90*$VOLUME_SIZE" )
35-
3633echo " # Uninstall file" >> uninstall.sh
3734chmod u+x uninstall.sh
3835
@@ -50,48 +47,47 @@ function getSecretValue() {
5047 exit 1
5148 fi
5249}
53-
5450logMessage () {
5551 echo " $( date) - $1 " >> $LOG_FILE
5652}
57-
5853checkCommand () {
5954 if [ $? -ne 0 ]; then
6055 logMessage " $1 failed. Aborting."
6156 ./uninstall.sh
6257 exit 1
6358 fi
6459}
65-
6660addUndoCommand () {
6761 sed -i " 1i$1 " uninstall.sh
6862}
69-
63+ invokeLambda () {
64+ aws lambda invoke \
65+ --function-name " arn:aws:lambda:${AWS_REGION} :718273455463:function:reporting-monitoring-dashboard-usage" \
66+ --payload " $LAMBDA_PAYLOAD " \
67+ --cli-binary-format raw-in-base64-out \
68+ /home/ec2-user/lambda_response.json 2> /home/ec2-user/lambda_error.log
69+ }
7070logMessage " Get secret data"
7171getSecretValue " ${SECRET_NAME} " " ${AWS_REGION} "
7272FSXN_PASSWORD=" ${SECRET_VALUE} "
7373logMessage " Secret data retrieved successfully"
74-
7574commandDescription=" Install linux iSCSI packages"
7675logMessage " ${commandDescription} "
7776yum install -y device-mapper-multipath iscsi-initiator-utils
7877checkCommand " ${commandDescription} "
7978addUndoCommand " yum remove -y device-mapper-multipath iscsi-initiator-utils"
80-
81- commandDescription=" Set multisession replacment time from default 120 sec to 5 sec"
79+ commandDescription=" Set multisession timeout from 120s to 5s"
8280logMessage " ${commandDescription} "
8381sed -i ' s/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 5/' /etc/iscsi/iscsid.conf; cat /etc/iscsi/iscsid.conf | grep node.session.timeo.replacement_timeout
8482cat /etc/iscsi/iscsid.conf | grep " node.session.timeo.replacement_timeout = 5"
8583checkCommand " ${commandDescription} "
8684addUndoCommand " sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf;"
87-
8885commandDescription=" Start iscsi service"
8986logMessage " ${commandDescription} "
9087systemctl enable iscsid
9188systemctl start iscsid
9289checkCommand " ${commandDescription} "
93-
94- # check if the service is running
90+ # check service status
9591isIscsciServiceRunning=$( systemctl is-active --quiet iscsid.service && echo " 1" || echo " 0" )
9692if [ " $isIscsciServiceRunning " -eq 1 ]; then
9793 logMessage " iscsi service is running"
@@ -100,38 +96,31 @@ else
10096 logMessage " iscsi service is not running, aborting"
10197 ./uninstall.sh
10298fi
103-
104- commandDescription=" Set multipath configuration which allow automatic failover between yours file servers"
99+ commandDescription=" Set multipath config for automatic failover"
105100logMessage " ${commandDescription} "
106101mpathconf --enable --with_multipathd y
107102checkCommand " ${commandDescription} "
108103addUndoCommand " mpathconf --disable"
109-
110- # set the initiator name of your Linux host
104+ # set Linux host initiator name
111105name=$( cat /etc/iscsi/initiatorname.iscsi)
112106initiatorName=" ${name: 14} "
113107logMessage " initiatorName is: ${initiatorName} "
114-
115108# Test connection to ONTAP
116109logMessage " Testing connection to ONTAP."
117-
118110versionResponse=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/cluster?fields=version" )
119111if [[ " $versionResponse " == * " version" * ]]; then
120112 logMessage " Connection to ONTAP is successful."
121113else
122114 logMessage " Connection to ONTAP failed, aborting."
123115 ./uninstall.sh
124116fi
125-
126- # group name should be the hostname of the linux host
117+ # group name = hostname
127118groupName=$( hostname)
128-
129119iGroupResult=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/protocols/san/igroups?svm.name=$SVM_NAME &name=$groupName &initiators.name=$initiatorName &protocol=iscsi&os_type=linux" )
130120initiatorExists=$( echo " ${iGroupResult} " | jq ' .num_records' )
131-
132121if [ " $initiatorExists " -eq 0 ]; then
133122 logMessage " Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
134- logMessage " Create initiator group for vserver: ${SVM_NAME} group name : ${groupName} and intiator name : ${initiatorName} "
123+ logMessage " Create initiator group for vserver: ${SVM_NAME} group: ${groupName} initiator : ${initiatorName} "
135124 createGroupResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -H " Content-Type: application/json" -k " https://$FSXN_ADMIN_IP /api/protocols/san/igroups" -d ' {
136125 "protocol": "iscsi",
137126 "initiators": [
@@ -154,7 +143,7 @@ if [ "$initiatorExists" -eq 0 ]; then
154143 logMessage " Initiator group ${groupName} was not created, aborting"
155144 ./uninstall.sh
156145 fi
157- # Add undo command for iGroup creation
146+ # Add undo for iGroup
158147 addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/protocols/san/igroups/$iGroupUuid \" "
159148else
160149 logMessage " Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
@@ -165,7 +154,7 @@ if [ -z "$instance_id" ]; then
165154 instance_id=" unknown"
166155fi
167156
168- logMessage " Create volume for vserver : ${SVM_NAME} volume name : ${VOLUME_NAME} and size: ${VOLUME_SIZE} g"
157+ logMessage " Create volume: ${SVM_NAME} vol : ${VOLUME_NAME} size: ${VOLUME_SIZE} g"
169158createVolumeResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/volumes" -d ' {
170159 "name": "' $VOLUME_NAME ' ",
171160 "size": "' $VOLUME_SIZE ' g",
@@ -193,7 +182,7 @@ if [ "$jobState" != "success" ]; then
193182 ./uninstall.sh
194183fi
195184
196- # validate if volume was created successfully
185+ # validate volume creation
197186volumeResult=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/volumes?name=${VOLUME_NAME} &svm.name=${SVM_NAME} " )
198187volumeUUid=$( echo " ${volumeResult} " | jq -r ' .records[] | select(.name == "' $VOLUME_NAME ' " ) | .uuid' )
199188if [ -n " $volumeUUid " ]; then
204193fi
205194addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/storage/volumes/${volumeUUid} \" "
206195
207- logMessage " Create iscsi lun for vserver : ${SVM_NAME} volume name : ${VOLUME_NAME} and lun name : ${LUN_NAME} and size: ${LUN_SIZE} g which is 90% of the volume size "
196+ logMessage " Create iscsi lun: ${SVM_NAME} vol : ${VOLUME_NAME} lun: ${LUN_NAME} size: ${LUN_SIZE} g ( 90% of volume) "
208197createLunResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns" -d ' {
209198 "name": "' /vol/${VOLUME_NAME} /$LUN_NAME ' ",
210199 "space": {
@@ -217,7 +206,7 @@ createLunResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k
217206 "os_type": "linux"
218207}' )
219208lunResult=$( curl -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns?fields=uuid&name=/vol/${VOLUME_NAME} /$LUN_NAME " )
220- # Validate if LUN was created successfully
209+ # Validate LUN creation
221210lunUuid=$( echo " ${lunResult} " | jq -r ' .records[] | select(.name == "' /vol/${VOLUME_NAME} /$LUN_NAME ' " ) | .uuid' )
222211if [ -n " $lunUuid " ]; then
223212 logMessage " LUN ${LUN_NAME} was created successfully with UUID: ${lunUuid} "
228217
229218addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/storage/luns/${lunUuid} \" "
230219
231- # The LUN ID integer is specific to the mapping, not to the LUN itself.
232- # This is used by the initiators in the igroup as the Logical Unit Number. Use this value for the initiator when accessing the storage.
220+ # LUN ID is mapping-specific, used by initiators as Logical Unit Number
233221logMessage " Create a mapping from the LUN you created to the igroup you created"
234222
235223lunMapResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/protocols/san/lun-maps" -d ' {
257245
258246addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/protocols/san/lun-maps?lun.name=/vol/${VOLUME_NAME} /${LUN_NAME} &igroup.name=${groupName} &svm.name=${SVM_NAME} \" "
259247
260- # The serial hex in needed for creating readable name for the block device.
248+ # Serial hex needed for readable block device name
261249getLunSerialNumberResult=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns?fields=serial_number" )
262250serialNumber=$( echo " ${getLunSerialNumberResult} " | jq -r ' .records[] | select(.name == "' /vol/$VOLUME_NAME /$LUN_NAME ' " ) | .serial_number' )
263251serialHex=$( echo -n " ${serialNumber} " | xxd -p)
@@ -290,10 +278,9 @@ logMessage "Getting target initiator"
290278targetInitiator=$( iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk ' {print $2}' | head -n 1)
291279logMessage " Target initiator is: ${targetInitiator} "
292280
293- # update the number of sessions to 8 (optional step )
281+ # update sessions to 8 (optional)
294282# iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
295-
296- # Log into the target initiators. Your iSCSI LUNs are presented as available disks
283+ # Login to target initiators - iSCSI LUNs presented as disks
297284logMessage " Log into target initiator: ${targetInitiator} "
298285iscsiadm --mode node -T $targetInitiator --login
299286addUndoCommand " iscsiadm --mode node -T $targetInitiator --logout"
@@ -305,7 +292,7 @@ addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
305292# alias ${VOLUME_NAME}
306293# }
307294# }
308- # Assign name to block device, this should be function that will get serial hex and device name
295+ # Assign block device name
309296logMessage " Update /etc/multipath.conf file, Assign name to block device."
310297cp /etc/multipath.conf /etc/multipath.conf_backup
311298
324311fileContent=" $( cat $CONF ) "
325312logMessage " Updated /etc/multipath.conf file content: $fileContent "
326313
327- commandDescription=" Restart the multipathd service for the changes at: /etc/multipathd.conf will take effect. "
314+ commandDescription=" Restart multipathd for /etc/multipathd.conf changes "
328315logMessage " ${commandDescription} "
329316systemctl restart multipathd.service
330317checkCommand " ${commandDescription} "
@@ -351,47 +338,64 @@ if [ ! -e "/dev/mapper/$VOLUME_NAME" ]; then
351338fi
352339
353340# Partition the LUN
354- # mount the LUN on the Linux client
355- # Create a directory directory_path as the mount point for your file system.
341+ # mount the LUN on Linux client
342+ # Create mount point directory
356343directory_path=mnt
357344mount_point=$VOLUME_NAME
358345
359- commandDescription=" Create a directory /${directory_path} /${mount_point} as the mount point for your file system "
346+ commandDescription=" Create mount point /${directory_path} /${mount_point} "
360347logMessage " ${commandDescription} "
361348mkdir /$directory_path /$mount_point
362349checkCommand " ${commandDescription} "
363350addUndoCommand " rm -rf /$directory_path /$mount_point "
364-
365- # volume_name = the friendly device name as we set it in the multipath.conf file
366- commandDescription=" Creating the file system for the new partition: /dev/mapper/${ALIAS} "
351+ # volume_name = friendly device name from multipath.conf
352+ commandDescription=" Create file system for /dev/mapper/${ALIAS} "
367353logMessage " ${commandDescription} "
368354mkfs.ext4 /dev/mapper/$ALIAS
369355checkCommand " ${commandDescription} "
370356
371- commandDescription=" Mount the file system using the following command. "
357+ commandDescription=" Mount the file system"
372358logMessage " ${commandDescription} "
373359mount -t ext4 /dev/mapper/$ALIAS /$directory_path /$mount_point
374360checkCommand " ${commandDescription} "
375361addUndoCommand " umount /$directory_path /$mount_point "
376-
377- # verify read write
378- # example: echo "test mount iscsci" > /mnt/myIscsi/testIscsi.txt
379- commandDescription=" Verify read write on the mounted file system"
362+ # verify read/write access
363+ commandDescription=" Verify read/write access"
380364logMessage " ${commandDescription} "
381365echo " test mount iscsci" > /$directory_path /$mount_point /testIscsi.txt
382366cat /$directory_path /$mount_point /testIscsi.txt
383367checkCommand " ${commandDescription} "
384368rm /$directory_path /$mount_point /testIscsi.txt
385369
386- logMessage " Mounting the FSXn iSCSI volume was successful."
387-
388- # Add the mount entry to /etc/fstab
389- commandDescription=" Add the mount entry to /etc/fstab"
370+ logMessage " FSXn iSCSI volume mount successful."
371+ # Add mount to /etc/fstab
372+ commandDescription=" Add mount to /etc/fstab"
390373logMessage " ${commandDescription} "
391374echo " /dev/mapper/$ALIAS /$directory_path /$mount_point ext4 defaults,_netdev 0 0" >> /etc/fstab
392375checkCommand " ${commandDescription} "
393376addUndoCommand " sed -i '/\/dev\/mapper\/$ALIAS \/mnt\/$mount_point ext4 defaults,_netdev 0 0/d' /etc/fstab"
394- # End of script
377+
378+ # Report usage
379+ logMessage " Report usage"
380+ logMessage " Attempting Lambda invoke"
381+ LAMBDA_PAYLOAD=' {"ResourceProperties":{"Source":"Deploy_EC2_Wizard","Region":"' $AWS_REGION ' "},"RequestType":"CLI"}'
382+
383+ # Try Lambda invoke
384+ invokeLambda
385+ if [ $? -ne 0 ] && grep -q " initializing" /home/ec2-user/lambda_error.log 2> /dev/null; then
386+ logMessage " Lambda initializing, retrying in 10s..."
387+ sleep 10
388+ invokeLambda
389+ fi
390+
391+ # Check final result
392+ if [ $? -eq 0 ]; then
393+ logMessage " Usage reporting completed successfully"
394+ else
395+ logMessage " Usage reporting failed"
396+ fi
397+
398+ # End
395399logMessage " Script completed successfully."
396400
397401rm -f uninstall.sh
0 commit comments