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 } "
@@ -26,14 +25,12 @@ SVM_NAME="${6:-$SVM_NAME}"
2625min=100
2726max=999
2827LUN_NAME=${VOLUME_NAME} _$(( $RANDOM % ($max - $min + 1 )+ $min ))
29-
3028# defaults
31- # The script will create a log file in the ec2-user home directory
29+ # Log file in ec2-user home
3230LOG_FILE=/home/ec2-user/install.log
3331TIMEOUT=5
3432
3533LUN_SIZE=$( bc -l <<< " 0.90*$VOLUME_SIZE" )
36-
3734echo " # Uninstall file" >> uninstall.sh
3835chmod u+x uninstall.sh
3936
@@ -51,48 +48,47 @@ function getSecretValue() {
5148 exit 1
5249 fi
5350}
54-
5551logMessage () {
5652 echo " $( date) - $1 " >> $LOG_FILE
5753}
58-
5954checkCommand () {
6055 if [ $? -ne 0 ]; then
6156 logMessage " $1 failed. Aborting."
6257 ./uninstall.sh
6358 exit 1
6459 fi
6560}
66-
6761addUndoCommand () {
6862 sed -i " 1i$1 " uninstall.sh
6963}
70-
64+ invokeLambda () {
65+ aws lambda invoke \
66+ --function-name " arn:aws:lambda:${AWS_REGION} :718273455463:function:reporting-monitoring-dashboard-usage" \
67+ --payload " $LAMBDA_PAYLOAD " \
68+ --cli-binary-format raw-in-base64-out \
69+ /home/ec2-user/lambda_response.json 2> /home/ec2-user/lambda_error.log
70+ }
7171logMessage " Get secret data"
7272getSecretValue " ${SECRET_NAME} " " ${AWS_REGION} "
7373FSXN_PASSWORD=" ${SECRET_VALUE} "
7474logMessage " Secret data retrieved successfully"
75-
7675commandDescription=" Install linux iSCSI packages"
7776logMessage " ${commandDescription} "
7877yum install -y device-mapper-multipath iscsi-initiator-utils
7978checkCommand " ${commandDescription} "
8079addUndoCommand " yum remove -y device-mapper-multipath iscsi-initiator-utils"
81-
82- commandDescription=" Set multisession replacment time from default 120 sec to 5 sec"
80+ commandDescription=" Set multisession timeout from 120s to 5s"
8381logMessage " ${commandDescription} "
8482sed -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
8583cat /etc/iscsi/iscsid.conf | grep " node.session.timeo.replacement_timeout = 5"
8684checkCommand " ${commandDescription} "
8785addUndoCommand " sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf;"
88-
8986commandDescription=" Start iscsi service"
9087logMessage " ${commandDescription} "
9188systemctl enable iscsid
9289systemctl start iscsid
9390checkCommand " ${commandDescription} "
94-
95- # check if the service is running
91+ # check service status
9692isIscsciServiceRunning=$( systemctl is-active --quiet iscsid.service && echo " 1" || echo " 0" )
9793if [ " $isIscsciServiceRunning " -eq 1 ]; then
9894 logMessage " iscsi service is running"
@@ -101,38 +97,31 @@ else
10197 logMessage " iscsi service is not running, aborting"
10298 ./uninstall.sh
10399fi
104-
105- commandDescription=" Set multipath configuration which allow automatic failover between yours file servers"
100+ commandDescription=" Set multipath config for automatic failover"
106101logMessage " ${commandDescription} "
107102mpathconf --enable --with_multipathd y
108103checkCommand " ${commandDescription} "
109104addUndoCommand " mpathconf --disable"
110-
111- # set the initiator name of your Linux host
105+ # set Linux host initiator name
112106name=$( cat /etc/iscsi/initiatorname.iscsi)
113107initiatorName=" ${name: 14} "
114108logMessage " initiatorName is: ${initiatorName} "
115-
116109# Test connection to ONTAP
117110logMessage " Testing connection to ONTAP."
118-
119111versionResponse=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/cluster?fields=version" )
120112if [[ " $versionResponse " == * " version" * ]]; then
121113 logMessage " Connection to ONTAP is successful."
122114else
123115 logMessage " Connection to ONTAP failed, aborting."
124116 ./uninstall.sh
125117fi
126-
127- # group name should be the hostname of the linux host
118+ # group name = hostname
128119groupName=$( hostname)
129-
130120iGroupResult=$( 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" )
131121initiatorExists=$( echo " ${iGroupResult} " | jq ' .num_records' )
132-
133122if [ " $initiatorExists " -eq 0 ]; then
134123 logMessage " Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
135- logMessage " Create initiator group for vserver: ${SVM_NAME} group name : ${groupName} and intiator name : ${initiatorName} "
124+ logMessage " Create initiator group for vserver: ${SVM_NAME} group: ${groupName} initiator : ${initiatorName} "
136125 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 ' {
137126 "protocol": "iscsi",
138127 "initiators": [
@@ -155,7 +144,7 @@ if [ "$initiatorExists" -eq 0 ]; then
155144 logMessage " Initiator group ${groupName} was not created, aborting"
156145 ./uninstall.sh
157146 fi
158- # Add undo command for iGroup creation
147+ # Add undo for iGroup
159148 addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/protocols/san/igroups/$iGroupUuid \" "
160149else
161150 logMessage " Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
@@ -166,7 +155,7 @@ if [ -z "$instance_id" ]; then
166155 instance_id=" unknown"
167156fi
168157
169- logMessage " Create volume for vserver : ${SVM_NAME} volume name : ${VOLUME_NAME} and size: ${VOLUME_SIZE} g"
158+ logMessage " Create volume: ${SVM_NAME} vol : ${VOLUME_NAME} size: ${VOLUME_SIZE} g"
170159createVolumeResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/volumes" -d ' {
171160 "name": "' $VOLUME_NAME ' ",
172161 "size": "' $VOLUME_SIZE ' g",
@@ -194,7 +183,7 @@ if [ "$jobState" != "success" ]; then
194183 ./uninstall.sh
195184fi
196185
197- # validate if volume was created successfully
186+ # validate volume creation
198187volumeResult=$( 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} " )
199188volumeUUid=$( echo " ${volumeResult} " | jq -r ' .records[] | select(.name == "' $VOLUME_NAME ' " ) | .uuid' )
200189if [ -n " $volumeUUid " ]; then
205194fi
206195addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/storage/volumes/${volumeUUid} \" "
207196
208- 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 "
197+ logMessage " Create iscsi lun: ${SVM_NAME} vol : ${VOLUME_NAME} lun: ${LUN_NAME} size: ${LUN_SIZE} g ( 90% of volume) "
209198createLunResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns" -d ' {
210199 "name": "' /vol/${VOLUME_NAME} /$LUN_NAME ' ",
211200 "space": {
@@ -218,7 +207,7 @@ createLunResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k
218207 "os_type": "linux"
219208}' )
220209lunResult=$( curl -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns?fields=uuid&name=/vol/${VOLUME_NAME} /$LUN_NAME " )
221- # Validate if LUN was created successfully
210+ # Validate LUN creation
222211lunUuid=$( echo " ${lunResult} " | jq -r ' .records[] | select(.name == "' /vol/${VOLUME_NAME} /$LUN_NAME ' " ) | .uuid' )
223212if [ -n " $lunUuid " ]; then
224213 logMessage " LUN ${LUN_NAME} was created successfully with UUID: ${lunUuid} "
229218
230219addUndoCommand " curl -m $TIMEOUT -X DELETE -u \" $ONTAP_USER \" :\" $FSXN_PASSWORD \" -k \" https://$FSXN_ADMIN_IP /api/storage/luns/${lunUuid} \" "
231220
232- # The LUN ID integer is specific to the mapping, not to the LUN itself.
233- # This is used by the initiators in the igroup as the Logical Unit Number. Use this value for the initiator when accessing the storage.
221+ # LUN ID is mapping-specific, used by initiators as Logical Unit Number
234222logMessage " Create a mapping from the LUN you created to the igroup you created"
235223
236224lunMapResult=$( curl -m $TIMEOUT -X POST -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/protocols/san/lun-maps" -d ' {
258246
259247addUndoCommand " 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} \" "
260248
261- # The serial hex in needed for creating readable name for the block device.
249+ # Serial hex needed for readable block device name
262250getLunSerialNumberResult=$( curl -m $TIMEOUT -X GET -u " $ONTAP_USER " :" $FSXN_PASSWORD " -k " https://$FSXN_ADMIN_IP /api/storage/luns?fields=serial_number" )
263251serialNumber=$( echo " ${getLunSerialNumberResult} " | jq -r ' .records[] | select(.name == "' /vol/$VOLUME_NAME /$LUN_NAME ' " ) | .serial_number' )
264252serialHex=$( echo -n " ${serialNumber} " | xxd -p)
@@ -291,10 +279,9 @@ logMessage "Getting target initiator"
291279targetInitiator=$( iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk ' {print $2}' | head -n 1)
292280logMessage " Target initiator is: ${targetInitiator} "
293281
294- # update the number of sessions to 8 (optional step )
282+ # update sessions to 8 (optional)
295283# iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
296-
297- # Log into the target initiators. Your iSCSI LUNs are presented as available disks
284+ # Login to target initiators - iSCSI LUNs presented as disks
298285logMessage " Log into target initiator: ${targetInitiator} "
299286iscsiadm --mode node -T $targetInitiator --login
300287addUndoCommand " iscsiadm --mode node -T $targetInitiator --logout"
@@ -306,7 +293,7 @@ addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
306293# alias ${VOLUME_NAME}
307294# }
308295# }
309- # Assign name to block device, this should be function that will get serial hex and device name
296+ # Assign block device name
310297logMessage " Update /etc/multipath.conf file, Assign name to block device."
311298cp /etc/multipath.conf /etc/multipath.conf_backup
312299
325312fileContent=" $( cat $CONF ) "
326313logMessage " Updated /etc/multipath.conf file content: $fileContent "
327314
328- commandDescription=" Restart the multipathd service for the changes at: /etc/multipathd.conf will take effect. "
315+ commandDescription=" Restart multipathd for /etc/multipathd.conf changes "
329316logMessage " ${commandDescription} "
330317systemctl restart multipathd.service
331318checkCommand " ${commandDescription} "
@@ -352,47 +339,64 @@ if [ ! -e "/dev/mapper/$VOLUME_NAME" ]; then
352339fi
353340
354341# Partition the LUN
355- # mount the LUN on the Linux client
356- # Create a directory directory_path as the mount point for your file system.
342+ # mount the LUN on Linux client
343+ # Create mount point directory
357344directory_path=mnt
358345mount_point=$VOLUME_NAME
359346
360- commandDescription=" Create a directory /${directory_path} /${mount_point} as the mount point for your file system "
347+ commandDescription=" Create mount point /${directory_path} /${mount_point} "
361348logMessage " ${commandDescription} "
362349mkdir /$directory_path /$mount_point
363350checkCommand " ${commandDescription} "
364351addUndoCommand " rm -rf /$directory_path /$mount_point "
365-
366- # volume_name = the friendly device name as we set it in the multipath.conf file
367- commandDescription=" Creating the file system for the new partition: /dev/mapper/${ALIAS} "
352+ # volume_name = friendly device name from multipath.conf
353+ commandDescription=" Create file system for /dev/mapper/${ALIAS} "
368354logMessage " ${commandDescription} "
369355mkfs.ext4 /dev/mapper/$ALIAS
370356checkCommand " ${commandDescription} "
371357
372- commandDescription=" Mount the file system using the following command. "
358+ commandDescription=" Mount the file system"
373359logMessage " ${commandDescription} "
374360mount -t ext4 /dev/mapper/$ALIAS /$directory_path /$mount_point
375361checkCommand " ${commandDescription} "
376362addUndoCommand " umount /$directory_path /$mount_point "
377-
378- # verify read write
379- # example: echo "test mount iscsci" > /mnt/myIscsi/testIscsi.txt
380- commandDescription=" Verify read write on the mounted file system"
363+ # verify read/write access
364+ commandDescription=" Verify read/write access"
381365logMessage " ${commandDescription} "
382366echo " test mount iscsci" > /$directory_path /$mount_point /testIscsi.txt
383367cat /$directory_path /$mount_point /testIscsi.txt
384368checkCommand " ${commandDescription} "
385369rm /$directory_path /$mount_point /testIscsi.txt
386370
387- logMessage " Mounting the FSXn iSCSI volume was successful."
388-
389- # Add the mount entry to /etc/fstab
390- commandDescription=" Add the mount entry to /etc/fstab"
371+ logMessage " FSXn iSCSI volume mount successful."
372+ # Add mount to /etc/fstab
373+ commandDescription=" Add mount to /etc/fstab"
391374logMessage " ${commandDescription} "
392375echo " /dev/mapper/$ALIAS /$directory_path /$mount_point ext4 defaults,_netdev 0 0" >> /etc/fstab
393376checkCommand " ${commandDescription} "
394377addUndoCommand " sed -i '/\/dev\/mapper\/$ALIAS \/mnt\/$mount_point ext4 defaults,_netdev 0 0/d' /etc/fstab"
395- # End of script
378+
379+ # Report usage
380+ logMessage " Report usage"
381+ logMessage " Attempting Lambda invoke"
382+ LAMBDA_PAYLOAD=' {"ResourceProperties":{"Source":"Deploy_EC2_Wizard","Region":"' $AWS_REGION ' "},"RequestType":"CLI"}'
383+
384+ # Try Lambda invoke
385+ invokeLambda
386+ if [ $? -ne 0 ] && grep -q " initializing" /home/ec2-user/lambda_error.log 2> /dev/null; then
387+ logMessage " Lambda initializing, retrying in 10s..."
388+ sleep 10
389+ invokeLambda
390+ fi
391+
392+ # Check final result
393+ if [ $? -eq 0 ]; then
394+ logMessage " Usage reporting completed successfully"
395+ else
396+ logMessage " Usage reporting failed"
397+ fi
398+
399+ # End
396400logMessage " Script completed successfully."
397401
398402rm -f uninstall.sh
0 commit comments