The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/distrobuilder/pull/336

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===

From e59128973490b648e230e8caca242b0986761022 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.h...@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.h...@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())
 }
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to