From e9838d6a94d3b49c8a85d42fbf5cddc69af6cdc5 Mon Sep 17 00:00:00 2001 From: Josep Puigdemont Date: Wed, 4 May 2016 14:27:23 +0200 Subject: virtual_fuel: initial support for remote libvirt With this patch it should be possible to create a fuel VM on a remote libvirt server by properly defining the LIBVIRT_DEFAULT_URI [1] environment variable. If the variable is not defined, then there should be no percievable change in behaviour for the script. This patch introduces the ability to create volumes (images) on a remote libvirt host where the Fuel VM is to be deployed. For now the volumes are created by default in a pool named jenkins, but the idea is to allow this to be configured, probably in the POD's DHA file. Since all virsh commands honor LIBVIRT_DEFAULT_URI, we use this environment variable to detect wheter we should create a volume or not. The rationale being that the variable will only be set if the user wants to to do the VM deployment on a remote libvirt host. We need to create a volume because we can not rely on being able to access the remote server's file system directly. The images are then transferred to the libvirt host using virsh commands. All this could also be done using scp and a user directory on the host machine, but using pools allows us to take advantage of libvirt's policies and file permissions. CHANGE: when LIBVIRT_DEFAULT_URI is defined, the script will not check for the presence of the required PXE bridge. This will still be checked when the Fuel VM is started and the bridge not found, but this happens at a later point than it does today. CHANGE: before this patch, the file system image was named like the VM: vm_name.raw. This patch introduces a change and adds a timestamp suffix to the image: vm_name-timestamp.raw. This is so to avoid collisions with an image with the same name on the remote pool (two PODs may be using the same pool). It may also be useful to keep around old file system images. FIXME: This patch requires a pool named "jenkins" in the remote libvirt server, and it will fail if it is not present. This should be configurable. Notice though that we can still define LIBVIRT_DEFAULT_URI as "qemu:///system" to create the Fuel VM on the local host. [1] https://libvirt.org/remote.html#Remote_URI_reference Change-Id: I40925ed31337d3ad9cf505f284f5c3d14e9129a0 Signed-off-by: Josep Puigdemont --- deploy/environments/virtual_fuel.py | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'deploy/environments') diff --git a/deploy/environments/virtual_fuel.py b/deploy/environments/virtual_fuel.py index ac5fc5356..f9f9f7ab9 100644 --- a/deploy/environments/virtual_fuel.py +++ b/deploy/environments/virtual_fuel.py @@ -11,14 +11,36 @@ from lxml import etree from execution_environment import ExecutionEnvironment import tempfile +import os +import re +import time from common import ( exec_cmd, check_file_exists, check_if_root, delete, + log, ) +VOL_XML_TEMPLATE = ''' + {name} + {size!s} + + + +''' + +DEFAULT_POOL = 'jenkins' + +def get_size_and_unit(s): + p = re.compile('^(\d+)\s*(\D+)') + m = p.match(s) + if m == None: + return None, None + size = m.groups()[0] + unit = m.groups()[1] + return size, unit class VirtualFuel(ExecutionEnvironment): @@ -55,14 +77,42 @@ class VirtualFuel(ExecutionEnvironment): with open(temp_vm_file, 'w') as f: vm_xml.write(f, pretty_print=True, xml_declaration=True) + def create_volume(self, pool, name, su, img_type='qcow2'): + log('Creating image using Libvirt volumes in pool %s, name: %s' % + (pool, name)) + size, unit = get_size_and_unit(su) + if size == None: + err('Could not determine size and unit of %s' % s) + + vol_xml = VOL_XML_TEMPLATE.format(name=name, unit=unit, size=size, + format_type=img_type) + fname = os.path.join(self.temp_dir, '%s_vol.xml' % name) + with file(fname, 'w') as f: + f.write(vol_xml) + + exec_cmd('virsh vol-create --pool %s %s' % (pool, fname)) + vol_path = exec_cmd('virsh vol-path --pool %s %s' % (pool, name)) + + delete(fname) + + return vol_path + def create_image(self, disk_path, disk_size): - exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size)) + if os.environ.get('LIBVIRT_DEFAULT_URI') == None: + exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size)) + else: + pool = DEFAULT_POOL # FIXME + name = os.path.basename(disk_path) + disk_path = self.create_volume(pool, name, disk_size) + + return disk_path def create_vm(self): - disk_path = '%s/%s.raw' % (self.storage_dir, self.vm_name) + stamp = time.strftime("%Y%m%d%H%M%S") + disk_path = '%s/%s-%s.raw' % (self.storage_dir, self.vm_name, stamp) disk_sizes = self.dha.get_disks() disk_size = disk_sizes['fuel'] - self.create_image(disk_path, disk_size) + disk_path = self.create_image(disk_path, disk_size) temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name) exec_cmd('cp %s %s' % (self.vm_template, temp_vm_file)) -- cgit 1.2.3-korg