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