diff --git a/doc/configuration.txt b/doc/configuration.txt index cddd70f5d..0b9776cfb 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -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 "issuers-chain-path" directory. All other certificates with the same issuer will share the chain in memory. +localpeer + 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
[len ] [format ] [sample :] [max level [min level]] Adds a global syslog server. Several global servers can be defined. They @@ -2323,18 +2333,19 @@ log
[len ] [format ] [sample :] peer : [param*] Defines a peer inside a peers section. If 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 :. Otherwise, : defines where to connect to - to join the remote peer, and 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 :. + Otherwise, : defines where to connect to in order to join the + remote peer, and is used at the protocol level to identify and + validate the remote peer on the server side. During a soft restart, local peer : 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. diff --git a/include/haproxy/global-t.h b/include/haproxy/global-t.h index 7db541209..f5bf21629 100644 --- a/include/haproxy/global-t.h +++ b/include/haproxy/global-t.h @@ -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 */ diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index a4fed0446..ea47bbd6a 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -15,6 +15,7 @@ #include #include #include +#include #include /* @@ -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; diff --git a/src/haproxy.c b/src/haproxy.c index 1efa3acac..06e5a222e 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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)) {