From: William Lallemand Date: Tue, 22 Nov 2016 11:34:35 +0000 (+0100) Subject: REORG: cli: move get/set weight to server.c X-Git-Tag: v1.7.0~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6b16094355d2768fe18b6485d54435ad2c9fbe21;p=thirdparty%2Fhaproxy.git REORG: cli: move get/set weight to server.c Move get/set weight CLI functions to server.c and use the cli keyword API to register it on the CLI. --- diff --git a/src/cli.c b/src/cli.c index efceb72d19..3662c50a3f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -153,8 +153,6 @@ static const char stats_sock_usage_msg[] = " show stat : report counters for each proxy and server\n" " show errors : report last request and response errors for each proxy\n" " show table [id]: report table usage stats or dump this table's contents\n" - " get weight : report a server's current weight\n" - " set weight : change a server's weight\n" " set table [id] : update or create a table entry's data\n" " set timeout : change a timeout setting\n" " set maxconn : change a maxconn setting\n" @@ -1190,58 +1188,8 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line) return 0; } } - else if (strcmp(args[0], "get") == 0) { - if (strcmp(args[1], "weight") == 0) { - struct proxy *px; - struct server *sv; - - /* split "backend/server" and make point to server */ - for (line = args[2]; *line; line++) - if (*line == '/') { - *line++ = '\0'; - break; - } - - if (!*line) { - appctx->ctx.cli.msg = "Require 'backend/server'.\n"; - appctx->st0 = STAT_CLI_PRINT; - return 1; - } - - if (!get_backend_server(args[2], line, &px, &sv)) { - appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; - appctx->st0 = STAT_CLI_PRINT; - return 1; - } - - /* return server's effective weight at the moment */ - snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight); - if (bi_putstr(si_ic(si), trash.str) == -1) - si_applet_cant_put(si); - - return 1; - } - else { /* not "get weight" */ - return 0; - } - } else if (strcmp(args[0], "set") == 0) { - if (strcmp(args[1], "weight") == 0) { - struct server *sv; - const char *warning; - - sv = expect_server_admin(s, si, args[2]); - if (!sv) - return 1; - - warning = server_parse_weight_change_request(sv, args[3]); - if (warning) { - appctx->ctx.cli.msg = warning; - appctx->st0 = STAT_CLI_PRINT; - } - return 1; - } - else if (strcmp(args[1], "timeout") == 0) { + if (strcmp(args[1], "timeout") == 0) { if (strcmp(args[2], "cli") == 0) { unsigned timeout; const char *res; diff --git a/src/server.c b/src/server.c index b07a2fe0ef..fb891f8275 100644 --- a/src/server.c +++ b/src/server.c @@ -3507,9 +3507,68 @@ static int cli_parse_set_server(char **args, struct appctx *appctx, void *privat return 1; } +static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private) +{ + struct stream_interface *si = appctx->owner; + struct proxy *px; + struct server *sv; + char *line; + + + /* split "backend/server" and make point to server */ + for (line = args[2]; *line; line++) + if (*line == '/') { + *line++ = '\0'; + break; + } + + if (!*line) { + appctx->ctx.cli.msg = "Require 'backend/server'.\n"; + appctx->st0 = STAT_CLI_PRINT; + return 1; + } + + if (!get_backend_server(args[2], line, &px, &sv)) { + appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n"; + appctx->st0 = STAT_CLI_PRINT; + return 1; + } + + /* return server's effective weight at the moment */ + snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight); + if (bi_putstr(si_ic(si), trash.str) == -1) + si_applet_cant_put(si); + + return 1; +} + +static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private) +{ + struct server *sv; + const char *warning; + + if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) + return 1; + + sv = cli_find_server(appctx, args[2]); + if (!sv) + return 1; + + warning = server_parse_weight_change_request(sv, args[3]); + if (warning) { + appctx->ctx.cli.msg = warning; + appctx->st0 = STAT_CLI_PRINT; + } + return 1; +} + + /* register cli keywords */ static struct cli_kw_list cli_kws = {{ },{ { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server }, + { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight }, + { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight }, + {{},} }};