From: phk Date: Tue, 17 Jun 2008 07:53:21 +0000 (+0000) Subject: Add a -vcl+backend argument to the varnish command which autogenerates X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d3bac0cbc13035459af6a3d0ea667fe5c5648ab;p=varnish Add a -vcl+backend argument to the varnish command which autogenerates backend stanzas for the servers we know about. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2717 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishtest/vtc.h b/varnish-cache/bin/varnishtest/vtc.h index b227b7df..cc1e8ff4 100644 --- a/varnish-cache/bin/varnishtest/vtc.h +++ b/varnish-cache/bin/varnishtest/vtc.h @@ -28,6 +28,8 @@ typedef void cmd_f(char **av, void *priv); +struct vsb; + struct cmds { const char *name; cmd_f *cmd; @@ -47,3 +49,5 @@ void cmd_varnish(char **av, void *priv); void http_process(const char *ident, const char *spec, int sock, int client); void vct_dump(const char *ident, const char *pfx, const char *str); + +void cmd_server_genvcl(struct vsb *vsb); diff --git a/varnish-cache/bin/varnishtest/vtc_server.c b/varnish-cache/bin/varnishtest/vtc_server.c index 5b168a25..460538f6 100644 --- a/varnish-cache/bin/varnishtest/vtc_server.c +++ b/varnish-cache/bin/varnishtest/vtc_server.c @@ -40,6 +40,7 @@ #include "vqueue.h" #include "miniobj.h" +#include "vsb.h" #include "vss.h" #include "libvarnish.h" @@ -116,6 +117,7 @@ server_new(char *name) AN(s); s->name = name; s->listen = ":9080"; + AZ(VSS_parse(s->listen, &s->addr, &s->port)); s->repeat = 1; s->depth = 1; s->sock = -1; @@ -135,7 +137,6 @@ server_start(struct server *s) CHECK_OBJ_NOTNULL(s, SERVER_MAGIC); printf("## %-4s Starting server\n", s->name); if (s->sock < 0) { - AZ(VSS_parse(s->listen, &s->addr, &s->port)); naddr = VSS_resolve(s->addr, s->port, &s->vss_addr); if (naddr != 1) { fprintf(stderr, @@ -174,6 +175,25 @@ server_wait(struct server *s) s->sock = -1; } +/********************************************************************** + * Generate VCL backend decls for our servers + */ + +void +cmd_server_genvcl(struct vsb *vsb) +{ + struct server *s; + + VTAILQ_FOREACH(s, &servers, list) { + vsb_printf(vsb, + "backend %s { .host = \"%s\"; .port = \"%s\"; }\n", + s->name, + s->addr == NULL ? "localhost" : s->addr, + s->port); + } +} + + /********************************************************************** * Server command dispatch */ @@ -213,6 +233,7 @@ cmd_server(char **av, void *priv) } if (!strcmp(*av, "-listen")) { s->listen = av[1]; + AZ(VSS_parse(s->listen, &s->addr, &s->port)); av++; continue; } diff --git a/varnish-cache/bin/varnishtest/vtc_varnish.c b/varnish-cache/bin/varnishtest/vtc_varnish.c index f928c5c4..571236ee 100644 --- a/varnish-cache/bin/varnishtest/vtc_varnish.c +++ b/varnish-cache/bin/varnishtest/vtc_varnish.c @@ -104,6 +104,28 @@ varnish_ask_cli(struct varnish *v, const char *cmd, char **repl) return (retval); } +static void +varnish_cli_encode(struct vsb *vsb, const char *str) +{ + + for (; *str != '\0'; str++) { + switch (*str) { + case '\\': + case '"': + vsb_printf(vsb, "\\%c", *str); break; + case '\n': + vsb_printf(vsb, "\\n"); break; + case '\t': + vsb_printf(vsb, "\\t"); break; + default: + if (isgraph(*str) || *str == ' ') + vsb_putc(vsb, *str); + else + vsb_printf(vsb, "\\x%02x", *str); + } + } +} + /********************************************************************** * Allocate and initialize a varnish */ @@ -281,6 +303,55 @@ varnish_vcl(struct varnish *v, char *vcl) vsb_delete(vsb); } +/********************************************************************** + * Load a VCL program prefixed by backend decls for our servers + */ + +static void +varnish_vclbackend(struct varnish *v, char *vcl) +{ + struct vsb *vsb, *vsb2; + char *p; + unsigned u; + + vsb = vsb_newauto(); + AN(vsb); + + vsb2 = vsb_newauto(); + AN(vsb2); + + cmd_server_genvcl(vsb2); + vsb_finish(vsb2); + AZ(vsb_overflowed(vsb2)); + + v->vcl_nbr++; + vsb_printf(vsb, "vcl.inline vcl%d \"", v->vcl_nbr); + + varnish_cli_encode(vsb, vsb_data(vsb2)); + + if (*vcl == '{') { + p = strchr(++vcl, '\0'); + if (p > vcl && p[-1] == '}') + p[-1] = '\0'; + } + varnish_cli_encode(vsb, vcl); + + vsb_printf(vsb, "\"", *vcl); + vsb_finish(vsb); + AZ(vsb_overflowed(vsb)); + + u = varnish_ask_cli(v, vsb_data(vsb), NULL); + assert(u == CLIS_OK); + vsb_clear(vsb); + vsb_printf(vsb, "vcl.use vcl%d", v->vcl_nbr); + vsb_finish(vsb); + AZ(vsb_overflowed(vsb)); + u = varnish_ask_cli(v, vsb_data(vsb), NULL); + assert(u == CLIS_OK); + vsb_delete(vsb); + vsb_delete(vsb2); +} + /********************************************************************** * Varnish server cmd dispatch */ @@ -336,6 +407,11 @@ cmd_varnish(char **av, void *priv) varnish_start(v); continue; } + if (!strcmp(*av, "-vcl+backend")) { + varnish_vclbackend(v, av[1]); + av++; + continue; + } if (!strcmp(*av, "-vcl")) { varnish_vcl(v, av[1]); av++;