]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: cli: move get/set weight to server.c
authorWilliam Lallemand <wlallemand@haproxy.com>
Tue, 22 Nov 2016 11:34:35 +0000 (12:34 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 24 Nov 2016 15:59:27 +0000 (16:59 +0100)
Move get/set weight CLI functions to server.c and use the cli keyword API
to register it on the CLI.

src/cli.c
src/server.c

index efceb72d194502fe0b7688ced2049d9438e4f77a..3662c50a3f1fa136c7127efcc10dc3927183dc06 100644 (file)
--- 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 <line> 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;
index b07a2fe0efef4dcc8f5af8c2d8e84c5d8281de0e..fb891f8275069464654543e5e22eedacc349559d 100644 (file)
@@ -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 <line> 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 },
+
        {{},}
 }};