[lxc-devel] [distrobuilder/master] Extend package definition

monstermunchkin on Github lxc-bot at linuxcontainers.org
Thu Dec 20 15:07:47 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181220/24b021e7/attachment.bin>
-------------- next part --------------
From bd4eabd4b3209ca014d00d051db816b94393d3d9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 20 Dec 2018 15:08:54 +0100
Subject: [PATCH 1/3] Extend package definition

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/chroot.go | 32 ++++++++++++++++++++++++++------
 distrobuilder/main.go   |  2 +-
 shared/definition.go    | 30 ++++++++++++++++++++++++------
 3 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/distrobuilder/chroot.go b/distrobuilder/chroot.go
index de3e0fd..d4aa30c 100644
--- a/distrobuilder/chroot.go
+++ b/distrobuilder/chroot.go
@@ -3,11 +3,14 @@ package main
 import (
 	"fmt"
 
+	lxd "github.com/lxc/lxd/shared"
+
 	"github.com/lxc/distrobuilder/managers"
 	"github.com/lxc/distrobuilder/shared"
 )
 
-func managePackages(def shared.DefinitionPackages, actions []shared.DefinitionAction) error {
+func managePackages(def shared.DefinitionPackages, actions []shared.DefinitionAction,
+	release string) error {
 	var err error
 
 	manager := managers.Get(def.Manager)
@@ -35,20 +38,37 @@ func managePackages(def shared.DefinitionPackages, actions []shared.DefinitionAc
 		}
 	}
 
-	err = manager.Install(def.Install)
-	if err != nil {
-		return err
+	var installablePackages []string
+	var removablePackages []string
+
+	for _, set := range def.Sets {
+		if len(set.Releases) > 0 && !lxd.StringInSlice(release, set.Releases) {
+			continue
+		}
+
+		if set.Action == "install" {
+			installablePackages = append(installablePackages, set.Packages...)
+		} else if set.Action == "remove" {
+			removablePackages = append(removablePackages, set.Packages...)
+		}
 	}
 
-	err = manager.Remove(def.Remove)
+	err = manager.Install(installablePackages)
 	if err != nil {
 		return err
 	}
 
-	err = manager.Clean()
+	err = manager.Remove(removablePackages)
 	if err != nil {
 		return err
 	}
 
+	if def.Cleanup {
+		err = manager.Clean()
+		if err != nil {
+			return err
+		}
+	}
+
 	return nil
 }
diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index ed14548..dbe2df4 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -214,7 +214,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 
 	// Install/remove/update packages
 	err = managePackages(c.definition.Packages,
-		c.definition.GetRunnableActions("post-update"))
+		c.definition.GetRunnableActions("post-update"), c.definition.Image.Release)
 	if err != nil {
 		return fmt.Errorf("Failed to manage packages: %s", err)
 	}
diff --git a/shared/definition.go b/shared/definition.go
index f3b78de..fab4f6d 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -14,13 +14,20 @@ import (
 	"github.com/lxc/lxd/shared"
 )
 
-// A DefinitionPackages list packages which are to be either installed or
-// removed.
+// A DefinitionPackagesSet is a set of packages which are to be installed
+// or removed.
+type DefinitionPackagesSet struct {
+	Packages []string `yaml:"packages"`
+	Action   string   `yaml:"action"`
+	Releases []string `yaml:"releases,omitempty"`
+}
+
+// A DefinitionPackages represents a package handler.
 type DefinitionPackages struct {
-	Manager string   `yaml:"manager"`
-	Install []string `yaml:"install,omitempty"`
-	Remove  []string `yaml:"remove,omitempty"`
-	Update  bool     `yaml:"update,omitempty"`
+	Manager string                  `yaml:"manager"`
+	Update  bool                    `yaml:"update,omitempty"`
+	Cleanup bool                    `yaml:"cleanup,omitempty"`
+	Sets    []DefinitionPackagesSet `yaml:"sets,omitempty"`
 }
 
 // A DefinitionImage represents the image.
