[Kimchi-devel] [PATCH 1/3] Make Template defaults configurable

Aline Manera alinefm at linux.vnet.ibm.com
Mon May 4 15:24:04 UTC 2015


Create a template configuration file (template.conf) to handle all the
template default values.
Also parse it to set the right values on osinfo.py

As template.conf file uses nested sections, it is required to install an
additional package python-configobj to parse it.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 contrib/DEBIAN/control.in     |  1 +
 contrib/kimchi.spec.fedora.in |  2 ++
 contrib/kimchi.spec.suse.in   |  2 ++
 docs/README.md                |  8 ++---
 src/Makefile.am               |  2 +-
 src/kimchi/model/templates.py |  2 --
 src/kimchi/osinfo.py          | 78 +++++++++++++++++++++++++++++++++++++------
 src/template.conf             | 47 ++++++++++++++++++++++++++
 8 files changed, 125 insertions(+), 17 deletions(-)
 create mode 100644 src/template.conf

diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in
index 069e14b..0747d0f 100644
--- a/contrib/DEBIAN/control.in
+++ b/contrib/DEBIAN/control.in
@@ -6,6 +6,7 @@ Architecture: all
 Depends: python-cherrypy3 (>= 3.2.0),
          python-cheetah,
          python-imaging,
+         python-configobj,
          websockify,
          novnc,
          python-jsonschema (>= 1.3.0),
diff --git a/contrib/kimchi.spec.fedora.in b/contrib/kimchi.spec.fedora.in
index 8217640..30f8417 100644
--- a/contrib/kimchi.spec.fedora.in
+++ b/contrib/kimchi.spec.fedora.in
@@ -15,6 +15,7 @@ Requires:	libvirt-daemon-config-network
 Requires:	python-cherrypy >= 3.2.0
 Requires:	python-cheetah
 Requires:	python-websockify
+Requires:	python-configobj
 Requires:	novnc
 Requires:	m2crypto
 Requires:	python-imaging
@@ -162,6 +163,7 @@ rm -rf $RPM_BUILD_ROOT
 %{_sysconfdir}/nginx/conf.d/kimchi.conf.in
 %{_sysconfdir}/nginx/conf.d/kimchi.conf
 %{_sysconfdir}/kimchi/kimchi.conf
+%{_sysconfdir}/kimchi/template.conf
 %{_sysconfdir}/kimchi/distros.d/debian.json
 %{_sysconfdir}/kimchi/distros.d/fedora.json
 %{_sysconfdir}/kimchi/distros.d/opensuse.json
diff --git a/contrib/kimchi.spec.suse.in b/contrib/kimchi.spec.suse.in
index 22c651e..bf85661 100644
--- a/contrib/kimchi.spec.suse.in
+++ b/contrib/kimchi.spec.suse.in
@@ -15,6 +15,7 @@ Requires:	libvirt-daemon-config-network
 Requires:	python-CherryPy >= 3.2.0
 Requires:	python-Cheetah
 Requires:	python-websockify
+Requires:	python-configobj
 Requires:	novnc
 Requires:	python-imaging
 Requires:	python-M2Crypto
@@ -103,6 +104,7 @@ rm -rf $RPM_BUILD_ROOT
 %{_sysconfdir}/nginx/conf.d/kimchi.conf.in
 %{_sysconfdir}/nginx/conf.d/kimchi.conf
 %{_sysconfdir}/kimchi/kimchi.conf
+%{_sysconfdir}/kimchi/template.conf
 %{_sysconfdir}/kimchi/distros.d/debian.json
 %{_sysconfdir}/kimchi/distros.d/fedora.json
 %{_sysconfdir}/kimchi/distros.d/opensuse.json
diff --git a/docs/README.md b/docs/README.md
index b9458ac..034a591 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -55,7 +55,7 @@ Install Dependencies
                         python-ipaddr python-ldap python-lxml nfs-utils \
                         iscsi-initiator-utils libxslt pyparted nginx \
                         python-libguestfs libguestfs-tools python-websockify \
-                        novnc spice-html5
+                        novnc spice-html5 python-configobj
 
      # If using RHEL, install the following additional packages:
      $ sudo yum install python-unittest2 python-ordereddict
@@ -81,7 +81,7 @@ channel at RHN Classic or Red Hat Satellite.
 
     $ sudo apt-get install gcc make autoconf automake gettext git \
                            python-cherrypy3 python-cheetah python-libvirt \
-                           libvirt-bin python-imaging \
+                           libvirt-bin python-imaging python-configobj \
                            python-pam python-m2crypto python-jsonschema \
                            qemu-kvm libtool python-psutil python-ethtool \
                            sosreport python-ipaddr python-ldap \
@@ -104,8 +104,8 @@ channel at RHN Classic or Red Hat Satellite.
                           python-imaging python-M2Crypto python-jsonschema \
                           rpm-build kvm python-psutil python-ethtool \
                           python-ipaddr python-ldap python-lxml nfs-client \
-                          open-iscsi libxslt-tools python-xml \
-                          python-parted nginx python-libguestfs \
+                          open-iscsi libxslt-tools python-xml python-parted \
+                          nginx python-libguestfs python-configobj \
                           guestfs-tools python-websockify novnc
 
     Packages version requirement:
diff --git a/src/Makefile.am b/src/Makefile.am
index f57d121..38d231c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,7 +27,7 @@ EXTRA_DIST = kimchid.in \
 bin_SCRIPTS = kimchid
 
 confdir = $(sysconfdir)/kimchi
-dist_conf_DATA = kimchi.conf
+dist_conf_DATA = kimchi.conf template.conf
 
 BUILT_SOURCES = kimchi.conf
 
