Integration testing preparation for Ironic

Add ability to create/register qemu vms for Ironic testing purposes

Implements bp:deprecate-baremetal-driver

Change-Id: If452438fcc0ff562531b33a36cd189b235654b48
diff --git a/tools/install_prereqs.sh b/tools/install_prereqs.sh
index 0c65fd9..9651083 100755
--- a/tools/install_prereqs.sh
+++ b/tools/install_prereqs.sh
@@ -55,7 +55,13 @@
 # ================
 
 # Install package requirements
-install_package $(get_packages general $ENABLED_SERVICES)
+PACKAGES=$(get_packages general $ENABLED_SERVICES)
+if is_ubuntu && echo $PACKAGES | grep -q dkms ; then
+    # ensure headers for the running kernel are installed for any DKMS builds
+    PACKAGES="$PACKAGES linux-headers-$(uname -r)"
+fi
+
+install_package $PACKAGES
 
 if [[ -n "$SYSLOG" && "$SYSLOG" != "False" ]]; then
     if is_ubuntu || is_fedora; then
diff --git a/tools/ironic/scripts/cleanup-nodes b/tools/ironic/scripts/cleanup-nodes
new file mode 100755
index 0000000..dc5a19d
--- /dev/null
+++ b/tools/ironic/scripts/cleanup-nodes
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+
+# **cleanup-nodes**
+
+# Cleans up baremetal poseur nodes and volumes created during ironic setup
+# Assumes calling user has proper libvirt group membership and access.
+
+set -exu
+
+LIBVIRT_STORAGE_POOL=${LIBVIRT_STORAGE_POOL:-"default"}
+
+VM_COUNT=$1
+NETWORK_BRIDGE=$2
+
+for (( idx=0; idx<$VM_COUNT; idx++ )); do
+    NAME="baremetal${NETWORK_BRIDGE}_${idx}"
+    VOL_NAME="baremetal${NETWORK_BRIDGE}-${idx}.qcow2"
+    virsh list | grep -q $NAME && virsh destroy $NAME
+    virsh list --inactive | grep -q $NAME && virsh undefine $NAME
+
+    if virsh pool-list | grep -q $LIBVIRT_STORAGE_POOL ; then
+      virsh vol-list $LIBVIRT_STORAGE_POOL | grep -q $VOL_NAME &&
+          virsh vol-delete $VOL_NAME --pool $LIBVIRT_STORAGE_POOL
+    fi
+done
diff --git a/tools/ironic/scripts/configure-vm b/tools/ironic/scripts/configure-vm
new file mode 100755
index 0000000..9936b76
--- /dev/null
+++ b/tools/ironic/scripts/configure-vm
@@ -0,0 +1,78 @@
+#!/usr/bin/env python
+
+import argparse
+import os.path
+
+import libvirt
+
+templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
+                           'templates')
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="Configure a kvm virtual machine for the seed image.")
+    parser.add_argument('--name', default='seed',
+                        help='the name to give the machine in libvirt.')
+    parser.add_argument('--image',
+                        help='Use a custom image file (must be qcow2).')
+    parser.add_argument('--engine', default='qemu',
+                        help='The virtualization engine to use')
+    parser.add_argument('--arch', default='i686',
+                        help='The architecture to use')
+    parser.add_argument('--memory', default='2097152',
+                        help="Maximum memory for the VM in KB.")
+    parser.add_argument('--cpus', default='1',
+                        help="CPU count for the VM.")
+    parser.add_argument('--bootdev', default='hd',
+                        help="What boot device to use (hd/network).")
+    parser.add_argument('--network', default="brbm",
+                        help='The libvirt network name to use')
+    parser.add_argument('--libvirt-nic-driver', default='e1000',
+                        help='The libvirt network driver to use')
+    parser.add_argument('--emulator', default=None,
+                        help='Path to emulator bin for vm template')
+    args = parser.parse_args()
+    with file(templatedir + '/vm.xml', 'rb') as f:
+        source_template = f.read()
+    params = {
+        'name': args.name,
+        'imagefile': args.image,
+        'engine': args.engine,
+        'arch': args.arch,
+        'memory': args.memory,
+        'cpus': args.cpus,
+        'bootdev': args.bootdev,
+        'network': args.network,
+        'emulator': args.emulator,
+    }
+
+    if args.emulator:
+        params['emulator'] = args.emulator
+    else:
+        if os.path.exists("/usr/bin/kvm"):  # Debian
+            params['emulator'] = "/usr/bin/kvm"
+        elif os.path.exists("/usr/bin/qemu-kvm"):  # Redhat
+            params['emulator'] = "/usr/bin/qemu-kvm"
+
+    nicparams = {
+        'nicdriver': args.libvirt_nic_driver,
+        'network': args.network,
+    }
+
+    params['bm_network'] = """
+<!-- neutron friendly 'bare metal' network -->
+<interface type='network'>
+  <source network='%(network)s'/>
+  <virtualport type='openvswitch'/>
+  <model type='%(nicdriver)s'/>
+  <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
+</interface>""" % nicparams
+
+    libvirt_template = source_template % params
+    conn = libvirt.open("qemu:///system")
+    a = conn.defineXML(libvirt_template)
+    print ("Created machine %s with UUID %s" % (args.name, a.UUIDString()))
+
+if __name__ == '__main__':
+    main()
diff --git a/tools/ironic/scripts/create-nodes b/tools/ironic/scripts/create-nodes
new file mode 100755
index 0000000..3232b50
--- /dev/null
+++ b/tools/ironic/scripts/create-nodes
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+# **create-nodes**
+
+# Creates baremetal poseur nodes for ironic testing purposes
+
+set -exu
+
+# Keep track of the devstack directory
+TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
+
+CPU=$1
+MEM=$(( 1024 * $2 ))
+# extra G to allow fuzz for partition table : flavor size and registered size
+# need to be different to actual size.
+DISK=$(( $3 + 1))
+
+case $4 in
+    i386) ARCH='i686' ;;
+    amd64) ARCH='x86_64' ;;
+    *) echo "Unsupported arch $4!" ; exit 1 ;;
+esac
+
+TOTAL=$(($5 - 1))
+BRIDGE=$6
+EMULATOR=$7
+
+LIBVIRT_NIC_DRIVER=${LIBVIRT_NIC_DRIVER:-"e1000"}
+LIBVIRT_STORAGE_POOL=${LIBVIRT_STORAGE_POOL:-"default"}
+
+if ! virsh pool-list --all | grep -q $LIBVIRT_STORAGE_POOL; then
+    virsh pool-define-as --name $LIBVIRT_STORAGE_POOL dir --target /var/lib/libvirt/images >&2
+    virsh pool-autostart $LIBVIRT_STORAGE_POOL >&2
+    virsh pool-start $LIBVIRT_STORAGE_POOL >&2
+fi
+
+pool_state=$(virsh pool-info $LIBVIRT_STORAGE_POOL | grep State | awk '{ print $2 }')
+if [ "$pool_state" != "running" ] ; then
+  [ ! -d /var/lib/libvirt/images ] && sudo mkdir /var/lib/libvirt/images
+  virsh pool-start $LIBVIRT_STORAGE_POOL >&2
+fi
+
+PREALLOC=
+if [ -f /etc/debian_version ]; then
+    PREALLOC="--prealloc-metadata"
+fi
+
+DOMS=""
+for idx in $(seq 0 $TOTAL) ; do
+    NAME="baremetal${BRIDGE}_${idx}"
+    DOMS="$DOMS $NAME"
+    VOL_NAME="baremetal${BRIDGE}-${idx}.qcow2"
+    (virsh list --all | grep -q $NAME) && continue
+
+    virsh vol-list --pool $LIBVIRT_STORAGE_POOL | grep -q $VOL_NAME &&
+        virsh vol-delete $VOL_NAME --pool $LIBVIRT_STORAGE_POOL >&2
+    virsh vol-create-as $LIBVIRT_STORAGE_POOL ${VOL_NAME} ${DISK}G --format qcow2 $PREALLOC >&2
+    volume_path=$(virsh vol-path --pool $LIBVIRT_STORAGE_POOL $VOL_NAME)
+    # Pre-touch the VM to set +C, as it can only be set on empty files.
+    sudo touch "$volume_path"
+    sudo chattr +C "$volume_path" || true
+    $TOP_DIR/scripts/configure-vm --bootdev network --name $NAME --image "$volume_path" --arch $ARCH --cpus $CPU --memory $MEM --libvirt-nic-driver $LIBVIRT_NIC_DRIVER --emulator $EMULATOR --network $BRIDGE >&2
+done
+
+for dom in $DOMS ; do
+    # echo mac
+    virsh dumpxml $dom | grep "mac address" | head -1 | cut -d\' -f2
+done
diff --git a/tools/ironic/scripts/setup-network b/tools/ironic/scripts/setup-network
new file mode 100755
index 0000000..8c3ea90
--- /dev/null
+++ b/tools/ironic/scripts/setup-network
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+# **setup-network**
+
+# Setups openvswitch libvirt network suitable for
+# running baremetal poseur nodes for ironic testing purposes
+
+set -exu
+
+# Keep track of the devstack directory
+TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
+BRIDGE_SUFFIX=${1:-''}
+BRIDGE_NAME=brbm$BRIDGE_SUFFIX
+
+# Only add bridge if missing
+(sudo ovs-vsctl list-br | grep ${BRIDGE_NAME}$) || sudo ovs-vsctl add-br ${BRIDGE_NAME}
+
+# remove bridge before replacing it.
+(virsh net-list | grep "${BRIDGE_NAME} ") && virsh net-destroy ${BRIDGE_NAME}
+(virsh net-list --inactive  | grep "${BRIDGE_NAME} ") && virsh net-undefine ${BRIDGE_NAME}
+
+virsh net-define <(sed s/brbm/$BRIDGE_NAME/ $TOP_DIR/templates/brbm.xml)
+virsh net-autostart ${BRIDGE_NAME}
+virsh net-start ${BRIDGE_NAME}
diff --git a/tools/ironic/templates/brbm.xml b/tools/ironic/templates/brbm.xml
new file mode 100644
index 0000000..0769d3f
--- /dev/null
+++ b/tools/ironic/templates/brbm.xml
@@ -0,0 +1,6 @@
+<network>
+  <name>brbm</name>
+  <forward mode='bridge'/>
+  <bridge name='brbm'/>
+  <virtualport type='openvswitch'/>
+</network>
diff --git a/tools/ironic/templates/tftpd-xinetd.template b/tools/ironic/templates/tftpd-xinetd.template
new file mode 100644
index 0000000..7b9b0f8
--- /dev/null
+++ b/tools/ironic/templates/tftpd-xinetd.template
@@ -0,0 +1,11 @@
+service tftp
+{
+  protocol        = udp
+  port            = 69
+  socket_type     = dgram
+  wait            = yes
+  user            = root
+  server          = /usr/sbin/in.tftpd
+  server_args     = -v -v -v -v -v --map-file %TFTPBOOT_DIR%/map-file %TFTPBOOT_DIR%
+  disable         = no
+}
diff --git a/tools/ironic/templates/vm.xml b/tools/ironic/templates/vm.xml
new file mode 100644
index 0000000..b18dec0
--- /dev/null
+++ b/tools/ironic/templates/vm.xml
@@ -0,0 +1,43 @@
+<domain type='%(engine)s'>
+  <name>%(name)s</name>
+  <memory unit='KiB'>%(memory)s</memory>
+  <vcpu>%(cpus)s</vcpu>
+  <os>
+    <type arch='%(arch)s' machine='pc-1.0'>hvm</type>
+    <boot dev='%(bootdev)s'/>
+    <bootmenu enable='no'/>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+    <pae/>
+  </features>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <devices>
+    <emulator>%(emulator)s</emulator>
+    <disk type='file' device='disk'>
+      <driver name='qemu' type='qcow2' cache='writeback'/>
+      <source file='%(imagefile)s'/>
+      <target dev='vda' bus='virtio'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
+    </disk>
+    <controller type='ide' index='0'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
+    </controller>
+    %(network)s
+    %(bm_network)s
+    <input type='mouse' bus='ps2'/>
+    <graphics type='vnc' port='-1' autoport='yes'/>
+    <video>
+      <model type='cirrus' vram='9216' heads='1'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
+    </video>
+    <memballoon model='virtio'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
+    </memballoon>
+  </devices>
+</domain>
+