#!/bin/bash

# Copyright (C) 2009 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This is an example ganeti hook that writes the instance mac addresses in the
# node's /etc/ether file. It will pic up the first nic connected to the
# TARGET_BRIDGE bridge, and write it down with the syntax "MAC	INSTANCE_NAME".

# The hook will also send a HUP signal the daemon whose PID is in
# DAEMON_PID_FILE, so that it can load the new /etc/ethers file and use it.
# This has been tested in conjunction with dnsmasq's dhcp implementation.

# It will also remove any other occurrences for the same instance in the
# aformentioned file. This hook supports the "instance-add", "instance-modify"
# "instance-remove", and "instance-mirror-replace" ganeti post hook paths. To
# install it add a symlink from those hooks' directories to where this file is
# installed (with a mode which permits execution).

# TARGET_BRIDGE: We'll only add the first nic which gets connected to this
# bridge to /etc/ethers.
TARGET_BRIDGE="br0"
DAEMON_PID_FILES="/var/run/dnsmasq.pid /var/run/dnsmasq/dnsmasq.pid"

# In order to handle concurrent execution of this lock, we use the $LOCKFILE.
# LOCKFILE_CREATE and LOCKFILE_REMOVE are the path names for the lockfile-progs
# programs which we use as helpers.
LOCKFILE="/var/lock/ganeti_ethers"
LOCKFILE_CREATE="/usr/bin/lockfile-create"
LOCKFILE_REMOVE="/usr/bin/lockfile-remove"

hooks_path=$GANETI_HOOKS_PATH
[ -n "$hooks_path" ] || exit 1
instance=$GANETI_INSTANCE_NAME
[ -n "$instance" ] || exit 1
nic_count=$GANETI_INSTANCE_NIC_COUNT

acquire_lockfile() {
  $LOCKFILE_CREATE $LOCKFILE || exit 1
  trap "$LOCKFILE_REMOVE $LOCKFILE" EXIT
}

update_ethers_from_new() {
  chmod 644 /etc/ethers.new
  mv /etc/ethers.new /etc/ethers
  for file in $DAEMON_PID_FILES; do
    [ -f "$file" ] && kill -HUP $(< $file)
  done
}

if [ "$hooks_path" = "instance-add" -o \
     "$hooks_path" = "instance-modify" -o \
     "$hooks_path" = "instance-mirror-replace" ]
then
  for i in $(seq 0 $((nic_count - 1)) ); do
    bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE"
    bridge=${!bridge_var}
    if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then
      mac_var="GANETI_INSTANCE_NIC${i}_MAC"
      mac=${!mac_var}
      acquire_lockfile
      cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/;
      END {print \"$mac\t$instance\"}" > /etc/ethers.new
      update_ethers_from_new
      break
    fi
  done
fi
if [ "$hooks_path" = "instance-remove" -o \
       \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then
  acquire_lockfile
  cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \
    > /etc/ethers.new
  update_ethers_from_new
fi

