[master] 799aaa963 Merge from VTEST:

Poul-Henning Kamp phk at FreeBSD.org
Mon Dec 2 12:00:08 UTC 2019


commit 799aaa963a856a18ca9cac0906483d8d61eb2d56
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Dec 2 11:42:36 2019 +0000

    Merge from VTEST:
    
        vtc_haproxy: enable mcli with a separate flag from worker mode
    
        Support for the master cli in worker mode was added with commit 86e65f1
        using the same flag as worker mode (-W). This is a problem with haproxy
        1.8 because it has worker mode but no mcli. This patch adds -S to enable
        mcli for haproxy in a vtc.
    
    and:
    
        The two patches in attachment fix a bug in the haproxy CLI handling and add
        the support for the haproxy master CLI in VTest. Could you merge them? Thanks
        You can also find in attachment a vtc file which use this new feature.
    
        Submitted by:   William Lallemand <wlallemand at haproxy.com>

diff --git a/bin/varnishtest/vtc_haproxy.c b/bin/varnishtest/vtc_haproxy.c
index b24af08ca..72e9d3438 100644
--- a/bin/varnishtest/vtc_haproxy.c
+++ b/bin/varnishtest/vtc_haproxy.c
@@ -47,6 +47,7 @@
 
 #define HAPROXY_PROGRAM_ENV_VAR	"HAPROXY_PROGRAM"
 #define HAPROXY_OPT_WORKER	"-W"
+#define HAPROXY_OPT_MCLI	"-S"
 #define HAPROXY_OPT_DAEMON	"-D"
 #define HAPROXY_SIGNAL		SIGINT
 #define HAPROXY_EXPECT_EXIT	(128 + HAPROXY_SIGNAL)
