[lxc-devel] [lxd/master] images: Properly extract the image expiry

stgraber on Github lxc-bot at linuxcontainers.org
Tue Sep 19 19:20:04 UTC 2017


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/20170919/dbf09e27/attachment.bin>
-------------- next part --------------
From 92e52ddbb426c36e340b182a49b696ab27758021 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 19 Sep 2017 15:18:45 -0400
Subject: [PATCH] images: Properly extract the image expiry
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/db/images.go | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/lxd/db/images.go b/lxd/db/images.go
index 66e1d6be1..b2d259925 100644
--- a/lxd/db/images.go
+++ b/lxd/db/images.go
@@ -40,11 +40,14 @@ func ImagesGet(db *sql.DB, public bool) ([]string, error) {
 }
 
 func ImagesGetExpired(db *sql.DB, expiry int64) ([]string, error) {
-	q := `SELECT fingerprint FROM images WHERE cached=1 AND creation_date<=strftime('%s', date('now', '-` + fmt.Sprintf("%d", expiry) + ` day'))`
+	q := `SELECT fingerprint, last_use_date, upload_date FROM images WHERE cached=1`
+
+	var fpStr string
+	var useStr string
+	var uploadStr string
 
-	var fp string
 	inargs := []interface{}{}
-	outfmt := []interface{}{fp}
+	outfmt := []interface{}{fpStr, useStr, uploadStr}
 	dbResults, err := QueryScan(db, q, inargs, outfmt)
 	if err != nil {
 		return []string{}, err
@@ -52,10 +55,28 @@ func ImagesGetExpired(db *sql.DB, expiry int64) ([]string, error) {
 
 	results := []string{}
 	for _, r := range dbResults {
+		// Figure out the expiry
+		timestamp := r[2]
+		if r[1] != "" {
+			timestamp = r[1]
+		}
+
+		var imageExpiry time.Time
+		err = imageExpiry.UnmarshalText([]byte(timestamp.(string)))
+		if err != nil {
+			return []string{}, err
+		}
+		imageExpiry = imageExpiry.Add(time.Duration(expiry*24) * time.Hour)
+
+		// Check if expired
+		if imageExpiry.After(time.Now()) {
+			continue
+		}
+
 		results = append(results, r[0].(string))
 	}
 
-	return results, nil
+	return []string{}, nil
 }
 
 func ImageSourceInsert(db *sql.DB, imageId int, server string, protocol string, certificate string, alias string) error {


More information about the lxc-devel mailing list