From cd26fcec5dd6b88d454f8e81a9cf0a689ac46b0e Mon Sep 17 00:00:00 2001 From: forest Date: Sun, 19 Dec 2021 12:10:32 -0600 Subject: [PATCH] skip assigning IP on creation and add back update_vm_ip for now because IP assignment on creation isn't done yet (need to manage MAC address) --- capsulflask/console.py | 9 ++++++--- capsulflask/db_model.py | 4 ++++ capsulflask/hub_api.py | 6 ++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/capsulflask/console.py b/capsulflask/console.py index 7b5feb8..3d64c24 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -27,10 +27,13 @@ def make_capsul_id(): letters_n_nummers = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10) return f"capsul-{letters_n_nummers}" -def double_check_capsul_address(id, get_ssh_host_keys): +def double_check_capsul_address(id, existing_ipv4, get_ssh_host_keys): try: result = current_app.config["HUB_MODEL"].get(id, get_ssh_host_keys) + if result != None and result != "" and (existing_ipv4 == None or existing_ipv4 == ""): + get_model().update_vm_ip(email=session["account"], id=id, ipv4=result.ipv4) + if result != None and result.ssh_host_keys != None and get_ssh_host_keys: get_model().update_vm_ssh_host_keys(email=session["account"], id=id, ssh_host_keys=result.ssh_host_keys) except: @@ -56,7 +59,7 @@ def index(): # for now we are going to check the IP according to the virt model # on every request. this could be done by a background job and cached later on... for vm in vms: - result = double_check_capsul_address(vm["id"], False) + result = double_check_capsul_address(vm["id"], vm["ipv4"], False) if result is not None: vm["ipv4"] = result.ipv4 vm["state"] = result.state @@ -166,7 +169,7 @@ def detail(id): else: needs_ssh_host_keys = "ssh_host_keys" not in vm or len(vm["ssh_host_keys"]) == 0 - vm_from_virt_model = double_check_capsul_address(vm["id"], needs_ssh_host_keys) + vm_from_virt_model = double_check_capsul_address(vm["id"], vm["ipv4"], needs_ssh_host_keys) if vm_from_virt_model is not None: vm["ipv4"] = vm_from_virt_model.ipv4 diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 213cc78..0537dbb 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -150,6 +150,10 @@ class DBModel: self.cursor.fetchall() )) + def update_vm_ip(self, email, id, ipv4): + self.cursor.execute("UPDATE vms SET public_ipv4 = %s WHERE email = %s AND id = %s", (ipv4, email, id)) + self.connection.commit() + def update_vm_ssh_host_keys(self, email, id, ssh_host_keys): for key in ssh_host_keys: self.cursor.execute(""" diff --git a/capsulflask/hub_api.py b/capsulflask/hub_api.py index 4f3a1ea..c3dc610 100644 --- a/capsulflask/hub_api.py +++ b/capsulflask/hub_api.py @@ -161,10 +161,12 @@ def can_claim_create(payload, host_id) -> (str, str): return "", f"host \"{host_id}\" does not have any avaliable IP addresses on any of its networks." # payload["network_name"] = allocated_network_name - # hard-code the network name for now until we can fix the phantom dhcp lease issues. + # payload["public_ipv4"] = allocated_ipv4_address + # hard-code the network name and IP for now until we can implement https://git.cyberia.club/cyberia/capsul-flask/issues/11 + # enable static IP -> capsul mapping via libvirt (manage MAC addresses) payload["network_name"] = 'public3' - payload["public_ipv4"] = allocated_ipv4_address + payload["public_ipv4"] = "" return payload, ""