Refactor tasks to include is_selinux_enabled()

Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
This commit is contained in:
Christian Heimes
2019-04-25 13:24:48 +02:00
parent 23d5c05232
commit dcd488b3d9
2 changed files with 31 additions and 24 deletions

View File

@@ -91,16 +91,24 @@ class BaseTaskNamespace:
return paths.SVC_LIST_FILE return paths.SVC_LIST_FILE
def check_selinux_status(self): def is_selinux_enabled(self):
"""Check if SELinux is available and enabled
:return: True if SELinux is available and enabled
""" """
Checks if SELinux is available on the platform. If it is, this task return False
also makes sure that restorecon tool is available.
def check_selinux_status(self):
"""Checks if SELinux is available on the platform.
If it is, this task also makes sure that restorecon tool is available.
If SELinux is available, but restorcon tool is not installed, raises If SELinux is available, but restorcon tool is not installed, raises
an RuntimeError, which suggest installing the package containing an RuntimeError, which suggest installing the package containing
restorecon and rerunning the installation. restorecon and rerunning the installation.
"""
:return: True if SELinux is available and enabled
"""
raise NotImplementedError() raise NotImplementedError()
def check_ipv6_stack_enabled(self): def check_ipv6_stack_enabled(self):

View File

@@ -75,22 +75,6 @@ NM_IPA_CONF = textwrap.dedent("""
""") """)
def selinux_enabled():
"""
Check if SELinux is enabled.
"""
if os.path.exists(paths.SELINUXENABLED):
try:
ipautil.run([paths.SELINUXENABLED])
return True
except ipautil.CalledProcessError:
# selinuxenabled returns 1 if not enabled
return False
else:
# No selinuxenabled, no SELinux
return False
@total_ordering @total_ordering
class IPAVersion: class IPAVersion:
_rpmvercmp_func = None _rpmvercmp_func = None
@@ -143,7 +127,7 @@ class RedHatTaskNamespace(BaseTaskNamespace):
ipautil.run() will do the logging. ipautil.run() will do the logging.
""" """
restorecon = paths.SBIN_RESTORECON restorecon = paths.SBIN_RESTORECON
if not selinux_enabled() or not os.path.exists(restorecon): if not self.is_selinux_enabled() or not os.path.exists(restorecon):
return return
# Force reset of context to match file_context for customizable # Force reset of context to match file_context for customizable
@@ -155,6 +139,20 @@ class RedHatTaskNamespace(BaseTaskNamespace):
args.append(filepath) args.append(filepath)
ipautil.run(args, raiseonerr=False) ipautil.run(args, raiseonerr=False)
def is_selinux_enabled(self):
"""Check if SELinux is available and enabled
"""
try:
ipautil.run([paths.SELINUXENABLED])
except ipautil.CalledProcessError:
# selinuxenabled returns 1 if not enabled
return False
except OSError:
# selinuxenabled binary not available
return False
else:
return True
def check_selinux_status(self, restorecon=paths.RESTORECON): def check_selinux_status(self, restorecon=paths.RESTORECON):
""" """
We don't have a specific package requirement for policycoreutils We don't have a specific package requirement for policycoreutils
@@ -165,13 +163,14 @@ class RedHatTaskNamespace(BaseTaskNamespace):
This function returns nothing but may raise a Runtime exception This function returns nothing but may raise a Runtime exception
if SELinux is enabled but restorecon is not available. if SELinux is enabled but restorecon is not available.
""" """
if not selinux_enabled(): if not self.is_selinux_enabled():
return return False
if not os.path.exists(restorecon): if not os.path.exists(restorecon):
raise RuntimeError('SELinux is enabled but %s does not exist.\n' raise RuntimeError('SELinux is enabled but %s does not exist.\n'
'Install the policycoreutils package and start ' 'Install the policycoreutils package and start '
'the installation again.' % restorecon) 'the installation again.' % restorecon)
return True
def check_ipv6_stack_enabled(self): def check_ipv6_stack_enabled(self):
"""Checks whether IPv6 kernel module is loaded. """Checks whether IPv6 kernel module is loaded.
@@ -458,7 +457,7 @@ class RedHatTaskNamespace(BaseTaskNamespace):
return args return args
if not selinux_enabled(): if not self.is_selinux_enabled():
return False return False
updated_vars = {} updated_vars = {}