@@ -68,6 +69,7 @@ struct haproxy {
 	const char		*filename;
 	struct vsb		*args;
 	int			opt_worker;
+	int			opt_mcli;
 	int			opt_daemon;
 	int			opt_check_mode;
 	char			*pid_fn;
@@ -87,6 +89,9 @@ struct haproxy {
 	/* TCP socket CLI. */
 	struct haproxy_cli *cli;
 
+	/* master CLI */
+	struct haproxy_cli *mcli;
+
 	char			*workdir;
 	struct vsb		*msgs;
 	char			closed_sock[256]; /* Closed TCP socket */
@@ -191,6 +196,7 @@ cmd_haproxy_cli_send(CMD_ARGS)
 {
 	struct vsb *vsb;
 	struct haproxy_cli *hc;
+	int j;
 
 	(void)cmd;
 	(void)vl;
@@ -225,6 +231,13 @@ cmd_haproxy_cli_send(CMD_ARGS)
 		vtc_fatal(hc->vl,
 		    "CLI fd %d send error %s", hc->sock, strerror(errno));
 
+	/* a CLI command must be followed by a SHUT_WR if we want HAProxy to
+	 * close after the response */
+	j = shutdown(hc->sock, SHUT_WR);
+	vtc_log(hc->vl, 3, "CLI shutting fd %d", hc->sock);
+	if (!VTCP_Check(j))
+		vtc_fatal(hc->vl, "Shutdown failed: %s", strerror(errno));
+
 	VSB_destroy(&vsb);
 }
 
@@ -475,6 +488,51 @@ haproxy_cli_new(struct haproxy *h)
 	return (hc);
 }
 
+/* creates a master CLI client (-mcli) */
+static struct haproxy_cli *
+haproxy_mcli_new(struct haproxy *h)
+{
+	struct haproxy_cli *hc;
+
+	ALLOC_OBJ(hc, HAPROXY_CLI_MAGIC);
+	AN(hc);
+
+	hc->vl = h->vl;
+	hc->sock = -1;
+	bprintf(hc->connect, "${%s_mcli_sock}", h->name);
+
+	hc->txbuf_sz = hc->rxbuf_sz = 2048 * 1024;
+	hc->txbuf = malloc(hc->txbuf_sz);
+	AN(hc->txbuf);
+	hc->rxbuf = malloc(hc->rxbuf_sz);
+	AN(hc->rxbuf);
+
+	return (hc);
+}
+
+/* Bind an address/port for the master CLI (-mcli) */
+static int
+haproxy_create_mcli(struct haproxy *h)
+{
+	int sock;
+	const char *err;
+	char buf[128], addr[128], port[128];
+
+	sock = VTCP_listen_on("localhost:0", NULL, 100, &err);
+	if (err != NULL)
+		vtc_fatal(h->vl,
+			  "Create listen socket failed: %s", err);
+	assert(sock > 0);
+
+	VTCP_myname(sock, addr, sizeof addr, port, sizeof port);
+	bprintf(buf, "%s_mcli", h->name);
+	macro_def(h->vl, buf, "sock", "%s %s", addr, port);
+	macro_def(h->vl, buf, "addr", "%s", addr);
+	macro_def(h->vl, buf, "port", "%s", port);
+
+	return sock;
+}
+
 static void
 haproxy_cli_delete(struct haproxy_cli *hc)
 {
@@ -545,6 +603,9 @@ haproxy_new(const char *name)
 	h->cli = haproxy_cli_new(h);
 	AN(h->cli);
 
+	h->mcli = haproxy_mcli_new(h);
+	AN(h->mcli);
+
 	bprintf(buf, "rm -rf \"%s\" ; mkdir -p \"%s\"", h->workdir, h->workdir);
 	AZ(system(buf));
 
@@ -578,6 +639,7 @@ haproxy_delete(struct haproxy *h)
 	free(h->pid_fn);
 	VSB_destroy(&h->args);
 	haproxy_cli_delete(h->cli);
+	haproxy_cli_delete(h->mcli);
 
 	/* XXX: MEMLEAK (?) */
 	FREE_OBJ(h);
@@ -598,6 +660,7 @@ haproxy_thread(void *priv)
 	return (NULL);
 }
 
+
 /**********************************************************************
  * Start a HAProxy instance.
  */
@@ -611,22 +674,28 @@ haproxy_start(struct haproxy *h)
 	vtc_log(h->vl, 2, "%s", __func__);
 
 	AZ(VSB_finish(h->args));
-	vtc_log(h->vl, 4, "opt_worker %d opt_daemon %d opt_check_mode %d",
-	    h->opt_worker, h->opt_daemon, h->opt_check_mode);
+	vtc_log(h->vl, 4, "opt_worker %d opt_daemon %d opt_check_mode %d opt_mcli %d",
+	    h->opt_worker, h->opt_daemon, h->opt_check_mode, h->opt_mcli);
 
 	vsb = VSB_new_auto();
 	AN(vsb);
 
 	VSB_printf(vsb, "exec \"%s\"", h->filename);
 	if (h->opt_check_mode)
-		VSB_printf(vsb, " -c");
+		VSB_cat(vsb, " -c");
 	else if (h->opt_daemon)
-		VSB_printf(vsb, " -D");
+		VSB_cat(vsb, " -D");
 	else
-		VSB_printf(vsb, " -d");
-
-	if (h->opt_worker)
-		VSB_printf(vsb, " -W");
+		VSB_cat(vsb, " -d");
+
+	if (h->opt_worker) {
+		VSB_cat(vsb, " -W");
+		if (h->opt_mcli) {
+			int sock;
+			sock = haproxy_create_mcli(h);
+			VSB_printf(vsb, " -S \"fd@%d\"", sock);
+		}
+	}
 
 	VSB_printf(vsb, " %s", VSB_data(h->args));
 
@@ -707,6 +776,9 @@ haproxy_wait(struct haproxy *h)
 	if (h->cli->spec)
 		haproxy_cli_run(h->cli);
 
+	if (h->mcli->spec)
+		haproxy_cli_run(h->mcli);
+
 	closefd(&h->fds[1]);
 
 	sig = SIGINT;
@@ -827,7 +899,7 @@ haproxy_store_conf(struct haproxy *h, const char *cfg, int auto_be)
 
 	VSB_printf(vsb, "    global\n\tstats socket \"%s\" "
 		   "level admin mode 600\n", h->cli_fn);
-	VSB_printf(vsb, "    stats socket \"fd@${cli}\" level admin\n");
+	VSB_cat(vsb, "    stats socket \"fd@${cli}\" level admin\n");
 	AZ(VSB_cat(vsb, cfg));
 
 	if (auto_be)
@@ -898,12 +970,19 @@ haproxy_write_conf(struct haproxy *h)
  * \-W
  *         Enable HAproxy in Worker mode.
  *
+ * \-S
+ *         Enable HAproxy Master CLI in Worker mode
+ *
  * \-arg STRING
  *         Pass an argument to haproxy, for example "-h simple_list".
  *
  * \-cli STRING
  *         Specify the spec to be run by the command line interface (CLI).
  *
+ * \-mcli STRING
+ *         Specify the spec to be run by the command line interface (CLI)
+ *         of the Master process.
+ *
  * \-conf STRING
  *         Specify the configuration to be loaded by this HAProxy instance.
  *
@@ -984,7 +1063,10 @@ cmd_haproxy(CMD_ARGS)
 			h->opt_worker = 1;
 			continue;
 		}
-
+		if (!strcmp(*av, HAPROXY_OPT_MCLI)) {
+			h->opt_mcli = 1;
+			continue;
+		}
 		if (!strcmp(*av, "-arg")) {
 			AN(av[1]);
 			AZ(h->pid);
@@ -1001,6 +1083,15 @@ cmd_haproxy(CMD_ARGS)
 			av++;
 			continue;
 		}
+
+		if (!strcmp(*av, "-mcli")) {
+			REPLACE(h->mcli->spec, av[1]);
+			if (h->tp)
+				haproxy_cli_run(h->mcli);
+			av++;
+			continue;
+		}
+
 		if (!strcmp(*av, "-conf")) {
 			AN(av[1]);
 			haproxy_store_conf(h, av[1], 0);


More information about the varnish-commit mailing list