Create NatNetwork if it doesn't exists

Before creating the labs, the code tries
to create the NatNetwork, if it already exists
it skips, if it doesn't it creates the NatNet
using the subnet defined in the localhost.yaml as CIDR.

Test Plan:
PASS: If NatNetwork doesn't exists, it is successfully created
PASS: If NatNetwork exists with different IP for CIDR, installation
is terminated.

Story: 2005051
Task: 48920

Change-Id: I80e361e66c5a936da019aa55ff7aa26643819b6f
Signed-off-by: Daniel Caires <daniel.caires@encora.com>
This commit is contained in:
Daniel Caires 2023-10-26 11:01:29 -03:00
parent c827cf81e9
commit 132113d247
3 changed files with 109 additions and 2 deletions

View File

@ -153,7 +153,7 @@ will be configured and used.
```
6. Now you're ready to run the script. From the `/virtualbox/pybox`
folder, do (remember to change the password on the below command before
folder, do (remember to change the password on the below command before
running it):
```shell

View File

@ -901,3 +901,96 @@ def vboxmanage_deleteportforward(rule_name, network):
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
LOG.error("Error while trying to delete port-forwarding rule. Continuing installation!")
def vboxmanage_createnatnet(network, cidr):
"""
Create new NatNetwork
Args:
network (str): Name of the NAT network.
cidr (str): CIDR for the NAT network.
Returns:
True if the command is executed with success.
False if the command throws an exception.
"""
exists = vboxmanage_natnetexists(network)
if exists:
LOG.info('NatNetwork named "%s" already exists, skipping creation.', network)
cidrcheck = vboxmanage_checkcidr(network, cidr)
if not cidrcheck:
return False
else:
cmd = [
"vboxmanage",
"natnetwork",
"add",
"--netname",
network,
"--network",
cidr,
"--dhcp",
"off",
"--ipv6",
"on"]
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
LOG.info('NatNetwork named "%s" was sucessfully created.', network)
except subprocess.CalledProcessError:
LOG.error("Error while trying to create NatNetwork")
raise
return True
def vboxmanage_natnetexists(network):
"""
Verify if NatNetwork already exists
Args:
network (str): Name of the NAT network.
Returns:
True if the NetNetwork exists.
False if the NatNetwork does not exists.
"""
cmd = ["vboxmanage", "list", "natnets", "--long"]
result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
natpattern = r"NetworkName:(.*?)IP"
natnetworks = re.findall(natpattern,result.decode(),re.DOTALL)
for natnetwork in natnetworks:
natname = natnetwork.strip().split('\n')
if natname[0] == network:
return True
return False
def vboxmanage_checkcidr(network, cidr):
"""
Check if the CIDR of a natnetwork corresponds to the OAM network
Args:
network (str): Name of the NAT network.
cidr (str): CIDR for the NAT network.
Returns:
True if CIDR is correct for the given NAT network.
False if CIDR is different for the given NAT network.
"""
cmd = ["vboxmanage", "list", "natnets", "--long"]
result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
natpattern = r"Name:(.*?)IPv6 Enabled"
natnetworks = re.findall(natpattern,result.decode(),re.DOTALL)
for natnetwork in natnetworks:
natname = natnetwork.strip().split('\n')
if network == natname[0] and cidr in natname[2]:
return True
return False

View File

@ -281,7 +281,7 @@ def yes_no_prompt(message):
Args:
message (str): Message to be displayed
Returns:
Answer to the prompt(bool)
"""
@ -367,6 +367,19 @@ def create_lab(m_vboxoptions):
node_name = m_vboxoptions.labname + f"-storage-{node_id}"
nodes_list.append(node_name)
if m_vboxoptions.vboxnet_name != None and m_vboxoptions.vboxnet_type == "nat":
LOG.info('Creating NatNetwork named "%s"', m_vboxoptions.vboxnet_name)
try:
return_nat = vboxmanage.vboxmanage_createnatnet(m_vboxoptions.vboxnet_name, m_vboxoptions.nat_cidr)
if not return_nat:
LOG.warning('NatNetwork named "%s" exists, but CIDR is different from OAM subnet', m_vboxoptions.vboxnet_name)
sys.exit(1)
except subprocess.CalledProcessError as exc:
# pylint: disable=logging-fstring-interpolation
LOG.error(f"Script was interrupted with error: {exc}",)
sys.exit(1)
LOG.info("#### We will create the following nodes: %s", nodes_list)
# pylint: disable=too-many-nested-blocks
for node in nodes_list:
@ -2364,6 +2377,7 @@ def load_config():
try:
with open(V_BOX_OPTIONS.ansible_controller_config, encoding="utf-8") as stream:
loaded = ruamel.yaml.safe_load(stream)
V_BOX_OPTIONS.nat_cidr = loaded.get('external_oam_subnet')
if V_BOX_OPTIONS.setup_type != AIO_SX:
V_BOX_OPTIONS.controller_floating_ip = loaded.get('external_oam_floating_address')
V_BOX_OPTIONS.controller0_ip = loaded.get('external_oam_node_0_address')