From 0f027869da6fe8c448d374eb272b2f394c688937 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Graber?= Date: Thu, 1 May 2014 18:35:21 -0400 Subject: [PATCH] lxc-ls: Update lxc.group handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This introduces a new -g/--group argument to filter containers based on their groups. This supports the rather obvious: --group blah Which will only list containers that are in group blah. It may also be passed multiple times: --group blah --group bleh Which will list containers that are in either (or both) blah or bleh. And it also takes: --group blah,bleh --group doh Which will list containers that are either in BOTH blah and bleh or in doh. Signed-off-by: Stéphane Graber Acked-by: Michael H. Warfield Acked-by: Serge E. Hallyn --- doc/lxc-ls.sgml.in | 13 +++++++++++++ src/lxc/lxc-ls.in | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/doc/lxc-ls.sgml.in b/doc/lxc-ls.sgml.in index 1c40d33de..20e31b2c0 100644 --- a/doc/lxc-ls.sgml.in +++ b/doc/lxc-ls.sgml.in @@ -56,6 +56,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA --stopped -f -F format + -g groups --nesting filter @@ -150,6 +151,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + + + + + + Comma separated list of groups the container must have to be displayed. + The parameter may be passed multiple times. + + + + diff --git a/src/lxc/lxc-ls.in b/src/lxc/lxc-ls.in index 3384dcba3..a165166d9 100755 --- a/src/lxc/lxc-ls.in +++ b/src/lxc/lxc-ls.in @@ -103,8 +103,8 @@ def get_root_path(path): # Constants FIELDS = ("name", "state", "interfaces", "ipv4", "ipv6", "autostart", "pid", - "memory", "ram", "swap") -DEFAULT_FIELDS = ("name", "state", "ipv4", "ipv6", "autostart") + "memory", "ram", "swap", "groups") +DEFAULT_FIELDS = ("name", "state", "ipv4", "ipv6", "groups", "autostart") # Begin parsing the command line parser = argparse.ArgumentParser(description=_("LXC: List containers"), @@ -141,6 +141,11 @@ parser.add_argument("-F", "--fancy-format", type=str, default=",".join(DEFAULT_FIELDS), help=_("comma separated list of fields to show")) +parser.add_argument("-g", "--groups", type=str, action="append", + metavar="GROUPS", + help=_("groups (comma separated) the container must " + "be a member of")) + parser.add_argument("--nesting", dest="nesting", action="store_true", help=_("show nested containers")) @@ -189,6 +194,10 @@ if args.nesting: parser.error(_("Showing nested containers requires setns to the " "PID namespace which your kernel doesn't support.")) +## Check that -g is passed alongside -f +if args.groups and not args.fancy: + parser.error(_("Group filtering requires fancy formatting.")) + # Set the actual lxcpath value if not args.lxcpath: args.lxcpath = lxc.default_config_path @@ -229,6 +238,19 @@ def get_containers(fd=None, base="/", root=False): except: continue + if args.groups: + try: + set_has = set(container.get_config_item("lxc.group")) + except KeyError: + set_has = set() + + for group in args.groups: + set_must = set(group.split(",")) + if not set_must - set_has: + break + else: + continue + if container.controllable: state = container.state else: @@ -254,15 +276,20 @@ def get_containers(fd=None, base="/", root=False): elif container.init_pid != -1: entry['pid'] = str(container.init_pid) + if 'groups' in args.fancy_format: + entry['groups'] = "-" + try: + groups = container.get_config_item("lxc.group") + if len(groups) > 0: + entry['groups'] = ", ".join(groups) + except KeyError: + pass + if 'autostart' in args.fancy_format: entry['autostart'] = "NO" try: if container.get_config_item("lxc.start.auto") == "1": entry['autostart'] = "YES" - - groups = container.get_config_item("lxc.group") - if len(groups) > 0: - entry['autostart'] = "YES (%s)" % ", ".join(groups) except KeyError: pass -- 2.47.2