[lxc-devel] [distrobuilder/master] generators/dump: Allow pongo2 templates for LXD

monstermunchkin on Github lxc-bot at linuxcontainers.org
Fri May 22 14:48:14 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200522/297ce92f/attachment.bin>
-------------- next part --------------
From e59128973490b648e230e8caca242b0986761022 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 22 May 2020 16:47:10 +0200
Subject: [PATCH] generators/dump: Allow pongo2 templates for LXD

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 generators/dump.go      | 21 ++++++++++++++++++---
 generators/dump_test.go | 10 +++++++---
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/generators/dump.go b/generators/dump.go
index d829a55..6f0ee84 100644
--- a/generators/dump.go
+++ b/generators/dump.go
@@ -5,6 +5,8 @@ import (
 	"path/filepath"
 	"strings"
 
+	"github.com/flosch/pongo2"
+
 	"github.com/lxc/distrobuilder/image"
 	"github.com/lxc/distrobuilder/shared"
 )
@@ -15,7 +17,7 @@ type DumpGenerator struct{}
 // RunLXC dumps content to a file.
 func (g DumpGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
 	target shared.DefinitionTargetLXC, defFile shared.DefinitionFile) error {
-	err := g.Run(cacheDir, sourceDir, defFile)
+	err := g.run(cacheDir, sourceDir, defFile, defFile.Content)
 	if err != nil {
 		return err
 	}
@@ -30,13 +32,26 @@ func (g DumpGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
 // RunLXD dumps content to a file.
 func (g DumpGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
 	target shared.DefinitionTargetLXD, defFile shared.DefinitionFile) error {
-	return g.Run(cacheDir, sourceDir, defFile)
+	tpl, err := pongo2.FromString(defFile.Content)
+	if err != nil {
+		return err
+	}
+
+	content, err := tpl.Execute(pongo2.Context{"lxd": target})
+	if err != nil {
+		return err
+	}
+
+	return g.run(cacheDir, sourceDir, defFile, content)
 }
 
 // Run dumps content to a file.
 func (g DumpGenerator) Run(cacheDir, sourceDir string, defFile shared.DefinitionFile) error {
+	return g.run(cacheDir, sourceDir, defFile, defFile.Content)
+}
+
+func (g DumpGenerator) run(cacheDir, sourceDir string, defFile shared.DefinitionFile, content string) error {
 	path := filepath.Join(sourceDir, defFile.Path)
-	content := defFile.Content
 
 	// Create any missing directory
 	err := os.MkdirAll(filepath.Dir(path), 0755)
diff --git a/generators/dump_test.go b/generators/dump_test.go
index 8b6f0e9..6e38273 100644
--- a/generators/dump_test.go
+++ b/generators/dump_test.go
@@ -51,10 +51,14 @@ func TestDumpGeneratorRunLXD(t *testing.T) {
 	generator := Get("dump")
 	require.Equal(t, DumpGenerator{}, generator)
 
-	err := generator.RunLXD(cacheDir, rootfsDir, nil, shared.DefinitionTargetLXD{},
+	err := generator.RunLXD(cacheDir, rootfsDir, nil, shared.DefinitionTargetLXD{
+		VM: shared.DefinitionTargetLXDVM{
+			Filesystem: "ext4",
+		},
+	},
 		shared.DefinitionFile{
 			Path:    "/hello/world",
-			Content: "hello world",
+			Content: "hello {{ lxd.VM.Filesystem }}",
 		})
 	require.NoError(t, err)
 
@@ -67,5 +71,5 @@ func TestDumpGeneratorRunLXD(t *testing.T) {
 
 	io.Copy(&buffer, file)
 
-	require.Equal(t, "hello world\n", buffer.String())
+	require.Equal(t, "hello ext4\n", buffer.String())
 }


More information about the lxc-devel mailing list