[lxc-devel] [lxd/master] lxc/action: add `--all` flag

schu on Github lxc-bot at linuxcontainers.org
Thu Dec 14 13:39:40 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 421 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171214/bdc86383/attachment.bin>
-------------- next part --------------
From bad631fae2622151227a4643013d9bc18b13dec8 Mon Sep 17 00:00:00 2001
From: Michael Schubert <michael at kinvolk.io>
Date: Thu, 14 Dec 2017 14:15:27 +0100
Subject: [PATCH] lxc/action: add `--all` flag

Allow running actions (start, stop, ...) against all containers.

Signed-off-by: Michael Schubert <michael at kinvolk.io>
---
 lxc/action.go        | 27 ++++++++++++++++++++++++---
 test/suites/basic.sh | 13 +++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/lxc/action.go b/lxc/action.go
index 50351e408..72a5840d8 100644
--- a/lxc/action.go
+++ b/lxc/action.go
@@ -22,6 +22,7 @@ type actionCmd struct {
 	force       bool
 	stateful    bool
 	stateless   bool
+	all         bool
 }
 
 func (c *actionCmd) showByDefault() bool {
@@ -35,7 +36,7 @@ func (c *actionCmd) usage() string {
 	}
 
 	return fmt.Sprintf(i18n.G(
-		`Usage: lxc %s [<remote>:]<container> [[<remote>:]<container>...]
+		`Usage: lxc %s [--all] [<remote>:]<container> [[<remote>:]<container>...]
 
 %s%s`), c.name, c.description, extra)
 }
@@ -48,6 +49,7 @@ func (c *actionCmd) flags() {
 	}
 	gnuflag.BoolVar(&c.stateful, "stateful", false, i18n.G("Store the container state (only for stop)"))
 	gnuflag.BoolVar(&c.stateless, "stateless", false, i18n.G("Ignore the container state (only for start)"))
+	gnuflag.BoolVar(&c.all, "all", false, i18n.G("Run command against all containers"))
 }
 
 func (c *actionCmd) doAction(conf *config.Config, nameArg string) error {
@@ -110,12 +112,31 @@ func (c *actionCmd) doAction(conf *config.Config, nameArg string) error {
 }
 
 func (c *actionCmd) run(conf *config.Config, args []string) error {
+	var names []string
 	if len(args) == 0 {
-		return errArgs
+		if !c.all {
+			return errArgs
+		}
+		d, err := conf.GetContainerServer(conf.DefaultRemote)
+		if err != nil {
+			return err
+		}
+		ctslist, err := d.GetContainers()
+		if err != nil {
+			return err
+		}
+		for _, ct := range ctslist {
+			names = append(names, ct.Name)
+		}
+	} else {
+		if c.all {
+			return fmt.Errorf("Both --all and container names given")
+		}
+		names = args
 	}
 
 	// Run the action for every listed container
-	results := runBatch(args, func(name string) error { return c.doAction(conf, name) })
+	results := runBatch(names, func(name string) error { return c.doAction(conf, name) })
 
 	// Single container is easy
 	if len(results) == 1 {
diff --git a/test/suites/basic.sh b/test/suites/basic.sh
index 7067b2d47..692304bed 100644
--- a/test/suites/basic.sh
+++ b/test/suites/basic.sh
@@ -436,6 +436,19 @@ test_basic_usage() {
   # Cleanup the containers
   lxc delete --force c1 c2 c3
 
+  # Test --all flag
+  lxc init testimage c1
+  lxc init testimage c2
+  lxc start --all
+  lxc list | grep c1 | grep RUNNING
+  lxc list | grep c2 | grep RUNNING
+  ! lxc stop --all c1 || false
+  lxc stop --all
+  lxc list | grep c1 | grep STOPPED
+  lxc list | grep c2 | grep STOPPED
+  # Cleanup the containers
+  lxc delete --force c1 c2
+
   # Ephemeral
   lxc launch testimage foo -e
 


More information about the lxc-devel mailing list