From 9f54b8f12818b0753dd7c35ad9b5540b135dcbf8 Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii <ishii@postgresql.org>
Date: Wed, 20 Aug 2025 15:08:50 +0900
Subject: [PATCH v1] Feature: allow to specify VIP in watchdog_setup.

This commit allows to specify VIP parameter (delegate_ip) in
watchdog_setup using new option "-vip [ip]". If ip is omitted,
'127.0.0.1' is assumed. Even if vip option is specified, pgpool will
not actually set the VIP to the system: if_up_cmd, if_down_cmd are
just set to echo command and do nothing except emit a log. This option
is intended to trace the action of Pgpool-II regarding VIP handling.
---
 doc.ja/src/sgml/ref/watchdog_setup.sgml | 18 +++++++++++++
 doc/src/sgml/ref/watchdog_setup.sgml    | 23 +++++++++++++++-
 src/test/watchdog_setup.in              | 35 +++++++++++++++++++++----
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/doc.ja/src/sgml/ref/watchdog_setup.sgml b/doc.ja/src/sgml/ref/watchdog_setup.sgml
index 0a51eda28..9bf5d785e 100644
--- a/doc.ja/src/sgml/ref/watchdog_setup.sgml
+++ b/doc.ja/src/sgml/ref/watchdog_setup.sgml
@@ -160,6 +160,24 @@ Pgpool-II documentation
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><option>-vip [<replaceable class="parameter">ip</replaceable>]</option></term>
+     <listitem>
+      <para>
+       仮想IPを指定します。
+       ipを省略した場合は、'127.0.0.1'を指定したものと見なされます。
+       ここで指定した仮想IPは、<xref linkend="guc-delegate-ip">パラメータに設定されます。
+       仮想IPを指定しても、実際に仮想IPがシステムに設定されるわけではなく、単に<xref linkend="PCP-WATCHDOG-INFO">や、ログファイルで仮想IPを設定する動作が確認できるだけですが、<productname>Pgpool-II</productname>の仮想IPに関する挙動をテストするのに役立ちます。
+       また、以下のパラメータも設定されます。
+	<programlisting>
+	 if_up_cmd = '/usr/bin/echo "if_up_cmd executed"'
+	 if_down_cmd = 'usr/bin/echo "if_down_cmd executed"'
+	 arping_cmd = '/usr/bin/true'
+	</programlisting>
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><option>--no-stop</option></term>
      <listitem>
diff --git a/doc/src/sgml/ref/watchdog_setup.sgml b/doc/src/sgml/ref/watchdog_setup.sgml
index 18f27958b..76d7f2f03 100644
--- a/doc/src/sgml/ref/watchdog_setup.sgml
+++ b/doc/src/sgml/ref/watchdog_setup.sgml
@@ -42,7 +42,6 @@ Pgpool-II documentation
   <para>
    <application>watchdog_setup</application> is for testing purpose
    only and should not be used to create production installations.
-   Also please note that heartbeat is not used.
   </para>
 
   <para>
@@ -112,6 +111,28 @@ Pgpool-II documentation
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><option>-vip [<replaceable class="parameter">ip</replaceable>]</option></term>
+     <listitem>
+      <para>
+       Specify the virtual IP. If ip is omitted, it is assumed that
+       '127.0.0.1' is specified.  The specified virtual IP is set
+       to <xref linkend="guc-delegate-ip"> parameter.  Even if virtual
+       IP is specified, it will not be set to the system: just you
+       could confirm the action trying to set the virtual IP
+       in <xref linkend="PCP-WATCHDOG-INFO">, or the log file, yet
+       this is useful how <productname>Pgpool-II</productname> reacts
+       trying to set the virtual IP. In additioin to delegate_ip,
+       following parameters will be set too.
+	<programlisting>
+	 if_up_cmd = '/usr/bin/echo "if_up_cmd executed"'
+	 if_down_cmd = 'usr/bin/echo "if_down_cmd executed"'
+	 arping_cmd = '/usr/bin/true'
+	</programlisting>
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><option>--no-stop</option></term>
      <listitem>
