diff --git a/capsulflask/admin.py b/capsulflask/admin.py index 2a80c9a..0cac03b 100644 --- a/capsulflask/admin.py +++ b/capsulflask/admin.py @@ -33,7 +33,7 @@ def index(): {'}'} """] - public_ipv4_by_capsul_id = dict() + vm_by_id = dict() for kv in hosts.items(): host_id = kv[0] @@ -51,7 +51,10 @@ def index(): if host_id in vms_by_host_and_network: if network['network_name'] in vms_by_host_and_network[host_id]: for vm in vms_by_host_and_network[host_id][network['network_name']]: - public_ipv4_by_capsul_id[vm['id']] = vm['public_ipv4'] + vm['network_name'] = network['network_name'] + vm['virtual_bridge_name'] = network['virtual_bridge_name'] + vm['host'] = host_id + vm_by_id[vm['id']] = vm ip_address_int = int(ipaddress.ip_address(vm['public_ipv4'])) if network_start_int <= ip_address_int and ip_address_int <= network_end_int: allocation = f"{host_id}_{network['network_name']}_{len(network['allocations'])}" @@ -74,7 +77,8 @@ def index(): # db_vms = get_model().all_vm_ids_with_desired_state() - virt_vms = current_app.config["HUB_MODEL"].list_ids_with_desired_state() + # TODO will be replaced + #virt_vms = current_app.config["HUB_MODEL"].virsh_list() virt_vms_dict = dict() for vm in virt_vms: @@ -88,10 +92,11 @@ def index(): if vm["id"] not in virt_vms_dict: in_db_but_not_in_virt.append(vm["id"]) elif vm["desired_state"] == "running" and virt_vms_dict[vm["id"]] != "running": - if vm["id"] in public_ipv4_by_capsul_id: - needs_to_be_started.append({"id": vm["id"], "ipv4": public_ipv4_by_capsul_id[vm["id"]]}) + if vm["id"] in vm_by_id: + needs_to_be_started.append(vm_by_id[vm["id"]]) else: needs_to_be_started_missing_ipv4.append(vm["id"]) + elif vm["ipv4"] != current_ipv4 csp_inline_style_nonce = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10) response_text = render_template( diff --git a/capsulflask/cli.py b/capsulflask/cli.py index 8218961..9fb5078 100644 --- a/capsulflask/cli.py +++ b/capsulflask/cli.py @@ -269,7 +269,8 @@ def notify_users_about_account_balance(): def ensure_vms_and_db_are_synced(): db_vms = get_model().all_vm_ids_with_desired_state() - virt_vms = current_app.config["HUB_MODEL"].list_ids_with_desired_state() + # TODO replaced + #virt_vms = current_app.config["HUB_MODEL"].virsh_list() db_ids_dict = dict() virt_ids_dict = dict() diff --git a/capsulflask/consistency.py b/capsulflask/consistency.py index 7a86c63..bb379e9 100644 --- a/capsulflask/consistency.py +++ b/capsulflask/consistency.py @@ -42,8 +42,8 @@ def get_all_vms_from_db() -> dict: return db_vms_by_id def get_all_vms_from_hosts() -> dict: - virt_vms = current_app.config["HUB_MODEL"].virsh_list() - virt_networks = current_app.config["HUB_MODEL"].virsh_netlist() + virt_vms = current_app.config["HUB_MODEL"].get_all_by_host_and_network() + #virt_networks = current_app.config["HUB_MODEL"].virsh_netlist() db_hosts = get_model().list_hosts_with_networks(None) virt_vms_by_id = dict() diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 1da4f78..e1922ad 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -59,7 +59,7 @@ class DBModel: # ------ VM & ACCOUNT MANAGEMENT --------- def non_deleted_vms_by_host_and_network(self, host_id): - query = "SELECT id, host, network_name, public_ipv4, public_ipv6 FROM vms WHERE deleted IS NULL" + query = "SELECT id, desired_state, host, network_name, public_ipv4, public_ipv6 FROM vms WHERE deleted IS NULL" if host_id is None: self.cursor.execute(query) else: @@ -73,22 +73,22 @@ class DBModel: hosts = dict() for row in self.cursor.fetchall(): - host_id = row[1] - network_name = row[2] + host_id = row[2] + network_name = row[3] if host_id not in hosts: hosts[host_id] = dict() if network_name not in hosts[host_id]: hosts[host_id][network_name] = [] hosts[host_id][network_name].append( - dict(id=row[0], public_ipv4=row[3], public_ipv6=row[4]) + dict(id=row[0], desired_state=row[1], public_ipv4=row[4], public_ipv6=row[5]) ) return hosts - def all_vm_ids_with_desired_state(self): - self.cursor.execute("SELECT id, desired_state FROM vms WHERE deleted IS NULL") - return list(map(lambda x: {"id": x[0], "desired_state": x[1]}, self.cursor.fetchall())) + # def all_vm_ids_with_desired_state(self): + # self.cursor.execute("SELECT id, desired_state FROM vms WHERE deleted IS NULL") + # return list(map(lambda x: {"id": x[0], "desired_state": x[1]}, self.cursor.fetchall())) def operating_systems_dict(self): self.cursor.execute("SELECT id, template_image_file_name, description FROM os_images WHERE deprecated = FALSE")