Skip to content

Instantly share code, notes, and snippets.

@mgarciap
Created March 19, 2013 13:27
Show Gist options
  • Save mgarciap/5196093 to your computer and use it in GitHub Desktop.
Save mgarciap/5196093 to your computer and use it in GitHub Desktop.
Script to reconfigure VCAP_SETUP after a change on eth0 IP. This will be done automatically when you put in /etc/network/if-up.d Be aware that you need to create a Ruby script following: https://gist.github.com/mgarciap/5196087
#! /bin/sh
# Reload the nats_server when an interface comes up, to allow it to start
# listening on new addresses.
# This is only enabled if the config file of the nats_server has a tracked-net-iface line
# That defines what interface its IP must be.
set -e
LOG_FILE=/var/log/nats_server.log
touch $LOG_FILE
# Don't bother to restart nats-server when lo is configured.
if [ "$IFACE" = lo ]; then
exit 0
fi
# Only run from ifup.
#if [ "$MODE" != start ]; then
# exit 0
#fi
# Is the exec ready?
if [ ! -e /etc/init.d/nats_server ]; then
exit 0
fi
# Update the host on which the nats-server is running.
# At the moment we only look after one particular interface which
# is defined in the config file of nats-server.
# Read the path of the config file: it look like this:
# DAEMON_ARGS="-d -c /home/ubuntu/cloudfoundry/.deployments/intalio_micro_cf/config/nats_server/nats_se$
NATS_CONF=`grep DAEMON_ARGS= /etc/init.d/nats_server | egrep -o ' -c [^\"]*' | cut -d' ' -f3`
# Now in that file look for the tracked interface:
# tracked-net-iface: eth0
NATS_TRACKED_IFACE=`grep tracked_net_iface: $NATS_CONF | cut -d' ' -f2`
if [ "$IFACE" != "$NATS_TRACKED_IFACE" ]; then
exit 0
fi
echo "NATS tracked interface: $NATS_TRACKED_IFACE" >> $LOG_FILE
# OK let's read the IP for this IFACE and then make sure that nats_server config file has that IP
IP=`ifconfig | sed -n '/'$IFACE'/{n;p;}' | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | head -1`
if [ -z "$IP" ]; then
#Unexpected to say the least but we don't want to replace a good IP by a blank.
echo "bue nmp IP could be read?" >> $LOG_FILE
exit 0
fi
echo "Current IP: $IP" >> $LOG_FILE
#Currently written IP: net: 192.168.1.8
NATS_IP=`grep net: $NATS_CONF | cut -d' ' -f2`
echo "Previous IP: $NATS_IP" >> $LOG_FILE
if [ "$NATS_IP" = "$IP" ]; then
#no change bye
date=$( date +"%Y-%m-%d_%H:%M:%S" )
echo "$date The tracked network interface's IP has not changed" >> $LOG_FILE
exit 0
fi
# It changed. Let's update them all:
date=$( date +"%Y-%m-%d_%H:%M:%S" )
echo "$date The tracked network interface's IP has changed from $NATS_IP to $IP. Updating all the config of the nats_server and the vcap components." >> $LOG_FILE
/home/manu/cloudfoundry/vcap/dev_setup/bin/vcap_dev_update_ip /home/manu/cloudfoundry/.deployments/devbox/config
#change the ‘listen IP’ from the postgresql DB engines
echo "change Postgresql listen IPs. $NATS_IP to $IP" >> $LOG_FILE
sed -i "s/$NATS_IP/$IP/g" /etc/postgresql/8.4/main/postgresql.conf
sed -i "s/$NATS_IP/$IP/g" /etc/postgresql/9.0/main/postgresql.conf
/etc/init.d/postgresql-8.4 restart
/etc/init.d/postgresql restart
# Make sure that the nats-server is started before we restart it.
if [ ! -f /var/run/nats_server.pid ] || \
[ "$(ps -p "$(cat /var/run/nats_server.pid)" -o comm=)" != 'nats-server' ]; then
exit 0
fi
echo " The nats_server was running. Restarting it." >> $LOG_FILE
# Let's use a force-reload.
/etc/init.d/nats_server stop || true >> $LOG_FILE
/etc/init.d/nats_server start || true >> $LOG_FILE
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment