Merge pull request #118 from monstermunchkin/fixes/extend-package-def

Extend package definition
This commit is contained in:
Stéphane Graber 2018-12-20 12:20:01 -05:00 committed by GitHub
commit 53bd6679b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 157 additions and 55 deletions

View File

@ -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,19 +38,36 @@ func managePackages(def shared.DefinitionPackages, actions []shared.DefinitionAc
}
}
err = manager.Install(def.Install)
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.Install(installablePackages)
if err != nil {
return err
}
err = manager.Remove(def.Remove)
err = manager.Remove(removablePackages)
if err != nil {
return err
}
err = manager.Clean()
if err != nil {
return err
if def.Cleanup {
err = manager.Clean()
if err != nil {
return err
}
}
return nil

View File

@ -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)
}

View File

@ -52,5 +52,9 @@ packages:
manager: apk
update: true
install:
- neovim
cleanup: true
sets:
- packages:
- neovim
action: install

View File

@ -54,6 +54,10 @@ packages:
manager: pacman
update: true
install:
- systemd-sysvcompat
- neovim
cleanup: true
sets:
- packages:
- systemd-sysvcompat
- neovim
action: install

View File

@ -57,8 +57,12 @@ packages:
manager: yum
update: true
install:
- vim
cleanup: true
sets:
- packages:
- vim
action: install
actions:
- trigger: post-unpack

View File

@ -50,9 +50,13 @@ packages:
manager: apt
update: true
install:
- systemd
- neovim
cleanup: true
sets:
- packages:
- systemd
- neovim
action: install
mappings:
architecture_map: debian

View File

@ -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

View File

@ -50,6 +50,10 @@ packages:
manager: dnf
update: true
install:
- systemd
- neovim
cleanup: true
sets:
- packages:
- systemd
- neovim
action: install

View File

@ -51,8 +51,12 @@ packages:
manager: portage
update: true
install:
- neovim
cleanup: true
sets:
- packages:
- neovim
action: install
actions:
- trigger: post-packages

View File

@ -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

View File

@ -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

View File

@ -60,9 +60,13 @@ packages:
manager: apt
update: true
install:
- systemd
- neovim
cleanup: true
sets:
- packages:
- systemd
- neovim
action: install
mappings:
architecture_map: debian

View File

@ -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 {

View File

@ -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 {