[lxc-devel] [distrobuilder/master] Definition updates

monstermunchkin on Github lxc-bot at linuxcontainers.org
Fri Feb 9 14:18:11 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 308 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180209/8843c8d5/attachment.bin>
-------------- next part --------------
From 3bd048b4b263a1f835a28a8a0aca6f64f287b60b Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 7 Feb 2018 12:30:57 +0100
Subject: [PATCH 1/5] *: Add Definition defaults

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main.go |  2 ++
 shared/definition.go  | 15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index f5c7345..a52d418 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -195,5 +195,7 @@ func getDefinition(fname string) (shared.Definition, error) {
 		return def, err
 	}
 
+	shared.SetDefinitionDefaults(&def)
+
 	return def, err
 }
diff --git a/shared/definition.go b/shared/definition.go
index d8df4a7..2a787b5 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -1,5 +1,7 @@
 package shared
 
+import "runtime"
+
 // A DefinitionPackages list packages which are to be either installed or
 // removed.
 type DefinitionPackages struct {
@@ -53,3 +55,16 @@ type Definition struct {
 	Files    []DefinitionFile   `yaml:"files,omitempty"`
 	Packages DefinitionPackages `yaml:"packages,omitempty"`
 }
+
+// SetDefinitionDefaults sets some default values for the given Definition.
+func SetDefinitionDefaults(def *Definition) {
+	// default to local arch
+	if def.Image.Arch == "" {
+		def.Image.Arch = runtime.GOARCH
+	}
+
+	// set default expiry of 30 days
+	if def.Image.Expiry == "" {
+		def.Image.Expiry = "30d"
+	}
+}

From 4fe0c6a3a74ea71140a5bdd648391e66af94cffa Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 7 Feb 2018 13:12:43 +0100
Subject: [PATCH 2/5] shared: Update Definition

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/definition.go | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/shared/definition.go b/shared/definition.go
index 2a787b5..42b164d 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -19,6 +19,7 @@ type DefinitionImage struct {
 	Arch         string `yaml:"arch,omitempty"`
 	Expiry       string `yaml:"expiry,omitempty"`
 	Variant      string `yaml:"variant,omitempty"`
+	Name         string `yaml:"name,omitempty"`
 }
 
 // A DefinitionSource specifies the download type and location
@@ -41,12 +42,20 @@ type DefinitionTarget struct {
 
 // A DefinitionFile represents a file which is to be created inside to chroot.
 type DefinitionFile struct {
-	Name      string   `yaml:"name"`
 	Generator string   `yaml:"generator"`
 	Path      string   `yaml:"path,omitempty"`
 	Releases  []string `yaml:"releases,omitempty"`
 }
 
+// DefinitionActions specifies custom actions (scripts) which are to be run after
+// certain actions.
+type DefinitionActions struct {
+	PostUnpack   string `yaml:"post-unpack,omitempty"`
+	PostUpdate   string `yaml:"post-update,omitempty"`
+	PostPackages string `yaml:"post-packages,omitempty"`
+	PostFiles    string `yaml:"post-files,omitempty"`
+}
+
 // A Definition a definition.
 type Definition struct {
 	Image    DefinitionImage    `yaml:"image"`
@@ -54,6 +63,7 @@ type Definition struct {
 	Targets  DefinitionTarget   `yaml:"targets,omitempty"`
 	Files    []DefinitionFile   `yaml:"files,omitempty"`
 	Packages DefinitionPackages `yaml:"packages,omitempty"`
+	Actions  DefinitionActions  `yaml:"actions,omitempty"`
 }
 
 // SetDefinitionDefaults sets some default values for the given Definition.

From 1a15ac757f21b438c979fbb34db1774ec0ab1458 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 9 Feb 2018 14:25:27 +0100
Subject: [PATCH 3/5] *: Add Definition validation

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main.go |  1 +
 shared/definition.go  | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index a52d418..c8187a4 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -196,6 +196,7 @@ func getDefinition(fname string) (shared.Definition, error) {
 	}
 
 	shared.SetDefinitionDefaults(&def)
+	err = shared.ValidateDefinition(def)
 
 	return def, err
 }
diff --git a/shared/definition.go b/shared/definition.go
index 42b164d..e03def0 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -1,6 +1,13 @@
 package shared
 
-import "runtime"
+import (
+	"errors"
+	"fmt"
+	"runtime"
+	"strings"
+
+	"github.com/lxc/lxd/shared"
+)
 
 // A DefinitionPackages list packages which are to be either installed or
 // removed.
@@ -78,3 +85,41 @@ func SetDefinitionDefaults(def *Definition) {
 		def.Image.Expiry = "30d"
 	}
 }
+
+// ValidateDefinition validates the given Definition.
+func ValidateDefinition(def Definition) error {
+	if strings.TrimSpace(def.Image.Distribution) == "" {
+		return errors.New("image.distribution may not be empty")
+	}
+
+	if strings.TrimSpace(def.Image.Release) == "" {
+		return errors.New("image.release may not be empty")
+	}
+
+	validDownloaders := []string{
+		"alpinelinux-http",
+		"archlinux-http",
+		"centos-http",
+		"debootstrap",
+		"ubuntu-http",
+	}
+	if !shared.StringInSlice(strings.TrimSpace(def.Source.Downloader), validDownloaders) {
+		return fmt.Errorf("source.downloader must be one of %v", validDownloaders)
+	}
+
+	if strings.TrimSpace(def.Source.URL) == "" {
+		return errors.New("source.url may not be empty")
+	}
+
+	validManagers := []string{
+		"apk",
+		"apt",
+		"yum",
+		"pacman",
+	}
+	if !shared.StringInSlice(strings.TrimSpace(def.Packages.Manager), validManagers) {
+		return fmt.Errorf("packages.manager must be one of %v", validManagers)
+	}
+
+	return nil
+}

From 442325801aea0ca9046abbf5ddbfbae13df37edc Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 9 Feb 2018 14:32:10 +0100
Subject: [PATCH 4/5] test: Add Definition tests

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/definition_test.go | 125 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)
 create mode 100644 shared/definition_test.go

