From 12ef43b8d030af0ed16c0886c5dc4f296c3b1521 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Wed, 17 Sep 2014 15:26:39 -0600 Subject: [PATCH] Re-implement Distribution as OS It now uses /etc/os-release instead of lsb_release, like the cool kids are doing. Signed-off-by: Zack Cerza --- teuthology/orchestra/remote.py | 80 +++++++++-------- teuthology/orchestra/test/test_integration.py | 12 ++- teuthology/orchestra/test/test_remote.py | 87 +++++++++++++------ 3 files changed, 110 insertions(+), 69 deletions(-) diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index c65e2fd187..12cead96ba 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -248,12 +248,12 @@ class Remote(object): self.remove(remote_temp_path) @property - def distro(self): - if not hasattr(self, '_distro'): - lsb_info = self.run(args=['lsb_release', '-a'], stdout=StringIO(), - stderr=StringIO()) - self._distro = Distribution(lsb_info.stdout.getvalue().strip()) - return self._distro + def os(self): + if not hasattr(self, '_os'): + os_info = self.run(args=['cat', '/etc/os-release'], + stdout=StringIO(), stderr=StringIO()) + self._os = OS(os_info.stdout.getvalue().strip()) + return self._os @property def arch(self): @@ -277,59 +277,65 @@ class Remote(object): node['name'] = self.hostname node['user'] = self.user node['arch'] = self.arch - node['os_type'] = self.distro.name - node['os_version'] = self.distro.release + node['os_type'] = self.os.id + node['os_version'] = self.os.version_id node['ssh_pub_key'] = self.host_key node['up'] = True return node -class Distribution(object): +class OS(object): """ - Parse 'lsb_release -a' output and populate attributes + Parse /etc/os-release and populate attributes Given output like: - Distributor ID: Ubuntu - Description: Ubuntu 12.04.4 LTS - Release: 12.04 - Codename: precise + NAME="Ubuntu" + VERSION="12.04.4 LTS, Precise Pangolin" + ID=ubuntu + ID_LIKE=debian + PRETTY_NAME="Ubuntu precise (12.04.4 LTS)" + VERSION_ID="12.04" Attributes will be: - distributor = 'Ubuntu' - description = 'Ubuntu 12.04.4 LTS' - release = '12.04' - codename = 'precise' - Additionally, a few convenience attributes will be set: - name = 'ubuntu' + name = 'Ubuntu' + version = '12.04.4 LTS, Precise Pangolin' + id = 'ubuntu' + id_like = 'debian' + pretty_name = 'Ubuntu precise (12.04.4 LTS)' + version_id = '12.04' + Additionally, we set the package type: package_type = 'deb' """ - __slots__ = ['_lsb_release_str', '__expr', 'distributor', 'description', - 'release', 'codename', 'name', 'package_type'] + __slots__ = ['_os_release_str', 'name', 'version', 'id', 'id_like', + 'pretty_name', 'version_id', 'package_type'] - def __init__(self, lsb_release_str): - self._lsb_release_str = lsb_release_str.strip() - self.distributor = self._get_match("Distributor ID:\s*(.*)") - self.name = self.distributor.lower() - self.description = self._get_match("Description:\s*(.*)") - self.release = self._get_match("Release:\s*(.*)") - self.codename = self._get_match("Codename:\s*(.*)") + _deb_distros = ('debian') + _rpm_distros = ('fedora', 'rhel', 'rhel fedora', 'suse') - if self.distributor in ['Ubuntu', 'Debian']: + def __init__(self, os_release_str): + self._os_release_str = os_release_str.strip() + self.name = self._get_value('NAME') + self.version = self._get_value('VERSION') + self.id = self._get_value('ID') + self.id_like = self._get_value('ID_LIKE') + self.pretty_name = self._get_value('PRETTY_NAME') + self.version_id = self._get_value('VERSION_ID') + + if self.id_like in self._deb_distros: self.package_type = "deb" - elif self.distributor in ['CentOS', 'Fedora', 'RedHatEnterpriseServer', - 'openSUSE project', 'SUSE LINUX']: + elif self.id_like in self._rpm_distros: self.package_type = "rpm" - def _get_match(self, regex): - match = re.search(regex, self._lsb_release_str) + def _get_value(self, name): + regex = name + '=(.+)' + match = re.search(regex, self._os_release_str) if match: - return match.groups()[0] + return match.groups()[0].strip('"\'') return '' def __str__(self): - return " ".join([self.distributor, self.release, - self.codename]).strip() + return " ".join([self.id, self.version_id]).strip() def getShortName(name): diff --git a/teuthology/orchestra/test/test_integration.py b/teuthology/orchestra/test/test_integration.py index 2722c86aea..c4731e5c95 100644 --- a/teuthology/orchestra/test/test_integration.py +++ b/teuthology/orchestra/test/test_integration.py @@ -74,11 +74,9 @@ class TestIntegration(): ) assert r.stdout.getvalue() == 'yup\n' - def test_distro(self): + def test_os(self): rem = remote.Remote(HOST) - assert rem.distro.distributor - assert rem.distro.name - assert rem.distro.description - assert rem.distro.release - assert rem.distro.codename - assert rem.distro.package_type + assert rem.os.name + assert rem.os.id + assert rem.os.version + assert rem.os.version_id diff --git a/teuthology/orchestra/test/test_remote.py b/teuthology/orchestra/test/test_remote.py index 79e0221489..f8efde5e11 100644 --- a/teuthology/orchestra/test/test_remote.py +++ b/teuthology/orchestra/test/test_remote.py @@ -139,35 +139,72 @@ class TestRemote(object): class TestDistribution(object): - lsb_centos = dedent(""" - LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch - Distributor ID: CentOS - Description: CentOS release 6.5 (Final) - Release: 6.5 - Codename: Final + str_centos = dedent(""" + NAME="CentOS Linux" + VERSION="7 (Core)" + ID="centos" + ID_LIKE="rhel fedora" + VERSION_ID="7" + PRETTY_NAME="CentOS Linux 7 (Core)" + ANSI_COLOR="0;31" + CPE_NAME="cpe:/o:centos:centos:7" + HOME_URL="https://www.centos.org/" + BUG_REPORT_URL="https://bugs.centos.org/" """) - lsb_ubuntu = dedent(""" - Distributor ID: Ubuntu - Description: Ubuntu 12.04.4 LTS - Release: 12.04 - Codename: precise + str_ubuntu = dedent(""" + NAME="Ubuntu" + VERSION="12.04.4 LTS, Precise Pangolin" + ID=ubuntu + ID_LIKE=debian + PRETTY_NAME="Ubuntu precise (12.04.4 LTS)" + VERSION_ID="12.04" + """) + + str_rhel = dedent(""" + NAME="Red Hat Enterprise Linux Server" + VERSION="7.0 (Maipo)" + ID="rhel" + ID_LIKE="fedora" + VERSION_ID="7.0" + PRETTY_NAME="Red Hat Enterprise Linux Server 7.0 (Maipo)" + ANSI_COLOR="0;31" + CPE_NAME="cpe:/o:redhat:enterprise_linux:7.0:GA:server" + HOME_URL="https://www.redhat.com/" + BUG_REPORT_URL="https://bugzilla.redhat.com/" + + REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" + REDHAT_BUGZILLA_PRODUCT_VERSION=7.0 + REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" + REDHAT_SUPPORT_PRODUCT_VERSION=7.0 """) def test_centos(self): - d = remote.Distribution(self.lsb_centos) - assert d.distributor == 'CentOS' - assert d.description == 'CentOS release 6.5 (Final)' - assert d.release == '6.5' - assert d.codename == 'Final' - assert d.name == 'centos' - assert d.package_type == 'rpm' + os = remote.OS(self.str_centos) + assert os.name == 'CentOS Linux' + assert os.version == '7 (Core)' + assert os.id == 'centos' + assert os.id_like == 'rhel fedora' + assert os.pretty_name == 'CentOS Linux 7 (Core)' + assert os.version_id == '7' + assert os.package_type == 'rpm' def test_ubuntu(self): - d = remote.Distribution(self.lsb_ubuntu) - assert d.distributor == 'Ubuntu' - assert d.description == 'Ubuntu 12.04.4 LTS' - assert d.release == '12.04' - assert d.codename == 'precise' - assert d.name == 'ubuntu' - assert d.package_type == 'deb' + os = remote.OS(self.str_ubuntu) + assert os.name == 'Ubuntu' + assert os.version == '12.04.4 LTS, Precise Pangolin' + assert os.id == 'ubuntu' + assert os.id_like == 'debian' + assert os.pretty_name == 'Ubuntu precise (12.04.4 LTS)' + assert os.version_id == '12.04' + assert os.package_type == 'deb' + + def test_rhel(self): + os = remote.OS(self.str_rhel) + assert os.name == 'Red Hat Enterprise Linux Server' + assert os.version == '7.0 (Maipo)' + assert os.id == 'rhel' + assert os.id_like == 'fedora' + assert os.pretty_name == 'Red Hat Enterprise Linux Server 7.0 (Maipo)' + assert os.version_id == '7.0' + assert os.package_type == 'rpm' -- 2.39.5