@@ -265,6 +272,17 @@ func (d *Definition) Validate() error {
 		}
 	}
 
+	validPackageActions := []string{
+		"install",
+		"remove",
+	}
+
+	for _, set := range d.Packages.Sets {
+		if !shared.StringInSlice(set.Action, validPackageActions) {
+			return fmt.Errorf("packages.*.set.*.action must be one of %v", validPackageActions)
+		}
+	}
+
 	// Mapped architecture (distro name)
 	archMapped, err := d.getMappedArchitecture()
 	if err != nil {

From fdf56fc2476ec7212318c407e8103b9ad7c39ef2 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 20 Dec 2018 15:09:17 +0100
Subject: [PATCH 2/3] test: Test new package definition

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

diff --git a/shared/definition_test.go b/shared/definition_test.go
index 36957eb..49b6a6a 100644
--- a/shared/definition_test.go
+++ b/shared/definition_test.go
@@ -202,6 +202,30 @@ func TestValidateDefinition(t *testing.T) {
 			"actions\\.\\*\\.trigger must be one of .+",
 			true,
 		},
+		{
+			"invalid package action",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "debootstrap",
+					URL:        "https://ubuntu.com",
+					Keys:       []string{"0xCODE"},
+				},
+				Packages: DefinitionPackages{
+					Manager: "apt",
+					Sets: []DefinitionPackagesSet{
+						{
+							Action: "update",
+						},
+					},
+				},
+			},
+			"packages\\.\\*\\.set\\.\\*\\.action must be one of .+",
+			true,
+		},
 	}
 
 	for i, tt := range tests {

From 3df563acc4187f68b9a6587193e9d741e688c911 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 20 Dec 2018 15:21:37 +0100
Subject: [PATCH 3/3] Update examples

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 doc/examples/alpine                 |  8 ++++++--
 doc/examples/archlinux              | 10 ++++++---
 doc/examples/centos                 |  8 ++++++--
 doc/examples/debian                 | 10 ++++++---
 doc/examples/debian-cloud-init      | 32 ++++++++++++++++-------------
 doc/examples/fedora                 | 10 ++++++---
 doc/examples/gentoo                 |  8 ++++++--
 doc/examples/mint                   | 14 ++++++++-----
 doc/examples/ubuntu                 | 14 ++++++++-----
 doc/examples/ubuntu-with-base-image | 10 ++++++---
 10 files changed, 82 insertions(+), 42 deletions(-)

diff --git a/doc/examples/alpine b/doc/examples/alpine
index 835d12c..238caa5 100644
--- a/doc/examples/alpine
+++ b/doc/examples/alpine
@@ -52,5 +52,9 @@ packages:
     manager: apk
 
     update: true
-    install:
-        - neovim
+    cleanup: true
+
+    sets:
+      - packages:
+          - neovim
+        action: install
diff --git a/doc/examples/archlinux b/doc/examples/archlinux
index 93a325b..f165a7e 100644
--- a/doc/examples/archlinux
+++ b/doc/examples/archlinux
@@ -54,6 +54,10 @@ packages:
   manager: pacman
 
   update: true
-  install:
-    - systemd-sysvcompat
-    - neovim
+  cleanup: true
+
+  sets:
+    - packages:
+        - systemd-sysvcompat
+        - neovim
+      action: install
diff --git a/doc/examples/centos b/doc/examples/centos
index 5af6619..268b6ba 100644
--- a/doc/examples/centos
+++ b/doc/examples/centos
@@ -57,8 +57,12 @@ packages:
     manager: yum
 
     update: true
-    install:
-        - vim
+    cleanup: true
+
+    sets:
+      - packages:
+          - vim
+        action: install
 
 actions:
   - trigger: post-unpack
diff --git a/doc/examples/debian b/doc/examples/debian
index b6de65e..7a88122 100644
--- a/doc/examples/debian
+++ b/doc/examples/debian
@@ -50,9 +50,13 @@ packages:
     manager: apt
 
     update: true
-    install:
-        - systemd
-        - neovim
+    cleanup: true
+
+    sets:
+      - packages:
+          - systemd
+          - neovim
+        action: install
 
 mappings:
   architecture_map: debian
diff --git a/doc/examples/debian-cloud-init b/doc/examples/debian-cloud-init
index 38c0798..154afb5 100644
--- a/doc/examples/debian-cloud-init
+++ b/doc/examples/debian-cloud-init
@@ -122,20 +122,24 @@ packages:
     manager: apt
 
     update: true
-    install:
-        - systemd
-        - dialog
-        - ifupdown
-        - locales
-        - netbase
-        - net-tools
-        - cloud-init
-        - cloud-utils
-        - sudo
-        - adduser
-        - locales
-        - file
-        - openssh-server
+    cleanup: true
+
+    sets:
+      - packages:
+          - systemd
+          - dialog
+          - ifupdown
+          - locales
+          - netbase
+          - net-tools
+          - cloud-init
+          - cloud-utils
+          - sudo
+          - adduser
+          - locales
+          - file
+          - openssh-server
+        action: install
 
 mappings:
   architecture_map: debian
diff --git a/doc/examples/fedora b/doc/examples/fedora
index db99ecc..ea2e446 100644
--- a/doc/examples/fedora
+++ b/doc/examples/fedora
@@ -50,6 +50,10 @@ packages:
   manager: dnf
 
   update: true
-  install:
-    - systemd
-    - neovim
+  cleanup: true
+
+  sets:
+    - packages:
+        - systemd
+        - neovim
+      action: install
diff --git a/doc/examples/gentoo b/doc/examples/gentoo
index fe6e67e..df1ad55 100644
--- a/doc/examples/gentoo
+++ b/doc/examples/gentoo
@@ -51,8 +51,12 @@ packages:
   manager: portage
 
   update: true
-  install:
-    - neovim
+  cleanup: true
+
+  sets:
+    - packages:
+        - neovim
+      action: install
 
 actions:
   - trigger: post-packages
diff --git a/doc/examples/mint b/doc/examples/mint
index d52f6e8..9006b33 100644
--- a/doc/examples/mint
+++ b/doc/examples/mint
@@ -119,11 +119,15 @@ packages:
 
     # Do not update here. Updates will take place in the post-unpack action.
     update: false
-    install:
-        - apt-transport-https
-        - language-pack-en
-        - openssh-client
-        - vim
+    cleanup: true
+
+    sets:
+      - packages:
+          - apt-transport-https
+          - language-pack-en
+          - openssh-client
+          - vim
+        action: install
 
 actions:
     - trigger: post-unpack
diff --git a/doc/examples/ubuntu b/doc/examples/ubuntu
index e8b2b2d..06de002 100644
--- a/doc/examples/ubuntu
+++ b/doc/examples/ubuntu
@@ -115,11 +115,15 @@ packages:
     manager: apt
 
     update: true
-    install:
-        - apt-transport-https
-        - language-pack-en
-        - openssh-client
-        - vim
+    cleanup: true
+
+    sets:
+      - packages:
+          - apt-transport-https
+          - language-pack-en
+          - openssh-client
+          - vim
+        action: install
 
 actions:
     - trigger: post-update
diff --git a/doc/examples/ubuntu-with-base-image b/doc/examples/ubuntu-with-base-image
index baf0dbf..eb80c6d 100644
--- a/doc/examples/ubuntu-with-base-image
+++ b/doc/examples/ubuntu-with-base-image
@@ -60,9 +60,13 @@ packages:
   manager: apt
 
   update: true
-  install:
-    - systemd
-    - neovim
+  cleanup: true
+
+  sets:
+    - packages:
+        - systemd
+        - neovim
+      action: install
 
 mappings:
   architecture_map: debian


More information about the lxc-devel mailing list