From a43b86a42a42901c3a703ac43492ba20db61752d Mon Sep 17 00:00:00 2001 From: vedp Date: Fri, 20 Sep 2024 14:15:53 -0700 Subject: [PATCH 1/2] Add flag to recover after server reboot --- deployer/globals.py | 1 + deployer/network.py | 26 +++++++++++++++++++------- deployer/utils.py | 5 +++++ deployer/virtual_machine.py | 11 +++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/deployer/globals.py b/deployer/globals.py index 548b870..a3c5c0c 100644 --- a/deployer/globals.py +++ b/deployer/globals.py @@ -7,6 +7,7 @@ DRY_RUN = False NO_NETWORK = False NO_VM = False +REBOOT_RECOVERY = False NETWORK_TYPES = ["nat", "isolated", "management"] VM_FLAVORS = ["pe", "ce", "dev"] diff --git a/deployer/network.py b/deployer/network.py index 3af5191..5bbb110 100644 --- a/deployer/network.py +++ b/deployer/network.py @@ -72,12 +72,19 @@ def _validate_network_conf(nw): name = nw["name"] ntype = nw["type"] + # In case of recovery after reboot, we just need to recreate Nat networks. + if G.REBOOT_RECOVERY: + if Network.IsLibvirtNetwork(ntype): + logging.debug(f"Reboot recovery not required for libvirt network {name}") + return True + + if len(name) > 12: logging.error(f"Max length for name is 12 characters : {name}") return False + # Check if interface with name already exists interfaces = psutil.net_if_addrs() - if name in interfaces: logging.error(f"Network with name {name} is already present") return False @@ -158,7 +165,9 @@ def _create_nat_network(self): AddLinuxBridge(self.name_, str(self.ip4_), str(self.ip6_), self.plen4_, self.plen6_) - AddDelIptableRules("A", self.name_, str(self.network4_)) + # Since iptables are persisted + if not G.REBOOT_RECOVERY: + AddDelIptableRules("A", self.name_, str(self.network4_)) # end _create_nat_network def _create_management_network(self): @@ -222,7 +231,7 @@ def _delete_libvirt_network(self): def Delete(self): if self.IsNat(): self._delete_nat_network() - elif self.IsLibvirtNetwork(): + elif Network.IsLibvirtNetwork(self.type_): self._delete_libvirt_network() else: logging.error(f"Unknown network type {self.type_}") @@ -233,9 +242,11 @@ def Create(self): if self.IsNat(): self._create_nat_network() elif self.IsIsolated(): - self._create_isolated_network() + if not G.REBOOT_RECOVERY: + self._create_isolated_network() elif self.IsManagement(): - self._create_management_network() + if not G.REBOOT_RECOVERY: + self._create_management_network() else: logging.error(f"Unknown network type {self.type_}") sys.exit(3) @@ -257,8 +268,9 @@ def IsNat(self): return self.type_ == "nat" # end IsNat - def IsLibvirtNetwork(self): - return self.type_ in ["isolated", "management"] + @staticmethod + def IsLibvirtNetwork(ntype): + return ntype in ["isolated", "management"] # end IsLibvirtNetwork def __str__(self): diff --git a/deployer/utils.py b/deployer/utils.py index 4d07950..d942f2a 100644 --- a/deployer/utils.py +++ b/deployer/utils.py @@ -30,6 +30,8 @@ def ProcessArguments(cli_args): help="Instead of executing, print the commands") skip_operation = parser.add_mutually_exclusive_group() + skip_operation.add_argument("--reboot-recovery", action="store_true", + help="Recover topology after server reboot") skip_operation.add_argument("--skip-network", action="store_true", help="Skip creating networks. Cannot be used with --skip-vm") skip_operation.add_argument("--skip-vm", action="store_true", @@ -57,6 +59,9 @@ def ProcessArguments(cli_args): if args.skip_vm: G.NO_VM = args.skip_vm + if args.reboot_recovery: + G.REBOOT_RECOVERY = args.reboot_recovery + if args.image.lower() == "ubuntu": G.BASE_OS = "ubuntu" G.OS_IMAGE_TEMPLATE = UBUNTU_TEMPLATE diff --git a/deployer/virtual_machine.py b/deployer/virtual_machine.py index 75fd4d7..d81a995 100644 --- a/deployer/virtual_machine.py +++ b/deployer/virtual_machine.py @@ -377,9 +377,20 @@ def __execute_virt_install_cmd(self): ExecuteCommand(cmd) # end __execute_virt_install_cmd + def __start_vm(self): + cmd = f"sudo virsh start {self.name_}" + ExecuteCommand(cmd) + # end __start_vm + def Create(self): logging.info(f"Creating Virtual Machine : {self.name_}") + # Incase of recovery after reboot, just start the + # VM after network creations + if G.REBOOT_RECOVERY: + self.__start_vm() + return + self.__generate_cloud_init_config() self.__generate_netplan_config() self.__create_root_disk() From 263656c8b20f1ba72e6f818ab28c062affd4051c Mon Sep 17 00:00:00 2001 From: vedp Date: Mon, 23 Sep 2024 21:16:29 -0700 Subject: [PATCH 2/2] Bump version to 0.2.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 3383708..4a71a93 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="topology-deployer", - version="0.2.1", + version="0.2.2", author="Ved Patel", description=("A python tool to deploy topology " "defined in a json config"),