
from __future__ import absolute_import
from __future__ import division
from . import YES, NO, MAYBE
from vdsm import utils


SEBOOL_ENABLED = "on"
SEBOOL_DISABLED = "off"

VDSM_SEBOOL_LIST = (
    "virt_use_fusefs",
    "virt_use_nfs",
    "virt_use_samba",
    "virt_use_sanlock",
    "sanlock_use_fusefs",
    "sanlock_use_nfs",
    "sanlock_use_samba",
)


def _setup_booleans(status):
    import seobject

    sebool_obj = seobject.booleanRecords()
    sebool_status = sebool_obj.get_all()

    sebool_obj.start()

    for sebool_variable in VDSM_SEBOOL_LIST:
        if status and not all(sebool_status[sebool_variable]):
            sebool_obj.modify(sebool_variable, SEBOOL_ENABLED)

        if not status and any(sebool_status[sebool_variable]):
            sebool_obj.modify(sebool_variable, SEBOOL_DISABLED)

    sebool_obj.finish()


def isconfigured():
    ret = YES
    if utils.get_selinux_enforce_mode() == -1:
        ret = MAYBE
    else:
        import seobject
        sebool_obj = seobject.booleanRecords()
        sebool_status = sebool_obj.get_all()

        for sebool_variable in VDSM_SEBOOL_LIST:
            if not all(sebool_status[sebool_variable]):
                ret = NO

    return ret


def configure():
    if utils.get_selinux_enforce_mode() > -1:
        _setup_booleans(True)


def removeConf():
    if utils.get_selinux_enforce_mode() > -1:
        _setup_booleans(False)
