Skip to content

Commit ea3d5a0

Browse files
authored
Merge pull request #254 from NetApp/EC2-script-use-rest-call
Create EC2 and mount it to FSxN volume using rest call
2 parents 0084e85 + 9c9438d commit ea3d5a0

File tree

2 files changed

+291
-216
lines changed

2 files changed

+291
-216
lines changed

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

Lines changed: 134 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ LUN_NAME=${VOLUME_NAME}_$(($RANDOM%($max-$min+1)+$min))
2323
# defaults
2424
# The script will create a log file in the ec2-user home directory
2525
LOG_FILE=/home/ec2-user/install.log
26+
TIMEOUT=5
2627

2728
LUN_SIZE=$(bc -l <<< "0.90*$VOLUME_SIZE" )
2829

@@ -91,7 +92,6 @@ if [ "$isIscsciServiceRunning" -eq 1 ]; then
9192
addUndoCommand "systemctl --now disable iscsid.service"
9293
else
9394
logMessage "iscsi service is not running, aborting"
94-
# now we have to rollback and exit
9595
./uninstall.sh
9696
fi
9797

@@ -106,94 +106,165 @@ name=$(cat /etc/iscsi/initiatorname.iscsi)
106106
initiatorName="${name:14}"
107107
logMessage "initiatorName is: ${initiatorName}"
108108

109-
# Configure iSCSI on the FSx for ONTAP file system
110-
commandDescription="Install sshpass which will allow to connect FSXn using SSH"
111-
logMessage "${commandDescription}"
112-
yum install -y sshpass
113-
checkCommand "${commandDescription}"
114-
addUndoCommand "yum remove -y sshpass"
115-
116109
# Test connection to ONTAP
117-
commandDescription="Testing connection to ONTAP."
118-
logMessage "${commandDescription}"
119-
sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "version"
120-
checkCommand "${commandDescription}"
110+
logMessage "Testing connection to ONTAP."
111+
112+
versionResponse=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/cluster?fields=version")
113+
if [[ "$versionResponse" == *"version"* ]]; then
114+
logMessage "Connection to ONTAP is successful."
115+
else
116+
logMessage "Connection to ONTAP failed, aborting."
117+
./uninstall.sh
118+
fi
121119

122120
# group name should be the hostname of the linux host
123121
groupName=$(hostname)
124122

125-
commandDescription="Create initiator group for vserver: ${SVM_NAME} group name: ${groupName} and intiator name: ${initiatorName}"
123+
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")
124+
initiatorExists=$(echo "${iGroupResult}" | jq '.num_records')
126125

127-
lunGroupresult=$(sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup show -vserver $SVM_NAME -igroup $groupName -initiator $initiatorName -protocol iscsi -ostype linux")
128-
if [[ "$lunGroupresult" == *"There are no entries matching your query."* ]]; then
126+
if [ "$initiatorExists" -eq 0 ]; then
129127
logMessage "Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
130-
logMessage "${commandDescription}"
131-
sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup create -vserver $SVM_NAME -igroup $groupName -initiator $initiatorName -protocol iscsi -ostype linux"
132-
checkCommand "${commandDescription}"
133-
addUndoCommand "sshpass -p \"${FSXN_PASSWORD}\" ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun igroup delete -vserver ${SVM_NAME} -igroup ${groupName} -force"
128+
logMessage "Create initiator group for vserver: ${SVM_NAME} group name: ${groupName} and intiator name: ${initiatorName}"
129+
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 '{
130+
"protocol": "iscsi",
131+
"initiators": [
132+
{
133+
"name": "'$initiatorName'"
134+
}
135+
],
136+
"os_type": "linux",
137+
"name": "'$groupName'",
138+
"svm": {
139+
"name": "'$SVM_NAME'"
140+
}
141+
}')
142+
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")
143+
iGroupUuid=$(echo ${iGroupResult} | jq -r '.records[] | select(.name == "'$groupName'" ) | .uuid')
144+
# Check if iGroup was created successfully
145+
if [ -n "$iGroupUuid" ]; then
146+
logMessage "Initiator group ${groupName} was created successfully with UUID: ${iGroupUuid}"
147+
else
148+
logMessage "Initiator group ${groupName} was not created, aborting"
149+
./uninstall.sh
150+
fi
151+
# Add undo command for iGroup creation
152+
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/protocols/san/igroups/$iGroupUuid\""
134153
else
135154
logMessage "Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
136155
fi
137156

