Merge "Create NatNetwork if it doesn't exists"
This commit is contained in:
commit
778b379768
@ -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
|
||||
|
@ -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
|
@ -284,7 +284,7 @@ def yes_no_prompt(message):
|
||||
|
||||
Args:
|
||||
message (str): Message to be displayed
|
||||
|
||||
|
||||
Returns:
|
||||
Answer to the prompt(bool)
|
||||
"""
|
||||
@ -370,6 +370,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:
|
||||
@ -2384,6 +2397,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')
|
||||
|
Loading…
x
Reference in New Issue
Block a user