Revert "try to restart statd everytime it fails, not just the first time"
[metze/ctdb/wip.git] / config / events.d / 11.natgw
1 #!/bin/sh
2 # Script to set up one of the nodes as a NAT gateway for all other nodes.
3 # This is used to ensure that all nodes in the cluster can still originate
4 # traffic to the external network even if there are no public addresses
5 # available.
6 #
7
8 . $CTDB_BASE/functions
9 loadconfig ctdb
10
11 [ -z "$CTDB_NATGW_PUBLIC_IFACE" ] && exit 0
12
13 cmd="$1"
14 shift
15 PATH=/usr/bin:/bin:/usr/sbin:/sbin:$PATH
16
17
18 delete_all() {
19         remove_ip $CTDB_NATGW_PUBLIC_IP $CTDB_NATGW_PUBLIC_IFACE
20         remove_ip $CTDB_NATGW_PUBLIC_IP_HOST lo
21
22         ip route del 0.0.0.0/0 metric 10 >/dev/null 2>/dev/null
23
24         # Delete the masquerading setup from a previous iteration where we
25         # were the NAT-GW
26         iptables -D POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK -d ! $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE >/dev/null 2>/dev/null
27
28 }
29
30 case $cmd in 
31      startup)
32         # do not respond to ARPs that are for ip addresses with scope 'host'
33         echo 3 > /proc/sys/net/ipv4/conf/all/arp_ignore
34         # do not send out arp requests from loopback addresses
35         echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
36         # update capabilities to show we are using natgw
37         ctdb setnatgwstate on
38         ;;
39
40      recovered)
41         MYPNN=`ctdb pnn | cut -d: -f2`
42         NATGWMASTER=`ctdb natgwlist | head -1 | sed -e "s/ .*//"`
43         NATGWIP=`ctdb natgwlist | head -1 | sed -e "s/^[^ ]* *//"`
44
45         CTDB_NATGW_PUBLIC_IP_HOST=`echo $CTDB_NATGW_PUBLIC_IP | sed -e "s/\/.*/\/32/"`
46         if [ "$NATGWMASTER" == "-1" ]; then
47                 echo "There is not NATGW master node"
48                 exit 1
49         fi
50
51         delete_all
52
53         if [ "$MYPNN" = "$NATGWMASTER" ]; then
54                 # This is the first node, set it up as the NAT GW
55                 echo 1 >/proc/sys/net/ipv4/ip_forward
56                 iptables -A POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK -d ! $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE
57                 ip addr add $CTDB_NATGW_PUBLIC_IP dev $CTDB_NATGW_PUBLIC_IFACE
58                 ip route add 0.0.0.0/0 via $CTDB_NATGW_DEFAULT_GATEWAY >/dev/null 2>/dev/null
59         else
60                 # This is not the NAT-GW
61                 # Assign the public ip to the private interface and make
62                 # sure we dont respond to ARPs.
63                 # We do this so that the ip address will exist on a
64                 # non-loopback interface so that samba may send it along in the
65                 # KDC requests.
66                 ip addr add $CTDB_NATGW_PUBLIC_IP_HOST dev lo scope host
67                 ip route add 0.0.0.0/0 via $NATGWIP metric 10
68         fi
69
70         # flush our route cache
71         echo 1 > /proc/sys/net/ipv4/route/flush
72         ;;
73
74      shutdown)
75         delete_all
76         ;;
77
78 esac
79
80 exit 0