diff --git a/src/test/watchdog_setup.in b/src/test/watchdog_setup.in
index d84a5b619..dfa362471 100644
--- a/src/test/watchdog_setup.in
+++ b/src/test/watchdog_setup.in
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 #
-# Copyright (c) 2013-2021 PgPool Global Development Group
+# Copyright (c) 2013-2025 PgPool Global Development Group
 #
 # Permission to use, copy, modify, and distribute this software and
 # its documentation for any purpose and without fee is hereby
@@ -18,10 +18,11 @@
 # this tool for production environment!
 # Note that this script uses pgpool_setup as a work horse.
 #
-# usage: watchdog_setup [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n|i][-n num_clusters][-p base_port][--no-stop][-d]
+# usage: watchdog_setup [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n|i][-n num_clusters][-p base_port][--no-stop][-vip [ip]][-d]
 # -wn num_pgpool: create num_pgpool pgpool nodes. The default is 3. Must be greater than 1.
 # -wp watchdog_base_port: starting port number. The default is
 #  50000.
+# -vip [ip]: allow to assign "ip" being set to delegate_ip. If "ip" is omitted, "127.0.0.1" is assumed.
 #-------------------------------------------
 # Configuration section
 #-------------------------------------------
@@ -40,6 +41,9 @@ export PGBASEPORT=`expr $W_BASE_PORT + 1000`
 # number of ports used in a single pgpool-II installation.
 # (port, pcp_port, wd_port, wd_heartbeat_port)
 num_ports_per_node=4
+
+# default vip
+DEFAULT_VIP="127.0.0.1"
 #-------------------------------------------
 # End of configuration section
 #-------------------------------------------
@@ -195,6 +199,12 @@ EOF
 		echo "wd_port$n = $wd_port" >> $conf
 		echo "heartbeat_hostname$n = 'localhost'" >> $conf
 		echo "heartbeat_port$n = $wd_heartbeat_port" >> $conf
+		if [ $VIP != "" ];then
+		    echo "delegate_ip = '$VIP'" >> $conf
+		    echo "if_up_cmd = '/usr/bin/echo \"if_up_cmd executed\"'" >> $conf
+		    echo "if_down_cmd = 'usr/bin/echo \"if_down_cmd executed\"'" >> $conf
+		    echo "arping_cmd = '/usr/bin/true'" >> $conf
+		fi
 		n=`expr $n + 1`
     done
 
@@ -208,17 +218,18 @@ EOF
 ################################################################################
 function usage()
 {
-    echo "usage: $0 [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n|i] [-n num_clusters] [-p base_port] [-pg pg_base_port][--no-stop] [-d]";exit 1
+    echo "usage: $0 [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n|i] [-n num_clusters] [-p base_port] [-pg pg_base_port][--no-stop] -vip [ip]] [-d]";exit 1
 }
 
 #-------------------------------------------
 # Argument check
-# usage: $0  [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d]
+# usage: $0  [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-vip [ip]][-d]
 #-------------------------------------------
 #
 # default mode is streaming replication mode
 MODE="s"
 NO_STOP="false"
+VIP=""
 
 while [ $# -gt 0 ]
 do
@@ -257,9 +268,23 @@ do
     elif [ $1 = "--no-stop" ];then
 	shift
 	NO_STOP="true"
+
+    elif [ $1 = "-vip" ];then
+	if [ $# = 1 ];then
+	    # there's no more argument. set the default vip
+	    VIP=$DEFAULT_VIP
+	else
+	    if [[ $2 =~ -.* ]];then
+		# next argument given. set the default vip
+		VIP=$DEFAULT_VIP
+	    else
+		# VIP is specified
+		shift
+		VIP=$1
+	    fi
+	fi
     elif [ $1 = "-d" ];then
 	export PGPOOLDEBUG="true"
-	shift;
     elif [ $1 = "--help" -o $1 = "-o" ];then
 	usage
 	exit
-- 
2.25.1

