[lxc-devel] [lxd/master] lxd/logging: Handle projects in log expiry

stgraber on Github lxc-bot at linuxcontainers.org
Thu Feb 20 22:51:18 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/20200220/08287a32/attachment.bin>
-------------- next part --------------
From 8501c5e56782aae8ff1af2451788325f1d378fd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 20 Feb 2020 17:50:53 -0500
Subject: [PATCH] lxd/logging: Handle projects in log expiry
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #6910

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxd/logging.go | 46 +++++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 25 deletions(-)

diff --git a/lxd/logging.go b/lxd/logging.go
index facc9be338..86787358dd 100644
--- a/lxd/logging.go
+++ b/lxd/logging.go
@@ -7,8 +7,10 @@ import (
 	"time"
 
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/instance/instancetype"
 	"github.com/lxc/lxd/lxd/operations"
+	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/state"
 	"github.com/lxc/lxd/lxd/task"
 	"github.com/lxc/lxd/shared"
@@ -43,30 +45,24 @@ func expireLogsTask(state *state.State) (task.Func, task.Schedule) {
 }
 
 func expireLogs(ctx context.Context, state *state.State) error {
-	entries, err := ioutil.ReadDir(state.OS.LogDir)
+	// List the instances.
+	instances, err := instance.LoadNodeAll(state, instancetype.Any)
 	if err != nil {
 		return err
 	}
 
-	// FIXME: our DB APIs don't yet support cancellation, se we need to run
-	//        them in a goroutine and abort this task if the context gets
-	//        cancelled.
-	var containers []string
-	ch := make(chan struct{})
-	go func() {
-		containers, err = state.Cluster.ContainersNodeList(instancetype.Container)
-		ch <- struct{}{}
-	}()
-	select {
-	case <-ctx.Done():
-		return nil // Context expired
-	case <-ch:
-	}
-
+	// List the directory.
+	entries, err := ioutil.ReadDir(state.OS.LogDir)
 	if err != nil {
 		return err
 	}
 
+	// Build the expected names.
+	names := []string{}
+	for _, inst := range instances {
+		names = append(names, project.Prefix(inst.Project(), inst.Name()))
+	}
+
 	newestFile := func(path string, dir os.FileInfo) time.Time {
 		newest := dir.ModTime()
 
@@ -92,14 +88,14 @@ func expireLogs(ctx context.Context, state *state.State) error {
 		default:
 		}
 
-		// We only care about container directories
+		// We only care about instance directories.
 		if !entry.IsDir() {
 			continue
 		}
 
-		// Check if the container still exists
-		if shared.StringInSlice(entry.Name(), containers) {
-			// Remove any log file which wasn't modified in the past 48 hours
+		// Check if the container still exists.
+		if shared.StringInSlice(entry.Name(), names) {
+			// Remove any log file which wasn't modified in the past 48 hours.
 			logs, err := ioutil.ReadDir(shared.LogPath(entry.Name()))
 			if err != nil {
 				return err
@@ -108,12 +104,12 @@ func expireLogs(ctx context.Context, state *state.State) error {
 			for _, logfile := range logs {
 				path := shared.LogPath(entry.Name(), logfile.Name())
 
-				// Always keep the LXC config
-				if logfile.Name() == "lxc.conf" {
+				// Always keep the config files.
+				if logfile.Name() == "lxc.conf" || logfile.Name() == "qemu.conf" {
 					continue
 				}
 
-				// Deal with directories (snapshots)
+				// Deal with directories (snapshots).
 				if logfile.IsDir() {
 					newest := newestFile(path, logfile)
 					if time.Since(newest).Hours() >= 48 {
@@ -126,7 +122,7 @@ func expireLogs(ctx context.Context, state *state.State) error {
 					continue
 				}
 
-				// Individual files
+				// Individual files.
 				if time.Since(logfile.ModTime()).Hours() >= 48 {
 					err := os.Remove(path)
 					if err != nil {
@@ -135,7 +131,7 @@ func expireLogs(ctx context.Context, state *state.State) error {
 				}
 			}
 		} else {
-			// Empty directory if unchanged in the past 24 hours
+			// Empty directory if unchanged in the past 24 hours.
 			path := shared.LogPath(entry.Name())
 			newest := newestFile(path, entry)
 			if time.Since(newest).Hours() >= 24 {


More information about the lxc-devel mailing list