ctdb-scripts: Quote some variable expansions
[nivanova/samba-autobuild/.git] / ctdb / config / events.d / 91.lvs
1 #!/bin/sh
2 # script to manage the lvs ip multiplexer for a single public address cluster
3
4 [ -n "$CTDB_BASE" ] || \
5     CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
6
7 . "${CTDB_BASE}/functions"
8
9 loadconfig ctdb
10
11 [ -n "$CTDB_LVS_NODES" ] || exit 0
12 export CTDB_LVS_NODES
13
14 if ! type ipvsadm >/dev/null 2>&1 ; then
15         echo "LVS configured but ipvsadm not found"
16         exit 0
17 fi
18
19
20 lvs_slave_only ()
21 {
22         ctdb_get_ip_address
23
24         awk -v my_ip="$ip_address" \
25             '$1 == my_ip { if ($2 ~ "slave-only") { exit 0 } else { exit 1 } }' \
26             "$CTDB_LVS_NODES"
27 }
28
29 lvs_check_config ()
30 {
31         [ -r "$CTDB_LVS_NODES" ] || \
32                 die "error: CTDB_LVS_NODES=${CTDB_LVS_NODES} unreadable"
33         [ -n "$CTDB_LVS_PUBLIC_IP" ] || \
34                 die "Invalid configuration: CTDB_LVS_PUBLIC_IP not set"
35         if ! lvs_slave_only ; then
36                 [ -n "$CTDB_LVS_PUBLIC_IFACE" ] || \
37                         die "Invalid configuration: CTDB_LVS_PUBLIC_IFACE not set"
38         fi
39
40         if [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" = "yes" ] ; then
41                 die "Invalid configuration: CTDB_PARTIALLY_ONLINE_INTERFACES=yes incompatible with LVS"
42         fi
43 }
44
45 case "$1" in
46 setup)
47         lvs_check_config
48         ;;
49 startup)
50         lvs_check_config
51
52         ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1
53         ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1
54
55         ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo scope host
56
57         # do not respond to ARPs that are for ip addresses with scope 'host'
58         set_proc_maybe sys/net/ipv4/conf/all/arp_ignore 3
59         # do not send out arp requests from loopback addresses
60         set_proc_maybe sys/net/ipv4/conf/all/arp_announce 2
61         ;;
62
63 shutdown)
64         lvs_check_config
65
66         ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP"
67         ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP"
68
69         ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1
70
71         flush_route_cache
72         ;;
73
74 ipreallocated)
75         lvs_check_config
76
77         # Kill connections
78         ipvsadm -D -t "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1
79         ipvsadm -D -u "$CTDB_LVS_PUBLIC_IP" >/dev/null 2>&1
80         kill_tcp_connections_local_only \
81                 "$CTDB_LVS_PUBLIC_IFACE" "$CTDB_LVS_PUBLIC_IP"
82
83         ctdb_get_pnn
84         lvsmaster=$("${CTDB_HELPER_BINDIR}/ctdb_lvs" master)
85         if [ "$pnn" != "$lvsmaster" ] ; then
86             # This node is not the LVS master so change the IP address
87             # to have scope "host" so this node won't respond to ARPs
88             ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1
89             ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo scope host
90             exit 0
91         fi
92
93         # Change the scope so this node starts responding to ARPs
94         ip addr del "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1
95         ip addr add "${CTDB_LVS_PUBLIC_IP}/32" dev lo >/dev/null 2>&1
96
97         ipvsadm -A -t "$CTDB_LVS_PUBLIC_IP" -p 1999999 -s lc
98         ipvsadm -A -u "$CTDB_LVS_PUBLIC_IP" -p 1999999 -s lc
99
100         # Add all nodes (except this node) as LVS servers
101         "${CTDB_HELPER_BINDIR}/ctdb_lvs" list |
102         awk -v pnn="$pnn" '$1 != pnn { print $2 }' |
103         while read ip ; do
104                 ipvsadm -a -t "$CTDB_LVS_PUBLIC_IP" -r "$ip" -g
105                 ipvsadm -a -u "$CTDB_LVS_PUBLIC_IP" -r "$ip" -g
106         done
107
108         # Add localhost too...
109         ipvsadm -a -t "$CTDB_LVS_PUBLIC_IP" -r 127.0.0.1
110         ipvsadm -a -u "$CTDB_LVS_PUBLIC_IP" -r 127.0.0.1
111
112         $CTDB gratiousarp \
113              "$CTDB_LVS_PUBLIC_IP" "$CTDB_LVS_PUBLIC_IFACE" >/dev/null 2>&1
114
115         flush_route_cache
116         ;;
117
118 monitor)
119         lvs_check_config
120
121         if [ -n "$CTDB_LVS_PUBLIC_IFACE" ] ; then
122                 interface_monitor "$CTDB_LVS_PUBLIC_IFACE" || exit 1
123         fi
124         ;;
125 esac
126
127 exit 0