ctdb: Call out to ctdb_natgw helper from 11.natgw
authorMartin Schwenke <martin@meltin.net>
Mon, 14 Dec 2015 10:37:44 +0000 (21:37 +1100)
committerAmitay Isaacs <amitay@samba.org>
Mon, 25 Jan 2016 06:18:25 +0000 (07:18 +0100)
To keep this commit comprehensible, 11.natgw and the CTDB CLI tool are
temporarily inconsistent.  The tool will be made consistent in a
subsequent commit.

ctdb_natgw_slave_only() is reimplemented to check for the option in
the appropriate line in $CTDB_NATGW_NODES.

Update unit tests and documentation.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/config/events.d/11.natgw
ctdb/doc/ctdb.7.xml
ctdb/doc/ctdbd.conf.5.xml
ctdb/tests/eventscripts/11.natgw.041.sh
ctdb/tests/eventscripts/11.natgw.042.sh
ctdb/tests/eventscripts/stubs/ctdb
ctdb/tests/eventscripts/stubs/ctdb_natgw [new file with mode: 0755]

index 1ca446d91c826da38cd41101aab31e609396d138..32e42c9e2b797177f3905d4ddd725ec4e35d8060 100755 (executable)
@@ -23,9 +23,26 @@ natgw_cfg_new="${service_state_dir}/cfg_new"
 natgw_cfg_old="${service_state_dir}/cfg_old"
 natgw_master_old="${service_state_dir}/master_old"
 
+# Cached retrieval of private IP address from local node.  This never
+# changes.  Sets $ip_address to avoid an unnecessary subprocess.
+ctdb_get_ip_address ()
+{
+    _ip_addr_file="${service_state_dir}/my-ip-address"
+    if [ ! -f "$_ip_addr_file" ] ; then
+       ctdb -X nodestatus |
+           awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file"
+    fi
+
+    read ip_address <"$_ip_addr_file"
+}
+
 ctdb_natgw_slave_only ()
 {
-    [ "$CTDB_NATGW_SLAVE_ONLY" = "yes" ]
+    ctdb_get_ip_address
+
+    awk -v my_ip="$ip_address" \
+       '$1 == my_ip { if ($2 ~ "slave-only") { exit 0 } else { exit 1 } }' \
+       "$CTDB_NATGW_NODES"
 }
 
 natgw_check_config ()
@@ -60,7 +77,6 @@ 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
 }
 
