[lxc-devel] [distrobuilder/master] WIP: dump-template generator to add templates to the image

mjrider on Github lxc-bot at linuxcontainers.org
Sun May 13 11:12:12 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 794 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180513/7ddca159/attachment.bin>
-------------- next part --------------
From ba5bdd49e59385614ac831b9f706604332e8c812 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robbert=20M=C3=BCller?= <spam.me at grols.ch>
Date: Sat, 12 May 2018 21:22:48 +0200
Subject: [PATCH] add dump-template generator to add templates to the image

This generator tries to follow to dump generator but for templates.

Changes:
- added name field `files: []` for the name of the template.
  .tpl is appended automaticly
- added dump-template to the generator code/definition
---
 generators/dump-template.go | 62 +++++++++++++++++++++++++++++++++++++++++++++
 generators/generators.go    |  2 ++
 shared/definition.go        |  2 ++
 3 files changed, 66 insertions(+)
 create mode 100644 generators/dump-template.go

diff --git a/generators/dump-template.go b/generators/dump-template.go
new file mode 100644
index 0000000..e9d89e8
--- /dev/null
+++ b/generators/dump-template.go
@@ -0,0 +1,62 @@
+package generators
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+
+	"github.com/lxc/distrobuilder/image"
+	"github.com/lxc/distrobuilder/shared"
+	"github.com/lxc/lxd/shared/api"
+)
+
+// DumpTemplateGenerator represents the Remove generator.
+type DumpTemplateGenerator struct{}
+
+// RunLXC dumps content to a file.
+func (g DumpTemplateGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+	defFile shared.DefinitionFile) error {
+	// no template support for LXC, ignoring generator
+	return nil
+}
+
+// RunLXD dumps content to a file.
+func (g DumpTemplateGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+	defFile shared.DefinitionFile) error {
+	templateDir := filepath.Join(cacheDir, "templates")
+
+	err := os.MkdirAll(templateDir, 0755)
+	if err != nil {
+		return err
+	}
+	template := fmt.Sprintf("%s.tpl", defFile.Name)
+
+	file, err := os.Create(filepath.Join(templateDir, template))
+	if err != nil {
+		return err
+	}
+
+	defer file.Close()
+
+	_, err = file.WriteString(defFile.Content)
+	if err != nil {
+		return fmt.Errorf("Failed to write to content to %s template: %s", defFile.Name, err)
+	}
+
+	// Add to LXD templates
+	img.Metadata.Templates[defFile.Path] = &api.ImageMetadataTemplate{
+		Template: template,
+		When: []string{
+			"create",
+			"copy",
+		},
+	}
+
+	return err
+}
+
+// Run does nothing.
+func (g DumpTemplateGenerator) Run(cacheDir, sourceDir string,
+	defFile shared.DefinitionFile) error {
+	return nil
+}
diff --git a/generators/generators.go b/generators/generators.go
index 60f2995..d91350b 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -29,6 +29,8 @@ func Get(generator string) Generator {
 		return RemoveGenerator{}
 	case "dump":
 		return DumpGenerator{}
+	case "dump-template":
+		return DumpTemplateGenerator{}
 	case "upstart-tty":
 		return UpstartTTYGenerator{}
 	}
diff --git a/shared/definition.go b/shared/definition.go
index 78b33ec..26e2050 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -77,6 +77,7 @@ type DefinitionFile struct {
 	Path      string   `yaml:"path,omitempty"`
 	Content   string   `yaml:"content,omitempty"`
 	Releases  []string `yaml:"releases,omitempty"`
+	Name      string   `yaml:"name"`
 }
 
 // A DefinitionAction specifies a custom action (script) which is to be run after
@@ -209,6 +210,7 @@ func (d *Definition) Validate() error {
 
 	validGenerators := []string{
 		"dump",
+		"dump-template",
 		"hostname",
 		"hosts",
 		"remove",


More information about the lxc-devel mailing list