summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/UNIT/eventscripts/scripts/13.per_ip_routing.sh
blob: aac2c3d835d8c75e521bb93f31dc231bd294c6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
setup()
{
	setup_public_addresses

	# shellcheck disable=SC2034
	# Used in expected output
	service_name="per_ip_routing"

	setup_script_options <<EOF
CTDB_PER_IP_ROUTING_CONF="${CTDB_BASE}/policy_routing"
CTDB_PER_IP_ROUTING_RULE_PREF=100
CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
EOF

	# Tests need to create and populate this file
	rm -f "$CTDB_PER_IP_ROUTING_CONF"
}

# Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
# $1 is the number of assigned IPs to use (<num>, all), defaulting to
# 1.  If $2 is "default" then a default route is also added.
create_policy_routing_config()
{
	_num_ips="${1:-1}"
	_should_add_default="$2"

	ctdb_get_my_public_addresses |
		if [ "$_num_ips" = "all" ]; then
			cat
		else
			{
				head -n "$_num_ips"
				cat >/dev/null
			}
		fi |
		while read -r _dev _ip _bits; do
			_net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
			_gw="${_net%.*}.254" # a dumb, calculated default

			echo "$_ip $_net"

			if [ "$_should_add_default" = "default" ]; then
				echo "$_ip 0.0.0.0/0 $_gw"
			fi
		done >"$CTDB_PER_IP_ROUTING_CONF"
}