[lxc-devel] [distrobuilder/master] Add LXC Templating

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon Feb 26 13:41:38 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 309 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180226/79661e5f/attachment.bin>
-------------- next part --------------
From 17e68c52812566721b1b8b80fe42cec3576f32b8 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 26 Feb 2018 14:20:25 +0100
Subject: [PATCH 1/2] image: Add LXC templating

Resolves #14.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 image/image.go      | 24 ++++++++++++++++++++
 image/image_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 image/lxc.go        | 13 ++++++++++-
 image/lxd.go        | 43 +++++++++++------------------------
 4 files changed, 113 insertions(+), 31 deletions(-)
 create mode 100644 image/image.go
 create mode 100644 image/image_test.go

diff --git a/image/image.go b/image/image.go
new file mode 100644
index 0000000..a1be0aa
--- /dev/null
+++ b/image/image.go
@@ -0,0 +1,24 @@
+package image
+
+import pongo2 "gopkg.in/flosch/pongo2.v3"
+
+func renderTemplate(template string, ctx pongo2.Context) (string, error) {
+	var (
+		err error
+		ret string
+	)
+
+	// Load template from string
+	tpl, err := pongo2.FromString(template)
+	if err != nil {
+		return ret, err
+	}
+
+	// Get rendered template
+	ret, err = tpl.Execute(ctx)
+	if err != nil {
+		return ret, err
+	}
+
+	return ret, err
+}
diff --git a/image/image_test.go b/image/image_test.go
new file mode 100644
index 0000000..200f189
--- /dev/null
+++ b/image/image_test.go
@@ -0,0 +1,64 @@
+package image
+
+import (
+	"log"
+	"testing"
+
+	pongo2 "gopkg.in/flosch/pongo2.v3"
+)
+
+func TestRenderTemplate(t *testing.T) {
+	tests := []struct {
+		name       string
+		context    pongo2.Context
+		template   string
+		expected   string
+		shouldFail bool
+	}{
+		{
+			"valid template",
+			pongo2.Context{
+				"foo": "bar",
+			},
+			"{{ foo }}",
+			"bar",
+			false,
+		},
+		{
+			"variable not in context",
+			pongo2.Context{},
+			"{{ foo }}",
+			"",
+			false,
+		},
+		{
+			"invalid template",
+			pongo2.Context{
+				"foo": nil,
+			},
+			"{{ foo }",
+			"",
+			true,
+		},
+		{
+			"invalid context",
+			pongo2.Context{
+				"foo.bar": nil,
+			},
+			"{{ foo.bar }}",
+			"",
+			true,
+		},
+	}
+
+	for i, tt := range tests {
+		log.Printf("Running test #%d: %s", i, tt.name)
+		ret, err := renderTemplate(tt.template, tt.context)
+		if tt.shouldFail && err == nil {
+			t.Fatal("test should have failed")
+		}
+		if ret != tt.expected {
+			t.Fatalf("expected '%s', got '%s'", tt.expected, ret)
+		}
+	}
+}
diff --git a/image/lxc.go b/image/lxc.go
index 0af4f2a..449eba4 100644
--- a/image/lxc.go
+++ b/image/lxc.go
@@ -7,6 +7,8 @@ import (
 	"strings"
 	"time"
 
+	pongo2 "gopkg.in/flosch/pongo2.v3"
+
 	"github.com/lxc/distrobuilder/shared"
 )
 
@@ -134,7 +136,16 @@ func (l *LXCImage) writeMetadata(filename, content string) error {
 	}
 	defer file.Close()
 
-	_, err = file.WriteString(content)
+	ctx := pongo2.Context{
+		"image": l.definition,
+	}
+
+	out, err := renderTemplate(content, ctx)
+	if err != nil {
+		return err
+	}
+
+	_, err = file.WriteString(out)
 	if err != nil {
 		return err
 	}
diff --git a/image/lxd.go b/image/lxd.go
index 73f6b61..783289e 100644
--- a/image/lxd.go
+++ b/image/lxd.go
@@ -61,8 +61,13 @@ func (l *LXDImage) Build(unified bool) error {
 		var fname string
 		paths := []string{"rootfs", "templates", "metadata.yaml"}
 
+		ctx := pongo2.Context{
+			"image":         l.definition,
+			"creation_date": l.creationDate,
+		}
+
 		if l.definition.Name != "" {
-			fname, _ = l.renderTemplate(l.definition.Name)
+			fname, _ = renderTemplate(l.definition.Name, ctx)
 		} else {
 			fname = "lxd"
 		}
@@ -108,12 +113,17 @@ func (l *LXDImage) createMetadata() error {
 	l.Metadata.Properties["os"] = l.definition.Distribution
 	l.Metadata.Properties["release"] = l.definition.Release
 
-	l.Metadata.Properties["description"], err = l.renderTemplate(l.definition.Description)
+	ctx := pongo2.Context{
+		"image":         l.definition,
+		"creation_date": l.creationDate,
+	}
+
+	l.Metadata.Properties["description"], err = renderTemplate(l.definition.Description, ctx)
 	if err != err {
 		return nil
 	}
 
-	l.Metadata.Properties["name"], err = l.renderTemplate(l.definition.Name)
+	l.Metadata.Properties["name"], err = renderTemplate(l.definition.Name, ctx)
 	if err != nil {
 		return err
 	}
@@ -122,30 +132,3 @@ func (l *LXDImage) createMetadata() error {
 
 	return err
 }
-
-func (l *LXDImage) renderTemplate(template string) (string, error) {
-	var (
-		err error
-		ret string
-	)
-
-	ctx := pongo2.Context{
-		"arch":          l.definition.Arch,
-		"os":            l.definition.Distribution,
-		"release":       l.definition.Release,
-		"variant":       l.definition.Variant,
-		"creation_date": l.creationDate.Format("20060201_1504"),
-	}
-
-	tpl, err := pongo2.FromString(template)
-	if err != nil {
-		return ret, err
-	}
-
-	ret, err = tpl.Execute(ctx)
-	if err != nil {
-		return ret, err
-	}
-
-	return ret, err
-}

From 59d87ef199f04f79977911bb5055e5e77a72de9c Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 26 Feb 2018 14:40:03 +0100
Subject: [PATCH 2/2] image: Fix creation date in pongo context

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 image/lxd.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/image/lxd.go b/image/lxd.go
index 783289e..ae3607c 100644
--- a/image/lxd.go
+++ b/image/lxd.go
@@ -63,7 +63,7 @@ func (l *LXDImage) Build(unified bool) error {
 
 		ctx := pongo2.Context{
 			"image":         l.definition,
-			"creation_date": l.creationDate,
+			"creation_date": l.creationDate.Format("20060201_1504"),
 		}
 
 		if l.definition.Name != "" {
@@ -115,7 +115,7 @@ func (l *LXDImage) createMetadata() error {
 
 	ctx := pongo2.Context{
 		"image":         l.definition,
-		"creation_date": l.creationDate,
+		"creation_date": l.creationDate.Format("20060201_1504"),
 	}
 
 	l.Metadata.Properties["description"], err = renderTemplate(l.definition.Description, ctx)


More information about the lxc-devel mailing list