]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-detect-init: detect init system by poking the system not checking its codename
authorKefu Chai <kchai@redhat.com>
Thu, 11 May 2017 06:18:44 +0000 (14:18 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 12 May 2017 06:11:09 +0000 (14:11 +0800)
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 <kchai@redhat.com>
src/ceph-detect-init/ceph_detect_init/__init__.py
src/ceph-detect-init/ceph_detect_init/debian/__init__.py
src/ceph-detect-init/tests/test_all.py

index 78374bef9fd6fce9ca3ca45e29fab1cd3c9a5721..fe0a2a787f4f9bc4746c640cc3e04359f1efc9d6 100644 (file)
@@ -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
index 73a7851a3fc9ab21d3c8ad73f0c94aa630358250..94217cdfa263b554c8446cfbacebc7dd4654654f 100644 (file)
@@ -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'
index 263cd9a289b13b9b41bbf85821672970e2ecd23e..18451bf46364380ea45a861ab1f02448af189ed5 100644 (file)
@@ -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', ''))):