From: Kefu Chai Date: Thu, 11 May 2017 06:18:44 +0000 (+0800) Subject: ceph-detect-init: detect init system by poking the system not checking its codename X-Git-Tag: v12.1.0~10^2~96^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=90a816076d28309d4a269169b4a2ea10215825d0;p=ceph.git ceph-detect-init: detect init system by poking the system not checking its codename ubuntu's codename is not strictly lexical ordered. and after Xenial, Yakkity and Zesty, it will be Artful. so we can not just compare it with 'vivid' to tell if it is using systemd or upstart. also, the current approach for debian only checks for squeeze and wheezy. this is not flexible and per BTS#862075, it's wrong. so, we'd better check the system by poking it instead of just looking at its codename --- [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=862075 Fixes: http://tracker.ceph.com/issues/19884 Signed-off-by: Kefu Chai --- diff --git a/src/ceph-detect-init/ceph_detect_init/__init__.py b/src/ceph-detect-init/ceph_detect_init/__init__.py index 78374bef9fd6..fe0a2a787f4f 100644 --- a/src/ceph-detect-init/ceph_detect_init/__init__.py +++ b/src/ceph-detect-init/ceph_detect_init/__init__.py @@ -141,23 +141,7 @@ def platform_information(): distro_lower = distro.lower() # 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 + pass # this is an empty string in Oracle elif distro_lower.startswith('oracle linux'): codename = 'OL' + release 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 73a7851a3fc9..94217cdfa263 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' + if 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 263cd9a289b1..18451bf46364 100644 --- a/src/ceph-detect-init/tests/test_all.py +++ b/src/ceph-detect-init/tests/test_all.py @@ -65,30 +65,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', @@ -183,8 +188,6 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual('debian', distro.distro) self.assertEqual(False, distro.is_el) self.assertEqual('6.0', distro.release) - self.assertEqual('squeeze', distro.codename) - self.assertEqual('sysvinit', distro.init) with mock.patch.multiple('platform', system=lambda: 'FreeBSD', @@ -251,30 +254,10 @@ class TestCephDetectInit(testtools.TestCase): @mock.patch('platform.system', lambda: 'Linux') def test_platform_information_linux(self): - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('debian', '6.0', ''))): - self.assertEqual(('debian', '6.0', 'squeeze'), - ceph_detect_init.platform_information()) - - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('debian', '7.0', ''))): - self.assertEqual(('debian', '7.0', 'wheezy'), - ceph_detect_init.platform_information()) - with mock.patch('platform.linux_distribution', lambda **kwargs: (('debian', '8.0', ''))): - self.assertEqual(('debian', '8.0', 'jessie'), - ceph_detect_init.platform_information()) - - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('debian', 'jessie/sid', ''))): - self.assertEqual(('debian', 'jessie/sid', 'sid'), - ceph_detect_init.platform_information()) - - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('debian', 'sid/jessie', ''))): - self.assertEqual(('debian', 'sid/jessie', 'sid'), - ceph_detect_init.platform_information()) + self.assertEqual(('debian', '8.0'), + ceph_detect_init.platform_information()[:-1]) with mock.patch('platform.linux_distribution', lambda **kwargs: (('Oracle Linux Server', '7.3', ''))):