Skip to content

Commit

Permalink
Task to create VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
bigjools committed Feb 8, 2017
1 parent 2cf3405 commit f5e5066
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
5 changes: 5 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ ironic_users_to_create:
- 127.0.0.1
- "{{ (network_pools.admin_net[mc_class]|net_range_to_list) }}"

ironic_vm_storage_pool_name: default
ironic_test_vm_names:
- ironic1
- ironic2
- ironic3
114 changes: 114 additions & 0 deletions files/vm-tools/scripts/configure-vm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python
#
# 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.


import argparse
import os.path

import libvirt

templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
'templates')


CONSOLE_LOG = """
<serial type='file'>
<source path='%(console_log)s'/>
<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='%(console_log)s'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
"""


CONSOLE_PTY = """
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
"""


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='virtio',
help='The libvirt network driver to use')
parser.add_argument('--console-log',
help='File to log console')
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,
'nicdriver': args.libvirt_nic_driver,
'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"

if args.console_log:
params['console'] = CONSOLE_LOG % {'console_log': args.console_log}
else:
params['console'] = CONSOLE_PTY
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()
49 changes: 49 additions & 0 deletions files/vm-tools/templates/vm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<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'/>
<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)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>
<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>
<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>
%(console)s
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</domain>

60 changes: 60 additions & 0 deletions tasks/create_test_vms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
# Copyright 2017 Cisco Systems
#
# 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.

- name: Create a storage pool
shell: |
virsh pool-define-as --name {{ ironic_vm_storage_pool_name }} dir --target /var/lib/libvirt/images &&
virsh pool-autostart {{ ironic_vm_storage_pool_name }} &&
virsh pool-start {{ ironic_vm_storage_pool_name }}
- name: Create storage volumes
shell: |
virsh vol-create-as {{ ironic_vm_storage_pool_name }} {{ item }}.qcow2 1G --format qcow2 --prealloc-metadata
with_items: "{{ ironic_test_vm_names }}"

# TODO: Create unique vlan tags per slot
- name: Create network bridges for VMs
shell: |
ip link add br-{{ item }} type bridge &&
ip set br-{{ item }} up &&
ip link add link {{ admin_network_interface }} name {{ admin_network_interface }}.999 type vlan id 999 &&
ip link set {{ admin_network_interface }}.999 master br-{{ item }} &&
ip link set {{ admin_network_interface }}.999 up
with_items: "{{ ironic_test_vm_names }}"

- name: Copy VM creation tool
copy:
src: vm-tools
dest: /tmp/

- name: Create VMs
shell: |
vol_path=$(virsh vol-path --pool {{ ironic_vm_storage_pool_name }} {{ item }}.qcow2) &&
/tmp/vm-tools/scripts/configure-vm.py --bootdev network --name {{ item }} --image $vol_path
--arch x86_64 --cpus 2 --memory 1310720 --network br-{{ item }}
--console-log /var/log/ironic/console-{{ item }}.log
with_items: {{ ironic_test_vm_names }}

- name: Connect VMBC to VMs
shell: |
vbmc add {{ item.1 }} --port $((6230+{{ item.0 }})) && vbmc start {{ item.1 }}
with_indexed_items: "{{ ironic_test_vm_names }}"

- name: Create Ironic ports
shell: |
mac=(virsh dumpxml {{ item }} | grep "mac address" | head -1 | cut -d\' -f2) &&
uuid=$(ironic node-show {{ item }} | grep " uuid " | awk -F'|' '{print $2}' | tr -d ' ') &&
ironic port-create -n $uuid -a $mac
with_items: {{ ironic_test_vm_names }}

0 comments on commit f5e5066

Please sign in to comment.