138-
# confirm that igroup was created
139-
isInitiatorGroupCreadted=$(sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup show -igroup $groupName -protocol iscsi" | grep $groupName | wc -l)
140-
if [ "$isInitiatorGroupCreadted" -eq 1 ]; then
141-
logMessage "Initiator group ${groupName} was created"
142-
else
143-
logMessage "Initiator group ${groupName} was not created, aborting"
144-
# now we have to rollback and exit
157+
logMessage "Create volume for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and size: ${VOLUME_SIZE}g"
158+
createVolumeResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/volumes" -d '{
159+
"name": "'$VOLUME_NAME'",
160+
"size": "'$VOLUME_SIZE'g",
161+
"state": "online",
162+
"svm": {
163+
"name": "'$SVM_NAME'"
164+
},
165+
"aggregates": [{
166+
"name": "aggr1"
167+
}]
168+
}')
169+
sleep 5
170+
jobId=$(echo "${createVolumeResult}" | jq -r '.job.uuid')
171+
jobStatus=$(curl -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/cluster/jobs/$jobId")
172+
jobState=$(echo "$jobStatus" | jq -r '.state')
173+
if [ "$jobState" != "success" ]; then
174+
logMessage "Volume creation job did not complete successfully, aborting"
175+
jobError=$(echo "$jobStatus" | jq -r '.error')
176+
logMessage "Error details: $jobError"
145177
./uninstall.sh
146178
fi
147179

148-
commandDescription="Create volume for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and size: ${VOLUME_SIZE}g"
149-
logMessage "${commandDescription}"
150-
sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "volume create -vserver ${SVM_NAME} -volume ${VOLUME_NAME} -aggregate aggr1 -size ${VOLUME_SIZE}g -state online"
151-
checkCommand "${commandDescription}"
152-
addUndoCommand "sshpass -p \"$FSXN_PASSWORD\" ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} volume delete -vserver ${SVM_NAME} -volume ${VOLUME_NAME} -force"
180+
# validate if volume was created successfully
181+
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}")
182+
volumeUUid=$(echo "${volumeResult}" | jq -r '.records[] | select(.name == "'$VOLUME_NAME'" ) | .uuid')
183+
if [ -n "$volumeUUid" ]; then
184+
logMessage "Volume ${VOLUME_NAME} was created successfully with UUID: ${volumeUUid}"
185+
else
186+
logMessage "Volume ${VOLUME_NAME} was not created, aborting"
187+
./uninstall.sh
188+
fi
189+
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/storage/volumes/${volumeUUid}\""
190+
191+
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"
192+
createLunResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns" -d '{
193+
"name": "'/vol/${VOLUME_NAME}/$LUN_NAME'",
194+
"space": {
195+
"size": "'$LUN_SIZE'GB",
196+
"scsi_thin_provisioning_support_enabled": true
197+
},
198+
"svm": {
199+
"name": "'$SVM_NAME'"
200+
},
201+
"os_type": "linux"
202+
}')
203+
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")
204+
# Validate if LUN was created successfully
205+
lunUuid=$(echo "${lunResult}" | jq -r '.records[] | select(.name == "'/vol/${VOLUME_NAME}/$LUN_NAME'" ) | .uuid')
206+
if [ -n "$lunUuid" ]; then
207+
logMessage "LUN ${LUN_NAME} was created successfully with UUID: ${lunUuid}"
208+
else
209+
logMessage "LUN ${LUN_NAME} was not created, aborting"
210+
./uninstall.sh
211+
fi
153212

154-
commandDescription="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"
155-
logMessage "${commandDescription}"
156-
sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun create -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/$LUN_NAME -size ${LUN_SIZE}g -ostype linux -space-allocation enabled"
157-
checkCommand "${commandDescription}"
158-
addUndoCommand "sshpass -p \"$FSXN_PASSWORD\" ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun delete -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/${LUN_NAME} -force"
213+
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/storage/luns/${lunUuid}\""
159214

160-
# Create a mapping from the LUN you created to the igroup you created
161215
# The LUN ID integer is specific to the mapping, not to the LUN itself.
162-
# This is used by the initiators in the igroup as the Logical Unit Number use this value for the initiator when accessing the storage.
163-
commandDescription="Create a mapping from the LUN you created to the igroup you created"
164-
logMessage "${commandDescription}"
165-
lun_id=0
166-
sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun mapping create -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/${LUN_NAME} -igroup ${groupName} -lun-id 0"
167-
checkCommand "${commandDescription}"
168-
169-
commandDescription="Validate the lun mapping was created"
170-
logMessage "${commandDescription}"
171-
serialHex=$(sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun show -path /vol/${VOLUME_NAME}/${LUN_NAME} -fields state,mapped,serial-hex" | grep $SVM_NAME | awk '{print $3}')
172-
if [ -n "$serialHex" ]; then
173-
logMessage "Lun mapping was created"
216+
# This is used by the initiators in the igroup as the Logical Unit Number. Use this value for the initiator when accessing the storage.
217+
logMessage "Create a mapping from the LUN you created to the igroup you created"
218+
219+
lunMapResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/protocols/san/lun-maps" -d '{
220+
"lun": {
221+
"name": "/vol/'${VOLUME_NAME}'/'${LUN_NAME}'"
222+
},
223+
"igroup": {
224+
"name": "'${groupName}'"
225+
},
226+
"svm": {
227+
"name": "'${SVM_NAME}'"
228+
},
229+
"logical_unit_number": 0
230+
}')
231+
logMessage "Validate the lun mapping was created"
232+
233+
getLunMap=$(curl -m $TIMEOUT -X GET -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}")
234+
lunGroupCreated=$(echo "${getLunMap}" | jq -r '.num_records')
235+
if [ "$lunGroupCreated" -eq 0 ]; then
236+
logMessage "LUN mapping was not created, aborting"
237+
./uninstall.sh
174238
else
175-
logMessage "Lun mapping was not created, aborting"
176-
addUndoCommand "sshpass -p \"${FSXN_PASSWORD}\" ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun mapping delete -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/${LUN_NAME} -igroup ${groupName}"
239+
logMessage "LUN mapping was created successfully"
177240
fi
178241

