systemd detection result caching nit fixes

Change-Id: If54dd5c33a2e6535cc2ae9cf96179a202f6ed1a3
Related-bug: #1756823
This commit is contained in:
Artem Vasilyev 2019-04-02 09:13:00 +03:00 committed by Matthew Booth
parent 357da989c1
commit 48f1826711
2 changed files with 28 additions and 29 deletions

View File

@ -33,8 +33,7 @@ from nova.virt.libvirt.volume import quobyte
@ddt.ddt @ddt.ddt
@mock.patch.object(quobyte, 'found_sysd', False) @mock.patch.object(quobyte, '_is_systemd', None)
@mock.patch.object(quobyte, 'sysd_checked', False)
class QuobyteTestCase(test.NoDBTestCase): class QuobyteTestCase(test.NoDBTestCase):
"""Tests the nova.virt.libvirt.volume.quobyte module utilities.""" """Tests the nova.virt.libvirt.volume.quobyte module utilities."""
@ -74,8 +73,7 @@ class QuobyteTestCase(test.NoDBTestCase):
"is-system-running", "is-system-running",
check_exit_code=[0, 1]) check_exit_code=[0, 1])
mock_proc.assert_called_once() mock_proc.assert_called_once()
self.assertTrue(quobyte.sysd_checked) self.assertTrue(quobyte.is_systemd())
self.assertTrue(quobyte.found_sysd)
@mock.patch.object(psutil.Process, "name", return_value="NOT_systemd") @mock.patch.object(psutil.Process, "name", return_value="NOT_systemd")
@mock.patch.object(os.path, "exists", return_value=False) @mock.patch.object(os.path, "exists", return_value=False)
@ -83,8 +81,7 @@ class QuobyteTestCase(test.NoDBTestCase):
self.assertFalse(quobyte.is_systemd()) self.assertFalse(quobyte.is_systemd())
mock_exists.assert_called_once_with(quobyte.SYSTEMCTL_CHECK_PATH) mock_exists.assert_called_once_with(quobyte.SYSTEMCTL_CHECK_PATH)
mock_proc.assert_called_once_with() mock_proc.assert_called_once_with()
self.assertTrue(quobyte.sysd_checked) self.assertFalse(quobyte.is_systemd())
self.assertFalse(quobyte.found_sysd)
@mock.patch.object(psutil.Process, "name", return_value="NOT_systemd") @mock.patch.object(psutil.Process, "name", return_value="NOT_systemd")
@mock.patch.object(processutils, "execute") @mock.patch.object(processutils, "execute")
@ -100,8 +97,7 @@ class QuobyteTestCase(test.NoDBTestCase):
"is-system-running", "is-system-running",
check_exit_code=[0, 1]) check_exit_code=[0, 1])
mock_proc.assert_called_once_with() mock_proc.assert_called_once_with()
self.assertTrue(quobyte.sysd_checked) self.assertFalse(quobyte.is_systemd())
self.assertFalse(quobyte.found_sysd)
@ddt.data(None, '/some/arbitrary/path') @ddt.data(None, '/some/arbitrary/path')
@mock.patch.object(fileutils, "ensure_tree") @mock.patch.object(fileutils, "ensure_tree")
@ -113,6 +109,7 @@ class QuobyteTestCase(test.NoDBTestCase):
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte._is_systemd = False
quobyte.mount_volume(quobyte_volume, export_mnt_base, cfg_file) quobyte.mount_volume(quobyte_volume, export_mnt_base, cfg_file)
mock_ensure_tree.assert_called_once_with(export_mnt_base) mock_ensure_tree.assert_called_once_with(export_mnt_base)
@ -129,7 +126,7 @@ class QuobyteTestCase(test.NoDBTestCase):
quobyte_volume = '192.168.1.1/volume-00001' quobyte_volume = '192.168.1.1/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte.found_sysd = True quobyte._is_systemd = True
quobyte.mount_volume(quobyte_volume, export_mnt_base, cfg_file) quobyte.mount_volume(quobyte_volume, export_mnt_base, cfg_file)
@ -146,7 +143,7 @@ class QuobyteTestCase(test.NoDBTestCase):
quobyte_volume = '192.168.1.1/volume-00001' quobyte_volume = '192.168.1.1/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte._is_systemd = False
self.assertRaises(processutils.ProcessExecutionError, self.assertRaises(processutils.ProcessExecutionError,
quobyte.mount_volume, quobyte.mount_volume,
quobyte_volume, quobyte_volume,
@ -160,6 +157,7 @@ class QuobyteTestCase(test.NoDBTestCase):
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte._is_systemd = False
quobyte.umount_volume(export_mnt_base) quobyte.umount_volume(export_mnt_base)
mock_lv_umount.assert_called_once_with(export_mnt_base) mock_lv_umount.assert_called_once_with(export_mnt_base)
@ -170,7 +168,7 @@ class QuobyteTestCase(test.NoDBTestCase):
quobyte_volume = '192.168.1.1/volume-00001' quobyte_volume = '192.168.1.1/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte.found_sysd = True quobyte._is_systemd = True
quobyte.umount_volume(export_mnt_base) quobyte.umount_volume(export_mnt_base)
@ -183,7 +181,7 @@ class QuobyteTestCase(test.NoDBTestCase):
quobyte_volume = '192.168.1.1/volume-00001' quobyte_volume = '192.168.1.1/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte.found_sysd = True quobyte._is_systemd = True
def exec_side_effect(*cmd, **kwargs): def exec_side_effect(*cmd, **kwargs):
exerror = processutils.ProcessExecutionError( exerror = processutils.ProcessExecutionError(
@ -205,7 +203,7 @@ class QuobyteTestCase(test.NoDBTestCase):
quobyte_volume = '192.168.1.1/volume-00001' quobyte_volume = '192.168.1.1/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
utils.get_hash_str(quobyte_volume)) utils.get_hash_str(quobyte_volume))
quobyte.found_sysd = True quobyte._is_systemd = True
quobyte.umount_volume(export_mnt_base) quobyte.umount_volume(export_mnt_base)

View File

@ -41,30 +41,34 @@ DRIVER_IO = 'native'
VALID_SYSD_STATES = ["starting", "running", "degraded"] VALID_SYSD_STATES = ["starting", "running", "degraded"]
SYSTEMCTL_CHECK_PATH = "/run/systemd/system" SYSTEMCTL_CHECK_PATH = "/run/systemd/system"
sysd_checked = False
found_sysd = False _is_systemd = None
def is_systemd(): def is_systemd():
"""Checks if the host is running systemd""" """Checks if the host is running systemd"""
global sysd_checked global _is_systemd
global found_sysd
if _is_systemd is not None:
return _is_systemd
tmp_is_systemd = False
if psutil.Process(1).name() == "systemd" or os.path.exists( if psutil.Process(1).name() == "systemd" or os.path.exists(
SYSTEMCTL_CHECK_PATH): SYSTEMCTL_CHECK_PATH):
# NOTE(kaisers): exit code might be >1 in theory but in practice this # NOTE(kaisers): exit code might be >1 in theory but in practice this
# is hard coded to 1. Due to backwards compatibility and systemd # is hard coded to 1. Due to backwards compatibility and systemd
# CODING_STYLE this is unlikely to change. # CODING_STYLE this is unlikely to change.
sysdout, sysderr = processutils.execute("systemctl", sysdout, sysderr = processutils.execute("systemctl",
"is-system-running", "is-system-running",
check_exit_code=[0, 1]) check_exit_code=[0, 1])
for state in VALID_SYSD_STATES: for state in VALID_SYSD_STATES:
if state == sysdout.strip(): if state == sysdout.strip():
found_sysd = True tmp_is_systemd = True
sysd_checked = True break
return found_sysd
sysd_checked = True _is_systemd = tmp_is_systemd
return False return _is_systemd
def mount_volume(volume, mnt_base, configfile=None): def mount_volume(volume, mnt_base, configfile=None):
@ -73,7 +77,7 @@ def mount_volume(volume, mnt_base, configfile=None):
# Note(kaisers): with systemd this requires a separate CGROUP to # Note(kaisers): with systemd this requires a separate CGROUP to
# prevent Nova service stop/restarts from killing the mount. # prevent Nova service stop/restarts from killing the mount.
if found_sysd: if is_systemd():
LOG.debug('Mounting volume %s at mount point %s via systemd-run', LOG.debug('Mounting volume %s at mount point %s via systemd-run',
volume, mnt_base) volume, mnt_base)
nova.privsep.libvirt.systemd_run_qb_mount(volume, mnt_base, nova.privsep.libvirt.systemd_run_qb_mount(volume, mnt_base,
@ -90,7 +94,7 @@ def mount_volume(volume, mnt_base, configfile=None):
def umount_volume(mnt_base): def umount_volume(mnt_base):
"""Wraps execute calls for unmouting a Quobyte volume""" """Wraps execute calls for unmouting a Quobyte volume"""
try: try:
if found_sysd: if is_systemd():
nova.privsep.libvirt.umount(mnt_base) nova.privsep.libvirt.umount(mnt_base)
else: else:
nova.privsep.libvirt.unprivileged_umount(mnt_base) nova.privsep.libvirt.unprivileged_umount(mnt_base)
@ -133,9 +137,6 @@ def validate_volume(mount_path):
class LibvirtQuobyteVolumeDriver(fs.LibvirtBaseFileSystemVolumeDriver): class LibvirtQuobyteVolumeDriver(fs.LibvirtBaseFileSystemVolumeDriver):
"""Class implements libvirt part of volume driver for Quobyte.""" """Class implements libvirt part of volume driver for Quobyte."""
def __init__(self, host):
super(LibvirtQuobyteVolumeDriver, self).__init__(host)
def _get_mount_point_base(self): def _get_mount_point_base(self):
return CONF.libvirt.quobyte_mount_point_base return CONF.libvirt.quobyte_mount_point_base