From f5e50665361a97a1a7335ac295e73b48efee9cb7 Mon Sep 17 00:00:00 2001 From: Julian Edwards Date: Wed, 8 Feb 2017 20:05:59 +1000 Subject: [PATCH] Task to create VMs --- defaults/main.yml | 5 ++ files/vm-tools/scripts/configure-vm.py | 114 +++++++++++++++++++++++++ files/vm-tools/templates/vm.xml | 49 +++++++++++ tasks/create_test_vms.yml | 60 +++++++++++++ 4 files changed, 228 insertions(+) create mode 100755 files/vm-tools/scripts/configure-vm.py create mode 100644 files/vm-tools/templates/vm.xml create mode 100644 tasks/create_test_vms.yml diff --git a/defaults/main.yml b/defaults/main.yml index 35a6aeab..e6e35067 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 diff --git a/files/vm-tools/scripts/configure-vm.py b/files/vm-tools/scripts/configure-vm.py new file mode 100755 index 00000000..51cee5fb --- /dev/null +++ b/files/vm-tools/scripts/configure-vm.py @@ -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 = """ + + + + + + + + + + + + + + + +""" + + +CONSOLE_PTY = """ + + + + + + +""" + + +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() diff --git a/files/vm-tools/templates/vm.xml b/files/vm-tools/templates/vm.xml new file mode 100644 index 00000000..0131e159 --- /dev/null +++ b/files/vm-tools/templates/vm.xml @@ -0,0 +1,49 @@ + + %(name)s + %(memory)s + %(cpus)s + + hvm + + + + + + + + + + + destroy + restart + restart + + %(emulator)s + + + + +
+ + +
+ + + + + +
+ + + +