Adding basic role to create VMs for testing

Adding a basic role to create virtual machines for testing
Bifrost.
This commit is contained in:
Julia Kreger
2015-03-27 20:29:02 -04:00
parent afc8426f4b
commit 565d3d6f0c
7 changed files with 434 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
bifrost-create-vm-nodes
=======================
This role creates virtual machines for testing bifrost.
Requirements
------------
The following packages are required and ensured to be present:
- libvirt-bin
- qemu-utils
- qemu-kvm
- sgabios
Role Variables
--------------
baremetal_csv_file: "/tmp/baremetal.csv"
Dependencies
------------
None at this time.
Example Playbook
----------------
- hosts: localhost
connection: local
sudo: yes
gather_facts: yes
roles:
- role: bifrost-create-vm-nodes
License
-------
Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
See the License for the specific language governing permissions and
limitations under the License.
Author Information
------------------
Ironic Developers

View File

@@ -0,0 +1,3 @@
---
# defaults file for bifrost-create-vm-nodes
baremetal_csv_file: "/tmp/baremetal.csv"

View File

@@ -0,0 +1,224 @@
#!/bin/bash
#############################################################################
# create_nodes.sh - Script to create VM nodes for use with Ironic.
#
# PURPOSE
# This script can be used to create VM instances without an operating
# system and that are ready for netbooting. They are connected to the
# bridge named 'brbm' (created if it does not exist).
#
# EXAMPLE USAGE
# # Use defaults - Create a single node with base name of 'testvm'
# sudo create_nodes.sh
#
# # Create 5 nodes
# sudo NODECOUNT=5 create_nodes.sh
#
# # Create 3 nodes with base name of 'junk'
# sudo NODEBASE=junk NODECOUNT=3 create_nodes.sh
#
# THANKS
# Thanks to the author(s) of the Ironic supporting code within devstack,
# from which all of this is derived.
#
# AUTHOR
# David Shrewsbury (shrewsbury.dave@gmail.com)
#############################################################################
set -e # exit immediately on command error
set -u # treat unset variables as error when substituting
LIBVIRT_CONNECT_URI=${LIBVIRT_CONNECT_URI:-"qemu:///system"}
export VIRSH_DEFAULT_CONNECT_URI="$LIBVIRT_CONNECT_URI"
# VM specs
VM_EMULATOR=${VM_EMULATOR:-/usr/bin/qemu-system-x86_64}
VM_CPU=${VM_CPU:-1}
VM_RAM=${VM_RAM:-3072}
VM_DISK=${VM_DISK:-10}
# VM network
VM_NET_BRIDGE=${VM_NET_BRIDGE:-default}
# VM logging directory
VM_LOGDIR=/var/log/libvirt/baremetal_logs
#############################################################################
# FUNCTION
# create_node
#
# PARAMETERS
# $1: Virtual machine name
# $2: Number of CPUs for the VM
# $3: Amount of RAM for the VM
# $4: Disk size (in GB) for the VM
# $5: CPU architecture (i386 or amd64)
# $6: Network bridge for the VMs
# $7: Path to VM emulator
# $8: Logging directory for the VMs
#############################################################################
function create_node {
NAME=$1
CPU=$2
MEM=$(( 1024 * $3 ))
# extra G to allow fuzz for partition table : flavor size and registered size
# need to be different to actual size.
DISK=$(( $4 + 1))
case $5 in
i386) ARCH='i686' ;;
amd64) ARCH='x86_64' ;;
*) echo "Unsupported arch $5!" ; exit 1 ;;
esac
BRIDGE=$6
EMULATOR=$7
LOGDIR=$8
LIBVIRT_NIC_DRIVER=${LIBVIRT_NIC_DRIVER:-"e1000"}
LIBVIRT_STORAGE_POOL=${LIBVIRT_STORAGE_POOL:-"default"}
LIBVIRT_CONNECT_URI=${LIBVIRT_CONNECT_URI:-"qemu:///system"}
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 ] && mkdir /var/lib/libvirt/images
virsh pool-start $LIBVIRT_STORAGE_POOL >&2
fi
if [ -n "$LOGDIR" ] ; then
mkdir -p "$LOGDIR"
fi
PREALLOC=
if [ -f /etc/debian_version ]; then
PREALLOC="--prealloc-metadata"
fi
VM_LOGGING="$LOGDIR/${NAME}_console.log"
VOL_NAME="${NAME}.qcow2"
if ! virsh list --all | grep -q $NAME; then
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.
touch "$volume_path"
chattr +C "$volume_path" || true
vm_xml="
<domain type='qemu'>
<name>${NAME}</name>
<memory unit='KiB'>${MEM}</memory>
<vcpu>${CPU}</vcpu>
<os>
<type arch='${ARCH}' machine='pc-1.0'>hvm</type>
<boot dev='network'/>
<bootmenu enable='no'/>
<bios useserial='yes'/>
</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}</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback'/>
<source file='${volume_path}'/>
<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>
<interface type='network'>
<source network='${BRIDGE}'/>
</interface>
<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>
<serial type='file'>
<source path='${VM_LOGGING}'/>
<target port='0'/>
<alias name='serial0'/>
</serial>
<serial type='pty'>
<source path='/dev/pts/49'/>
<target port='1'/>
<alias name='serial1'/>
</serial>
<console type='file'>
<source path='${VM_LOGGING}'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</domain>
"
echo ${vm_xml} > /tmp/vm.xml
virsh create /tmp/vm.xml 2>&1 >/dev/null
if [ $? != 0 ]
then
echo "failed to create VM $NAME"
rm -f /tmp/vm.xml
exit 1
fi
rm -f /tmp/vm.xml
fi
# echo mac
local macaddr=`virsh dumpxml $NAME | grep "mac address" | head -1 | cut -d\' -f2`
echo $macaddr
}
####################
# Main script code
####################
NODEBASE=${NODEBASE:-testvm}
NODECOUNT=${NODECOUNT:-1}
NODEOUTPUT=${NODEOUTPUT:-"/tmp/baremetal.csv"}
TEMPFILE=`mktemp`
# must be root
user=`whoami`
if [ "$user" != "root" ]
then
echo "Must be run as root. You are $user."
exit 1
fi
for (( i=1; i<=${NODECOUNT}; i++ ))
do
name=${NODEBASE}${i}
mac=$(create_node $name $VM_CPU $VM_RAM $VM_DISK amd64 $VM_NET_BRIDGE $VM_EMULATOR $VM_LOGDIR)
printf "$mac,root,undefined,192.168.122.1,$VM_CPU,$VM_RAM,$VM_DISK,flavor,type,a8cb6624-0d9f-c882-affc-046ebb96ec0${i},$name,192.168.122.$((i+1))\n" >>$TEMPFILE
done
mv "${TEMPFILE}" "${NODEOUTPUT}"

