Statistics
| Branch: | Tag: | Revision:

root / doc / examples / hooks / ethers @ e24999ab

History | View | Annotate | Download (3.3 kB)

1 93db3d8f Guido Trotter
#!/bin/bash
2 93db3d8f Guido Trotter
3 93db3d8f Guido Trotter
# Copyright (C) 2009 Google Inc.
4 93db3d8f Guido Trotter
#
5 93db3d8f Guido Trotter
# This program is free software; you can redistribute it and/or modify
6 93db3d8f Guido Trotter
# it under the terms of the GNU General Public License as published by
7 93db3d8f Guido Trotter
# the Free Software Foundation; either version 2 of the License, or
8 93db3d8f Guido Trotter
# (at your option) any later version.
9 93db3d8f Guido Trotter
#
10 93db3d8f Guido Trotter
# This program is distributed in the hope that it will be useful, but
11 93db3d8f Guido Trotter
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 93db3d8f Guido Trotter
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 93db3d8f Guido Trotter
# General Public License for more details.
14 93db3d8f Guido Trotter
#
15 93db3d8f Guido Trotter
# You should have received a copy of the GNU General Public License
16 93db3d8f Guido Trotter
# along with this program; if not, write to the Free Software
17 93db3d8f Guido Trotter
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 93db3d8f Guido Trotter
# 02110-1301, USA.
19 93db3d8f Guido Trotter
20 93db3d8f Guido Trotter
# This is an example ganeti hook that writes the instance mac addresses in the
21 93db3d8f Guido Trotter
# node's /etc/ether file. It will pic up the first nic connected to the
22 93db3d8f Guido Trotter
# TARGET_BRIDGE bridge, and write it down with the syntax "MAC	INSTANCE_NAME".
23 93db3d8f Guido Trotter
24 93db3d8f Guido Trotter
# The hook will also send a HUP signal the daemon whose PID is in
25 93db3d8f Guido Trotter
# DAEMON_PID_FILE, so that it can load the new /etc/ethers file and use it.
26 93db3d8f Guido Trotter
# This has been tested in conjunction with dnsmasq's dhcp implementation.
27 93db3d8f Guido Trotter
28 93db3d8f Guido Trotter
# It will also remove any other occurrences for the same instance in the
29 93db3d8f Guido Trotter
# aformentioned file. This hook supports the "instance-add", "instance-modify"
30 93db3d8f Guido Trotter
# "instance-remove", and "instance-mirror-replace" ganeti post hook paths. To
31 93db3d8f Guido Trotter
# install it add a symlink from those hooks' directories to where this file is
32 93db3d8f Guido Trotter
# installed (with a mode which permits execution).
33 93db3d8f Guido Trotter
34 93db3d8f Guido Trotter
# TARGET_BRIDGE: We'll only add the first nic which gets connected to this
35 93db3d8f Guido Trotter
# bridge to /etc/ethers.
36 93db3d8f Guido Trotter
TARGET_BRIDGE="br0"
37 93db3d8f Guido Trotter
DAEMON_PID_FILE="/var/run/dnsmasq.pid"
38 e24999ab Guido Trotter
39 e24999ab Guido Trotter
# In order to handle concurrent execution of this lock, we use the $LOCKFILE.
40 e24999ab Guido Trotter
# LOCKFILE_CREATE and LOCKFILE_REMOVE are the path names for the lockfile-progs
41 e24999ab Guido Trotter
# programs which we use as helpers.
42 93db3d8f Guido Trotter
LOCKFILE="/var/lock/ganeti_ethers.lock"
43 e24999ab Guido Trotter
LOCKFILE_CREATE="/usr/bin/lockfile-create"
44 e24999ab Guido Trotter
LOCKFILE_REMOVE="/usr/bin/lockfile-remove"
45 93db3d8f Guido Trotter
46 93db3d8f Guido Trotter
hooks_path=$GANETI_HOOKS_PATH
47 93db3d8f Guido Trotter
[ -n "$hooks_path" ] || exit 1
48 93db3d8f Guido Trotter
instance=$GANETI_INSTANCE_NAME
49 93db3d8f Guido Trotter
[ -n "$instance" ] || exit 1
50 93db3d8f Guido Trotter
nic_count=$GANETI_INSTANCE_NIC_COUNT
51 93db3d8f Guido Trotter
52 93db3d8f Guido Trotter
acquire_lockfile() {
53 e24999ab Guido Trotter
  $LOCKFILE_CREATE -l $LOCKFILE || exit 1
54 e24999ab Guido Trotter
  trap "$LOCKFILE_REMOVE -l $LOCKFILE" EXIT
55 93db3d8f Guido Trotter
}
56 93db3d8f Guido Trotter
57 93db3d8f Guido Trotter
update_ethers_from_new() {
58 93db3d8f Guido Trotter
  chmod 644 /etc/ethers.new
59 93db3d8f Guido Trotter
  mv /etc/ethers.new /etc/ethers
60 93db3d8f Guido Trotter
  [ -f "$DAEMON_PID_FILE" ] && kill -HUP $(< $DAEMON_PID_FILE)
61 93db3d8f Guido Trotter
}
62 93db3d8f Guido Trotter
63 93db3d8f Guido Trotter
if [ "$hooks_path" = "instance-add" -o \
64 93db3d8f Guido Trotter
     "$hooks_path" = "instance-modify" -o \
65 93db3d8f Guido Trotter
     "$hooks_path" = "instance-mirror-replace" ]
66 93db3d8f Guido Trotter
then
67 93db3d8f Guido Trotter
  for i in $(seq 0 $((nic_count - 1)) ); do
68 93db3d8f Guido Trotter
    bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE"
69 93db3d8f Guido Trotter
    bridge=${!bridge_var}
70 93db3d8f Guido Trotter
    if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then
71 93db3d8f Guido Trotter
      mac_var="GANETI_INSTANCE_NIC${i}_MAC"
72 93db3d8f Guido Trotter
      mac=${!mac_var}
73 93db3d8f Guido Trotter
      acquire_lockfile
74 93db3d8f Guido Trotter
      cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/;
75 93db3d8f Guido Trotter
      END {print \"$mac\t$instance\"}" > /etc/ethers.new
76 93db3d8f Guido Trotter
      update_ethers_from_new
77 93db3d8f Guido Trotter
      break
78 93db3d8f Guido Trotter
    fi
79 93db3d8f Guido Trotter
  done
80 93db3d8f Guido Trotter
fi
81 93db3d8f Guido Trotter
if [ "$hooks_path" = "instance-remove" -o \
82 93db3d8f Guido Trotter
       \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then
83 93db3d8f Guido Trotter
  acquire_lockfile
84 93db3d8f Guido Trotter
  cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \
85 93db3d8f Guido Trotter
    > /etc/ethers.new
86 93db3d8f Guido Trotter
  update_ethers_from_new
87 93db3d8f Guido Trotter
fi