[Kimchi-devel] [PATCH v2 1/2] Dynamically generate template parameters for different distros
Mark Wu
wudxw at linux.vnet.ibm.com
Tue Jan 7 07:28:34 UTC 2014
We define hard coded template for different distros. It's not
flexible to generate templates for other platforms or new distros.
This patch changes to dynamically generate vm template. With
this change, it can support new distro, like Fedora20, without any
modifications.
Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
src/kimchi/osinfo.py | 184 ++++++++++-------------------------------------
src/kimchi/vmtemplate.py | 2 +-
tests/test_osinfo.py | 31 ++++++--
3 files changed, 63 insertions(+), 154 deletions(-)
diff --git a/src/kimchi/osinfo.py b/src/kimchi/osinfo.py
index 72c2a74..59198a0 100644
--- a/src/kimchi/osinfo.py
+++ b/src/kimchi/osinfo.py
@@ -23,144 +23,25 @@
import copy
import os
-osinfo = [
- # Entries are searched in order and the first match will be returned
- ('debian', {
- 'version': lambda d,v: bool(d == 'debian' and v in ('6.0', '7.0')),
- 'icon': 'images/icon-debian.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('debian-old', {
- 'version': lambda d,v: bool(d == 'debian'),
- 'icon': 'images/icon-debian.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('ubuntu', {
- 'version': lambda d,v: bool(d == 'ubuntu' and v in
- ('7.10', '8.04', '8.10', '9.04', '9.10', '10.04', '10.10',
- '11.04', '11.10', '12.04', '12.10', '13.04', '13.10')),
- 'icon': 'images/icon-ubuntu.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('ubuntu-old', {
- 'version': lambda d,v: bool(d == 'ubuntu'),
- 'icon': 'images/icon-ubuntu.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('opensuse', {
- 'version': lambda d,v: bool(d == 'opensuse' and v in
- ('10.3', '11.0', '11.1', '11.2', '11.3', '11.4', '12.1', '12.2',
- '12.3',)),
- 'icon': 'images/icon-opensuse.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('opensuse-old', {
- 'version': lambda d,v: bool(d == 'opensuse'),
- 'icon': 'images/icon-opensuse.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('fedora', {
- 'version': lambda d,v: bool(d == 'fedora' and v in
- ('16', '17', '18', '19')),
- 'icon': 'images/icon-fedora.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('fedora-old', {
- 'version': lambda d,v: bool(d == 'fedora'),
- 'icon': 'images/icon-fedora.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('rhel', {
- 'version': lambda d,v: bool(d == 'rhel' and
- v.startswith('6.')),
- 'icon': 'images/icon-vm.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('rhel-old', {
- 'version': lambda d,v: bool(d == 'rhel'),
- 'icon': 'images/icon-vm.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('centos', {
- 'version': lambda d,v: bool(d == 'centos' and
- (v in ('5.3', '5.4', '5.5') or v.startswith('6.'))),
- 'icon': 'images/icon-centos.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('centos-old', {
- 'version': lambda d,v: bool(d == 'centos'),
- 'icon': 'images/icon-centos.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('gentoo', {
- 'version': lambda d,v: bool(d == 'gentoo'),
- 'icon': 'images/icon-gentoo.png',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'virtio', 'nic_model': 'virtio',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
- ('unknown', {
- 'version': lambda d,v: True,
- 'icon': 'images/icon-vm.png',
- 'os_distro': 'unknown', 'os_version': 'unknown',
- 'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1,
- 'memory': 1024,
- 'cdrom': '',
- 'disks': [{'index': 0, 'size': 10}],
- 'disk_bus': 'ide', 'nic_model': 'e1000',
- 'cdrom_bus': 'ide', 'cdrom_index': 2,
- }),
-]
+
+from distutils.version import LooseVersion
+
+
+common_spec = {'cpus': 1, 'cpu_cores': 1, 'cpu_threads': 1, 'memory': 1024,
+ 'disks': [{'index': 0, 'size': 10}], 'cdrom_bus': 'ide',
+ 'cdrom_index': 2}
+
+
+modern_spec = dict(common_spec, disk_bus='virtio', nic_model='virtio')
+
+
+old_spec = dict(common_spec, disk_bus='ide', nic_model='e1000')
+
+
+modern_version_bases = {'debian': '6.0', 'ubuntu': '7.10', 'opensuse': '10.3',
+ 'centos': '5.3', 'rhel': '6.0', 'fedora': '16',
+ 'gentoo': '0'}
+
isolinks = {
'debian': {
@@ -191,13 +72,20 @@ def lookup(distro, version):
'defaults' and merging the parameters given for the identified OS. If
known, a link to a remote install CD is added.
"""
- for name, entry in osinfo:
- # Test if this entry is a valid match
- if entry['version'](distro, version):
- params = copy.copy(defaults)
- params['os_distro'] = distro
- params['os_version'] = version
- params.update(entry)
- params['cdrom'] = isolinks.get(distro, {}).get(version, '')
- del params['version'] # Don't pass around the version function
- return (name, params)
+ params = copy.copy(defaults)
+ params['os_distro'] = distro
+ params['os_version'] = version
+ params['cdrom'] = isolinks.get(distro, {}).get(version, '')
+
+ if distro in modern_version_bases:
+ params['icon'] = 'images/icon-%s.png' % distro
+ if LooseVersion(version) >= LooseVersion(modern_version_bases[distro]):
+ params.update(modern_spec)
+ else:
+ params.update(old_spec)
+ else:
+ params['icon'] = 'images/icon-vm.png'
+ params['os_distro'] = params['os_version'] = "unknown"
+ params.update(old_spec)
+
+ return params
diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py
index 5d31f2a..2862992 100644
--- a/src/kimchi/vmtemplate.py
+++ b/src/kimchi/vmtemplate.py
@@ -70,7 +70,7 @@ class VMTemplate(object):
# Fetch defaults based on the os distro and version
os_distro = args.get('os_distro', iso_distro)
os_version = args.get('os_version', iso_version)
- name, entry = osinfo.lookup(os_distro, os_version)
+ entry = osinfo.lookup(os_distro, os_version)
self.info.update(entry)
# Override with the passed in parameters
diff --git a/tests/test_osinfo.py b/tests/test_osinfo.py
index 69fdaf9..0f88a35 100644
--- a/tests/test_osinfo.py
+++ b/tests/test_osinfo.py
@@ -23,21 +23,42 @@
import unittest
-from kimchi.osinfo import lookup
+from kimchi.osinfo import lookup, modern_version_bases
class OSInfoTests(unittest.TestCase):
def test_default_lookup(self):
- name, entry = lookup(None, None)
- self.assertEquals(name, 'unknown')
+ entry = lookup(None, None)
self.assertEquals('unknown', entry['os_distro'])
self.assertEquals('unknown', entry['os_version'])
self.assertEquals(['default'], entry['networks'])
def test_fedora_lookup(self):
cd = 'http://fedora.mirrors.tds.net/pub/fedora/releases/17/Live/x86_64/Fedora-17-x86_64-Live-Desktop.iso'
- name, entry = lookup('fedora', '17')
- self.assertEquals(name, 'fedora')
+ entry = lookup('fedora', '17')
self.assertEquals(10, entry['disks'][0]['size'])
self.assertEquals(cd, entry['cdrom'])
self.assertEquals('/storagepools/default', entry['storagepool'])
+
+ def test_old_distros(self):
+ old_versions = {'debian': '5.0', 'ubuntu': '7.04', 'opensuse': '10.1',
+ 'centos': '5.1', 'rhel': '5.1', 'fedora': '15'}
+ for distro, version in old_versions.iteritems():
+ entry = lookup(distro, version)
+ self.assertEquals(entry['disk_bus'], 'ide')
+ self.assertEquals(entry['nic_model'], 'e1000')
+
+ def test_modern_bases(self):
+ for distro, version in modern_version_bases.iteritems():
+ entry = lookup(distro, version)
+ self.assertEquals(entry['disk_bus'], 'virtio')
+ self.assertEquals(entry['nic_model'], 'virtio')
+
+ def test_modern_distros(self):
+ modern_versions = {'debian': '7.0', 'ubuntu': '12.04',
+ 'opensuse': '12.3', 'centos': '6.4', 'rhel': '6.3',
+ 'fedora': '18', 'gentoo': '12.1'}
+ for distro, version in modern_versions.iteritems():
+ entry = lookup(distro, version)
+ self.assertEquals(entry['disk_bus'], 'virtio')
+ self.assertEquals(entry['nic_model'], 'virtio')
--
1.8.4.2
More information about the Kimchi-devel
mailing list