Skip to content

Commit

Permalink
cli: add -p <vcl_path> arg to vcl.load
Browse files Browse the repository at this point in the history
This commit adds a -p argument to the vcl.load cli command to allow the
user to specify a path that will be appended to the begining of VCL_PATH
when loading the vcl
  • Loading branch information
walid-git committed Mar 13, 2023
1 parent 8ec1201 commit 0de1539
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bin/varnishd/mgt/mgt.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void STV_Init(void);
/* mgt_vcc.c */
void mgt_DumpBuiltin(void);
char *mgt_VccCompile(struct cli *, struct vclprog *, const char *vclname,
const char *vclsrc, const char *vclsrcfile, int C_flag);
const char *vclsrc, const char *vclsrcfile, const char *vcl_path, int C_flag);

void mgt_vcl_init(void);
void mgt_vcl_startup(struct cli *, const char *vclsrc, const char *origin,
Expand Down
17 changes: 14 additions & 3 deletions bin/varnishd/mgt/mgt_vcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct vcc_priv {
#define VCC_PRIV_MAGIC 0x70080cb8
const char *vclsrc;
const char *vclsrcfile;
const char *vcl_path;
struct vsb *dir;
struct vsb *csrcfile;
struct vsb *libfile;
Expand Down Expand Up @@ -103,7 +104,7 @@ vcc_vext_iter_func(const char *filename, void *priv)
static void v_noreturn_ v_matchproto_(vsub_func_f)
run_vcc(void *priv)
{
struct vsb *sb = NULL;
struct vsb *sb = NULL, *vsb = NULL;
struct vclprog *vpg;
struct vcc_priv *vp;
struct vcc *vcc;
Expand All @@ -118,8 +119,17 @@ run_vcc(void *priv)
vcc = VCC_New();
AN(vcc);
VCC_Builtin_VCL(vcc, builtin_vcl);
VCC_VCL_path(vcc, mgt_vcl_path);
VCC_VMOD_path(vcc, mgt_vmod_path);
if (vp->vcl_path != NULL) {
vsb = VSB_new_auto();
AN(vsb);
VSB_printf(vsb, "%s:%s", vp->vcl_path, mgt_vcl_path);
AZ(VSB_finish(vsb));
VCC_VCL_path(vcc, VSB_data(vsb));
VSB_destroy(&vsb);
} else {
VCC_VCL_path(vcc, mgt_vcl_path);
}

#define VCC_FEATURE_BIT(U, l, d) \
VCC_Opt_ ## l(vcc, MGT_VCC_FEATURE(VCC_FEATURE_ ## U));
Expand Down Expand Up @@ -347,7 +357,7 @@ mgt_vcc_fini_vp(struct vcc_priv *vp, int leave_lib)

char *
mgt_VccCompile(struct cli *cli, struct vclprog *vcl, const char *vclname,
const char *vclsrc, const char *vclsrcfile, int C_flag)
const char *vclsrc, const char *vclsrcfile, const char* vcl_path, int C_flag)
{
struct vcc_priv vp[1];
struct vsb *sb;
Expand All @@ -362,6 +372,7 @@ mgt_VccCompile(struct cli *cli, struct vclprog *vcl, const char *vclname,
mgt_vcc_init_vp(vp);
vp->vclsrc = vclsrc;
vp->vclsrcfile = vclsrcfile;
vp->vcl_path = vcl_path;

/*
* The subdirectory must have a unique name to 100% certain evade
Expand Down
21 changes: 15 additions & 6 deletions bin/varnishd/mgt/mgt_vcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mgt_vcl_setstate(struct cli *cli, struct vclprog *vp, const struct vclstate *vs)

static struct vclprog *
mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
const char *vclsrcfile, const char *state, int C_flag)
const char *vclsrcfile, const char *state, const char* vcl_path, int C_flag)
{
unsigned status;
char *lib, *p;
Expand All @@ -463,7 +463,7 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
return (NULL);

vp = mgt_vcl_add(vclname, vs);
lib = mgt_VccCompile(cli, vp, vclname, vclsrc, vclsrcfile, C_flag);
lib = mgt_VccCompile(cli, vp, vclname, vclsrc, vclsrcfile, vcl_path, C_flag);
if (lib == NULL) {
mgt_vcl_del(vp);
return (NULL);
Expand Down Expand Up @@ -515,7 +515,7 @@ mgt_vcl_startup(struct cli *cli, const char *vclsrc, const char *vclname,
bprintf(buf, "boot%d", n++);
vclname = buf;
}
vp = mgt_new_vcl(cli, vclname, vclsrc, origin, NULL, C_flag);
vp = mgt_new_vcl(cli, vclname, vclsrc, origin, NULL, NULL, C_flag);
if (vp != NULL) {
/* Last startup VCL becomes the automatically selected
* active VCL. */
Expand Down Expand Up @@ -607,7 +607,7 @@ mcf_vcl_inline(struct cli *cli, const char * const *av, void *priv)
if (!mcf_find_no_vcl(cli, av[i]))
return;

vp = mgt_new_vcl(cli, av[i], av[i+1], "<vcl.inline>", state, 0);
vp = mgt_new_vcl(cli, av[i], av[i+1], "<vcl.inline>", state, NULL, 0);
if (vp != NULL && !MCH_Running())
VCLI_Out(cli, "VCL compiled.\n");
}
Expand All @@ -618,11 +618,20 @@ mcf_vcl_load(struct cli *cli, const char * const *av, void *priv)
struct vclprog *vp;
int i = 2;
const char *state = NULL;
const char *vcl_path = NULL;

if (av[i] != NULL && !strcmp(av[i], "-s")) {
while (av[i] != NULL) {
if (!strcmp(av[i], "-s")) {
FAIL_NO_ARG(av[i+1], "-s");
state = av[i+1];
i += 2;
} else if (!strcmp(av[i], "-p")) {
FAIL_NO_ARG(av[i+1], "-p");
vcl_path = av[i+1];
i += 2;
} else {
break;
}
}

if (av[i] == NULL || av[i+1] == NULL) {
Expand All @@ -635,7 +644,7 @@ mcf_vcl_load(struct cli *cli, const char * const *av, void *priv)
if (!mcf_find_no_vcl(cli, av[i]))
return;

vp = mgt_new_vcl(cli, av[i], NULL, av[i+1], state, 0);
vp = mgt_new_vcl(cli, av[i], NULL, av[i+1], state, vcl_path, 0);
if (vp != NULL && !MCH_Running())
VCLI_Out(cli, "VCL compiled.\n");
}
Expand Down
39 changes: 39 additions & 0 deletions bin/varnishtest/tests/c00123.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
varnishtest "Test vcl.load -p arg"

server s1 {
rxreq
txresp
} -start

varnish v1 -vcl+backend {} -start

shell {
(
echo 'vcl 4.0;'
echo 'backend default {'
echo ' .host="${s1_addr}";'
echo ' .port="${s1_port}";'
echo '}'
echo ''
echo 'include "inc.vcl";'
) > ${tmpdir}/c00123.vcl
}

shell {
(
echo 'sub vcl_deliver {'
echo ' set resp.http.test = "c00123";'
echo '}'
) > ${tmpdir}/inc.vcl
}

varnish v1 -cliexpect "No such file or directory" {vcl.load foo c00123.vcl}

varnish v1 -cliok {vcl.load -p ${tmpdir} foo c00123.vcl}
varnish v1 -cliok {vcl.use foo}

client c1 {
txreq
rxresp
expect resp.http.test == "c00123"
} -run
4 changes: 2 additions & 2 deletions include/tbl/cli_cmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ CLI_CMD(BAN_LIST,

CLI_CMD(VCL_LOAD,
"vcl.load",
"vcl.load [-s auto|cold|warm] <configname> <filename>",
"vcl.load [-s auto|cold|warm] [-p vcl_path] <configname> <filename>",
"Compile and load the VCL file under the name provided.",
"",
2, 4
2, 6
)

CLI_CMD(VCL_INLINE,
Expand Down

0 comments on commit 0de1539

Please sign in to comment.