Skip to content

Commit

Permalink
Use new dependency abstraction to install kvm packages
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleasa committed Aug 28, 2019
1 parent e7fa729 commit cd44181
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
43 changes: 7 additions & 36 deletions container/kvm/initialisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

"github.com/juju/errors"
"github.com/juju/os/series"
"github.com/juju/packaging/manager"
"github.com/juju/utils/arch"

"github.com/juju/juju/container"
"github.com/juju/juju/juju/paths"
"github.com/juju/juju/packaging"
"github.com/juju/juju/packaging/dependency"
)

type containerInitialiser struct{}
Expand Down Expand Up @@ -43,49 +43,20 @@ func (ci *containerInitialiser) Initialise() error {
return nil
}

// getPackageManager is a helper function which returns the
// package manager implementation for the current system.
func getPackageManager() (manager.PackageManager, error) {
hostSeries, err := series.HostSeries()
if err != nil {
return nil, errors.Trace(err)
}
return manager.NewPackageManager(hostSeries)
}

func ensureDependencies() error {
pacman, err := getPackageManager()
hostSeries, err := series.HostSeries()
if err != nil {
return err
return errors.Trace(err)
}

for _, pack := range getRequiredPackages(runtime.GOARCH) {
if err := pacman.Install(pack); err != nil {
return err
}
dep := dependency.KVM(runtime.GOARCH)
if err = packaging.InstallDependency(dep, hostSeries); err != nil {
return errors.Trace(err)
}

return nil
}

func getRequiredPackages(a string) []string {
var requiredPackages = []string{
// `qemu-kvm` must be installed before `libvirt-bin` on trusty. It appears
// that upstart doesn't reload libvirtd if installed after, and we see
// errors related to `qemu-kvm` not being installed.
"qemu-kvm",
"qemu-utils",
"genisoimage",
"libvirt-bin",
}
if a == arch.ARM64 {
// ARM64 doesn't support legacy BIOS so it requires Extensible Firmware
// Interface.
requiredPackages = append([]string{"qemu-efi"}, requiredPackages...)
}
return requiredPackages
}

// ensurePool creates the libvirt storage pool and ensures its is active.
// runCmd and chownFunc are here for testing. runCmd so we can check the
// right shell out calls are made, and chownFunc because we cannot chown
Expand Down
19 changes: 15 additions & 4 deletions container/kvm/initialisation_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"os"

"github.com/juju/juju/packaging"
"github.com/juju/juju/packaging/dependency"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
Expand Down Expand Up @@ -90,11 +92,20 @@ func (initialisationInternalSuite) TestAutoStartPool(c *gc.C) {
}

func (initialisationInternalSuite) TestRequiredPackagesAMD64(c *gc.C) {
got := getRequiredPackages("amd64")
c.Assert(got, jc.DeepEquals, []string{"qemu-kvm", "qemu-utils", "genisoimage", "libvirt-bin"})
got, err := dependency.KVM("amd64").PackageList("bionic")
c.Assert(err, gc.IsNil)
assertPkgListMatches(c, got, []string{"qemu-kvm", "qemu-utils", "genisoimage", "libvirt-bin"})
}

func (initialisationInternalSuite) TestRequiredPackagesARM64(c *gc.C) {
got := getRequiredPackages("arm64")
c.Assert(got, jc.DeepEquals, []string{"qemu-efi", "qemu-kvm", "qemu-utils", "genisoimage", "libvirt-bin"})
got, err := dependency.KVM("arm64").PackageList("bionic")
c.Assert(err, gc.IsNil)
assertPkgListMatches(c, got, []string{"qemu-efi", "qemu-kvm", "qemu-utils", "genisoimage", "libvirt-bin"})
}

func assertPkgListMatches(c *gc.C, got []packaging.Package, exp []string) {
c.Assert(len(got), gc.Equals, len(exp))
for i := 0; i < len(got); i++ {
c.Assert(got[i].Name, gc.Equals, exp[i])
}
}
47 changes: 47 additions & 0 deletions packaging/dependency/kvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package dependency

import (
"github.com/juju/errors"
"github.com/juju/juju/packaging"
"github.com/juju/utils/arch"
)

// KVM returns a dependency instance for installing KVM support.
func KVM(arch string) packaging.Dependency {
return &kvmDependency{arch: arch}
}

type kvmDependency struct {
arch string
}

// PackageList implements packaging.Dependency.
func (dep kvmDependency) PackageList(series string) ([]packaging.Package, error) {
var pkgList []string

switch series {
case "centos7", "opensuseleap":
return nil, errors.NotSupportedf("installing kvm on series %q", series)
default:
if dep.arch == arch.ARM64 {
// ARM64 doesn't support legacy BIOS so it requires Extensible Firmware
// Interface.
pkgList = append(pkgList, "qemu-efi")
}

pkgList = append(pkgList,
// `qemu-kvm` must be installed before `libvirt-bin` on trusty. It appears
// that upstart doesn't reload libvirtd if installed after, and we see
// errors related to `qemu-kvm` not being installed.
"qemu-kvm",
"qemu-utils",
"genisoimage",
"libvirt-bin",
)

return packaging.MakePackageList(packaging.AptPackageManager, "", pkgList...), nil
}
}

0 comments on commit cd44181

Please sign in to comment.