diff --git a/shared/definition_test.go b/shared/definition_test.go
new file mode 100644
index 0000000..e2e64f6
--- /dev/null
+++ b/shared/definition_test.go
@@ -0,0 +1,125 @@
+package shared
+
+import (
+	"log"
+	"regexp"
+	"runtime"
+	"testing"
+)
+
+func TestSetDefinitionDefaults(t *testing.T) {
+	def := Definition{}
+
+	SetDefinitionDefaults(&def)
+
+	if def.Image.Arch != runtime.GOARCH {
+		t.Fatalf("Expected image.arch to be '%s', got '%s'", runtime.GOARCH, def.Image.Arch)
+	}
+
+	if def.Image.Expiry != "30d" {
+		t.Fatalf("Expected image.expiry to be '%s', got '%s'", "30d", def.Image.Expiry)
+	}
+}
+
+func TestValidateDefinition(t *testing.T) {
+	tests := []struct {
+		name       string
+		definition Definition
+		expected   string
+		shouldFail bool
+	}{
+		{
+			"valid Definition",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "debootstrap",
+					URL:        "https://ubuntu.com",
+				},
+				Packages: DefinitionPackages{
+					Manager: "apt",
+				},
+			},
+			"",
+			false,
+		},
+		{
+			"empty image.distribution",
+			Definition{},
+			"image.distribution may not be empty",
+			true,
+		},
+		{
+			"empty image.release",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+				},
+			},
+			"image.release may not be empty",
+			true,
+		},
+		{
+			"invalid source.downloader",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "foo",
+				},
+			},
+			"source.downloader must be one of .+",
+			true,
+		},
+		{
+			"empty source.url",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "debootstrap",
+				},
+			},
+			"source.url may not be empty",
+			true,
+		},
+		{
+			"invalid package.manager",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "debootstrap",
+					URL:        "https://ubuntu.com",
+				},
+				Packages: DefinitionPackages{
+					Manager: "foo",
+				},
+			},
+			"packages.manager must be one of .+",
+			true,
+		},
+	}
+
+	for i, tt := range tests {
+		log.Printf("Running test #%d: %s", i, tt.name)
+		err := ValidateDefinition(tt.definition)
+		if !tt.shouldFail && err != nil {
+			t.Fatalf("Validation failed: %s", err)
+		} else if tt.shouldFail {
+			match, _ := regexp.MatchString(tt.expected, err.Error())
+			if !match {
+				t.Fatalf("Validation failed: Expected '%s', got '%s'", tt.expected, err.Error())
+			}
+		}
+	}
+}

From efeef86fbc28f33d4d64cc715a8e01b008159f5c Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 9 Feb 2018 14:52:32 +0100
Subject: [PATCH 5/5] Add travis.yml

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 .travis.yml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 .travis.yml

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..17e670e
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,17 @@
+language: go
+
+os:
+  - linux
+
+go:
+  - 1.9
+
+install:
+  - "mkdir -p $GOPATH/github.com/lxc"
+  - "rsync -az ${TRAVIS_BUILD_DIR}/ $HOME/gopath/src/github.com/lxc/distrobuilder/"
+
+script:
+  - "make check"
+
+notifications:
+  webhooks: https://linuxcontainers.org/webhook-lxcbot/


More information about the lxc-devel mailing list