Skip to content

Commit

Permalink
Merge pull request ceph#8317 from dreamhost/gentoo-python
Browse files Browse the repository at this point in the history
Gentoo support for ceph-disk / ceph-detect-init; pip speedup

Reviewed-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov authored Jul 11, 2016
2 parents 72c42b4 + e442f56 commit fc30482
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ceph-detect-init/ceph_detect_init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
from ceph_detect_init import fedora
from ceph_detect_init import rhel
from ceph_detect_init import suse
from ceph_detect_init import gentoo
import logging
import platform


def get(use_rhceph=False):
distro_name, release, codename = platform_information()
if not codename or not _get_distro(distro_name):
# Not all distributions have a concept that maps to codenames
# (or even releases really)
if not codename and not _get_distro(distro_name):
raise exc.UnsupportedPlatform(
distro=distro_name,
codename=codename,
Expand Down Expand Up @@ -57,6 +60,9 @@ def _get_distro(distro, use_rhceph=False):
'redhat': centos,
'fedora': fedora,
'suse': suse,
'gentoo': gentoo,
'funtoo': gentoo,
'exherbo': gentoo,
}

if distro == 'redhat' and use_rhceph:
Expand All @@ -75,6 +81,8 @@ def _normalized_distro_name(distro):
return 'suse'
elif distro.startswith('centos'):
return 'centos'
elif distro.startswith(('gentoo', 'funtoo', 'exherbo')):
return 'gentoo'
return distro


Expand Down
37 changes: 37 additions & 0 deletions src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
distro = None
release = None
codename = None


# From ceph-disk, but there is no way to access it since it's not in a module
def is_systemd():
"""
Detect whether systemd is running;
WARNING: not mutually exclusive with openrc
"""
with open('/proc/1/comm', 'rb') as i:
for line in i:
if 'systemd' in line:
return True
return False


def is_openrc():
"""
Detect whether openrc is running.
"""
OPENRC_CGROUP = '/sys/fs/cgroup/openrc'
return os.path.isdir(OPENRC_CGROUP)


def choose_init():
"""Select a init system
Returns the name of a init system (upstart, sysvinit ...).
"""
if is_openrc():
return 'openrc'
if is_systemd():
return 'systemd'
return 'unknown'
46 changes: 46 additions & 0 deletions src/ceph-detect-init/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ceph_detect_init import main
from ceph_detect_init import rhel
from ceph_detect_init import suse
from ceph_detect_init import gentoo

logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG)
Expand Down Expand Up @@ -95,6 +96,44 @@ def test_suse(self):
'13.2'):
self.assertEqual('systemd', suse.choose_init())

def test_gentoo_is_openrc(self):
with mock.patch('os.path.isdir', return_value=True):
self.assertEqual(gentoo.is_openrc(), True)
with mock.patch('os.path.isdir', return_value=False):
self.assertEqual(gentoo.is_openrc(), False)

def test_gentoo_is_systemd(self):
f = mock.mock_open(read_data='systemd')
with mock.patch('__main__.file', f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), True)
m.assert_called_once_with('/proc/1/comm')
f = mock.mock_open(read_data='init')
with mock.patch('__main__.file', f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), False)
m.assert_called_once_with('/proc/1/comm')
f = mock.mock_open(read_data='upstart')
with mock.patch('__main__.file', f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), False)
m.assert_called_once_with('/proc/1/comm')

def test_gentoo(self):
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: True),
is_openrc=(lambda: True)):
self.assertEqual('openrc', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: True),
is_openrc=(lambda: False)):
self.assertEqual('systemd', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: False),
is_openrc=(lambda: True)):
self.assertEqual('openrc', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: False),
is_openrc=(lambda: False)):
self.assertEqual('unknown', gentoo.choose_init())

def test_get(self):
g = ceph_detect_init.get
with mock.patch('platform.linux_distribution',
Expand Down Expand Up @@ -127,6 +166,7 @@ def test_get_distro(self):
self.assertEqual(fedora, g('fedora'))
self.assertEqual(suse, g('suse'))
self.assertEqual(rhel, g('redhat', use_rhceph=True))
self.assertEqual(gentoo, g('gentoo'))

def test_normalized_distro_name(self):
n = ceph_detect_init._normalized_distro_name
Expand All @@ -148,6 +188,12 @@ def test_normalized_distro_name(self):
self.assertEqual('debian', n('debian'))
self.assertEqual('ubuntu', n('Ubuntu'))
self.assertEqual('ubuntu', n('ubuntu'))
self.assertEqual('gentoo', n('Gentoo'))
self.assertEqual('gentoo', n('gentoo'))
self.assertEqual('gentoo', n('Funtoo'))
self.assertEqual('gentoo', n('funtoo'))
self.assertEqual('gentoo', n('Exherbo'))
self.assertEqual('gentoo', n('exherbo'))

def test_platform_information(self):
with mock.patch('platform.linux_distribution',
Expand Down
22 changes: 22 additions & 0 deletions src/ceph-disk/ceph_disk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def is_dmcrypt(ptype, name):
'upstart',
'sysvinit',
'systemd',
'openrc',
'auto',
'none',
]
Expand Down Expand Up @@ -2875,6 +2876,20 @@ def start_daemon(
'ceph-osd@{osd_id}'.format(osd_id=osd_id),
],
)
elif os.path.exists(os.path.join(path, 'openrc')):
base_script = '/etc/init.d/ceph-osd'
osd_script = '{base}.{osd_id}'.format(
base=base_script,
osd_id=osd_id
)
if not os.path.exists(osd_script):
os.symlink(base_script, osd_script)
command_check_call(
[
osd_script,
'start',
],
)
else:
raise Error('{cluster} osd.{osd_id} is not tagged '
'with an init system'.format(
Expand Down Expand Up @@ -2932,6 +2947,13 @@ def stop_daemon(
'ceph-osd@{osd_id}'.format(osd_id=osd_id),
],
)
elif os.path.exists(os.path.join(path, 'openrc')):
command_check_call(
[
'/etc/init.d/ceph-osd.{osd_id}'.format(osd_id=osd_id),
'stop',
],
)
else:
raise Error('{cluster} osd.{osd_id} is not tagged with an init '
' system'.format(cluster=cluster, osd_id=osd_id))
Expand Down

0 comments on commit fc30482

Please sign in to comment.