Skip to content

Commit bb38496

Browse files
authored
Merge pull request #261 from NetApp/report-CF-user-data-script
Add report to cf-usage service
2 parents 999fc13 + ce2e3d0 commit bb38496

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

Management-Utilities/ec2-user-data-iscsi-create-and-mount/linux_userData.sh

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
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
55
SECRET_NAME=
66
AWS_REGION=
7-
# Fsx admin ip, e.g. 172.25.45.32
7+
# Set the FSx admin IP
88
FSXN_ADMIN_IP=
9-
# FSxN Volume name , e.g. iscsiVol
9+
# Volume name
1010
VOLUME_NAME=
11-
# Volume size in GB e.g 100
11+
# Volume size in GB
1212
VOLUME_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)
1414
SVM_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)
1616
ONTAP_USER=fsxadmin
1717
# end - user data
18-
1918
SECRET_NAME="${SECRET_NAME:=$1}"
2019
AWS_REGION="${AWS_REGION:=$2}"
2120
FSXN_ADMIN_IP="${FSXN_ADMIN_IP:=$3}"
@@ -25,14 +24,12 @@ VOLUME_SIZE="${VOLUME_SIZE:=$5}"
2524
min=100
2625
max=999
2726
LUN_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
3129
LOG_FILE=/home/ec2-user/install.log
3230
TIMEOUT=5
3331

3432
LUN_SIZE=$(bc -l <<< "0.90*$VOLUME_SIZE" )
35-
3633
echo "# Uninstall file" >> uninstall.sh
3734
chmod u+x uninstall.sh
3835

