From 83067bbceaf9db19037191bc1b51389d4678c9f1 Mon Sep 17 00:00:00 2001 From: Ceph Maintainers Date: Sun, 30 Jul 2017 10:48:17 +0100 Subject: [PATCH] fix-init-system-detection Gbp-Pq: Name fix-init-system-detection.patch --- .../ceph_detect_init/__init__.py | 21 +------- .../ceph_detect_init/debian/__init__.py | 22 ++++---- src/ceph-detect-init/tests/test_all.py | 52 ++++++++++--------- 3 files changed, 40 insertions(+), 55 deletions(-) diff --git a/src/ceph-detect-init/ceph_detect_init/__init__.py b/src/ceph-detect-init/ceph_detect_init/__init__.py index a2bcc7cf2..7797e4ed4 100644 --- a/src/ceph-detect-init/ceph_detect_init/__init__.py +++ b/src/ceph-detect-init/ceph_detect_init/__init__.py @@ -25,7 +25,7 @@ import platform def get(use_rhceph=False): distro_name, release, codename = platform_information() - if not codename or not _get_distro(distro_name): + if not _get_distro(distro_name): raise exc.UnsupportedPlatform( distro=distro_name, codename=codename, @@ -83,25 +83,6 @@ def platform_information(): logging.debug('platform_information: linux_distribution = ' + str(platform.linux_distribution())) distro, release, codename = platform.linux_distribution() - # this could be an empty string in Debian - if not codename and 'debian' in distro.lower(): - debian_codenames = { - '8': 'jessie', - '7': 'wheezy', - '6': 'squeeze', - } - major_version = release.split('.')[0] - codename = debian_codenames.get(major_version, '') - - # In order to support newer jessie/sid or wheezy/sid strings - # we test this if sid is buried in the minor, we should use - # sid anyway. - if not codename and '/' in release: - major, minor = release.split('/') - if minor == 'sid': - codename = minor - else: - codename = major return ( str(distro).rstrip(), diff --git a/src/ceph-detect-init/ceph_detect_init/debian/__init__.py b/src/ceph-detect-init/ceph_detect_init/debian/__init__.py index 73a7851a3..00a4841dd 100644 --- a/src/ceph-detect-init/ceph_detect_init/debian/__init__.py +++ b/src/ceph-detect-init/ceph_detect_init/debian/__init__.py @@ -1,3 +1,6 @@ +import os +import subprocess + distro = None release = None codename = None @@ -8,14 +11,11 @@ def choose_init(): Returns the name of a init system (upstart, sysvinit ...). """ - assert(distro and codename) - if distro.lower() in ('ubuntu', 'linuxmint'): - if codename >= 'vivid': - return 'systemd' - else: - return 'upstart' - if distro.lower() == 'debian': - if codename in ('squeeze', 'wheezy'): - return 'sysvinit' - else: - return 'systemd' + # yes, this is heuristics + if os.path.isdir('/run/systemd/system'): + return 'systemd' + if not subprocess.call('. /lib/lsb/init-functions ; init_is_upstart', + shell=True): + return 'upstart' + elif os.path.isfile('/sbin/init') and not os.path.islink('/sbin/init'): + return 'sysvinit' diff --git a/src/ceph-detect-init/tests/test_all.py b/src/ceph-detect-init/tests/test_all.py index 22cff5bca..c84bf3b2b 100644 --- a/src/ceph-detect-init/tests/test_all.py +++ b/src/ceph-detect-init/tests/test_all.py @@ -44,30 +44,35 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual('sysvinit', centos.choose_init()) def test_debian(self): - with mock.patch.multiple('ceph_detect_init.debian', - distro='debian', - codename='wheezy'): - self.assertEqual('sysvinit', debian.choose_init()) - with mock.patch.multiple('ceph_detect_init.debian', - distro='debian', - codename='squeeze'): - self.assertEqual('sysvinit', debian.choose_init()) - with mock.patch.multiple('ceph_detect_init.debian', - distro='debian', - codename='jessie'): + with mock.patch.multiple('os.path', + isdir=lambda x: x == '/run/systemd/system'): self.assertEqual('systemd', debian.choose_init()) - with mock.patch.multiple('ceph_detect_init.debian', - distro='ubuntu', - codename='trusty'): - self.assertEqual('upstart', debian.choose_init()) - with mock.patch.multiple('ceph_detect_init.debian', - distro='ubuntu', - codename='vivid'): - self.assertEqual('systemd', debian.choose_init()) - with mock.patch.multiple('ceph_detect_init.debian', - distro='not-debian', - codename='andy'): - self.assertIs(None, debian.choose_init()) + + def mock_call_with_upstart(*args, **kwargs): + if args[0] == '. /lib/lsb/init-functions ; init_is_upstart' and \ + kwargs['shell']: + return 0 + else: + return 1 + with mock.patch.multiple('os.path', + isdir=lambda x: False, + isfile=lambda x: False): + with mock.patch.multiple('subprocess', + call=mock_call_with_upstart): + self.assertEqual('upstart', debian.choose_init()) + with mock.patch.multiple('os.path', + isdir=lambda x: False, + isfile=lambda x: x == '/sbin/init', + islink=lambda x: x != '/sbin/init'): + with mock.patch.multiple('subprocess', + call=lambda *args, **kwargs: 1): + self.assertEqual('sysvinit', debian.choose_init()) + with mock.patch.multiple('os.path', + isdir=lambda x: False, + isfile=lambda x: False): + with mock.patch.multiple('subprocess', + call=lambda *args, **kwargs: 1): + self.assertIs(None, debian.choose_init()) def test_fedora(self): with mock.patch('ceph_detect_init.fedora.release', @@ -115,7 +120,6 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual(False, distro.is_el) self.assertEqual('6.0', distro.release) self.assertEqual('squeeze', distro.codename) - self.assertEqual('sysvinit', distro.init) def test_get_distro(self): g = ceph_detect_init._get_distro -- 2.30.2