#!/bin/bash # MIT No Attribution # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ( # TODO: Specify below the FSx mount point you are using FSXPATH=/fsx cd / TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") if [ "$?" -ne 0 ]; then echo "Error running 'curl' command" >&2 exit 1 fi # Periodically check for termination while sleep 5 do HTTP_CODE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s -w %{http_code} -o /dev/null http://169.254.169.254/latest/meta-data/spot/instance-action) if [[ "$HTTP_CODE" -eq 401 ]] ; then # Refreshing Authentication Token TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30") continue elif [[ "$HTTP_CODE" -ne 200 ]] ; then # If the return code is not 200, the instance is not going to be interrupted continue fi echo "Instance is getting terminated. Clean and unmount '$FSXPATH' ..." curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/spot/instance-action echo # Gracefully stop applications accessing the filesystem # # TODO*: Replace with the proper command to stop your application if possible* # Kill every process still accessing Lustre filesystem echo "Kill every process still accessing Lustre filesystem..." fuser -kMm -TERM "${FSXPATH}"; sleep 2 fuser -kMm -KILL "${FSXPATH}"; sleep 2 # Unmount FSx For Lustre filesystem if ! umount -c "${FSXPATH}"; then echo "Error unmouting '$FSXPATH'. Processes accessing it:" >&2 lsof "${FSXPATH}" echo "Retrying..." continue fi # Start a graceful shutdown of the host shutdown now done ) &