]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: peers: add the "localpeer" global option
authorDragan Dosen <ddosen@haproxy.com>
Thu, 18 Jun 2020 16:24:05 +0000 (18:24 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 19 Jun 2020 09:37:30 +0000 (11:37 +0200)
localpeer <name>
  Sets the local instance's peer name. It will be ignored if the "-L"
  command line argument is specified or if used after "peers" section
  definitions. In such cases, a warning message will be emitted during
  the configuration parsing.

  This option will also set the HAPROXY_LOCALPEER environment variable.
  See also "-L" in the management guide and "peers" section in the
  configuration manual.

doc/configuration.txt
include/haproxy/global-t.h
src/cfgparse-global.c
src/haproxy.c

index cddd70f5d480e48e98a21dae63530fb1808ed1c3..0b9776cfbd322cd19a9062c4ee189ceb1b3a8225 100644 (file)
@@ -603,6 +603,7 @@ The following keywords are supported in the "global" section :
    - insecure-fork-wanted
    - insecure-setuid-wanted
    - issuers-chain-path
+   - localpeer
    - log
    - log-tag
    - log-send-hostname
@@ -969,6 +970,15 @@ issuers-chain-path <dir>
   "issuers-chain-path" directory. All other certificates with the same issuer
   will share the chain in memory.
 
+localpeer <name>
+  Sets the local instance's peer name. It will be ignored if the "-L"
+  command line argument is specified or if used after "peers" section
+  definitions. In such cases, a warning message will be emitted during
+  the configuration parsing.
+
+  This option will also set the HAPROXY_LOCALPEER environment variable.
+  See also "-L" in the management guide and "peers" section below.
+
 log <address> [len <length>] [format <format>] [sample <ranges>:<smp_size>]
     <facility> [max level [min level]]
   Adds a global syslog server. Several global servers can be defined. They
@@ -2323,18 +2333,19 @@ log <address> [len <length>] [format <format>] [sample <ranges>:<smp_size>]
 peer <peername> <ip>:<port> [param*]
   Defines a peer inside a peers section.
   If <peername> is set to the local peer name (by default hostname, or forced
-  using "-L" command line option), haproxy will listen for incoming remote peer
-  connection on <ip>:<port>. Otherwise, <ip>:<port> defines where to connect to
-  to join the remote peer, and <peername> is used at the protocol level to
-  identify and validate the remote peer on the server side.
+  using "-L" command line option or "localpeer" global configuration setting),
+  haproxy will listen for incoming remote peer connection on <ip>:<port>.
+  Otherwise, <ip>:<port> defines where to connect to in order to join the
+  remote peer, and <peername> is used at the protocol level to identify and
+  validate the remote peer on the server side.
 
   During a soft restart, local peer <ip>:<port> is used by the old instance to
   connect the new one and initiate a complete replication (teaching process).
 
   It is strongly recommended to have the exact same peers declaration on all
-  peers and to only rely on the "-L" command line argument to change the local
-  peer name. This makes it easier to maintain coherent configuration files
-  across all peers.
+  peers and to only rely on the "-L" command line argument or the "localpeer"
+  global configuration setting to change the local peer name. This makes it
+  easier to maintain coherent configuration files across all peers.
 
   You may want to reference some environment variables in the address
   parameter, see section 2.3 about environment variables.
index 7db541209b0c834815ae82d075aa912615827d79..f5bf2162994d08bea50af7ba993e9a8decef9e26 100644 (file)
@@ -127,6 +127,7 @@ struct global {
        char *chroot;
        char *pidfile;
        char *node, *desc;              /* node name & description */
+       int localpeer_cmdline;          /* whether or not the commandline "-L" was set */
        struct buffer log_tag;           /* name for syslog */
        struct list logsrvs;
        char *log_send_hostname;   /* set hostname in syslog header */
index a4fed04464827db40c9214171d6e2556c0d09480..ea47bbd6ab307b7bd75ceda373dfeaf46346b9f3 100644 (file)
@@ -15,6 +15,7 @@
 #include <haproxy/compression.h>
 #include <haproxy/global.h>
 #include <haproxy/log.h>
+#include <haproxy/peers.h>
 #include <haproxy/tools.h>
 
 /*
@@ -1207,6 +1208,40 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                else
                        global.tune.options |= GTUNE_STRICT_LIMITS;
        }
+       else if (!strcmp(args[0], "localpeer")) {
+               if (alertif_too_many_args(1, file, linenum, args, &err_code))
+                       goto out;
+
+               if (*(args[1]) == 0) {
+                       ha_alert("parsing [%s:%d] : '%s' expects a name as an argument.\n",
+                                file, linenum, args[0]);
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
+
+               if (global.localpeer_cmdline != 0) {
+                       ha_warning("parsing [%s:%d] : '%s' ignored since it is already set by using the '-L' "
+                                  "command line argument.\n", file, linenum, args[0]);
+                       err_code |= ERR_WARN;
+                       goto out;
+               }
+
+               if (cfg_peers) {
+                       ha_warning("parsing [%s:%d] : '%s' ignored since it is used after 'peers' section.\n",
+                                  file, linenum, args[0]);
+                       err_code |= ERR_WARN;
+                       goto out;
+               }
+
+               free(localpeer);
+               if ((localpeer = strdup(args[1])) == NULL) {
+                       ha_alert("parsing [%s:%d]: cannot allocate memory for '%s'.\n",
+                                file, linenum, args[0]);
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
+               setenv("HAPROXY_LOCALPEER", localpeer, 1);
+       }
        else {
                struct cfg_kw_list *kwl;
                int index;
index 1efa3acac8bba7c6a1d90cf6866d77cf5670eca4..06e5a222e6a79bbec50bab2cfb3b050268026b1c 100644 (file)
@@ -1964,6 +1964,7 @@ static void init(int argc, char **argv)
                                                exit(EXIT_FAILURE);
                                        }
                                        setenv("HAPROXY_LOCALPEER", localpeer, 1);
+                                       global.localpeer_cmdline = 1;
                                        break;
                                case 'f' :
                                        if (!list_append_word(&cfg_cfgfiles, *argv, &err_msg)) {