[lxc-devel] [distrobuilder/master] generators: Extend generators by Run() command

monstermunchkin on Github lxc-bot at linuxcontainers.org
Thu Mar 8 16:05:35 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 363 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180308/b0de8e5c/attachment.bin>
-------------- next part --------------
From d4c62c74ac509f4a84e74108a8bce9629c40fdef Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 8 Mar 2018 17:02:25 +0100
Subject: [PATCH] generators: Extend generators by Run() command

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main_build-dir.go | 30 +++++++++++++++++++++++++++++-
 generators/dump.go              |  5 +++++
 generators/generators.go        |  1 +
 generators/hostname.go          |  6 ++++++
 generators/hosts.go             |  6 ++++++
 generators/remove.go            |  6 ++++++
 6 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/distrobuilder/main_build-dir.go b/distrobuilder/main_build-dir.go
index 416914d..a1f1acf 100644
--- a/distrobuilder/main_build-dir.go
+++ b/distrobuilder/main_build-dir.go
@@ -1,6 +1,13 @@
 package main
 
-import "github.com/spf13/cobra"
+import (
+	"fmt"
+
+	lxd "github.com/lxc/lxd/shared"
+	"github.com/spf13/cobra"
+
+	"github.com/lxc/distrobuilder/generators"
+)
 
 type cmdBuildDir struct {
 	cmd    *cobra.Command
@@ -13,6 +20,27 @@ func (c *cmdBuildDir) command() *cobra.Command {
 		Short: "Build plain rootfs",
 		Args:  cobra.ExactArgs(2),
 		RunE:  c.global.preRunBuild,
+		PostRunE: func(cmd *cobra.Command, args []string) error {
+			// Run global generators
+			for _, file := range c.global.definition.Files {
+				generator := generators.Get(file.Generator)
+				if generator == nil {
+					return fmt.Errorf("Unknown generator '%s'", file.Generator)
+				}
+
+				if len(file.Releases) > 0 && !lxd.StringInSlice(
+					c.global.definition.Image.Release, file.Releases) {
+					continue
+				}
+
+				err := generator.Run(c.global.flagCacheDir, c.global.sourceDir, file)
+				if err != nil {
+					continue
+				}
+			}
+
+			return nil
+		},
 	}
 
 	c.cmd = cmd
diff --git a/generators/dump.go b/generators/dump.go
index 7ca4479..3de96c5 100644
--- a/generators/dump.go
+++ b/generators/dump.go
@@ -23,6 +23,11 @@ func (g DumpGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
 	return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
 }
 
+// Run dumps content to a file.
+func (g DumpGenerator) Run(cacheDir, sourceDir string, defFile shared.DefinitionFile) error {
+	return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
+}
+
 func (g DumpGenerator) dumpFile(path, content string) error {
 	err := os.MkdirAll(filepath.Dir(path), 0755)
 	if err != nil {
diff --git a/generators/generators.go b/generators/generators.go
index 595b0cf..4ffb064 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -16,6 +16,7 @@ import (
 type Generator interface {
 	RunLXC(string, string, *image.LXCImage, shared.DefinitionFile) error
 	RunLXD(string, string, *image.LXDImage, shared.DefinitionFile) error
+	Run(string, string, shared.DefinitionFile) error
 }
 
 // Get returns a Generator.
diff --git a/generators/hostname.go b/generators/hostname.go
index 48fe45e..a227532 100644
--- a/generators/hostname.go
+++ b/generators/hostname.go
@@ -72,3 +72,9 @@ func (g HostnameGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImag
 
 	return err
 }
+
+// Run does nothing.
+func (g HostnameGenerator) Run(cacheDir, sourceDir string,
+	defFile shared.DefinitionFile) error {
+	return nil
+}
diff --git a/generators/hosts.go b/generators/hosts.go
index 98553d8..3cdc4c7 100644
--- a/generators/hosts.go
+++ b/generators/hosts.go
@@ -74,3 +74,9 @@ func (g HostsGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
 
 	return err
 }
+
+// Run does nothing.
+func (g HostsGenerator) Run(cacheDir, sourceDir string,
+	defFile shared.DefinitionFile) error {
+	return nil
+}
diff --git a/generators/remove.go b/generators/remove.go
index 9beb181..5726a2c 100644
--- a/generators/remove.go
+++ b/generators/remove.go
@@ -22,3 +22,9 @@ func (g RemoveGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
 	defFile shared.DefinitionFile) error {
 	return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
 }
+
+// Run removes a path.
+func (g RemoveGenerator) Run(cacheDir, sourceDir string,
+	defFile shared.DefinitionFile) error {
+	return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
+}


More information about the lxc-devel mailing list