Skip to content

Instantly share code, notes, and snippets.

@manabusakai
Last active January 2, 2016 06:49
Show Gist options
  • Save manabusakai/8266181 to your computer and use it in GitHub Desktop.
Save manabusakai/8266181 to your computer and use it in GitHub Desktop.
kentaro/serf-hosts の Shell Script 版。本家はこっちです → https://github.com/kentaro/serf-hosts
#!/bin/sh
hosts='/etc/hosts'
event=${SERF_EVENT}
read member
name=`echo ${member} | cut -d ' ' -f 1`
addr=`echo ${member} | cut -d ' ' -f 2`
role=`echo ${member} | cut -d ' ' -f 3`
if [ ! -w ${hosts} ]; then
echo "Cannot write file: ${hosts}" 1>&2
exit 1
fi
if [ ${event} = 'member-join' ]; then
if ! grep ${name} ${hosts} > /dev/null; then
echo -e "${addr}\t${name}" >> ${hosts}
fi
elif [ ${event} = 'member-leave' ] || [ ${event} = 'member-failed' ]; then
sed -i "/${name}/d" ${hosts}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment