[lxc-devel] [lxd/master] lxc/action: Properly handle --all with remotes

stgraber on Github lxc-bot at linuxcontainers.org
Sun Jun 14 19:40:56 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200614/16310bcb/attachment.bin>
-------------- next part --------------
From f6f18a6c6966de1318a643cfde829ebcc6b2cc93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 14 Jun 2020 15:40:08 -0400
Subject: [PATCH] lxc/action: Properly handle --all with remotes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #7529

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxc/action.go | 49 +++++++++++++++++++++++++++----------------------
 lxc/main.go   |  4 +++-
 2 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/lxc/action.go b/lxc/action.go
index d595464ef7..2a715a4d31 100644
--- a/lxc/action.go
+++ b/lxc/action.go
@@ -205,39 +205,44 @@ func (c *cmdAction) Run(cmd *cobra.Command, args []string) error {
 	conf := c.global.conf
 
 	var names []string
-	if len(args) == 0 {
-		if !c.flagAll {
-			cmd.Help()
-			return nil
+	if c.flagAll {
+		// If no server passed, use current default.
+		if len(args) == 0 {
+			args = []string{fmt.Sprintf("%s:", conf.DefaultRemote)}
 		}
 
-		d, err := conf.GetInstanceServer(conf.DefaultRemote)
+		// Get all the servers.
+		resources, err := c.global.ParseServers(args...)
 		if err != nil {
 			return err
 		}
 
-		ctslist, err := d.GetInstances(api.InstanceTypeAny)
-		if err != nil {
-			return err
-		}
+		for _, resource := range resources {
+			// We don't allow instance names with --all.
+			if resource.name != "" {
+				return fmt.Errorf(i18n.G("Both --all and instance name given"))
+			}
 
-		for _, ct := range ctslist {
-			switch cmd.Name() {
-			case "start":
-				if ct.StatusCode == api.Running {
-					continue
-				}
-			case "stop":
-				if ct.StatusCode == api.Stopped {
-					continue
+			ctslist, err := resource.server.GetInstances(api.InstanceTypeAny)
+			if err != nil {
+				return err
+			}
+
+			for _, ct := range ctslist {
+				switch cmd.Name() {
+				case "start":
+					if ct.StatusCode == api.Running {
+						continue
+					}
+				case "stop":
+					if ct.StatusCode == api.Stopped {
+						continue
+					}
 				}
+				names = append(names, fmt.Sprintf("%s:%s", resource.remote, ct.Name))
 			}
-			names = append(names, ct.Name)
 		}
 	} else {
-		if c.flagAll {
-			return fmt.Errorf(i18n.G("Both --all and instance name given"))
-		}
 		names = args
 	}
 
diff --git a/lxc/main.go b/lxc/main.go
index c247818110..902c0ecea5 100644
--- a/lxc/main.go
+++ b/lxc/main.go
@@ -370,6 +370,7 @@ func (c *cmdGlobal) PostRun(cmd *cobra.Command, args []string) error {
 }
 
 type remoteResource struct {
+	remote string
 	server lxd.InstanceServer
 	name   string
 }
@@ -387,7 +388,8 @@ func (c *cmdGlobal) ParseServers(remotes ...string) ([]remoteResource, error) {
 
 		// Setup the struct
 		resource := remoteResource{
-			name: name,
+			remote: remoteName,
+			name:   name,
 		}
 
 		// Look at our cache


More information about the lxc-devel mailing list