Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to recover after server reboot #35

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deployer/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
26 changes: 19 additions & 7 deletions deployer/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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_}")
Expand All @@ -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)
Expand All @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions deployer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions deployer/virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading