[lxc-devel] [lxd/master] Make it possible to have templates only apply for non-existing files

stgraber on Github lxc-bot at linuxcontainers.org
Tue Mar 15 21:25:38 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 664 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160315/0ad2507e/attachment.bin>
-------------- next part --------------
From 0d393544248367e6370d9f346a16af85e1b37abf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 15 Mar 2016 17:23:52 -0400
Subject: [PATCH] Make it possible to have templates only apply for
 non-existing files
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We unfortunately can't use the suggested "overwrite" keyword because
we'd need it to default to true when unset and the yaml parser doesn't
let us do that.

So instead we call it "create_only" and have it default to false
(default for bool types) which gives us an equivalent behavior.

Closes #1757

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go    | 4 ++++
 lxd/images.go           | 7 ++++---
 specs/image-handling.md | 7 +++++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index f6e80b7..5eec978 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2626,6 +2626,10 @@ func (c *containerLXC) TemplateApply(trigger string) error {
 		// Open the file to template, create if needed
 		fullpath := filepath.Join(c.RootfsPath(), strings.TrimLeft(templatePath, "/"))
 		if shared.PathExists(fullpath) {
+			if template.CreateOnly {
+				continue
+			}
+
 			// Open the existing file
 			w, err = os.Create(fullpath)
 			if err != nil {
diff --git a/lxd/images.go b/lxd/images.go
index 4c54274..96a9f8e 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -144,9 +144,10 @@ func compressFile(path string, compress string) (string, error) {
 }
 
 type templateEntry struct {
-	When       []string
-	Template   string
-	Properties map[string]string
+	When       []string          `yaml:"when"`
+	CreateOnly bool              `yaml:"create_only"`
+	Template   string            `yaml:"template"`
+	Properties map[string]string `yaml:"properties"`
 }
 
 type imagePostReq struct {
diff --git a/specs/image-handling.md b/specs/image-handling.md
index 14a8efe..6a47440 100644
--- a/specs/image-handling.md
+++ b/specs/image-handling.md
@@ -77,6 +77,11 @@ LXD, at the moment, this contains:
         when:
           - start
         template: hostname.tpl
+      /etc/network/interfaces:
+        when:
+          - create
+        template: interfaces.tpl
+        create_only: true
 
 The architecture and creation\_date fields are mandatory, the properties
 are just a set of default properties for the image. The os, release,
@@ -96,6 +101,8 @@ The templates will always receive the following context:
  - devices: key/value map of the devices assigned to this container (map[string]map[string]string)
  - properties: key/value map of the template properties specified in metadata.yaml (map[string]string)
 
+The "create\_only" key can be set to have LXD only only create missing files but not overwrite an existing file.
+
 As a general rule, you should never template a file which is owned by a
 package or is otherwise expected to be overwritten by normal operation
 of the container.


More information about the lxc-devel mailing list