MEDIUM: peers: add the "localpeer" global option

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.
This commit is contained in:
Dragan Dosen 2020-06-18 18:24:05 +02:00 committed by William Lallemand
parent 4f01415d3b
commit 13cd54c08b
4 changed files with 55 additions and 7 deletions

View 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.

View 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 */

View 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;

View 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)) {