[lxc-devel] [lxd/master] lxd/containers: Fix template handling

stgraber on Github lxc-bot at linuxcontainers.org
Fri Jul 5 23:26:28 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 451 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190705/7bbf29d6/attachment.bin>
-------------- next part --------------
From a15792a22d43890b0eccd0961ca6723e328888fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 5 Jul 2019 19:23:01 -0400
Subject: [PATCH] lxd/containers: Fix template handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix error messages and handle cases where templates.yaml or templates
directory are missing.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_metadata.go | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/lxd/container_metadata.go b/lxd/container_metadata.go
index 36d139890a..adfce53c57 100644
--- a/lxd/container_metadata.go
+++ b/lxd/container_metadata.go
@@ -47,6 +47,11 @@ func containerMetadataGet(d *Daemon, r *http.Request) Response {
 		defer c.StorageStop()
 	}
 
+	// If missing, just return empty result
+	if !shared.PathExists(metadataPath) {
+		return SyncResponse(true, api.ImageMetadata{})
+	}
+
 	// Read the metadata
 	metadataFile, err := os.Open(metadataPath)
 	if err != nil {
@@ -149,6 +154,11 @@ func containerMetadataTemplatesGet(d *Daemon, r *http.Request) Response {
 	// Look at the request
 	templateName := r.FormValue("path")
 	if templateName == "" {
+		templates := []string{}
+		if !shared.PathExists(filepath.Join(c.Path(), "templates")) {
+			return SyncResponse(true, templates)
+		}
+
 		// List templates
 		templatesPath := filepath.Join(c.Path(), "templates")
 		filesInfo, err := ioutil.ReadDir(templatesPath)
@@ -156,7 +166,6 @@ func containerMetadataTemplatesGet(d *Daemon, r *http.Request) Response {
 			return InternalError(err)
 		}
 
-		templates := []string{}
 		for _, info := range filesInfo {
 			if !info.IsDir() {
 				templates = append(templates, info.Name())
@@ -173,7 +182,7 @@ func containerMetadataTemplatesGet(d *Daemon, r *http.Request) Response {
 	}
 
 	if !shared.PathExists(templatePath) {
-		return NotFound(fmt.Errorf("Path '%s' not found", templatePath))
+		return NotFound(fmt.Errorf("Template '%s' not found", templateName))
 	}
 
 	// Create a temporary file with the template content (since the container
@@ -237,6 +246,13 @@ func containerMetadataTemplatesPostPut(d *Daemon, r *http.Request) Response {
 		return BadRequest(fmt.Errorf("missing path argument"))
 	}
 
+	if !shared.PathExists(filepath.Join(c.Path(), "templates")) {
+		err := os.MkdirAll(filepath.Join(c.Path(), "templates"), 0711)
+		if err != nil {
+			return SmartError(err)
+		}
+	}
+
 	// Check if the template already exists
 	templatePath, err := getContainerTemplatePath(c, templateName)
 	if err != nil {
@@ -304,7 +320,7 @@ func containerMetadataTemplatesDelete(d *Daemon, r *http.Request) Response {
 	}
 
 	if !shared.PathExists(templatePath) {
-		return NotFound(fmt.Errorf("Path '%s' not found", templatePath))
+		return NotFound(fmt.Errorf("Template '%s' not found", templateName))
 	}
 
 	// Delete the template


More information about the lxc-devel mailing list