root / doc / examples / hooks / ethers @ f7b769b1
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 | 691dfed9 | Guido Trotter | DAEMON_PID_FILES="/var/run/dnsmasq.pid /var/run/dnsmasq/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 | 87a5035c | Luca Bigliardi | LOCKFILE="/var/lock/ganeti_ethers" |
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 | 87a5035c | Luca Bigliardi | $LOCKFILE_CREATE $LOCKFILE || exit 1 |
54 | 87a5035c | Luca Bigliardi | trap "$LOCKFILE_REMOVE $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 | 691dfed9 | Guido Trotter | for file in $DAEMON_PID_FILES; do |
61 | 691dfed9 | Guido Trotter | [ -f "$file" ] && kill -HUP $(< $file) |
62 | 691dfed9 | Guido Trotter | done |
63 | 93db3d8f | Guido Trotter | } |
64 | 93db3d8f | Guido Trotter | |
65 | 93db3d8f | Guido Trotter | if [ "$hooks_path" = "instance-add" -o \ |
66 | 93db3d8f | Guido Trotter | "$hooks_path" = "instance-modify" -o \ |
67 | 93db3d8f | Guido Trotter | "$hooks_path" = "instance-mirror-replace" ] |
68 | 93db3d8f | Guido Trotter | then |
69 | 93db3d8f | Guido Trotter | for i in $(seq 0 $((nic_count - 1)) ); do |
70 | 93db3d8f | Guido Trotter | bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE" |
71 | 93db3d8f | Guido Trotter | bridge=${!bridge_var} |
72 | 93db3d8f | Guido Trotter | if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then |
73 | 93db3d8f | Guido Trotter | mac_var="GANETI_INSTANCE_NIC${i}_MAC" |
74 | 93db3d8f | Guido Trotter | mac=${!mac_var} |
75 | 93db3d8f | Guido Trotter | acquire_lockfile |
76 | 93db3d8f | Guido Trotter | cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/; |
77 | 93db3d8f | Guido Trotter | END {print \"$mac\t$instance\"}" > /etc/ethers.new |
78 | 93db3d8f | Guido Trotter | update_ethers_from_new |
79 | 93db3d8f | Guido Trotter | break |
80 | 93db3d8f | Guido Trotter | fi |
81 | 93db3d8f | Guido Trotter | done |
82 | 93db3d8f | Guido Trotter | fi |
83 | 93db3d8f | Guido Trotter | if [ "$hooks_path" = "instance-remove" -o \ |
84 | 93db3d8f | Guido Trotter | \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then |
85 | 93db3d8f | Guido Trotter | acquire_lockfile |
86 | 93db3d8f | Guido Trotter | cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \ |
87 | 93db3d8f | Guido Trotter | > /etc/ethers.new |
88 | 93db3d8f | Guido Trotter | update_ethers_from_new |
89 | 93db3d8f | Guido Trotter | fi |