[lxc-devel] [lxd/master] lxd/instances/post: Delete restored instance on backup post hook failure

tomponline on Github lxc-bot at linuxcontainers.org
Fri May 15 14:07:43 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 361 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200515/0ddb8c36/attachment.bin>
-------------- next part --------------
From 61ea7804fc3352f8c44929544e6c5c8ef7407ea4 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 15 May 2020 15:06:50 +0100
Subject: [PATCH] lxd/instances/post: Delete restored instance on backup post
 hook failure

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instances_post.go | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lxd/instances_post.go b/lxd/instances_post.go
index 01878626fe..d036908234 100644
--- a/lxd/instances_post.go
+++ b/lxd/instances_post.go
@@ -687,15 +687,18 @@ func createFromBackup(d *Daemon, project string, data io.Reader, pool string) re
 			return fmt.Errorf("Internal import request: %v", resp.String())
 		}
 
-		c, err := instance.LoadByProjectAndName(d.State(), project, bInfo.Name)
+		inst, err := instance.LoadByProjectAndName(d.State(), project, bInfo.Name)
 		if err != nil {
 			return errors.Wrap(err, "Load instance")
 		}
 
+		// Clean up created instance if the post hook fails below.
+		runRevert.Add(func() { inst.Delete() })
+
 		// Run the storage post hook to perform any final actions now that the instance has been created
 		// in the database (this normally includes unmounting volumes that were mounted).
 		if postHook != nil {
-			err = postHook(c)
+			err = postHook(inst)
 			if err != nil {
 				return errors.Wrap(err, "Post hook")
 			}


More information about the lxc-devel mailing list