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 8, 2023
1 parent 9dfc726 commit eec728a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
18 changes: 16 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 @@ -341,6 +342,15 @@ interactive(int sock)
}

/*--------------------------------------------------------------------*/
static void
vadm_cli_cb_after(const struct cli *cli) {
if (e_arg &&
cli->result != CLIS_OK && *VSB_data(cli->cmd) != '-') {
printf("\nCommand failed (%d)\n%s\n",
cli->result, VSB_data(cli->sb));
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 +404,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 @@ -406,7 +417,7 @@ usage(int status)
{
fprintf(stderr,
"Usage: varnishadm [-h] [-n ident] [-p] [-S secretfile] "
"[-T [address]:port] [-t timeout] [command [...]]\n");
"[-T [address]:port] [-t timeout] [-e] [command [...]]\n");
fprintf(stderr, "\t-n is mutually exclusive with -S and -T\n");
exit(status);
}
Expand Down Expand Up @@ -469,7 +480,7 @@ t_arg_timeout(const char *t_arg)
return (1);
}

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

int
main(int argc, char * const *argv)
Expand Down Expand Up @@ -512,6 +523,9 @@ main(int argc, char * const *argv)
case 't':
t_arg = optarg;
break;
case 'e':
e_arg = 1;
break;
default:
usage(1);
}
Expand Down
34 changes: 34 additions & 0 deletions bin/varnishtest/tests/u00001.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,37 @@ 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
status
EOF
}

shell { varnishadm -n ${v1_name} < cli_file.txt }
shell { test 2 -eq $(varnishadm -n ${v1_name} < cli_file.txt | grep "Child in state" | wc -l) }

shell -exit 101 { varnishadm -e -n ${v1_name} < cli_file.txt }
shell { test 1 -eq $(varnishadm -e -n ${v1_name} < cli_file.txt | grep "Child in state" | wc -l) }

shell { cat cli_file.txt | varnishadm -n ${v1_name} }
shell { test 2 -eq $(cat cli_file.txt | varnishadm -n ${v1_name} | grep "Child in state" | wc -l) }

shell -exit 101 { cat cli_file.txt | varnishadm -e -n ${v1_name} }
shell { test 1 -eq $(cat cli_file.txt | varnishadm -e -n ${v1_name} | grep "Child in state" | wc -l) }

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

shell { varnishadm -e -n ${v1_name} < cli_file.txt }
shell { test 2 -eq $(varnishadm -e -n ${v1_name} < cli_file.txt | grep "Child in state" | wc -l) }

shell { cat cli_file.txt | varnishadm -e -n ${v1_name} }
shell { test 2 -eq $(cat cli_file.txt | varnishadm -e -n ${v1_name} | grep "Child in state" | wc -l) }
8 changes: 7 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 [-h] [-n ident] [-p] [-S secretfile] [-T [address]:port] [-t timeout] [-e] [command [...]]


DESCRIPTION
Expand Down Expand Up @@ -62,6 +62,12 @@ OPTIONS
-t timeout
Wait no longer than this many seconds for an operation to finish.

-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.

The syntax and operation of the actual CLI interface is described in
the :ref:`varnish-cli(7)` manual page. Parameters are described in
Expand Down

0 comments on commit eec728a

Please sign in to comment.