From 4aa45d2900a3063305a7b2c727abfc2371909b21 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 13 Dec 2012 20:07:36 +0000 Subject: [PATCH] Allow loading of product/vendor/package info from external file Currently the version.py file hardcodes some basic info for the NOVA_PRODUCT, NOVA_VENDOR and NOVA_PACKAGE constants. It is desirable that OS distro vendors can override/extend this data in packages they ship. Cloud providers may also desire to do the same. This allows Nova to optionally load this data from a /etc/nova/release file. That file should contain the data in a trivial "ini" format. For example: [Nova] vendor = Fedora Project package = 1.fc18 Note that vendor sare not required to override all strings. In this example, the 'product' field is not set, leaving it on the default 'OpenStack Nova' value. Change-Id: I3d8d32eaf35268c4b0908b0a93b7c9a2ea9c580a Signed-off-by: Daniel P. Berrange --- etc/nova/release.sample | 4 ++++ nova/tests/test_versions.py | 31 ++++++++++++++++++++++++ nova/version.py | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 etc/nova/release.sample diff --git a/etc/nova/release.sample b/etc/nova/release.sample new file mode 100644 index 000000000000..4c0d8e48e9b4 --- /dev/null +++ b/etc/nova/release.sample @@ -0,0 +1,4 @@ +[Nova] +vendor = Fedora Project +product = OpenStack Nova +package = 1.fc18 diff --git a/nova/tests/test_versions.py b/nova/tests/test_versions.py index 68e77ff9e888..e864c00dd832 100644 --- a/nova/tests/test_versions.py +++ b/nova/tests/test_versions.py @@ -14,7 +14,11 @@ # License for the specific language governing permissions and limitations # under the License. +import __builtin__ +import os +import StringIO +from nova.openstack.common import cfg from nova import test from nova import version @@ -53,3 +57,30 @@ class VersionTestCase(test.TestCase): """Ensure uninstalled code get version string""" self.assertEqual("2012.10-g9ec3421", self.version.version_string_with_package()) + + def test_release_file(self): + version.loaded = False + real_open = __builtin__.open + real_find_file = cfg.CONF.find_file + + def fake_find_file(self, name): + if name == "release": + return "/etc/nova/release" + return real_find_file(self, name) + + def fake_open(path, *args, **kwargs): + if path == "/etc/nova/release": + data = """[Nova] +vendor = ACME Corporation +product = ACME Nova +package = 1337""" + return StringIO.StringIO(data) + + return real_open(path, *args, **kwargs) + + self.stubs.Set(__builtin__, 'open', fake_open) + self.stubs.Set(cfg.ConfigOpts, 'find_file', fake_find_file) + + self.assertEqual(version.vendor_string(), "ACME Corporation") + self.assertEqual(version.product_string(), "ACME Nova") + self.assertEqual(version.package_string(), "1337") diff --git a/nova/version.py b/nova/version.py index 16b0d876a10c..82f3bb970fab 100644 --- a/nova/version.py +++ b/nova/version.py @@ -21,16 +21,63 @@ NOVA_VERSION = ['2013', '1', None] YEAR, COUNT, REVISION = NOVA_VERSION FINAL = False # This becomes true at Release Candidate time +loaded = False + + +def _load_config(): + # Don't load in global context, since we can't assume + # these modules are accessible when distutils uses + # this module + import ConfigParser + + from nova.openstack.common import cfg + from nova.openstack.common import log as logging + + global loaded, NOVA_VENDOR, NOVA_PRODUCT, NOVA_PACKAGE + if loaded: + return + + loaded = True + + cfgfile = cfg.CONF.find_file("release") + if cfgfile is None: + return + + try: + cfg = ConfigParser.RawConfigParser() + cfg.read(cfgfile) + + NOVA_VENDOR = cfg.get("Nova", "vendor") + if cfg.has_option("Nova", "vendor"): + NOVA_VENDOR = cfg.get("Nova", "vendor") + + NOVA_PRODUCT = cfg.get("Nova", "product") + if cfg.has_option("Nova", "product"): + NOVA_PRODUCT = cfg.get("Nova", "product") + + NOVA_PACKAGE = cfg.get("Nova", "package") + if cfg.has_option("Nova", "package"): + NOVA_PACKAGE = cfg.get("Nova", "package") + except Exception, ex: + LOG = logging.getLogger(__name__) + LOG.error("Failed to load %(cfgfile)s: %(ex)s" % locals()) + def vendor_string(): + _load_config() + return NOVA_VENDOR def product_string(): + _load_config() + return NOVA_PRODUCT def package_string(): + _load_config() + return NOVA_PACKAGE