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


class SetSystemUuid(SetupStep):
    def __init__(self, machine=None):
        super().__init__()
        self.machine = machine

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        import uuid
        machine = self.machine if self.machine is not None else MACHINE_COLLECTION.server
        log(f'Setting new system UUID on {machine.get_hostname_short()}...')
        system_uuid = uuid.uuid4()
        stdout, stderr, retval = machine.execute_shell_command('ucr', 'set', f'uuid/system={system_uuid}')
        if retval != 0:
            log(f'Could not set UUID: {stderr}', Severity.ERROR)
            return False
        return True