@@ -166,7 +182,7 @@ natgw_set_slave ()
 
 natgw_ensure_master ()
 {
-    set -- $(ctdb natgwlist)
+    set -- $(ctdb_natgw master)
     natgwmaster="${1:--1}" # Default is -1 if natgwlist fails
     natgwip="$2"
 
index 45d7c23bd143ab3bc7b228fecc9a88a9eff95bde..c4d5f1c43b9c66406b6de250853584e7bdf4e7a7 100644 (file)
@@ -681,14 +681,15 @@ CTDB_NATGW_DEFAULT_GATEWAY=10.0.0.1
       <para>
        Normally any node in a NATGW group can act as the NATGW
        master.  Some configurations may have special nodes that lack
-       connectivity to a public network.  In such cases,
-       <varname>CTDB_NATGW_SLAVE_ONLY</varname> can be used to limit the
-       NATGW functionality of thos nodes.
+       connectivity to a public network.  In such cases, those nodes
+       can be flagged with the "slave-only" option in the
+       <varname>CTDB_NATGW_NODES</varname> file to limit the NATGW
+       functionality of those nodes.
       </para>
 
       <para>
        See the <citetitle>NAT GATEWAY</citetitle> section in
-       <citerefentry><refentrytitle>ctdb.conf</refentrytitle>
+       <citerefentry><refentrytitle>ctdbd.conf</refentrytitle>
        <manvolnum>5</manvolnum></citerefentry> for more details of
        NATGW configuration.
       </para>
index a8ebe5669cac96c79d6f0f4e84248d1603c262ae..5494b51ad0ab462189617d9c5e7a38b0c729ffc7 100644 (file)
            <para>
              File format:
              <screen>
-<parameter>IPADDR</parameter>
+<parameter>IPADDR</parameter> <optional>slave-only</optional>
              </screen>
            </para>
+           <para>
+             IPADDR is the private IP address of each node in the NAT
+             gateway group.
+           </para>
+           <para>
+             If "slave-only" is specified then the corresponding node
+             can not be the NAT gateway master node.  In this case
+             <varname>CTDB_NATGW_PUBLIC_IFACE</varname> and
+             <varname>CTDB_NATGW_PUBLIC_IP</varname> are optional and
+             unused.
+           </para>
            <para>
              No default, usually
              <filename>/usr/local/etc/ctdb/natgw_nodes</filename> when enabled.
          </listitem>
        </varlistentry>
 
-       <varlistentry>
-         <term>CTDB_NATGW_SLAVE_ONLY=yes|no</term>
-         <listitem>
-           <para>
-             When set to "yes" a node can not be a NAT gateway master
-             node.  In this case
-             <varname>CTDB_NATGW_PUBLIC_IFACE</varname> and
-             <varname>CTDB_NATGW_PUBLIC_IP</varname> are optional
-             and unused.
-           </para>
-           <para>
-             Default is no.
-           </para>
-         </listitem>
-       </varlistentry>
-
        <varlistentry>
          <term>CTDB_NATGW_STATIC_ROUTES=<parameter>IPADDR/MASK[@GATEWAY]</parameter> ...</term>
          <listitem>
index 22dd3929bc1fe439c97958c9290184353e5c8fbf..e8126e1b47cf3cb2fffb3a034f3a152f6035b5c5 100755 (executable)
@@ -2,18 +2,17 @@
 
 . "${TEST_SCRIPTS_DIR}/unit.sh"
 
-define_test "CTDB_NATGW_SLAVE_ONLY=yes, CTDB_NATGW_PUBLIC_IFACE unset"
+define_test "slave-only, CTDB_NATGW_PUBLIC_IFACE unset"
 
 setup_ctdb
 setup_ctdb_natgw <<EOF
-192.168.1.21
+192.168.1.21 slave-only
 192.168.1.22 master
 192.168.1.23
 192.168.1.24
 EOF
 
 CTDB_NATGW_PUBLIC_IFACE=""
-CTDB_NATGW_SLAVE_ONLY="yes"
 
 ok_null
 simple_test_event "ipreallocated"
index 9019d4a6706759135b3ce409741e4f3da0439c68..001698cbf6978c44f50fe19bc9befedd73314331 100755 (executable)
@@ -2,11 +2,11 @@
 
 . "${TEST_SCRIPTS_DIR}/unit.sh"
 
-define_test "CTDB_NATGW_SLAVE_ONLY=yes, CTDB_NATGW_PUBLIC_IP unset"
+define_test "slave-only, CTDB_NATGW_PUBLIC_IP unset"
 
 setup_ctdb
 setup_ctdb_natgw <<EOF
-192.168.1.21
+192.168.1.21 slave-only
 192.168.1.22 master
 192.168.1.23
 192.168.1.24
@@ -14,7 +14,6 @@ EOF
 
 CTDB_NATGW_PUBLIC_IFACE=""
 CTDB_NATGW_PUBLIC_IP=""
-CTDB_NATGW_SLAVE_ONLY="yes"
 
 ok_null
 simple_test_event "ipreallocated"
index 021353d3dbfb87736717ba2bc6f9e9e5451754a9..4ed99102e5607e6216ed041ae357c882c1c1f9aa 100755 (executable)
@@ -250,62 +250,17 @@ ctdb_setnatgwstate ()
     echo "$2" >"$FAKE_CTDB_NATGW_STATE"
 }
 
