Skip to content

Commit

Permalink
varnishadm: Add -e argument
Browse files Browse the repository at this point in the history
Fixes: #4012
  • Loading branch information
walid-git committed Nov 9, 2023
1 parent 785c495 commit 0639c81
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
27 changes: 25 additions & 2 deletions bin/varnishadm/varnishadm.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ enum pass_mode_e {
static double timeout = 5;
static int p_arg = 0;
struct vsb *cli_buf;
static int e_arg = 0;
static int line_sock;

static void
Expand Down Expand Up @@ -342,6 +343,24 @@ interactive(int sock)

/*--------------------------------------------------------------------*/

static void
vadm_cli_cb_after(const struct cli *cli)
{
const char *cmd;

if (!e_arg)
return;
if (cli->result == CLIS_OK || cli->result == CLIS_TRUNCATED)
return;
cmd = VSB_data(cli->cmd);
if (*cmd == '-')
return;

fprintf(stderr, "\nCommand \"%s\" failed with error code %u\n",
cmd, cli->result);
RL_EXIT(cli->result);
}

static void v_matchproto_(cli_func_t)
ask_mgt(struct cli *cli, const char * const *av, void *priv)
{
Expand Down Expand Up @@ -394,6 +413,7 @@ pass(int sock)
AN(cli);
cli->auth = 1;
VCLS_AddFunc(vcls, 1, cli_askmgt);
VCLS_SetHooks(vcls, NULL, vadm_cli_cb_after);
do {
i = VCLS_Poll(vcls, cli, -1);
} while (i == 0);
Expand All @@ -405,7 +425,7 @@ static void v_noreturn_
usage(int status)
{
fprintf(stderr,
"Usage: varnishadm [-h] [-n ident] [-p] [-S secretfile] "
"Usage: varnishadm [-e] [-h] [-n ident] [-p] [-S secretfile] "
"[-T [address]:port] [-t timeout] [command [...]]\n");
fprintf(stderr, "\t-n is mutually exclusive with -S and -T\n");
exit(status);
Expand Down Expand Up @@ -469,7 +489,7 @@ t_arg_timeout(const char *t_arg)
return (1);
}

#define OPTARG "hn:pS:T:t:"
#define OPTARG "ehn:pS:T:t:"

int
main(int argc, char * const *argv)
Expand All @@ -494,6 +514,9 @@ main(int argc, char * const *argv)
*/
while ((opt = getopt(argc, argv, "+" OPTARG)) != -1) {
switch (opt) {
case 'e':
e_arg = 1;
break;
case 'h':
/* Usage help */
usage(0);
Expand Down
54 changes: 54 additions & 0 deletions bin/varnishtest/tests/u00001.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,57 @@ shell -err {
varnishadm -n ${v1_name} -p |
python3 -c 'import sys, json; print(json.load(sys.stdin))'
}

shell {
cat > cli_file.txt <<-EOF
status
foo
vcl.list
EOF
}

shell {
cat > expected_success.txt <<-EOF
Child in state running
Unknown request.
Type 'help' for more info.

active auto warm 0 vcl1

EOF
}

shell {
cat > expected_abort.txt <<-EOF
Child in state running
Unknown request.
Type 'help' for more info.

EOF
}

shell { varnishadm -n ${v1_name} < cli_file.txt }
shell { varnishadm -n ${v1_name} < cli_file.txt | diff -u expected_success.txt - }

shell -exit 101 { varnishadm -e -n ${v1_name} < cli_file.txt }
shell { varnishadm -e -n ${v1_name} < cli_file.txt | diff -u expected_abort.txt - }

shell { cat cli_file.txt | varnishadm -n ${v1_name} }
shell { cat cli_file.txt | varnishadm -n ${v1_name} | diff -u expected_success.txt - }

shell -exit 101 { cat cli_file.txt | varnishadm -e -n ${v1_name} }
shell { cat cli_file.txt | varnishadm -e -n ${v1_name} | diff -u expected_abort.txt - }

shell {
cat > cli_file.txt <<-EOF
status
-foo
vcl.list
EOF
}

shell { varnishadm -e -n ${v1_name} < cli_file.txt }
shell { varnishadm -e -n ${v1_name} < cli_file.txt | diff -u expected_success.txt - }

shell { cat cli_file.txt | varnishadm -e -n ${v1_name} }
shell { cat cli_file.txt | varnishadm -e -n ${v1_name} | diff -u expected_success.txt - }
9 changes: 8 additions & 1 deletion doc/sphinx/reference/varnishadm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Control a running Varnish instance
SYNOPSIS
========

varnishadm [-h] [-n ident] [-p] [-S secretfile] [-T [address]:port] [-t timeout] [command [...]]
varnishadm [-e] [-h] [-n ident] [-p] [-S secretfile] [-T [address]:port] [-t timeout] [command [...]]


DESCRIPTION
Expand All @@ -40,6 +40,13 @@ replies between the CLI socket and stdin/stdout.
OPTIONS
=======

-e
Exit immediately if a command fails in `pass` mode and return the CLI
status code of the failing command. This has no effect on `interactive`
mode (except when `-p` is used). Similarly to `CLI Command File` (see
:ref:`varnishd(1)`), if a command is prefixed with '-', its failure will
be ignored and will not cause varnishadm to exit.

-h
Print program usage and exit.

Expand Down

0 comments on commit 0639c81

Please sign in to comment.