From: Amaury Denoyelle Date: Wed, 17 Dec 2025 09:57:40 +0000 (+0100) Subject: MINOR: proxy: define "add backend" handler X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ac5088c50446f4e94cff0ff8e3f99df6aed9cb4;p=thirdparty%2Fhaproxy.git MINOR: proxy: define "add backend" handler Define a basic CLI handler for "add backend". For now, this handler only performs a parsing of the name argument and return an error if a duplicate already exists. It runs under thread isolation, to guarantee thread safety during the proxy creation. This feature is considered in development. CLI command requires to set experimental-mode. --- diff --git a/doc/management.txt b/doc/management.txt index b2b1d539d..b07f287ba 100644 --- a/doc/management.txt +++ b/doc/management.txt @@ -1725,6 +1725,19 @@ add acl [@] This command cannot be used if the reference is a name also used with a map. In this case, the "add map" command must be used instead. +add backend from [ EXPERIMENTAL ] + Instantiate a new backend proxy with the name . + + Only TCP or HTTP proxies can be created. All of the settings are inherited + from default proxy instance. Servers can be added via the command + "add server". The backend is initialized in the unpublished state. Once + considered ready for traffic, use "publish backend" to expose the newly + created instance. + + This command is restricted and can only be issued on sockets configured for + level "admin". Moreover, this feature is still considered in development so it + also requires experimental mode (see "experimental-mode on"). + add map [@] add map [@] Add an entry into the map to associate the value to the key diff --git a/src/proxy.c b/src/proxy.c index 207f85a98..3d9f9440b 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include @@ -4777,6 +4778,70 @@ static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx return 1; } +/* Parses a "add backend" CLI command to allocate a new backend instance, + * derived from a default proxy instance. This operation is performed under + * thread isolation. + * + * Always returns 1. + */ +static int cli_parse_add_backend(char **args, char *payload, struct appctx *appctx, void *private) +{ + struct proxy *px, *defpx; + const char *be_name, *def_name, *err; + char *msg = NULL; + + usermsgs_clr("CLI"); + + if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) + return 1; + + ++args; + be_name = args[1]; + if (!*be_name) { + cli_err(appctx, "Require backend name.\n"); + return 1; + } + if ((err = invalid_char(be_name))) { + cli_dynerr(appctx, memprintf(&msg, "Invalid character '%c' in backend name.\n", *err)); + return 1; + } + + ++args; + def_name = args[2]; + if (!*args[1] || !*def_name || strcmp(args[1], "from") != 0) { + cli_err(appctx, "Usage: add backend from .\n"); + return 1; + } + + defpx = proxy_find_by_name(def_name, PR_CAP_DEF, 0); + if (!defpx) { + cli_dynerr(appctx, memprintf(&msg, "Cannot find default proxy '%s'.\n", def_name)); + return 1; + } + + thread_isolate(); + + if ((px = proxy_find_by_name(be_name, PR_CAP_NONE, 0)) || + (px = proxy_find_by_name(be_name, PR_CAP_DEF, 0))) { + memprintf(&msg, + "name is already used by other proxy '%s %s'", + proxy_cap_str(px->cap), be_name); + px = NULL; + goto err; + } + + thread_release(); + ha_notice("New backend registered.\n"); + cli_umsg(appctx, LOG_INFO); + return 1; + + err: + thread_release(); + if (msg) + cli_dynerr(appctx, msg); + return 1; +} + /* Parses the "disable frontend" directive, it always returns 1. * * Grabs the proxy lock. @@ -5098,6 +5163,7 @@ static int cli_io_handler_show_errors(struct appctx *appctx) /* register cli keywords */ static struct cli_kw_list cli_kws = {{ },{ + { { "add", "backend", NULL }, "add backend : add a new backend", cli_parse_add_backend, NULL, NULL, NULL, ACCESS_EXPERIMENTAL }, { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL }, { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL }, { { "publish", "backend", NULL }, "publish backend : mark backend as ready for traffic", cli_parse_publish_backend, NULL, NULL },