-ctdb_natgwlist ()
+# This is only used by the NAT gateway code at the moment, so use a
+# hack.  Assume that $CTDB_NATGW_NODES contains all nodes in the
+# cluster (which is what current tests assume).  Use the PNN to find
+# the address from this file.  The NAT gateway code only used the
+# address, so just mark the node healthy.
+ctdb_nodestatus ()
 {
-    [ -r "$CTDB_NATGW_NODES" ] || \
-       die "error: missing CTDB_NATGW_NODES=${CTDB_NATGW_NODES}"
-
-    # Determine if the current node has the (fake) NAT gateway
-    # capability.  This is only used to make sure tests are sane and
-    # don't try to use inconsistent setup.
-    if [ -r "$FAKE_CTDB_NATGW_STATE" ] ; then
-       read _state <"$FAKE_CTDB_NATGW_STATE"
-    else
-       _state="off"
-    fi
-
-    # Determine the master node
-    _master="-1 0.0.0.0"
-    _pnn=0
-    while read _ip ; do
-       if [ "$FAKE_CTDB_NATGW_MASTER" = "$_ip" ] ; then
-           _master="${_pnn} ${_ip}"
-           if [ "$_pnn" =  "$FAKE_CTDB_PNN" -a "$_state" = "off" ] ; then
-               die "Inconsistent test configuration - master node is slave-only"
-           fi
-           break
-       fi
-       _pnn=$(($_pnn + 1))
-    done <"$CTDB_NATGW_NODES"
-    echo "$_master"
-
-    # Now print the node information - it is clearer to do this in a
-    # second pass.  Any nodes before the master that have state not
-    # "off" are tagged as unhealthy, just so the output makes some
-    # sense.
-    _pnn=0
-    _found_master=false
-    while read _ip ; do
-       if [ "$FAKE_CTDB_NATGW_MASTER" = "$_ip" ] ; then
-           _found_master=true
-       fi
-       if $_found_master ; then
-           _outstate="HEALTHY"
-       else
-           if [ $FAKE_CTDB_PNN -eq $_pnn -a "$_state" = "off" ] ; then
-               _outstate="HEALTHY"
-           else
-               _outstate="UNHEALTHY"
-           fi
-       fi
-       if [ $FAKE_CTDB_PNN -eq $_pnn ] ; then
-           _outstate="${_outstate} (THIS NODE)"
-       fi
-       printf "pnn:%d %-16s ${_outstate}\n" $_pnn "$_ip"
-
-       _pnn=$(($_pnn + 1))
-    done <"$CTDB_NATGW_NODES"
-
+    echo '|Node|IP|Disconnected|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode|'
+    _line=$(( $FAKE_CTDB_PNN + 1 ))
+    _ip=$(sed -e "${_line}p" "$CTDB_NATGW_NODES")
+    echo "|${FAKE_CTDB_PNN}|${_ip}|0|0|0|0|0|0|0|Y|"
 }
 
 ######################################################################
@@ -514,7 +469,7 @@ case "$1" in
     moveip)        ctdb_moveip "$@";;
     shutdown)      ctdb_shutdown "$@";;
     setnatgwstate) ctdb_setnatgwstate "$@" ;;
-    natgwlist)     ctdb_natgwlist "$@" ;;
     setvar)       ctdb_setvar "$@" ;;
+    nodestatus)           ctdb_nodestatus "$@" ;;
     *) not_implemented "$1" ;;
 esac
diff --git a/ctdb/tests/eventscripts/stubs/ctdb_natgw b/ctdb/tests/eventscripts/stubs/ctdb_natgw
new file mode 100755 (executable)
index 0000000..96ba7ef
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+prog="ctdb_natgw"
+
+not_implemented_exit_code=1
+
+not_implemented ()
+{
+    echo "${prog}: command \"$1\" not implemented in stub" >&2
+    exit $not_implemented_exit_code
+}
+
+ctdb_natgw_master ()
+{
+    [ -r "$CTDB_NATGW_NODES" ] || \
+       die "error: missing CTDB_NATGW_NODES=${CTDB_NATGW_NODES}"
+
+    # Determine the master node
+    _master="-1 0.0.0.0"
+    _pnn=0
+    while read _ip ; do
+       if [ "$FAKE_CTDB_NATGW_MASTER" = "$_ip" ] ; then
+           _master="${_pnn} ${_ip}"
+           break
+       fi
+       _pnn=$(($_pnn + 1))
+    done <"$CTDB_NATGW_NODES"
+    echo "$_master"
+}
+
+case "$1" in
+    master) ctdb_natgw_master "$@" ;;
+    *) not_implemented "$1" ;;
+esac