[lxc-devel] [lxd/master] lxd/containers: Emit lifecycle event on user shutdown

stgraber on Github lxc-bot at linuxcontainers.org
Thu Mar 26 19:09:00 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200326/8718ec5c/attachment-0001.bin>
-------------- next part --------------
From c2d8cc63aee70c188955dbefa32191463c03769a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 26 Mar 2020 15:08:44 -0400
Subject: [PATCH] lxd/containers: Emit lifecycle event on user shutdown
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/instance/drivers/driver_lxc.go | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index 1d08e49f82..5bafe611bf 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -2796,16 +2796,16 @@ func (c *lxc) onStop(args map[string]string) error {
 	}
 
 	// Log user actions
-	if op == nil {
-		ctxMap := log.Ctx{
-			"project":   c.project,
-			"name":      c.name,
-			"action":    target,
-			"created":   c.creationDate,
-			"ephemeral": c.ephemeral,
-			"used":      c.lastUsedDate,
-			"stateful":  false}
+	ctxMap := log.Ctx{
+		"project":   c.project,
+		"name":      c.name,
+		"action":    target,
+		"created":   c.creationDate,
+		"ephemeral": c.ephemeral,
+		"used":      c.lastUsedDate,
+		"stateful":  false}
 
+	if op == nil {
 		logger.Info(fmt.Sprintf("Container initiated %s", target), ctxMap)
 	}
 
@@ -2845,6 +2845,13 @@ func (c *lxc) onStop(args map[string]string) error {
 			logger.Error("Unable to remove disk devices", log.Ctx{"container": c.Name(), "err": err})
 		}
 
+		// Log and emit lifecycle if not user triggered
+		if op == nil {
+			logger.Info("Shut down container", ctxMap)
+			c.state.Events.SendLifecycle(c.project, "container-shutdown",
+				fmt.Sprintf("/1.0/containers/%s", c.name), nil)
+		}
+
 		// Reboot the container
 		if target == "reboot" {
 			// Start the container again


More information about the lxc-devel mailing list