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

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

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        machine = MACHINE_COLLECTION.backup
        log('Preparing Opsi license directory...', Severity.INFO)
        log('Creating licenses directory...', Severity.TRACE)
        machine.mkdir('/etc/opsi/licenses')
        log('Setting permissions...', Severity.TRACE)
        stdout, stderr, retval = machine.execute_shell_command('opsi-set-rights')
        if retval != 0:
            log('Failed to set permissions.', Severity.ERROR)
            return False

        log('Deleting old opsilic...', Severity.TRACE)
        machine.delete('/etc/opsi/licenses/opsilic.tar.gz')
        return True