add public_ipv4_first_usable_ip, public_ipv4_last_usable_ip

This commit is contained in:
forest 2021-07-12 14:38:56 -05:00
parent fbe9c7fca4
commit 06a2bd3a6f
6 changed files with 48 additions and 17 deletions

View File

@ -35,17 +35,10 @@ def index():
display_host = dict(name=host_id, networks=value['networks']) display_host = dict(name=host_id, networks=value['networks'])
for network in display_host['networks']: for network in display_host['networks']:
public_ipv4_cidr_block_split = network["public_ipv4_cidr_block"].split("/")
if len(public_ipv4_cidr_block_split) != 2:
raise ValueError(f"network {network['network_name']} has invalid cidr block {network['public_ipv4_cidr_block']}")
network_start_int = int(ipaddress.ip_address(public_ipv4_cidr_block_split[0]))+1 network_start_int = int(ipaddress.ip_address(network["public_ipv4_first_usable_ip"]))
network_end_int = int(ipaddress.ip_address(network["public_ipv4_last_usable_ip"]))
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
network_end_int = -1
for ipv4_address in ipv4_network:
network_end_int = int(ipv4_address)
network['allocations'] = [] network['allocations'] = []
network_addresses_width = float((network_end_int-network_start_int)+1) network_addresses_width = float((network_end_int-network_start_int)+1)

View File

@ -43,7 +43,7 @@ def init_app(app, is_running_server):
hasSchemaVersionTable = False hasSchemaVersionTable = False
actionWasTaken = False actionWasTaken = False
schemaVersion = 0 schemaVersion = 0
desiredSchemaVersion = 17 desiredSchemaVersion = 18
cursor = connection.cursor() cursor = connection.cursor()

View File

@ -332,7 +332,9 @@ class DBModel:
def list_hosts_with_networks(self, host_id: str): def list_hosts_with_networks(self, host_id: str):
query = """ query = """
SELECT hosts.id, hosts.last_health_check, host_network.network_name, host_network.public_ipv4_cidr_block FROM hosts SELECT hosts.id, hosts.last_health_check, host_network.network_name,
host_network.public_ipv4_cidr_block, host_network.public_ipv4_first_usable_ip, host_network.public_ipv4_last_usable_ip
FROM hosts
JOIN host_network ON host_network.host = hosts.id JOIN host_network ON host_network.host = hosts.id
""" """
if host_id is None: if host_id is None:
@ -351,7 +353,12 @@ class DBModel:
if row[0] not in hosts: if row[0] not in hosts:
hosts[row[0]] = dict(last_health_check=row[1], networks=[]) hosts[row[0]] = dict(last_health_check=row[1], networks=[])
hosts[row[0]]["networks"].append(dict(network_name=row[2], public_ipv4_cidr_block=row[3])) hosts[row[0]]["networks"].append(dict(
network_name=row[2],
public_ipv4_cidr_block=row[3],
public_ipv4_first_usable_ip=row[4],
public_ipv4_last_usable_ip=row[5]
))
return hosts return hosts

View File

@ -142,14 +142,14 @@ def can_claim_create(payload, host_id) -> (str, str):
for vm in vms: for vm in vms:
claimed_ipv4s[vm['public_ipv4']] = True claimed_ipv4s[vm['public_ipv4']] = True
public_ipv4_cidr_block_split = network["public_ipv4_cidr_block"].split("/")
if len(public_ipv4_cidr_block_split) != 2:
raise ValueError(f"network {network['network_name']} has invalid cidr block {network['public_ipv4_cidr_block']}")
ipv4_network_first_address_int = int(ipaddress.ip_address(public_ipv4_cidr_block_split[0]))
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False) ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
ipv4_first_usable_ip = network["public_ipv4_first_usable_ip"]
ipv4_last_usable_ip = network["public_ipv4_last_usable_ip"]
for ipv4_address in ipv4_network: for ipv4_address in ipv4_network:
if int(ipv4_address) > ipv4_network_first_address_int and str(ipv4_address) not in claimed_ipv4s: within_usable_range = ipv4_first_usable_ip <= str(ipv4_address) and str(ipv4_address) <= ipv4_last_usable_ip
if within_usable_range and str(ipv4_address) not in claimed_ipv4s:
allocated_ipv4_address = str(ipv4_address) allocated_ipv4_address = str(ipv4_address)
break break

View File

@ -0,0 +1,6 @@
ALTER TABLE host_network DROP COLUMN public_ipv4_first_usable_ip;
ALTER TABLE host_network DROP COLUMN public_ipv4_last_usable_ip;
UPDATE schemaversion SET version = 17;

View File

@ -0,0 +1,25 @@
ALTER TABLE host_network ADD COLUMN public_ipv4_first_usable_ip TEXT;
ALTER TABLE host_network ADD COLUMN public_ipv4_last_usable_ip TEXT;
-- public1, 69.61.2.162/27
UPDATE host_network
SET public_ipv4_first_usable_ip = '69.61.2.163', public_ipv4_last_usable_ip = '69.61.2.190'
WHERE network_name = 'public1';
-- public2, 69.61.2.194/26
UPDATE host_network
SET public_ipv4_first_usable_ip = '69.61.2.195', public_ipv4_last_usable_ip = '69.61.2.254'
WHERE network_name = 'public2';
-- public3, 69.61.38.193/26
UPDATE host_network
SET public_ipv4_first_usable_ip = '69.61.38.194', public_ipv4_last_usable_ip = '69.61.38.254'
WHERE network_name = 'public3';
ALTER TABLE host_network ALTER COLUMN public_ipv4_first_usable_ip SET NOT NULL;
ALTER TABLE host_network ALTER COLUMN public_ipv4_last_usable_ip SET NOT NULL;
UPDATE schemaversion SET version = 18;