From 3e12575c6405304af0d30565664d4ea7da6f1031 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 9 May 2014 12:40:17 +0100 Subject: [PATCH] Move libvirt XML generation to separate module. Code refactoring. --- Makefile.am | 1 + libvirt_xml.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mclu_build.py | 75 ++++------------------------------------------ 3 files changed, 102 insertions(+), 69 deletions(-) create mode 100644 libvirt_xml.py diff --git a/Makefile.am b/Makefile.am index a1ae11d..1d9c8bc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ bin_SCRIPTS = mclu pkgdata_SCRIPTS = \ config.py \ lib.py \ + libvirt_xml.py \ mclu.py \ mclu_build.py \ mclu_console.py \ diff --git a/libvirt_xml.py b/libvirt_xml.py new file mode 100644 index 0000000..a2f082d --- /dev/null +++ b/libvirt_xml.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import os +import sys + +# Generate libvirt XML. Would be nice to use virt-install here, but +# it doesn't work: RHBZ#1095789 +def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output): + network_model = "virtio" + if not virtio: + network_model = "e1000" + + # XXX Quoting, and we should use a real XML generator. + xml = """ + + %s + %d + %d + %d + + hvm + + + + + + + + + + + + + + destroy + restart + restart + + + + + + + + + + + + + +""" % (vm_name, memory, memory, vcpus, network_model) + + # virtio-scsi or IDE disk: + if virtio: + xml += """ + + + + + + +""" % output + else: + xml += """ + + + + + +""" % output + + xml += """ + + +""" + + return xml diff --git a/mclu_build.py b/mclu_build.py index ce077a6..97b3ade 100644 --- a/mclu_build.py +++ b/mclu_build.py @@ -25,6 +25,7 @@ import libvirt import config import lib +import libvirt_xml def cmdline (subparsers): p = subparsers.add_parser ( @@ -105,75 +106,11 @@ def run (c, args, nodes): # Generate the XML. Would be nice to use virt-install here, but # it doesn't work: RHBZ#1095789 - network_model = "virtio" - if not args.virtio: - network_model = "e1000" - - # XXX Quoting, and we should use a real XML generator. - xml = """ - - %s - %d - %d - %d - - hvm - - - - - - - - - - - - - - destroy - restart - restart - - - - - - - - - - - - - -""" % (vm_name, args.memory, args.memory, args.vcpus, network_model) - - # virtio-scsi or IDE disk: - if args.virtio: - xml += """ - - - - - - -""" % output - else: - xml += """ - - - - - -""" % output - - xml += """ - - -""" + xml = libvirt_xml.generate_libvirt_xml (vm_name, + args.memory, + args.vcpus, + args.virtio, + output) # Write the XML to the xmls_dir. fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w") -- 1.8.3.1