From 06a2bd3a6f096e7bf5f694e5b80e56a0b8195c8f Mon Sep 17 00:00:00 2001 From: forest Date: Mon, 12 Jul 2021 14:38:56 -0500 Subject: [PATCH] add public_ipv4_first_usable_ip, public_ipv4_last_usable_ip --- capsulflask/admin.py | 11 ++------ capsulflask/db.py | 2 +- capsulflask/db_model.py | 11 ++++++-- capsulflask/hub_api.py | 10 ++++---- .../18_down_start_ip_end_ip.sql | 6 +++++ .../18_up_start_ip_end_ip.sql | 25 +++++++++++++++++++ 6 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 capsulflask/schema_migrations/18_down_start_ip_end_ip.sql create mode 100644 capsulflask/schema_migrations/18_up_start_ip_end_ip.sql diff --git a/capsulflask/admin.py b/capsulflask/admin.py index 8368224..50cc61e 100644 --- a/capsulflask/admin.py +++ b/capsulflask/admin.py @@ -35,17 +35,10 @@ def index(): display_host = dict(name=host_id, networks=value['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_addresses_width = float((network_end_int-network_start_int)+1) diff --git a/capsulflask/db.py b/capsulflask/db.py index 1b75fc0..23b3621 100644 --- a/capsulflask/db.py +++ b/capsulflask/db.py @@ -43,7 +43,7 @@ def init_app(app, is_running_server): hasSchemaVersionTable = False actionWasTaken = False schemaVersion = 0 - desiredSchemaVersion = 17 + desiredSchemaVersion = 18 cursor = connection.cursor() diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index a6ef58d..976985a 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -332,7 +332,9 @@ class DBModel: def list_hosts_with_networks(self, host_id: str): 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 """ if host_id is None: @@ -351,7 +353,12 @@ class DBModel: if row[0] not in hosts: 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 diff --git a/capsulflask/hub_api.py b/capsulflask/hub_api.py index 3482cf9..066c41b 100644 --- a/capsulflask/hub_api.py +++ b/capsulflask/hub_api.py @@ -142,14 +142,14 @@ def can_claim_create(payload, host_id) -> (str, str): for vm in vms: 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_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: - 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) break diff --git a/capsulflask/schema_migrations/18_down_start_ip_end_ip.sql b/capsulflask/schema_migrations/18_down_start_ip_end_ip.sql new file mode 100644 index 0000000..0b7725f --- /dev/null +++ b/capsulflask/schema_migrations/18_down_start_ip_end_ip.sql @@ -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; diff --git a/capsulflask/schema_migrations/18_up_start_ip_end_ip.sql b/capsulflask/schema_migrations/18_up_start_ip_end_ip.sql new file mode 100644 index 0000000..b7dc702 --- /dev/null +++ b/capsulflask/schema_migrations/18_up_start_ip_end_ip.sql @@ -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; +