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

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

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        file_path = '/etc/fwradius.secret'
        try:
            if MACHINE_COLLECTION.server.check_if_file_exists(file_path):
                log(f'{file_path} has been found. Reading contents...', Severity.TRACE)
                credentials.fwradius = MACHINE_COLLECTION.server.read_file(file_path).splitlines()[0].decode('utf-8')
            else:
                log(f'{file_path} could not be found. Leaving credentials as is.', Severity.TRACE)
            return True
        except:
            log(f"Failed to read fwradius secret!", Severity.ERROR)
            return False