| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# This script installs and configures the Serf agent that runs on |
| 4 |
# every node. As with the other scripts, this should probably be done with |
| 5 |
# formal configuration management, but a shell script is simple as well. |
| 6 |
# |
| 7 |
# The SERF_ROLE environmental variable must be passed into this script |
| 8 |
# in order to set the role of the machine. This should be either "lb" or |
| 9 |
# "web". |
| 10 |
# |
| 11 |
set -e |
| 12 |
|
| 13 |
sudo apt-get install -y unzip |
| 14 |
|
| 15 |
# Download and install Serf |
| 16 |
cd /tmp |
| 17 |
until wget -O serf.zip https://dl.bintray.com/mitchellh/serf/0.6.4_linux_amd64.zip; do |
| 18 |
sleep 1 |
| 19 |
done |
| 20 |
unzip serf.zip |
| 21 |
sudo mv serf /usr/local/bin/serf |
| 22 |
|
| 23 |
# The member join script is invoked when a member joins the Serf cluster. |
| 24 |
# Our join script simply adds the node to the load balancer. |
| 25 |
cat <<EOF >/tmp/join.sh |
| 26 |
if [ "x\${SERF_TAG_ROLE}" != "xlb" ]; then |
| 27 |
echo "Not an lb. Ignoring member join." |
| 28 |
exit 0 |
| 29 |
fi |
| 30 |
|
| 31 |
while read line; do |
| 32 |
ROLE=\`echo \$line | awk '{print \\\$3 }'\` |
| 33 |
if [ "x\${ROLE}" != "xweb" ]; then |
| 34 |
continue |
| 35 |
fi |
| 36 |
|
| 37 |
echo \$line | \\ |
| 38 |
awk '{ printf " server %s %s check\\n", \$1, \$2 }' >>/etc/haproxy/haproxy.cfg |
| 39 |
done |
| 40 |
|
| 41 |
/etc/init.d/haproxy reload |
| 42 |
EOF |
| 43 |
sudo mv /tmp/join.sh /usr/local/bin/serf_member_join.sh |
| 44 |
chmod +x /usr/local/bin/serf_member_join.sh |
| 45 |
|
| 46 |
# The member leave script is invoked when a member leaves or fails out |
| 47 |
# of the serf cluster. Our script removes the node from the load balancer. |
| 48 |
cat <<EOF >/tmp/leave.sh |
| 49 |
if [ "x\${SERF_TAG_ROLE}" != "xlb" ]; then |
| 50 |
echo "Not an lb. Ignoring member leave" |
| 51 |
exit 0 |
| 52 |
fi |
| 53 |
|
| 54 |
while read line; do |
| 55 |
NAME=\`echo \$line | awk '{print \\\$1 }'\` |
| 56 |
sed -i'' "/\${NAME} /d" /etc/haproxy/haproxy.cfg |
| 57 |
done |
| 58 |
|
| 59 |
/etc/init.d/haproxy reload |
| 60 |
EOF |
| 61 |
sudo mv /tmp/leave.sh /usr/local/bin/serf_member_left.sh |
| 62 |
chmod +x /usr/local/bin/serf_member_left.sh |
| 63 |
|
| 64 |
# Configure the agent |
| 65 |
cat <<EOF >/tmp/agent.conf |
| 66 |
description "Serf agent" |
| 67 |
|
| 68 |
start on runlevel [2345] |
| 69 |
stop on runlevel [!2345] |
| 70 |
|
| 71 |
exec /usr/local/bin/serf agent \\ |
| 72 |
-event-handler "member-join=/usr/local/bin/serf_member_join.sh" \\ |
| 73 |
-event-handler "member-leave,member-failed=/usr/local/bin/serf_member_left.sh" \\ |
| 74 |
-event-handler "query:load=uptime" \\ |
| 75 |
-tag role=${SERF_ROLE} >>/var/log/serf.log 2>&1 |
| 76 |
EOF |
| 77 |
sudo mv /tmp/agent.conf /etc/init/serf.conf |
| 78 |
|
| 79 |
# Start the agent! |
| 80 |
sudo start serf |
| 81 |
|
| 82 |
# If we're the web node, then we need to configure the join retry |
| 83 |
if [ "x${SERF_ROLE}" != "xweb" ]; then |
| 84 |
exit 0 |
| 85 |
fi |
| 86 |
|
| 87 |
cat <<EOF >/tmp/join.conf |
| 88 |
description "Join the serf cluster" |
| 89 |
|
| 90 |
start on runlevel [2345] |
| 91 |
stop on runlevel [!2345] |
| 92 |
|
| 93 |
task |
| 94 |
respawn |
| 95 |
|
| 96 |
script |
| 97 |
sleep 5 |
| 98 |
exec /usr/local/bin/serf join 10.0.0.5 |
| 99 |
end script |
| 100 |
EOF |
| 101 |
sudo mv /tmp/join.conf /etc/init/serf-join.conf |
| 102 |
sudo start serf-join |
| 103 |
|
| 104 |
cat <<EOF >/tmp/query.conf |
| 105 |
description "Query the serf cluster load" |
| 106 |
|
| 107 |
start on runlevel [2345] |
| 108 |
stop on runlevel [!2345] |
| 109 |
|
| 110 |
respawn |
| 111 |
|
| 112 |
script |
| 113 |
echo `date` I am "${HOSTNAME}<br>" > /var/www/index.html.1 |
| 114 |
serf query -no-ack load | sed 's|$|<br>|' >> /var/www/index.html.1 |
| 115 |
mv /var/www/index.html.1 /var/www/index.html |
| 116 |
sleep 10 |
| 117 |
end script |
| 118 |
EOF |
| 119 |
sudo mv /tmp/query.conf /etc/init/serf-query.conf |
| 120 |
sudo start serf-query |