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

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 81501c9220ca4c48e3e94b9af20e5b3217cce127 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.h...@canonical.com>
Date: Wed, 7 Mar 2018 11:40:50 +0100
Subject: [PATCH 1/2] *: Move renderTemplate to shared/utils

Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
---
 image/image.go      | 24 --------------------
 image/image_test.go | 64 -----------------------------------------------------
 image/lxc.go        |  2 +-
 image/lxd.go        |  6 ++---
 image/meta.tar      |  0
 shared/util.go      | 23 +++++++++++++++++++
 shared/util_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 84 insertions(+), 92 deletions(-)
 delete mode 100644 image/image.go
 delete mode 100644 image/image_test.go
 create mode 100644 image/meta.tar

diff --git a/image/image.go b/image/image.go
deleted file mode 100644
index 3c15761..0000000
--- a/image/image.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package image
-
-import "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
deleted file mode 100644
index 200f189..0000000
--- a/image/image_test.go
+++ /dev/null
@@ -1,64 +0,0 @@
-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 24d1eff..d8467f1 100644
--- a/image/lxc.go
+++ b/image/lxc.go
@@ -162,7 +162,7 @@ func (l *LXCImage) writeMetadata(filename, content string) 
error {
                "image": l.definition,
        }
 
-       out, err := renderTemplate(content, ctx)
+       out, err := shared.RenderTemplate(content, ctx)
        if err != nil {
                return err
        }
diff --git a/image/lxd.go b/image/lxd.go
index b92b74e..56b5054 100644
--- a/image/lxd.go
+++ b/image/lxd.go
@@ -80,7 +80,7 @@ func (l *LXDImage) Build(unified bool, compression string) 
error {
                var fname string
                if l.definition.Name != "" {
                        // Use a custom name for the unified tarball.
-                       fname, _ = renderTemplate(l.definition.Name, ctx)
+                       fname, _ = shared.RenderTemplate(l.definition.Name, ctx)
                } else {
                        // Default name for the unified tarball.
                        fname = "lxd"
@@ -148,12 +148,12 @@ func (l *LXDImage) createMetadata() error {
                "creation_date": l.creationDate.Format("20060201_1504"),
        }
 
-       l.Metadata.Properties["description"], err = 
renderTemplate(l.definition.Description, ctx)
+       l.Metadata.Properties["description"], err = 
shared.RenderTemplate(l.definition.Description, ctx)
        if err != err {
                return nil
        }
 
-       l.Metadata.Properties["name"], err = renderTemplate(l.definition.Name, 
ctx)
+       l.Metadata.Properties["name"], err = 
shared.RenderTemplate(l.definition.Name, ctx)
        if err != nil {
                return err
        }
diff --git a/image/meta.tar b/image/meta.tar
new file mode 100644
index 0000000..e69de29
diff --git a/shared/util.go b/shared/util.go
index 51fc1ed..06fe875 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -13,6 +13,7 @@ import (
        "time"
 
        lxd "github.com/lxc/lxd/shared"
+       "gopkg.in/flosch/pongo2.v3"
 )
 
 // Copy copies a file.
@@ -188,3 +189,25 @@ func GetExpiryDate(creationDate time.Time, format string) 
time.Time {
 
        return expiryDate
 }
+
+// RenderTemplate renders a pongo2 template.
+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/shared/util_test.go b/shared/util_test.go
index dd23c02..eaad590 100644
--- a/shared/util_test.go
+++ b/shared/util_test.go
@@ -8,6 +8,7 @@ import (
        "testing"
 
        lxd "github.com/lxc/lxd/shared"
+       "gopkg.in/flosch/pongo2.v3"
 )
 
 func TestVerifyFile(t *testing.T) {
@@ -114,3 +115,59 @@ func TestCreateGPGKeyring(t *testing.T) {
        }
        os.RemoveAll(path.Dir(keyring))
 }
+
+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)
+               }
+       }
+}

From 34a7882f9157e4bcf02b2fd4d2c01b9aa7ee3b4b Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.h...@canonical.com>
Date: Wed, 7 Mar 2018 12:14:24 +0100
Subject: [PATCH 2/2] sources: Add AptSources