diff --git a/src/kimchi/model/templates.py b/src/kimchi/model/templates.py
index 8a1820d..ef83706 100644
--- a/src/kimchi/model/templates.py
+++ b/src/kimchi/model/templates.py
@@ -68,8 +68,6 @@ class TemplatesModel(object):
                 # exception if a topology is invalid.
                 CPUInfoModel(conn=self.conn).\
                     check_topology(params['cpus'], topology)
-        else:
-            params['cpu_info'] = dict()
 
         conn = self.conn.get()
         pool_uri = params.get(u'storagepool', '')
diff --git a/src/kimchi/osinfo.py b/src/kimchi/osinfo.py
index 05c685e..67d85be 100644
--- a/src/kimchi/osinfo.py
+++ b/src/kimchi/osinfo.py
@@ -21,10 +21,10 @@ import copy
 import glob
 import os
 
-
+from collections import defaultdict
+from configobj import ConfigObj
 from distutils.version import LooseVersion
 
-
 from kimchi.config import paths
 
 
@@ -85,14 +85,72 @@ icon_available_distros = [icon[5:-4] for icon in glob.glob1('%s/images/'
                           % paths.ui_dir, 'icon-*.png')]
 
 
-defaults = {'networks': ['default'],
-            'storagepool': '/storagepools/default',
-            'domain': 'kvm', 'arch': os.uname()[4],
-            'graphics': {'type': 'vnc', 'listen': '127.0.0.1'},
-            'cpus': 1,
-            'memory': 1024,
-            'disks': [{'index': 0, 'size': 10, 'format': 'qcow2'}],
-            'cdrom_bus': 'ide', 'cdrom_index': 2, 'mouse_bus': 'ps2'}
+def _get_tmpl_defaults():
+    """
+    ConfigObj returns a dict like below when no changes were made in the
+    template configuration file (template.conf)
+
+    {'main': {}, 'storage': {'disk.0': {}}, 'processor': {}, 'graphics': {}}
+
+    The default values should be like below:
+
+    {'main': {'networks': ['default'], 'memory': '1024'},
+     'storage': {'pool': 'default',
+                 'disk.0': {'format': 'qcow2', 'size': '10'}},
+     'processor': {'cpus': '1'},
+     'graphics': {'type': 'spice', 'listen': '127.0.0.1'}}
+    """
+    # Create dict with default values
+    tmpl_defaults = defaultdict(dict)
+    tmpl_defaults['main']['networks'] = ['default']
+    tmpl_defaults['main']['memory'] = 1024
+    tmpl_defaults['storage']['pool'] = 'default'
+    tmpl_defaults['storage']['disk.0'] = {'size': 10, 'format': 'qcow2'}
+    tmpl_defaults['processor']['cpus'] = 1
+    tmpl_defaults['graphics'] = {'type': 'vnc', 'listen': '127.0.0.1'}
+
+    default_config = ConfigObj(tmpl_defaults)
+
+    # Load template configuration file
+    config_file = os.path.join(paths.conf_dir, 'template.conf')
+    config = ConfigObj(config_file)
+
+    # Merge default configuration with file configuration
+    default_config.merge(config)
+
+    # Create a dict with default values according to data structure
+    # expected by VMTemplate
+    defaults = {'domain': 'kvm', 'arch': os.uname()[4],
+                'cdrom_bus': 'ide', 'cdrom_index': 2, 'mouse_bus': 'ps2'}
+
+    # Parse main section to get networks and memory values
+    main_section = default_config.pop('main')
+    defaults.update(main_section)
+
+    # Parse storage section to get storage pool and disks values
+    storage_section = default_config.pop('storage')
+    defaults['storagepool'] = '/storagepools/' + storage_section.pop('pool')
+    defaults['disks'] = []
+    for disk in storage_section.keys():
+        data = storage_section[disk]
+        data['index'] = int(disk.split('.')[1])
+        defaults['disks'].append(data)
+
+    # Parse processor section to get cpus and cpu_topology values
+    processor_section = default_config.pop('processor')
+    defaults['cpus'] = processor_section.pop('cpus')
+    defaults['cpu_info'] = {}
+    if len(processor_section.keys()) > 0:
+        defaults['cpu_info']['topology'] = processor_section
+
+    # Update defaults values with graphics values
+    defaults['graphics'] = default_config.pop('graphics')
+
+    return defaults
+
+
+# Set defaults values according to template.conf file
+defaults = _get_tmpl_defaults()
 
 
 def _get_arch():
diff --git a/src/template.conf b/src/template.conf
new file mode 100644
index 0000000..f3615e6
--- /dev/null
+++ b/src/template.conf
@@ -0,0 +1,47 @@
+#
+# Configuration file for Kimchi Templates
+#
+
+[main]
+# Memory in MB
+#memory = 1024
+
+# List of networks separated by comma
+# Represents the virtual network interfaces to be assigned to guest
+#networks = default,
+
+[storage]
+# Storage pool used to handle the guest disk
+#pool = default
+
+# Specify multiple [[disk.X]] sub-sections to add multiples disks to guest
+# All the disk files will be created in the same storage pool as set above
+[[disk.0]]
+# Disk size in GB
+#size = 10
+
+# Disk format
+#format = qcow2
+
+[graphics]
+# Graphics type
+# Valid options: vnc | spice
+#type = vnc
+
+# The network which the vnc/spice server listens on
+#listen = 127.0.0.1
+
+[processor]
+# Number of vcpus
+# When specifying CPU topology, make sure cpus value is equal to the product
+# of sockets, cores, and threads.
+#cpus = 1
+
+# Number of sockets (not set by default)
+#sockets =
+
+# Number of cores per socket (not set by default)
+#cores =
+
+# Number of threads per core (not set by default)
+#threads =
-- 
2.1.0




More information about the Kimchi-devel mailing list