export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")
. $CTDB_BASE/functions
+
+service_name=natgw
+
loadconfig
[ -n "$CTDB_NATGW_NODES" ] || exit 0
export CTDB_NATGW_NODES
+ctdb_setup_service_state_dir
+
+natgw_cfg_new="${service_state_dir}/cfg_new"
+natgw_cfg_old="${service_state_dir}/cfg_old"
+natgw_master_old="${service_state_dir}/master_old"
+
natgw_check_config ()
{
[ -r "$CTDB_NATGW_NODES" ] || \
[ -n "$CTDB_NATGW_STATIC_ROUTES" ] || CTDB_NATGW_STATIC_ROUTES="0.0.0.0/0"
}
+natgw_write_config ()
+{
+ _f="$1"
+
+ cat >"$_f" <<EOF
+CTDB_NATGW_NODES="$CTDB_NATGW_NODES"
+CTDB_NATGW_PUBLIC_IP="$CTDB_NATGW_PUBLIC_IP"
+CTDB_NATGW_PUBLIC_IFACE="$CTDB_NATGW_PUBLIC_IFACE"
+CTDB_NATGW_DEFAULT_GATEWAY="$CTDB_NATGW_DEFAULT_GATEWAY"
+CTDB_NATGW_PRIVATE_NETWORK="$CTDB_NATGW_PRIVATE_NETWORK"
+CTDB_NATGW_STATIC_ROUTES="$CTDB_NATGW_STATIC_ROUTES"
+CTDB_NATGW_SLAVE_ONLY="$CTDB_NATGW_SLAVE_ONLY"
+EOF
+}
+
+natgw_config_has_changed ()
+{
+ natgw_write_config "$natgw_cfg_new"
+
+ # Non-existent old returns true, no log message
+ if [ ! -f "$natgw_cfg_old" ] ; then
+ return 0
+ fi
+
+ # Handle no change
+ if cmp "$natgw_cfg_old" "$natgw_cfg_new" >/dev/null 2>&1 ; then
+ return 1
+ fi
+
+ echo "NAT gateway configuration has changed"
+ return 0
+}
+
natgw_set_capability ()
{
# Set NATGW capability depending on configuration
fi
}
-natgw_clear ()
+_natgw_clear ()
{
_ip="${CTDB_NATGW_PUBLIC_IP%/*}"
_maskbits="${CTDB_NATGW_PUBLIC_IP#*/}"
iptables -D INPUT -p tcp --syn -d $_ip/32 -j REJECT 2>/dev/null
}
+natgw_clear ()
+{
+ if [ -r "$natgw_cfg_old" ] ; then
+ (. $natgw_cfg_old ; _natgw_clear)
+ else
+ _natgw_clear
+ fi
+}
+
natgw_set_master ()
{
set_proc sys/net/ipv4/ip_forward 1
fi
}
+natgw_master_has_changed ()
+{
+ if [ -r "$natgw_master_old" ] ; then
+ read _old_natgwmaster <"$natgw_master_old"
+ else
+ _old_natgwmaster=""
+ fi
+ [ "$_old_natgwmaster" != "$natgwmaster" ]
+}
+
+natgw_save_state ()
+{
+ echo "$natgwmaster" >"$natgw_master_old"
+ # Created by natgw_config_has_changed()
+ mv "$natgw_cfg_new" "$natgw_cfg_old"
+}
+
+
case "$1" in
setup)
natgw_check_config
natgw_set_capability
natgw_ensure_master
+ natgw_config_has_changed || natgw_master_has_changed || exit 0
+
natgw_clear
if [ "$mypnn" = "$natgwmaster" ]; then
# flush our route cache
set_proc sys/net/ipv4/route/flush 1
+
+ # Only update saved state when NATGW successfully updated
+ natgw_save_state
;;
shutdown|removenatgw)
192.168.1.24
EOF
-ok_null
+ok "NAT gateway configuration has changed"
simple_test_event "ipreallocated"
ok "default via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
192.168.1.24
EOF
-ok_null
+ok "NAT gateway configuration has changed"
simple_test_event "ipreallocated"
ok "default via ${CTDB_NATGW_DEFAULT_GATEWAY} dev ethXXX metric 10 "
192.168.1.24
EOF
-ok_null
+ok "NAT gateway configuration has changed"
simple_test_event "ipreallocated"
ok_natgw_slave_static_routes
192.168.1.24
EOF
-ok_null
+ok "NAT gateway configuration has changed"
simple_test_event "ipreallocated"
ok_natgw_master_static_routes
--- /dev/null
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "master node, static routes, custom gateway, config change"
+
+setup_ctdb
+
+export CTDB_NATGW_STATIC_ROUTES="10.1.1.0/24 10.1.2.0/24@10.1.1.253"
+
+echo "##################################################"
+echo "Static routes..."
+
+setup_ctdb_natgw <<EOF
+192.168.1.21 master
+192.168.1.22
+192.168.1.23
+192.168.1.24
+EOF
+
+ok_null
+simple_test_event "ipreallocated"
+
+ok_natgw_master_static_routes
+simple_test_command ip route show
+
+ok_natgw_master_ip_addr_show
+simple_test_command ip addr show "$CTDB_NATGW_PUBLIC_IFACE"
+
+echo "##################################################"
+echo "Default routes..."
+
+unset CTDB_NATGW_STATIC_ROUTES
+
+ok "NAT gateway configuration has changed"
+simple_test_event "ipreallocated"
+
+ok "default via ${CTDB_NATGW_DEFAULT_GATEWAY} dev ethXXX metric 10 "
+simple_test_command ip route show
+
+ok_natgw_master_ip_addr_show
+simple_test_command ip addr show "$CTDB_NATGW_PUBLIC_IFACE"
+
+echo "##################################################"
+echo "Static routes again..."
+
+export CTDB_NATGW_STATIC_ROUTES="10.1.3.0/24 10.1.4.4/32 10.1.2.0/24@10.1.1.252"
+
+ok "NAT gateway configuration has changed"
+simple_test_event "ipreallocated"
+
+ok_natgw_master_static_routes
+simple_test_command ip route show
+
+ok_natgw_master_ip_addr_show
+simple_test_command ip addr show "$CTDB_NATGW_PUBLIC_IFACE"
_t="${_t}${_t:+${_nl}}"
_t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
done
+ _t=$(echo "$_t" | sort)
ok "$_t"
}
_t="${_t}${_t:+${_nl}}"
_t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
done
+ _t=$(echo "$_t" | sort)
ok "$_t"
}
ip_route_common "$@"
# Missing file is just an empty table
- cat "$FAKE_IP_STATE/routes/${_table}" 2>/dev/null || true
+ sort "$FAKE_IP_STATE/routes/${_table}" 2>/dev/null || true
}
ip_route_flush ()