View File

@@ -0,0 +1,2 @@
---
# handlers file for bifrost-create-vm-nodes

View File

@@ -0,0 +1,116 @@
---
galaxy_info:
author: Ironic Developers
description:
company: OpenStack
license: Apache
min_ansible_version: 1.9
#
# Below are all platforms currently available. Just uncomment
# the ones that apply to your role. If you don't see your
# platform on this list, let us know and we'll get it added!
#
platforms:
#- name: EL
# versions:
# - all
# - 5
# - 6
# - 7
#- name: GenericUNIX
# versions:
# - all
# - any
#- name: Fedora
# versions:
# - all
# - 16
# - 17
# - 18
# - 19
# - 20
#- name: SmartOS
# versions:
# - all
# - any
#- name: opensuse
# versions:
# - all
# - 12.1
# - 12.2
# - 12.3
# - 13.1
# - 13.2
#- name: Amazon
# versions:
# - all
# - 2013.03
# - 2013.09
#- name: GenericBSD
# versions:
# - all
# - any
#- name: FreeBSD
# versions:
# - all
# - 8.0
# - 8.1
# - 8.2
# - 8.3
# - 8.4
# - 9.0
# - 9.1
# - 9.1
# - 9.2
#- name: Ubuntu
# versions:
# - all
# - lucid
# - maverick
# - natty
# - oneiric
# - precise
# - quantal
# - raring
# - saucy
- trusty
#- name: SLES
# versions:
# - all
# - 10SP3
# - 10SP4
# - 11
# - 11SP1
# - 11SP2
# - 11SP3
#- name: GenericLinux
# versions:
# - all
# - any
#- name: Debian
# versions:
# - all
# - etch
# - lenny
# - squeeze
# - wheezy
#
# Below are all categories currently available. Just as with
# the platforms above, uncomment those that apply to your role.
#
categories:
- cloud
- cloud:openstack
#- cloud:gce
#- cloud:rax
#- clustering
#- database
#- database:nosql
#- database:sql
#- development
#- monitoring
#- networking
#- packaging
#- system
#- web
dependencies: []

View File

@@ -0,0 +1,31 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# TODO: Consider converting to ansible virt module.
---
- name: "Install testing packages"
apt: name={{ item }} state=present
with_items:
- libvirt-bin
- qemu-utils
- qemu-kvm
- sgabios
- name: "Create virtual machines"
script: create_vm_nodes-for-role.sh creates="{{baremetal_csv_file}}"
environment:
NODEOUTPUT: "{{baremetal_csv_file}}"
delegate_to: localhost
- name: "Setting file permissions such that the baremetal csv file at /tmp/baremetal.csv can be read by the user executing Ansible"
file: path="{{baremetal_csv_file}}" owner="{{ansible_env.SUDO_USER}}"
when: ansible_env.SUDO_USER is defined and baremetal_csv_file is defined and baremetal_csv_file != ""

View File

@@ -0,0 +1,2 @@
---
# vars file for bifrost-create-vm-nodes