179-
# The serail hex in needed for creating readable name for the block device.
180-
commandDescription="Get the iscsi interface addresses for the svm ${SVM_NAME}"
181-
logMessage "${commandDescription}"
182-
iscsi1IP=$(sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "network interface show -vserver ${SVM_NAME}" | grep -e iscsi_1 | awk '{print $3}')
183-
iscsi2IP=$(sshpass -p "$FSXN_PASSWORD" ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "network interface show -vserver ${SVM_NAME}" | grep -e iscsi_2 | awk '{print $3}')
242+
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}\""
184243

185-
if [ -n "$i$iscsi1IP" ] && [ -n "$iscsi2IP" ]; then
244+
# The serial hex in needed for creating readable name for the block device.
245+
getLunSerialNumberResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns?fields=serial_number")
246+
serialNumber=$(echo "${getLunSerialNumberResult}" | jq -r '.records[] | select(.name == "'/vol/$VOLUME_NAME/$LUN_NAME'" ) | .serial_number')
247+
serialHex=$(echo -n "${serialNumber}" | xxd -p)
248+
if [ -z "$serialHex" ]; then
249+
logMessage "Serial number for the LUN is not available, aborting"
250+
./uninstall.sh
251+
fi
252+
253+
logMessage "Get the iscsi interface addresses for the svm ${SVM_NAME}"
254+
getInterfacesResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/network/ip/interfaces?svm.name=$SVM_NAME&fields=ip")
255+
iscsi1IP=$(echo "$getInterfacesResult" | jq -r '.records[] | select(.name == "iscsi_1") | .ip.address')
256+
iscsi2IP=$(echo "$getInterfacesResult" | jq -r '.records[] | select(.name == "iscsi_2") | .ip.address')
257+
258+
if [ -n "$iscsi1IP" ] && [ -n "$iscsi2IP" ]; then
186259
iscsi1IP=$(echo ${iscsi1IP%/*})
187260
iscsi2IP=$(echo ${iscsi2IP%/*})
188261
logMessage "iscsi interface addresses for the svm ${SVM_NAME} are: ${iscsi1IP} and ${iscsi2IP}"
189262
else
190263
logMessage "iscsi interface addresses for the svm ${SVM_NAME} are not available, aborting"
191-
# now we have to rollback and exit
192264
./uninstall.sh
193265
fi
194266

195-
commandDescription="Discover the target iSCSI nodes, iscsi IP: ${iscsi1IP}"
196-
logMessage "${commandDescription}"
267+
logMessage "Discover the target iSCSI nodes, iscsi IP: ${iscsi1IP}"
197268
iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP
198269
checkCommand "${commandDescription}"
199270
addUndoCommand "iscsiadm --mode discovery --op delete --type sendtargets --portal ${iscsi1IP}"
@@ -219,8 +290,7 @@ addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
219290
# }
220291
# }
221292
# Assign name to block device, this should be function that will get serial hex and device name
222-
commandDescription="Update /etc/multipath.conf file, Assign name to block device."
223-
logMessage "${commandDescription}"
293+
logMessage "Update /etc/multipath.conf file, Assign name to block device."
224294
cp /etc/multipath.conf /etc/multipath.conf_backup
225295

226296
SERIAL_HEX=$serialHex
@@ -266,7 +336,6 @@ fi
266336

267337
# Partition the LUN
268338
# mount the LUN on the Linux client
269-
270339
# Create a directory directory_path as the mount point for your file system.
271340
directory_path=mnt
272341
mount_point=$VOLUME_NAME
@@ -277,8 +346,7 @@ mkdir /$directory_path/$mount_point
277346
checkCommand "${commandDescription}"
278347
addUndoCommand "rm -rf /$directory_path/$mount_point"
279348

280-
#check this command
281-
# volume_name=the friendly device name as we set it in the multipath.conf file
349+
# volume_name = the friendly device name as we set it in the multipath.conf file
282350
commandDescription="Creating the file system for the new partition: /dev/mapper/${ALIAS}"
283351
logMessage "${commandDescription}"
284352
mkfs.ext4 /dev/mapper/$ALIAS
@@ -310,5 +378,4 @@ addUndoCommand "sed -i '/\/dev\/mapper\/$ALIAS \/mnt\/$mount_point ext4 defaults
310378
# End of script
311379
logMessage "Script completed successfully."
312380

313-
314-
rm -f uninstall.sh
381+
rm -f uninstall.sh

0 commit comments

Comments
 (0)