@@ -50,48 +47,47 @@ function getSecretValue() {
5047
exit 1
5148
fi
5249
}
53-
5450
logMessage() {
5551
echo "$(date) - $1" >> $LOG_FILE
5652
}
57-
5853
checkCommand() {
5954
if [ $? -ne 0 ]; then
6055
logMessage "$1 failed. Aborting."
6156
./uninstall.sh
6257
exit 1
6358
fi
6459
}
65-
6660
addUndoCommand() {
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+
}
7070
logMessage "Get secret data"
7171
getSecretValue "${SECRET_NAME}" "${AWS_REGION}"
7272
FSXN_PASSWORD="${SECRET_VALUE}"
7373
logMessage "Secret data retrieved successfully"
74-
7574
commandDescription="Install linux iSCSI packages"
7675
logMessage "${commandDescription}"
7776
yum install -y device-mapper-multipath iscsi-initiator-utils
7877
checkCommand "${commandDescription}"
7978
addUndoCommand "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"
8280
logMessage "${commandDescription}"
8381
sed -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
8482
cat /etc/iscsi/iscsid.conf | grep "node.session.timeo.replacement_timeout = 5"
8583
checkCommand "${commandDescription}"
8684
addUndoCommand "sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf;"
87-
8885
commandDescription="Start iscsi service"
8986
logMessage "${commandDescription}"
9087
systemctl enable iscsid
9188
systemctl start iscsid
9289
checkCommand "${commandDescription}"
93-
94-
# check if the service is running
90+
# check service status
9591
isIscsciServiceRunning=$(systemctl is-active --quiet iscsid.service && echo "1" || echo "0")
9692
if [ "$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
10298
fi
103-
104-
commandDescription="Set multipath configuration which allow automatic failover between yours file servers"
99+
commandDescription="Set multipath config for automatic failover"
105100
logMessage "${commandDescription}"
106101
mpathconf --enable --with_multipathd y
107102
checkCommand "${commandDescription}"
108103
addUndoCommand "mpathconf --disable"
109-
110-
# set the initiator name of your Linux host
104+
# set Linux host initiator name
111105
name=$(cat /etc/iscsi/initiatorname.iscsi)
112106
initiatorName="${name:14}"
113107
logMessage "initiatorName is: ${initiatorName}"
114-
115108
# Test connection to ONTAP
116109
logMessage "Testing connection to ONTAP."
117-
118110
versionResponse=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/cluster?fields=version")
119111
if [[ "$versionResponse" == *"version"* ]]; then
120112
logMessage "Connection to ONTAP is successful."
121113
else
122114
logMessage "Connection to ONTAP failed, aborting."
123115
./uninstall.sh
124116
fi
125-
126-
# group name should be the hostname of the linux host
117+
# group name = hostname
127118
groupName=$(hostname)
128-
129119
iGroupResult=$(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")
130120
initiatorExists=$(echo "${iGroupResult}" | jq '.num_records')
131-
132121
if [ "$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\""
159148
else
160149
logMessage "Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
@@ -165,7 +154,7 @@ if [ -z "$instance_id" ]; then
165154
instance_id="unknown"
166155
fi
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"
169158
createVolumeResult=$(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
194183
fi
195184

196-
# validate if volume was created successfully
185+
# validate volume creation
197186
volumeResult=$(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}")
198187
volumeUUid=$(echo "${volumeResult}" | jq -r '.records[] | select(.name == "'$VOLUME_NAME'" ) | .uuid')
199188
if [ -n "$volumeUUid" ]; then
@@ -204,7 +193,7 @@ else
204193
fi
205194
addUndoCommand "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)"
208197
createLunResult=$(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
}')
219208
lunResult=$(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
221210
lunUuid=$(echo "${lunResult}" | jq -r '.records[] | select(.name == "'/vol/${VOLUME_NAME}/$LUN_NAME'" ) | .uuid')
222211
if [ -n "$lunUuid" ]; then
223212
logMessage "LUN ${LUN_NAME} was created successfully with UUID: ${lunUuid}"
@@ -228,8 +217,7 @@ fi
228217

229218
addUndoCommand "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
233221
logMessage "Create a mapping from the LUN you created to the igroup you created"
234222

235223
lunMapResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/protocols/san/lun-maps" -d '{
@@ -257,7 +245,7 @@ fi
257245

258246
addUndoCommand "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
261249
getLunSerialNumberResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns?fields=serial_number")
262250
serialNumber=$(echo "${getLunSerialNumberResult}" | jq -r '.records[] | select(.name == "'/vol/$VOLUME_NAME/$LUN_NAME'" ) | .serial_number')
263251
serialHex=$(echo -n "${serialNumber}" | xxd -p)
@@ -290,10 +278,9 @@ logMessage "Getting target initiator"
290278
targetInitiator=$(iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk '{print $2}' | head -n 1)
291279
logMessage "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
297284
logMessage "Log into target initiator: ${targetInitiator}"
298285
iscsiadm --mode node -T $targetInitiator --login
299286
addUndoCommand "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
309296
logMessage "Update /etc/multipath.conf file, Assign name to block device."
310297
cp /etc/multipath.conf /etc/multipath.conf_backup
311298

@@ -324,7 +311,7 @@ fi
324311
fileContent="$(cat $CONF)"
325312
logMessage "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"
328315
logMessage "${commandDescription}"
329316
systemctl restart multipathd.service
330317
checkCommand "${commandDescription}"
@@ -351,47 +338,64 @@ if [ ! -e "/dev/mapper/$VOLUME_NAME" ]; then
351338
fi
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
356343
directory_path=mnt
357344
mount_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}"
360347
logMessage "${commandDescription}"
361348
mkdir /$directory_path/$mount_point
362349
checkCommand "${commandDescription}"
363350
addUndoCommand "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}"
367353
logMessage "${commandDescription}"
368354
mkfs.ext4 /dev/mapper/$ALIAS
369355
checkCommand "${commandDescription}"
370356

371-
commandDescription="Mount the file system using the following command."
357+
commandDescription="Mount the file system"
372358
logMessage "${commandDescription}"
373359
mount -t ext4 /dev/mapper/$ALIAS /$directory_path/$mount_point
374360
checkCommand "${commandDescription}"
375361
addUndoCommand "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"
380364
logMessage "${commandDescription}"
381365
echo "test mount iscsci" > /$directory_path/$mount_point/testIscsi.txt
382366
cat /$directory_path/$mount_point/testIscsi.txt
383367
checkCommand "${commandDescription}"
384368
rm /$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"
390373
logMessage "${commandDescription}"
391374
echo "/dev/mapper/$ALIAS /$directory_path/$mount_point ext4 defaults,_netdev 0 0" >> /etc/fstab
392375
checkCommand "${commandDescription}"
393376
addUndoCommand "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
395399
logMessage "Script completed successfully."
396400

397401
rm -f uninstall.sh

0 commit comments

Comments
 (0)