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

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 <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
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:
try:
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,
set(checks).difference(constants.OS_VALIDATE_CALLS))
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:
return
try:
variant = name.split("+", 1)[1]
except IndexError:
variant = objects.OS.GetVariant(name)
if not variant:
raise errors.OpPrereqError("OS name must include a variant",
errors.ECODE_INVAL)
......
#
#
# 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__ = [
"name",
......@@ -875,6 +878,41 @@ class OS(ConfigObject):
"supported_parameters",
]
VARIANT_DELIM = "+"
@classmethod
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:
nv.append("")
return nv
@classmethod
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]
@classmethod
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."""
......
#!/usr/bin/python
#
# 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):
self.fake_cl.FillHV(fake_inst))
class TestOS(unittest.TestCase):
ALL_DATA = [
"debootstrap",
"debootstrap+default",
"debootstrap++default",
]
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__':
testutils.GanetiTestProgram()
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