lxc-info: add option -t, --state-is=STATE to test for a given test

Add an option to test for a give state. This is useful for scripts.
It lets us you do thing like:

 if lxc-info --name myname --state-is RUNNING; then
   ...

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Natanael Copa 2012-11-26 20:04:32 +01:00 committed by Stéphane Graber
parent 83cab6e068
commit b917ef7522

View File

@ -34,12 +34,14 @@
static bool state;
static bool pid;
static char *test_state = NULL;
static int my_parser(struct lxc_arguments* args, int c, char* arg)
{
switch (c) {
case 's': state = true; break;
case 'p': pid = true; break;
case 't': test_state = arg; break;
}
return 0;
}
@ -47,6 +49,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
static const struct option my_longopts[] = {
{"state", no_argument, 0, 's'},
{"pid", no_argument, 0, 'p'},
{"state-is", required_argument, 0, 't'},
LXC_COMMON_OPTIONS,
};
@ -58,9 +61,11 @@ static struct lxc_arguments my_args = {
lxc-info display some information about a container with the identifier NAME\n\
\n\
Options :\n\
-n, --name=NAME NAME for name of the container\n\
-s, --state shows the state of the container\n\
-p, --pid shows the process id of the init container\n",
-n, --name=NAME NAME for name of the container\n\
-s, --state shows the state of the container\n\
-p, --pid shows the process id of the init container\n\
-t, --state-is=STATE test if current state is STATE\n\
returns success if it matches, false otherwise\n",
.options = my_longopts,
.parser = my_parser,
.checker = NULL,
@ -81,10 +86,12 @@ int main(int argc, char *argv[])
if (!state && !pid)
state = pid = true;
if (state) {
if (state || test_state) {
ret = lxc_getstate(my_args.name);
if (ret < 0)
return 1;
if (test_state)
return strcmp(lxc_state2str(ret), test_state) != 0;
printf("state:%10s\n", lxc_state2str(ret));
}