This allows the apt sources to be templated after the image has been
unpacked.

This resolves #31

Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
---
 shared/definition.go   |  1 +
 sources/debootstrap.go | 39 ++++++++++++++++++++++++++++++++++++++-
 sources/ubuntu-http.go | 38 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/shared/definition.go b/shared/definition.go
index fb42f5e..63cba8c 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -37,6 +37,7 @@ type DefinitionSource struct {
        Keyserver  string   `yaml:"keyserver,omitempty"`
        Variant    string   `yaml:"variant,omitempty"`
        Suite      string   `yaml:"suite,omitempty"`
+       AptSources string   `yaml:"apt_sources,omitempty"`
 }
 
 // A DefinitionTargetLXC represents LXC specific files as part of the metadata.
diff --git a/sources/debootstrap.go b/sources/debootstrap.go
index da98d55..9921f01 100644
--- a/sources/debootstrap.go
+++ b/sources/debootstrap.go
@@ -5,6 +5,8 @@ import (
        "path"
        "path/filepath"
 
+       "gopkg.in/flosch/pongo2.v3"
+
        "github.com/lxc/distrobuilder/shared"
 )
 
@@ -57,5 +59,40 @@ func (s *Debootstrap) Run(source shared.DefinitionSource, 
release, arch, rootfsD
                defer os.Remove(link)
        }
 
-       return shared.RunCommand("debootstrap", args...)
+       err := shared.RunCommand("debootstrap", args...)
+       if err != nil {
+               return err
+       }
+
+       if source.AptSources != "" {
+               ctx := pongo2.Context{
+                       "source": source,
+                       // We use an anonymous struct instead of 
DefinitionImage because we
+                       // need the mapped architecture, and Arch and Release 
is all one
+                       // needs in the sources.list.
+                       "image": struct {
+                               Arch    string
+                               Release string
+                       }{
+                               arch,
+                               release,
+                       },
+               }
+
+               out, err := shared.RenderTemplate(source.AptSources, ctx)
+               if err != nil {
+                       return err
+               }
+
+               // Replace content of sources.list with the templated content.
+               file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", 
"sources.list"))
+               if err != nil {
+                       return err
+               }
+               defer file.Close()
+
+               file.WriteString(out)
+       }
+
+       return nil
 }
diff --git a/sources/ubuntu-http.go b/sources/ubuntu-http.go
index ef61a00..6de80b4 100644
--- a/sources/ubuntu-http.go
+++ b/sources/ubuntu-http.go
@@ -11,6 +11,7 @@ import (
        "strings"
 
        lxd "github.com/lxc/lxd/shared"
+       "gopkg.in/flosch/pongo2.v3"
 
        "github.com/lxc/distrobuilder/shared"
 )
@@ -61,7 +62,42 @@ func (s *UbuntuHTTP) Run(source shared.DefinitionSource, 
release, arch, rootfsDi
                return fmt.Errorf("Error downloading Ubuntu image: %s", err)
        }
 
-       return s.unpack(filepath.Join(os.TempDir(), s.fname), rootfsDir)
+       err = s.unpack(filepath.Join(os.TempDir(), s.fname), rootfsDir)
+       if err != nil {
+               return err
+       }
+
+       if source.AptSources != "" {
+               ctx := pongo2.Context{
+                       "source": source,
+                       // We use an anonymous struct instead of 
DefinitionImage because we
+                       // need the mapped architecture, and Arch and Release 
is all one
+                       // needs in the sources.list.
+                       "image": struct {
+                               Arch    string
+                               Release string
+                       }{
+                               arch,
+                               release,
+                       },
+               }
+
+               out, err := shared.RenderTemplate(source.AptSources, ctx)
+               if err != nil {
+                       return err
+               }
+
+               // Replace content of sources.list with the templated content.
+               file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", 
"sources.list"))
+               if err != nil {
+                       return err
+               }
+               defer file.Close()
+
+               file.WriteString(out)
+       }
+
+       return nil
 }
 
 func (s UbuntuHTTP) unpack(filePath, rootDir string) error {
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to