Commit 870dc44c authored by Iustin Pop's avatar Iustin Pop

Abstract OS name/variant functions

Currently, the computation of the 'pure' name or the variant is
hardcoded and spread around the functions that need it. This is not
nice, and in the future we'd spread it even more with more usage of
variants/pure os names.

This patch abstracts these functions into the OS class, and then
replaces the hardcoded uses with the new functions.
Signed-off-by: default avatarIustin Pop <>
Reviewed-by: default avatarGuido Trotter <>
parent 677a6451
......@@ -1943,7 +1943,7 @@ def OSFromDisk(name, base_dir=None):
@raise RPCFail: if we don't find a valid OS
name_only = name.split("+", 1)[0]
name_only = objects.OS.GetName(name)
status, payload = _TryOSFromDisk(name_only, base_dir)
if not status:
......@@ -1978,9 +1978,8 @@ def OSCoreEnv(os_name, inst_os, os_params, debug=0):
# OS variants
if api_version >= constants.OS_API_V15:
variant = os_name.split('+', 1)[1]
except IndexError:
variant = objects.OS.GetVariant(os_name)
if not variant:
variant = inst_os.supported_variants[0]
result['OS_VARIANT'] = variant
......@@ -2528,7 +2527,7 @@ def ValidateOS(required, osname, checks, osparams):
_Fail("Unknown checks required for OS %s: %s", osname,
name_only = osname.split("+", 1)[0]
name_only = objects.OS.GetName(osname)
status, tbv = _TryOSFromDisk(name_only, None)
if not status:
......@@ -1088,9 +1088,8 @@ def _CheckOSVariant(os_obj, name):
if not os_obj.supported_variants:
variant = name.split("+", 1)[1]
except IndexError:
variant = objects.OS.GetVariant(name)
if not variant:
raise errors.OpPrereqError("OS name must include a variant",
# Copyright (C) 2006, 2007 Google Inc.
# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google 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
......@@ -861,6 +861,9 @@ class OS(ConfigObject):
@ivar supported_parameters: a list of tuples, name and description,
containing the supported parameters by this OS
@type VARIANT_DELIM: string
@cvar VARIANT_DELIM: the variant delimiter
__slots__ = [
......@@ -875,6 +878,41 @@ class OS(ConfigObject):
def SplitNameVariant(cls, name):
"""Splits the name into the proper name and variant.
@param name: the OS (unprocessed) name
@rtype: list
@return: a list of two elements; if the original name didn't
contain a variant, it's returned as an empty string
nv = name.split(cls.VARIANT_DELIM, 1)
if len(nv) == 1:
return nv
def GetName(cls, name):
"""Returns the proper name of the os (without the variant).
@param name: the OS (unprocessed) name
return cls.SplitNameVariant(name)[0]
def GetVariant(cls, name):
"""Returns the variant the os (without the base name).
@param name: the OS (unprocessed) name
return cls.SplitNameVariant(name)[1]
class Node(TaggableObject):
"""Config object representing a node."""
# Copyright (C) 2006, 2007, 2008 Google Inc.
# Copyright (C) 2006, 2007, 2008, 2010 Google 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
......@@ -142,5 +142,21 @@ class TestClusterObject(unittest.TestCase):
class TestOS(unittest.TestCase):
def testSplitNameVariant(self):
for name in self.ALL_DATA:
self.assertEqual(len(objects.OS.SplitNameVariant(name)), 2)
def testVariant(self):
self.assertEqual(objects.OS.GetVariant("debootstrap"), "")
self.assertEqual(objects.OS.GetVariant("debootstrap+default"), "default")
if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment