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


class SetOpsiDomadminPassword(SetupStep):
    def __init__(self, password):
        super().__init__(),
        self.password = password

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        log('Setting domadmin password on opsi server...')
        stdout, stderr, retval = MACHINE_COLLECTION.backup.execute_shell_command(
            'opsi-admin', '-d', 'method', 'productPropertyState_create', 'windomain', 'password', MACHINE_COLLECTION.backup.get_hostname(), self.password)

        if retval != 0:
            log(f"Failed to set domadmin password on opsi server: {stderr}", Severity.ERROR)
            return False

        log('Domadmin password has been set.', Severity.DEBUG)
        return True