Skip to content

Instantly share code, notes, and snippets.

@tcooper
Created May 9, 2015 20:51
Show Gist options
  • Save tcooper/fe1d039e45f2102d728e to your computer and use it in GitHub Desktop.
Save tcooper/fe1d039e45f2102d728e to your computer and use it in GitHub Desktop.
Remove a single node of 'matchable' Slurm nodelist from a Slurm reservation with SPEC_NODES specified
#!/bin/sh
EXPECTED_ARGS=2
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` {reservationname} {nodelist}"
exit $E_BADARGS
fi
RESERVATION_NAME=$1
NODELIST_TO_REMOVE=$2
if [ ${#NODELIST_TO_REMOVE} -lt 11 ]
then
echo "{nodelist} must contain at least one node"
exit $E_BADARGS
fi
LOCKDIR=/tmp/${RESERVATION_NAME}-mutex
if mkdir "${LOCKDIR}" &>/dev/null; then
RESERVATION_NAME_ESCAPED=$(echo $RESERVATION_NAME | sed 's/\-/\\-/g')
RES_NODELIST=$(sinfo -T | egrep "^${RESERVATION_NAME_ESCAPED} " | awk '{print $6}')
echo "NODELIST_TO_MODIFY = $RES_NODELIST"
echo "NODELIST_TO_REMOVE = $NODELIST_TO_REMOVE"
# If the nodelist in the reservation is changed by another process
# at this point on we'll screw things up...
#/bin/sleep 10
NODELIST_TO_REMOVE_ESCAPED=$(echo $NODELIST_TO_REMOVE | sed 's/\[/\\[/g;s/\]/\\]/g')
RES_NODELIST_BY_NODELIST=$(echo $RES_NODELIST | /bin/sed 's/,c/ c/g')
COMPOUND_NODELIST=""
this_nl=$(mktemp)
for CURRENT_NODELIST in ${RES_NODELIST_BY_NODELIST}; do
THIS_NODELIST=$(/usr/bin/scontrol show hostnames ${CURRENT_NODELIST} | sed 's/,c/\nc/g' | egrep -v ${NODELIST_TO_REMOVE_ESCAPED} > $this_nl; /usr/bin/scontrol show hostlistsorted ${this_nl})
COMPOUND_NODELIST="${COMPOUND_NODELIST},${THIS_NODELIST}"
done
NEW_NODELIST=$(echo ${COMPOUND_NODELIST} | /usr/bin/tr -s ',' ',' | /bin/sed 's/^,//g;s/,$//g')
/bin/rm ${this_nl}
echo "NEW_NODELIST = ${NEW_NODELIST}"
/usr/bin/scontrol update reservationname=$RESERVATION_NAME Nodes=${NEW_NODELIST}
/bin/rmdir ${LOCKDIR}
else
# If we didn't get the lock what should we do...
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment