lxc-info: Rework based on mailinglist thread

So this implements the changes we discussed yesterday:
 - Only one container may be queried at the time
 - -n is now required once again
 - -H + a single filter only returns the value
 - -t/--is-state is now removed

Note that -S is considered as more than a single filter, so -H in that
case only affects the formatting of the values.

For the same reason, I haven't yet implemented the -H + multiple filters
case which we said should return a simple "key: value" output as it
wasn't trivial to re-arrange the stats code to print a different format
(for the other options, it's just a two lines change in the print
functions).

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
This commit is contained in:
Stéphane Graber 2013-11-15 15:14:00 -05:00
parent 4df7f012b9
commit 5444216b7e
5 changed files with 54 additions and 121 deletions

View File

@ -58,7 +58,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
<arg choice="opt">-s</arg> <arg choice="opt">-s</arg>
<arg choice="opt">-p</arg> <arg choice="opt">-p</arg>
<arg choice="opt">-i</arg> <arg choice="opt">-i</arg>
<arg choice="opt">-t <replaceable>state</replaceable></arg>
<arg choice="opt">-S</arg> <arg choice="opt">-S</arg>
<arg choice="opt">-H</arg> <arg choice="opt">-H</arg>
</cmdsynopsis> </cmdsynopsis>
@ -212,20 +211,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>
<option><optional>-t <replaceable>state</replaceable></optional></option>
</term>
<listitem>
<para>
<!--
Check whether the container is in the provided state.
-->
コンテナが指定した状態かどうかをチェックする.
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</refsect1> </refsect1>
@ -258,18 +243,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>lxc-info -n foo -t RUNNING</term>
<listitem>
<para>
<!--
exits 0 if foo is RUNNING, 1 otherwise.
-->
もしコンテナ foo が RUNNING であれば 0 を返します.それ以外は 1 を返します.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term>lxc-info -n foo -c lxc.network.0.veth.pair</term> <term>lxc-info -n foo -c lxc.network.0.veth.pair</term>
<listitem> <listitem>

View File

@ -52,7 +52,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
<arg choice="opt">-s</arg> <arg choice="opt">-s</arg>
<arg choice="opt">-p</arg> <arg choice="opt">-p</arg>
<arg choice="opt">-i</arg> <arg choice="opt">-i</arg>
<arg choice="opt">-t <replaceable>state</replaceable></arg>
<arg choice="opt">-S</arg> <arg choice="opt">-S</arg>
<arg choice="opt">-H</arg> <arg choice="opt">-H</arg>
</cmdsynopsis> </cmdsynopsis>
@ -169,17 +168,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>
<option><optional>-t <replaceable>state</replaceable></optional></option>
</term>
<listitem>
<para>
Check whether the container is in the provided state.
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</refsect1> </refsect1>
@ -206,15 +194,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>lxc-info -n foo -t RUNNING</term>
<listitem>
<para>
exits 0 if foo is RUNNING, 1 otherwise.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term>lxc-info -n foo -c lxc.network.0.veth.pair</term> <term>lxc-info -n foo -c lxc.network.0.veth.pair</term>
<listitem> <listitem>

View File

@ -117,7 +117,7 @@ if [ -z "$exec" ]; then
exec @BINDIR@/lxc-unshare -s MOUNT -- $0 -n $name -P "$lxc_path" --exec -- "$@" exec @BINDIR@/lxc-unshare -s MOUNT -- $0 -n $name -P "$lxc_path" --exec -- "$@"
fi fi
if lxc-info -n $name -P "$lxc_path" --state-is 'STOPPED'; then if lxc-wait -n $name -P "$lxc_path" -s 'STOPPED' -t 0; then
echo "$(basename $0): container '$name' is not running" >&2 echo "$(basename $0): container '$name' is not running" >&2
exit 1 exit 1
fi fi

View File

@ -116,7 +116,7 @@ for container in ${containers}; do
container_field_width=${#container} container_field_width=${#container}
fi fi
if ! lxc-info -P $lxc_path -t STOPPED -n $container; then if ! lxc-wait -P $lxc_path -s STOPPED -n $container -t 0; then
initpid=`lxc-info -P $lxc_path -p -n $container | awk -F: '{ print $2 }' | awk '{ print $1 }'` initpid=`lxc-info -P $lxc_path -p -n $container | awk -F: '{ print $2 }' | awk '{ print $1 }'`
cgroup=`head -n 1 /proc/$initpid/cgroup | awk -F: '{ print $3}'` cgroup=`head -n 1 /proc/$initpid/cgroup | awk -F: '{ print $3}'`
if [ -f "$parent_cgroup/$cgroup/tasks" ]; then if [ -f "$parent_cgroup/$cgroup/tasks" ]; then

View File

@ -25,7 +25,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <regex.h>
#include <limits.h> #include <limits.h>
#include <libgen.h> #include <libgen.h>
#include <sys/types.h> #include <sys/types.h>
@ -43,9 +42,9 @@ static bool state;
static bool pid; static bool pid;
static bool stats; static bool stats;
static bool humanize = true; static bool humanize = true;
static char *test_state = NULL;
static char **key = NULL; static char **key = NULL;
static int keys = 0; static int keys = 0;
static int filter_count = 0;
static int my_parser(struct lxc_arguments* args, int c, char* arg) static int my_parser(struct lxc_arguments* args, int c, char* arg)
{ {
@ -55,12 +54,11 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
key[keys] = arg; key[keys] = arg;
keys++; keys++;
break; break;
case 'i': ips = true; break; case 'i': ips = true; filter_count += 1; break;
case 's': state = true; break; case 's': state = true; filter_count += 1; break;
case 'p': pid = true; break; case 'p': pid = true; filter_count += 1; break;
case 'S': stats = true; break; case 'S': stats = true; filter_count += 5; break;
case 'H': humanize = false; break; case 'H': humanize = false; break;
case 't': test_state = arg; break;
} }
return 0; return 0;
} }
@ -72,7 +70,6 @@ static const struct option my_longopts[] = {
{"pid", no_argument, 0, 'p'}, {"pid", no_argument, 0, 'p'},
{"stats", no_argument, 0, 'S'}, {"stats", no_argument, 0, 'S'},
{"no-humanize", no_argument, 0, 'H'}, {"no-humanize", no_argument, 0, 'H'},
{"state-is", required_argument, 0, 't'},
LXC_COMMON_OPTIONS, LXC_COMMON_OPTIONS,
}; };
@ -90,10 +87,8 @@ Options :\n\
-p, --pid shows the process id of the init container\n\ -p, --pid shows the process id of the init container\n\
-S, --stats shows usage stats\n\ -S, --stats shows usage stats\n\
-H, --no-humanize shows stats as raw numbers, not humanized\n\ -H, --no-humanize shows stats as raw numbers, not humanized\n\
-s, --state shows the state of the container\n\ -s, --state shows the state of the container\n",
-t, --state-is=STATE test if current state is STATE\n\ .name = NULL,
returns success if it matches, false otherwise\n",
.name = ".*",
.options = my_longopts, .options = my_longopts,
.parser = my_parser, .parser = my_parser,
.checker = NULL, .checker = NULL,
@ -249,14 +244,40 @@ static void print_stats(struct lxc_container *c)
} }
} }
void print_info_msg_int(const char *key, int value)
{
if (humanize)
printf("%-15s %d\n", key, value);
else {
if (filter_count == 1)
printf("%d\n", value);
else
printf("%-15s %d\n", key, value);
}
}
void print_info_msg_str(const char *key, const char *value)
{
if (humanize)
printf("%-15s %s\n", key, value);
else {
if (filter_count == 1)
printf("%s\n", value);
else
printf("%-15s %s\n", key, value);
}
}
static int print_info(const char *name, const char *lxcpath) static int print_info(const char *name, const char *lxcpath)
{ {
int i; int i;
struct lxc_container *c; struct lxc_container *c;
c = lxc_container_new(name, lxcpath); c = lxc_container_new(name, lxcpath);
if (!c) if (!c) {
fprintf(stderr, "Failure to retrieve information on %s\n", c->name);
return -1; return -1;
}
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name); fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
@ -264,16 +285,19 @@ static int print_info(const char *name, const char *lxcpath)
return -1; return -1;
} }
if (!state && !pid && !ips && !stats && keys <= 0) if (!c->is_running(c) && !c->is_defined(c)) {
fprintf(stderr, "%s doesn't exist\n", c->name);
lxc_container_put(c);
return -1;
}
if (!state && !pid && !ips && !stats && keys <= 0) {
state = pid = ips = stats = true; state = pid = ips = stats = true;
print_info_msg_str("Name:", c->name);
}
printf("%-15s %s\n", "Name:", c->name); if (state) {
print_info_msg_str("State:", c->state(c));
if (state || test_state) {
if (test_state)
return strcmp(c->state(c), test_state) != 0;
printf("%-15s %s\n", "State:", c->state(c));
} }
if (pid) { if (pid) {
@ -281,7 +305,7 @@ static int print_info(const char *name, const char *lxcpath)
initpid = c->init_pid(c); initpid = c->init_pid(c);
if (initpid >= 0) if (initpid >= 0)
printf("%-15s %d\n", "Pid:", initpid); print_info_msg_int("PID:", initpid);
} }
if (ips) { if (ips) {
@ -291,7 +315,7 @@ static int print_info(const char *name, const char *lxcpath)
i = 0; i = 0;
while (addresses[i]) { while (addresses[i]) {
address = addresses[i]; address = addresses[i];
printf("%-15s %s\n", "IP:", address); print_info_msg_str("IP:", address);
i++; i++;
} }
} }
@ -325,63 +349,20 @@ static int print_info(const char *name, const char *lxcpath)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int rc, i, len, ret = EXIT_FAILURE; int ret = EXIT_FAILURE;
char *regexp;
regex_t preg;
int ct_cnt;
char **ct_name;
bool printed;
if (lxc_arguments_parse(&my_args, argc, argv)) if (lxc_arguments_parse(&my_args, argc, argv))
goto err1; return ret;
if (!my_args.log_file) if (!my_args.log_file)
my_args.log_file = "none"; my_args.log_file = "none";
if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
my_args.progname, my_args.quiet, my_args.lxcpath[0])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
goto err1; return ret;
len = strlen(my_args.name) + 3; if (print_info(my_args.name, my_args.lxcpath[0]) == 0)
regexp = malloc(len + 3);
if (!regexp) {
fprintf(stderr, "failed to allocate memory");
goto err1;
}
rc = snprintf(regexp, len, "^%s$", my_args.name);
if (rc < 0 || rc >= len) {
fprintf(stderr, "Name too long");
goto err2;
}
if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
fprintf(stderr, "failed to compile the regex '%s'", my_args.name);
goto err2;
}
printed = false;
ct_cnt = list_all_containers(my_args.lxcpath[0], &ct_name, NULL);
if (ct_cnt < 0)
goto err3;
for (i = 0; i < ct_cnt; i++) {
if (regexec(&preg, ct_name[i], 0, NULL, 0) == 0)
{
if (printed)
printf("\n");
print_info(ct_name[i], my_args.lxcpath[0]);
printed = true;
}
free(ct_name[i]);
}
if (ct_name)
free(ct_name);
ret = EXIT_SUCCESS; ret = EXIT_SUCCESS;
err3:
regfree(&preg);
err2:
free(regexp);
err1:
return ret; return ret;
} }