[lxc-devel] [lxd/master] lxc {copy,move}: improve error handling

brauner on Github lxc-bot at linuxcontainers.org
Fri Apr 28 09:24:38 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170428/29378b2f/attachment.bin>
-------------- next part --------------
From 129fc5520ae968a5c94e68844b24ad17fce1e07c Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 28 Apr 2017 11:23:13 +0200
Subject: [PATCH] lxc {copy,move}: improve error handling

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxc/copy.go | 27 ++++++++++++++++++---------
 lxc/move.go | 12 ++++++++++--
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/lxc/copy.go b/lxc/copy.go
index d2ebf4e..95127c6 100644
--- a/lxc/copy.go
+++ b/lxc/copy.go
@@ -216,17 +216,20 @@ func (c *copyCmd) copyContainer(config *lxd.Config, sourceResource string, destR
 		if err != nil {
 			msg[senderid] = err
 			ch <- msg
+			return
 		}
 
 		msg[senderid] = nil
 		ch <- msg
 	}
+
+	var migrationErrFromClient error
 	for _, addr := range addresses {
 		var migration *api.Response
 
 		sourceWSUrl := "https://" + addr + sourceWSResponse.Operation
-		migration, err = dest.MigrateFrom(destName, sourceWSUrl, source.Certificate, secrets, status.Architecture, status.Config, status.Devices, status.Profiles, baseImage, ephemeral == 1, false, source, sourceWSResponse.Operation, containerOnly)
-		if err != nil {
+		migration, migrationErrFromClient = dest.MigrateFrom(destName, sourceWSUrl, source.Certificate, secrets, status.Architecture, status.Config, status.Devices, status.Profiles, baseImage, ephemeral == 1, false, source, sourceWSResponse.Operation, containerOnly)
+		if migrationErrFromClient != nil {
 			continue
 		}
 
@@ -238,18 +241,24 @@ func (c *copyCmd) copyContainer(config *lxd.Config, sourceResource string, destR
 		sourceOpId := 1
 		go wait(source, sourceWSResponse.Operation, waitchan, sourceOpId)
 
-		opStatus := make([]map[int]error, 2)
+		var sourceOpErr error
+		var destOpErr error
 		for i := 0; i < cap(waitchan); i++ {
-			opStatus[i] = <-waitchan
+			tmp := <-waitchan
+			err, ok := tmp[sourceOpId]
+			if ok {
+				sourceOpErr = err
+			} else {
+				destOpErr = err
+			}
 		}
 
-		if opStatus[0][destOpId] != nil {
+		if destOpErr != nil {
 			continue
 		}
 
-		err = opStatus[1][sourceOpId]
-		if err != nil {
-			return err
+		if sourceOpErr != nil {
+			return sourceOpErr
 		}
 
 		if destResource == "" {
@@ -277,7 +286,7 @@ func (c *copyCmd) copyContainer(config *lxd.Config, sourceResource string, destR
 	}
 
 	// Return the error from destination
-	return fmt.Errorf(i18n.G("Migration failed on target host: %s"), err)
+	return fmt.Errorf(i18n.G("Migration failed on target host: %s"), migrationErrFromClient)
 }
 
 func (c *copyCmd) run(config *lxd.Config, args []string) error {
diff --git a/lxc/move.go b/lxc/move.go
index 9f7d258..56834a8 100644
--- a/lxc/move.go
+++ b/lxc/move.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"github.com/lxc/lxd"
 	"github.com/lxc/lxd/shared/i18n"
 
@@ -66,9 +67,16 @@ func (c *moveCmd) run(config *lxd.Config, args []string) error {
 
 	// A move is just a copy followed by a delete; however, we want to
 	// keep the volatile entries around since we are moving the container.
-	if err := cpy.copyContainer(config, args[0], args[1], true, -1, true, c.containerOnly); err != nil {
+	err := cpy.copyContainer(config, args[0], args[1], true, -1, true, c.containerOnly)
+	if err != nil {
 		return err
 	}
 
-	return commands["delete"].run(config, args[:1])
+	err = commands["delete"].run(config, args[:1])
+	if err != nil {
+		fmt.Println("asdf")
+		return err
+	}
+
+	return nil
 }


More information about the lxc-devel mailing list