from ..Playbooks.Playbook import FileCleanupTask, PlaybookCredentialCollection
from ...Logging.Logger import log, Severity
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ..SetupStep import SetupStep

class PopulateLdapBase(SetupStep):
    def __init__(self):
        super().__init__()

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        log(f'Reading LDAP base...', Severity.DEBUG)
        stdout, stderr, retval = MACHINE_COLLECTION.server.execute_shell_command('/usr/sbin/univention-config-registry', 'get', 'ldap/base')
        if retval == 0:
            log(f'LDAP base: {stdout.strip()}', Severity.DEBUG)
            LMZ_COMMON_CREDENTIALS.set_ldap_base(stdout.strip())
            return True
        else:
            log(f'Error reading LDAP base: {stderr}', Severity.ERROR)
            return False
