from .RebootServers import RebootServers
from .SetupLmzRepo import SetupLmzRepo
from .UpdatePfSenseConfiguration import UpdatePfSenseConfiguration
from ..General.CheckForSshSession import CheckForSSHSession
from ..General.SetPamPassword import SetPamPassword
from ..General.TestConnection import TestConnection
from ..General.TriggerUcsPasswordChange import TriggerUcsPasswordChange
from ..General.WriteMli import WriteMli
from ..General.WriteRootSecret import WriteRootSecret
from ..OpsiServer.ApplyCertificatesToOpsi import ApplyCertificatesToOpsi
from ..OpsiServer.CopyCertificates import CopyCertificates
from ..OpsiServer.CopyVeyonKeys import CopyVeyonKeys
from ..OpsiServer.OpsiChangeMysqlSecret import OpsiChangeMysqlSecret
from ..OpsiServer.SetOpsiDomadminPassword import SetOpsiDomadminPassword
from ..OpsiServer.SetOpsiProductDefaultValues import SetOpsiProductDefaultValues
from ..Server.PopulateDomadminSecret import PopulateDomadminSecret
from ..Server.PopulateRootSecret import PopulateRootSecret
from ..General.SetSystemUuid import SetSystemUuid
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ...Logging.Logger import Severity, log
from ...SetupSteps.Playbooks.Playbook import Playbook


class ReinitializeBackup(Playbook):
    def start(self):
        self.run_steps(
            CheckForSSHSession(),
            TestConnection(MACHINE_COLLECTION.backup, 22),
            PopulateRootSecret()
        )

        if not MACHINE_COLLECTION.initialized:
            if self.credentials.root is not None:
                log("Found existing credentials.", Severity.DEBUG)
                LMZ_COMMON_CREDENTIALS.set_password(self.credentials.root)
            log("Initializing machine connections...", Severity.INFO)
            MACHINE_COLLECTION.initialize()

        self.run_steps(
            PopulateDomadminSecret(),
            SetPamPassword(MACHINE_COLLECTION.backup, "root", self.credentials.root, "sha512crypt"),
            WriteRootSecret(MACHINE_COLLECTION.backup),
            TriggerUcsPasswordChange(MACHINE_COLLECTION.backup),
            SetSystemUuid(MACHINE_COLLECTION.backup)
        )

        if self.credentials.mli is None or self.credentials.mli_password is None:
            self.credentials.mli = self.ui.get_input("Please enter your MLI:")
            self.credentials.mli_password = self.ui.get_password("Please enter your MLI password:")

        self.run_steps(
            WriteMli(MACHINE_COLLECTION.backup)
        )

        self.run_sub_playbook(SetupLmzRepo)

        self.run_steps(
            SetOpsiDomadminPassword(self.credentials.domadmin),
            CopyVeyonKeys(),
            SetOpsiProductDefaultValues(),
            OpsiChangeMysqlSecret(),
            CopyCertificates(),
            ApplyCertificatesToOpsi()
        )

        log('!!SUCCESS!! Backup reinitialization completed successfully.')
        if not self.credentials.no_reboot:
            self.ui.confirm('A reboot of the opsi server is required. Confirm to reboot now.')
            MACHINE_COLLECTION.backup.execute_shell_command('reboot')
