from ..Firewall.ConfigurePfSense import ConfigurePfSense
from ..Firewall.RebootFirewall import RebootFirewall
from ..Firewall.SetFirewallPassword import SetFirewallPassword
from ..Server.PopulateFwradiusSecret import PopulateFwradiusSecret
from ..Server.PopulateLdapsucheSecret import PopulateLdapsucheSecret
from ..Server.PopulateRootSecret import PopulateRootSecret
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ...Logging.Logger import Severity, log
from ..General.CheckForSshSession import CheckForSSHSession
from ..General.TestCustomerAccess import TestCustomerAccess
from ..General.TestConnection import TestConnection
from ..General.SetPamPassword import SetPamPassword
from ..Playbooks.Playbook import Playbook


class UpdatePfSenseConfiguration(Playbook):
    def start(self):

        if not self.is_sub_playbook:
            self.run_steps(CheckForSSHSession())

        self.run_steps(TestConnection(MACHINE_COLLECTION.firewall, 22),
                   TestConnection(MACHINE_COLLECTION.backup, 22),
                   PopulateRootSecret(),
                   PopulateLdapsucheSecret(),
                   PopulateFwradiusSecret(),
                   )

        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(
            ConfigurePfSense(),
            SetFirewallPassword("root", self.credentials.root),
            SetFirewallPassword("admin", self.credentials.root)
        )
        if not self.credentials.no_reboot:
            self.run_steps(RebootFirewall())