Last active
October 1, 2018 10:00
-
-
Save erszcz/1d7b2f84b9d309d7845b2221463314a2 to your computer and use it in GitHub Desktop.
Sequential Erlang node name based on EPMD entries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# file: node-name.sh | |
set -eu | |
PREFIX=history | |
MAXNODES=100 | |
DEFAULT=${PREFIX}_$(uuidgen | sed 's/-//g') | |
TEMP=$(mktemp) | |
REGISTERED=$TEMP.registered | |
SEQUENTIAL=$TEMP.sequential | |
# Generate potential names | |
for n in $(seq 1 $MAXNODES); do | |
echo ${PREFIX}_$n | |
done > $SEQUENTIAL | |
# Filter out existing names and take the first free one | |
NODENAME= | |
if epmd -names 2>/dev/null | grep ${PREFIX} | cut -d' ' -f2 > $REGISTERED; then | |
# fgrep -f requires a non-empty pattern file | |
echo ${PREFIX}_$(expr $MAXNODES + 1) >> $REGISTERED | |
NODENAME=$(fgrep -f $REGISTERED -v $SEQUENTIAL | head -1) | |
fi | |
# Did we get anything useful? If not, let's use the default UUID-based node name | |
if [ x"$NODENAME" == x"" ]; then | |
NODENAME=$DEFAULT | |
fi | |
echo ${NODENAME}@${HOSTNAME} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment