From 9a4d47d88f1051b23d30cc3cad8aa13a196dbfac Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 5 Nov 2018 18:59:35 -0700 Subject: Libvirt: docs inc. sample usage --- src/Propellor/Property/Libvirt.hs | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/Propellor/Property/Libvirt.hs') diff --git a/src/Propellor/Property/Libvirt.hs b/src/Propellor/Property/Libvirt.hs index 053b60a6..abec2e99 100644 --- a/src/Propellor/Property/Libvirt.hs +++ b/src/Propellor/Property/Libvirt.hs @@ -13,14 +13,26 @@ import Propellor.Property.DiskImage import Propellor.Property.Chroot.Util (removeChroot) import qualified Propellor.Property.Apt as Apt +-- | The number of virtual CPUs to assign to the virtual machine type NumVCPUs = Int + +-- | The number of MiB of memory to assign to the virtual machine type MiBMemory = Int + +-- | Whether the virtual machine should be started after it is defined, and at +-- host system boot data AutoStart = AutoStart | NoAutoStart + +-- | Which type of disk image to build for the virtual machine data DiskImageType = Raw | QCow2 +-- | Install basic libvirt components installed :: Property DebianLike installed = Apt.installed ["libvirt-clients", "virtinst"] +-- | Ensure that the default libvirt network is set to autostart. +-- +-- On Debian, it is not started by default after installation of libvirt. defaultNetworkAutostarted :: Property UnixLike defaultNetworkAutostarted = check (not <$> doesFileExist autostartFile) (cmdProperty "virsh" ["net-autostart", "default"]) @@ -28,6 +40,40 @@ defaultNetworkAutostarted = check (not <$> doesFileExist autostartFile) where autostartFile = "/etc/libvirt/qemu/networks/autostart/default.xml" +-- | Builds a disk image with the properties of the given Host, installs a +-- libvirt configuration file to boot the image, and if it is set to autostart, +-- start the VM. +-- +-- Note that building the disk image happens only once. So if you change the +-- properties of the given Host, this property will not modify the disk image. +-- In order to later apply properties to the VM, you should spin it directly, or +-- arrange to have it spun with a property like 'Cron.runPropellor', or use +-- 'Propellor.Property.Conductor' from the VM host. +-- +-- Suggested usage in @config.hs@: +-- +-- > mybox = host "mybox.example.com" $ props +-- > & osDebian (Stable "stretch") X86_64 +-- > & Libvirt.defaultNetworkAutostarted +-- > `onChange` (cmdProperty "virsh" ["net-start", "default"] +-- > `assume` MadeChange) +-- > & Libvirt.kvmDefined Libvirt.Raw 2048 2 Libvirt.NoAutoStart subbox +-- > +-- > subbox = host "subbox.mybox.example.com" $ props +-- > & osDebian Unstable X86_64 +-- > & hasPartition +-- > ( partition EXT4 +-- > `mountedAt` "/" +-- > `addFreeSpace` MegaBytes 10240 +-- > ) +-- > & Apt.installed ["linux-image-amd64"] +-- > & Grub.installed PC +-- > +-- > & ipv4 "192.168.122.31" +-- > & Network.static "ens3" (IPv4 "192.168.122.31") +-- > (Just (Network.Gateway (IPv4 "192.168.122.1"))) +-- > `requires` Network.cleanInterfacesFile +-- > & Hostname.sane kvmDefined :: DiskImageType -> MiBMemory @@ -83,6 +129,8 @@ kvmDefined imageType mem cpus auto h = AutoStart -> " --autostart" NoAutoStart -> "" +-- ==== utility functions ==== + osType :: Host -> Maybe String osType h = hostSystem h >>= \s -> case s of System (Debian Linux _) _ -> Just "Linux" @@ -90,6 +138,7 @@ osType h = hostSystem h >>= \s -> case s of System ArchLinux _ -> Just "Linux" _ -> Nothing +-- TODO specify more of these osVariant :: Host -> Maybe String osVariant h = hostSystem h >>= \s -> case s of System (Debian _ (Stable "stretch")) _ -> Just "debian9" -- cgit v1.2.3