[lxc-devel] [distrobuilder/master] Add more generators
monstermunchkin on Github
lxc-bot at linuxcontainers.org
Wed Mar 7 16:37:00 UTC 2018
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 322 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180307/31e34a0d/attachment.bin>
-------------- next part --------------
From ba33cd231cc359fb376def9d1d81a86023cd1b5d Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 7 Mar 2018 16:37:25 +0100
Subject: [PATCH 1/3] generators: Change interface functions
Rename to RunLX{C,D} since they are not only for file creation.
Change signature for possible future arguments.
Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
distrobuilder/main_lxc.go | 4 ++--
distrobuilder/main_lxd.go | 4 ++--
generators/generators.go | 11 ++++++-----
generators/hostname.go | 19 +++++++++++--------
generators/hostname_test.go | 10 ++++++----
generators/hosts.go | 21 ++++++++++++---------
generators/hosts_test.go | 10 ++++++----
7 files changed, 45 insertions(+), 34 deletions(-)
diff --git a/distrobuilder/main_lxc.go b/distrobuilder/main_lxc.go
index 8690ef7..8697b60 100644
--- a/distrobuilder/main_lxc.go
+++ b/distrobuilder/main_lxc.go
@@ -58,8 +58,8 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string) error {
continue
}
- err := generator.CreateLXCData(c.global.flagCacheDir, c.global.sourceDir,
- file.Path, img)
+ err := generator.RunLXC(c.global.flagCacheDir, c.global.sourceDir, img,
+ file)
if err != nil {
continue
}
diff --git a/distrobuilder/main_lxd.go b/distrobuilder/main_lxd.go
index 144abc6..024e38d 100644
--- a/distrobuilder/main_lxd.go
+++ b/distrobuilder/main_lxd.go
@@ -76,8 +76,8 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Unknown generator '%s'", file.Generator)
}
- err := generator.CreateLXDData(c.global.flagCacheDir, c.global.sourceDir,
- file.Path, img)
+ err := generator.RunLXD(c.global.flagCacheDir, c.global.sourceDir,
+ img, file)
if err != nil {
return fmt.Errorf("Failed to create LXD data: %s", err)
}
diff --git a/generators/generators.go b/generators/generators.go
index c126f14..0d0df56 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -6,15 +6,16 @@ import (
"path/filepath"
"strings"
- "github.com/lxc/lxd/shared"
+ lxd "github.com/lxc/lxd/shared"
"github.com/lxc/distrobuilder/image"
+ "github.com/lxc/distrobuilder/shared"
)
// Generator interface.
type Generator interface {
- CreateLXCData(string, string, string, *image.LXCImage) error
- CreateLXDData(string, string, string, *image.LXDImage) error
+ RunLXC(string, string, *image.LXCImage, shared.DefinitionFile) error
+ RunLXD(string, string, *image.LXDImage, shared.DefinitionFile) error
}
// Get returns a Generator.
@@ -37,7 +38,7 @@ func StoreFile(cacheDir, sourceDir, path string) error {
return err
}
- return shared.FileCopy(filepath.Join(sourceDir, path),
+ return lxd.FileCopy(filepath.Join(sourceDir, path),
filepath.Join(cacheDir, "tmp", path))
}
@@ -50,7 +51,7 @@ func RestoreFiles(cacheDir, sourceDir string) error {
return nil
}
- return shared.FileCopy(path, filepath.Join(sourceDir,
+ return lxd.FileCopy(path, filepath.Join(sourceDir,
strings.TrimPrefix(path, filepath.Join(cacheDir, "tmp"))))
}
diff --git a/generators/hostname.go b/generators/hostname.go
index ff47a00..48fe45e 100644
--- a/generators/hostname.go
+++ b/generators/hostname.go
@@ -8,21 +8,23 @@ import (
"github.com/lxc/lxd/shared/api"
"github.com/lxc/distrobuilder/image"
+ "github.com/lxc/distrobuilder/shared"
)
// HostnameGenerator represents the Hostname generator.
type HostnameGenerator struct{}
-// CreateLXCData creates a hostname template.
-func (g HostnameGenerator) CreateLXCData(cacheDir, sourceDir, path string, img *image.LXCImage) error {
+// RunLXC creates a hostname template.
+func (g HostnameGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+ defFile shared.DefinitionFile) error {
// Store original file
- err := StoreFile(cacheDir, sourceDir, path)
+ err := StoreFile(cacheDir, sourceDir, defFile.Path)
if err != nil {
return err
}
// Create new hostname file
- file, err := os.Create(filepath.Join(sourceDir, path))
+ file, err := os.Create(filepath.Join(sourceDir, defFile.Path))
if err != nil {
return err
}
@@ -35,11 +37,12 @@ func (g HostnameGenerator) CreateLXCData(cacheDir, sourceDir, path string, img *
}
// Add hostname path to LXC's templates file
- return img.AddTemplate(path)
+ return img.AddTemplate(defFile.Path)
}
-// CreateLXDData creates a hostname template.
-func (g HostnameGenerator) CreateLXDData(cacheDir, sourceDir, path string, img *image.LXDImage) error {
+// RunLXD creates a hostname template.
+func (g HostnameGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+ defFile shared.DefinitionFile) error {
templateDir := filepath.Join(cacheDir, "templates")
err := os.MkdirAll(templateDir, 0755)
@@ -59,7 +62,7 @@ func (g HostnameGenerator) CreateLXDData(cacheDir, sourceDir, path string, img *
}
// Add to LXD templates
- img.Metadata.Templates[path] = &api.ImageMetadataTemplate{
+ img.Metadata.Templates[defFile.Path] = &api.ImageMetadataTemplate{
Template: "hostname.tpl",
When: []string{
"create",
diff --git a/generators/hostname_test.go b/generators/hostname_test.go
index efc8131..446ae36 100644
--- a/generators/hostname_test.go
+++ b/generators/hostname_test.go
@@ -9,7 +9,7 @@ import (
"github.com/lxc/distrobuilder/shared"
)
-func TestHostnameGeneratorCreateLXCData(t *testing.T) {
+func TestHostnameGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
rootfsDir := filepath.Join(cacheDir, "rootfs")
@@ -35,7 +35,8 @@ func TestHostnameGeneratorCreateLXCData(t *testing.T) {
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hostname"), "hostname")
- err = generator.CreateLXCData(cacheDir, rootfsDir, "/etc/hostname", image)
+ err = generator.RunLXC(cacheDir, rootfsDir, image,
+ shared.DefinitionFile{Path: "/etc/hostname"})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
@@ -51,7 +52,7 @@ func TestHostnameGeneratorCreateLXCData(t *testing.T) {
validateTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hostname"), "hostname")
}
-func TestHostnameGeneratorCreateLXDData(t *testing.T) {
+func TestHostnameGeneratorRunLXD(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
rootfsDir := filepath.Join(cacheDir, "rootfs")
@@ -75,7 +76,8 @@ func TestHostnameGeneratorCreateLXDData(t *testing.T) {
t.Fatalf("Unexpected error: %s", err)
}
- err = generator.CreateLXDData(cacheDir, rootfsDir, "/etc/hostname", image)
+ err = generator.RunLXD(cacheDir, rootfsDir, image,
+ shared.DefinitionFile{Path: "/etc/hostname"})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
diff --git a/generators/hosts.go b/generators/hosts.go
index 177ced8..98553d8 100644
--- a/generators/hosts.go
+++ b/generators/hosts.go
@@ -6,21 +6,23 @@ import (
"path/filepath"
"github.com/lxc/distrobuilder/image"
+ "github.com/lxc/distrobuilder/shared"
"github.com/lxc/lxd/shared/api"
)
// HostsGenerator represents the hosts generator.
type HostsGenerator struct{}
-// CreateLXCData creates a LXC specific entry in the hosts file.
-func (g HostsGenerator) CreateLXCData(cacheDir, sourceDir, path string, img *image.LXCImage) error {
+// RunLXC creates a LXC specific entry in the hosts file.
+func (g HostsGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+ defFile shared.DefinitionFile) error {
// Store original file
- err := StoreFile(cacheDir, sourceDir, path)
+ err := StoreFile(cacheDir, sourceDir, defFile.Path)
if err != nil {
return err
}
- file, err := os.OpenFile(filepath.Join(sourceDir, path),
+ file, err := os.OpenFile(filepath.Join(sourceDir, defFile.Path),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
@@ -31,11 +33,12 @@ func (g HostsGenerator) CreateLXCData(cacheDir, sourceDir, path string, img *ima
file.WriteString("127.0.0.1\tLXC_NAME\n")
// Add hostname path to LXC's templates file
- return img.AddTemplate(path)
+ return img.AddTemplate(defFile.Path)
}
-// CreateLXDData creates a hosts template.
-func (g HostsGenerator) CreateLXDData(cacheDir, sourceDir, path string, img *image.LXDImage) error {
+// RunLXD creates a hosts template.
+func (g HostsGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+ defFile shared.DefinitionFile) error {
templateDir := filepath.Join(cacheDir, "templates")
// Create templates path
@@ -51,7 +54,7 @@ func (g HostsGenerator) CreateLXDData(cacheDir, sourceDir, path string, img *ima
}
defer file.Close()
- hostsFile, err := os.Open(filepath.Join(sourceDir, path))
+ hostsFile, err := os.Open(filepath.Join(sourceDir, defFile.Path))
if err != nil {
return err
}
@@ -61,7 +64,7 @@ func (g HostsGenerator) CreateLXDData(cacheDir, sourceDir, path string, img *ima
io.Copy(file, hostsFile)
file.WriteString("127.0.0.1\t{{ container.name }}\n")
- img.Metadata.Templates[path] = &api.ImageMetadataTemplate{
+ img.Metadata.Templates[defFile.Path] = &api.ImageMetadataTemplate{
Template: "hosts.tpl",
When: []string{
"create",
diff --git a/generators/hosts_test.go b/generators/hosts_test.go
index b19133e..318243f 100644
--- a/generators/hosts_test.go
+++ b/generators/hosts_test.go
@@ -9,7 +9,7 @@ import (
"github.com/lxc/distrobuilder/shared"
)
-func TestHostsGeneratorCreateLXCData(t *testing.T) {
+func TestHostsGeneratorRunLXC(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
rootfsDir := filepath.Join(cacheDir, "rootfs")
@@ -36,7 +36,8 @@ func TestHostsGeneratorCreateLXCData(t *testing.T) {
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
"127.0.0.1\tlocalhost\n")
- err = generator.CreateLXCData(cacheDir, rootfsDir, "/etc/hosts", image)
+ err = generator.RunLXC(cacheDir, rootfsDir, image,
+ shared.DefinitionFile{Path: "/etc/hosts"})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
@@ -55,7 +56,7 @@ func TestHostsGeneratorCreateLXCData(t *testing.T) {
"127.0.0.1\tlocalhost\n")
}
-func TestHostsGeneratorCreateLXDData(t *testing.T) {
+func TestHostsGeneratorRunLXD(t *testing.T) {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
rootfsDir := filepath.Join(cacheDir, "rootfs")
@@ -82,7 +83,8 @@ func TestHostsGeneratorCreateLXDData(t *testing.T) {
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
"127.0.0.1\tlocalhost\n")
- err = generator.CreateLXDData(cacheDir, rootfsDir, "/etc/hosts", image)
+ err = generator.RunLXD(cacheDir, rootfsDir, image,
+ shared.DefinitionFile{Path: "/etc/hosts"})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
From 64ca66571f26bdbd7c735d0d70bb23e0f43d6640 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 7 Mar 2018 17:12:41 +0100
Subject: [PATCH 2/3] generators: Add generator for removing files
Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
generators/generators.go | 2 ++
generators/remove.go | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+)
create mode 100644 generators/remove.go
diff --git a/generators/generators.go b/generators/generators.go
index 0d0df56..224dab5 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -25,6 +25,8 @@ func Get(generator string) Generator {
return HostnameGenerator{}
case "hosts":
return HostsGenerator{}
+ case "remove":
+ return RemoveGenerator{}
}
return nil
diff --git a/generators/remove.go b/generators/remove.go
new file mode 100644
index 0000000..9beb181
--- /dev/null
+++ b/generators/remove.go
@@ -0,0 +1,24 @@
+package generators
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/lxc/distrobuilder/image"
+ "github.com/lxc/distrobuilder/shared"
+)
+
+// RemoveGenerator represents the Remove generator.
+type RemoveGenerator struct{}
+
+// RunLXC removes a path.
+func (g RemoveGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+ defFile shared.DefinitionFile) error {
+ return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
+}
+
+// RunLXD removes a path.
+func (g RemoveGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+ defFile shared.DefinitionFile) error {
+ return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
+}
From 23c736ba580e3fad57a161c77ebbbdde070f416d Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 7 Mar 2018 17:35:28 +0100
Subject: [PATCH 3/3] generators: Add dump generator
Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
generators/dump.go | 44 +++++++++++++++++++++++
generators/dump_test.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++
generators/generators.go | 2 ++
shared/definition.go | 1 +
4 files changed, 138 insertions(+)
create mode 100644 generators/dump.go
create mode 100644 generators/dump_test.go
diff --git a/generators/dump.go b/generators/dump.go
new file mode 100644
index 0000000..7ca4479
--- /dev/null
+++ b/generators/dump.go
@@ -0,0 +1,44 @@
+package generators
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/lxc/distrobuilder/image"
+ "github.com/lxc/distrobuilder/shared"
+)
+
+// DumpGenerator represents the Remove generator.
+type DumpGenerator struct{}
+
+// RunLXC dumps content to a file.
+func (g DumpGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
+ defFile shared.DefinitionFile) error {
+ return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
+}
+
+// RunLXD dumps content to a file.
+func (g DumpGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
+ 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 {
+ return err
+ }
+
+ file, err := os.Create(path)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ _, err = file.WriteString(content)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/generators/dump_test.go b/generators/dump_test.go
new file mode 100644
index 0000000..17a25a6
--- /dev/null
+++ b/generators/dump_test.go
@@ -0,0 +1,91 @@
+package generators
+
+import (
+ "bytes"
+ "io"
+ "os"
+ "path/filepath"
+ "testing"
+
+ lxd "github.com/lxc/lxd/shared"
+
+ "github.com/lxc/distrobuilder/shared"
+)
+
+func TestDumpGeneratorRunLXC(t *testing.T) {
+ cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
+ rootfsDir := filepath.Join(cacheDir, "rootfs")
+
+ setup(t, cacheDir)
+ defer teardown(cacheDir)
+
+ generator := Get("dump")
+ if generator == nil {
+ t.Fatal("Expected dump generator, got nil")
+ }
+
+ err := generator.RunLXC(cacheDir, rootfsDir, nil,
+ shared.DefinitionFile{
+ Path: "/hello/world",
+ Content: "hello world",
+ })
+ if err != nil {
+ t.Fatalf("Unexpected error: %s", err)
+ }
+
+ if !lxd.PathExists(filepath.Join(rootfsDir, "hello", "world")) {
+ t.Fatalf("Directory '%s' wasn't created", "/hello/world")
+ }
+
+ var buffer bytes.Buffer
+ file, err := os.Open(filepath.Join(rootfsDir, "hello", "world"))
+ if err != nil {
+ t.Fatalf("Unexpected error: %s", err)
+ }
+ defer file.Close()
+
+ io.Copy(&buffer, file)
+
+ if buffer.String() != "hello world" {
+ t.Fatalf("Expected '%s', got '%s'", "hello world", buffer.String())
+ }
+}
+
+func TestDumpGeneratorRunLXD(t *testing.T) {
+ cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")
+ rootfsDir := filepath.Join(cacheDir, "rootfs")
+
+ setup(t, cacheDir)
+ defer teardown(cacheDir)
+
+ generator := Get("dump")
+ if generator == nil {
+ t.Fatal("Expected dump generator, got nil")
+ }
+
+ err := generator.RunLXD(cacheDir, rootfsDir, nil,
+ shared.DefinitionFile{
+ Path: "/hello/world",
+ Content: "hello world",
+ })
+ if err != nil {
+ t.Fatalf("Unexpected error: %s", err)
+ }
+
+ if !lxd.PathExists(filepath.Join(rootfsDir, "hello", "world")) {
+ t.Fatalf("Directory '%s' wasn't created", "/hello/world")
+ }
+
+ var buffer bytes.Buffer
+ file, err := os.Open(filepath.Join(rootfsDir, "hello", "world"))
+ if err != nil {
+ t.Fatalf("Unexpected error: %s", err)
+ }
+ defer file.Close()
+
+ io.Copy(&buffer, file)
+
+ if buffer.String() != "hello world" {
+ t.Fatalf("Expected '%s', got '%s'", "hello world", buffer.String())
+ }
+}
diff --git a/generators/generators.go b/generators/generators.go
index 224dab5..595b0cf 100644
--- a/generators/generators.go
+++ b/generators/generators.go
@@ -27,6 +27,8 @@ func Get(generator string) Generator {
return HostsGenerator{}
case "remove":
return RemoveGenerator{}
+ case "dump":
+ return DumpGenerator{}
}
return nil
diff --git a/shared/definition.go b/shared/definition.go
index 93950bc..1888e80 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -63,6 +63,7 @@ type DefinitionTarget struct {
type DefinitionFile struct {
Generator string `yaml:"generator"`
Path string `yaml:"path,omitempty"`
+ Content string `yaml:"content,omitempty"`
Releases []string `yaml:"releases,omitempty"`
}
More information about the lxc-devel
mailing list