#import pprint from flask import current_app from capsulflask.db import get_model # { # "capsul-123abc45": { # "id": "capsul-123abc45", # "public_ipv4": "123.123.123.123", # "public_ipv6": "::::", # "host": "baikal", # "network_name": "public1", # "virtual_bridge_name": "virbr1", # "desired_state": "running" # }, # { ... }, # ... # } def get_all_vms_from_db() -> dict: db_hosts = get_model().list_hosts_with_networks(None) db_vms_by_host_and_network = get_model().non_deleted_vms_by_host_and_network(None) db_vms_by_id = dict() for kv in db_hosts.items(): host_id = kv[0] value = kv[1] for network in value['networks']: if host_id in db_vms_by_host_and_network and network['network_name'] in db_vms_by_host_and_network[host_id]: for vm in db_vms_by_host_and_network[host_id][network['network_name']]: vm['network_name'] = network['network_name'] vm['virtual_bridge_name'] = network['virtual_bridge_name'] vm['host'] = host_id db_vms_by_id[vm['id']] = vm return db_vms_by_id # this returns the same shape of object as get_all_vms_from_db except it has 'state' instead of 'desired_state'. # also it has `macs` which is a list of strings def get_all_vms_from_hosts(db_vms_by_id: dict) -> dict: virt_vms_by_id = current_app.config["HUB_MODEL"].get_all_by_id() #current_app.logger.info(f"\n*******************2:\n{pprint.pformat(virt_vms_by_id)}\n\n\n\n") db_hosts = get_model().list_hosts_with_networks(None) for vm_id, virt_vm in virt_vms_by_id.items(): if vm_id in db_vms_by_id: if 'host' not in virt_vm: virt_vm['host'] = db_vms_by_id[vm_id]['host'] if 'network_name' not in virt_vm: virt_vm['network_name'] = db_vms_by_id[vm_id]['network_name'] else: if 'host' not in virt_vm: virt_vm['host'] = "unknown" if 'network_name' not in virt_vm: virt_vm['network_name'] = "unknown" if virt_vm['host'] in db_hosts and virt_vm['network_name'] in db_hosts[virt_vm['host']]: virt_vm['virtual_bridge_name'] = db_hosts[virt_vm['host']][virt_vm['network_name']]['virtual_bridge_name'] else: virt_vm['virtual_bridge_name'] = "unknown" return virt_vms_by_id def get_inconsistent_capsuls_information(db_vms_by_id: dict, virt_vms_by_id: dict) -> dict: virt_vm_id_by_ipv4 = dict() for vm_id, virt_vm in virt_vms_by_id.items(): if 'public_ipv4' in virt_vm and virt_vm['public_ipv4'] != "": virt_vm_id_by_ipv4[virt_vm['public_ipv4']] = vm_id db_vm_id_by_ipv4 = dict() for vm_id, db_vm in db_vms_by_id.items(): if 'public_ipv4' in db_vm and db_vm['public_ipv4'] != "": db_vm_id_by_ipv4[db_vm['public_ipv4']] = vm_id in_db_but_not_in_virt = [] state_not_equal_to_desired_state = [] has_no_desired_ip_address = [] has_not_aquired_ip_address_yet = [] stole_someone_elses_ip_and_own_ip_avaliable = [] stole_someone_elses_ip_but_own_ip_also_stolen = [] has_wrong_ip = [] for vm_id, db_vm in db_vms_by_id.items(): if vm_id not in virt_vms_by_id: in_db_but_not_in_virt.append(db_vm) elif virt_vms_by_id[vm_id]['state'] != db_vm["desired_state"]: db_vm["state"] = virt_vms_by_id[vm_id]['state'] state_not_equal_to_desired_state.append(db_vm) elif 'public_ipv4' not in db_vm or db_vm["public_ipv4"] == "": has_no_desired_ip_address.append(db_vm) elif db_vm["desired_state"] == "running" and ('public_ipv4' not in virt_vms_by_id[vm_id] or virt_vms_by_id[vm_id]['public_ipv4'] == ""): has_not_aquired_ip_address_yet.append(db_vm) elif db_vm["desired_state"] == "running" and virt_vms_by_id[vm_id]['public_ipv4'] != db_vm["public_ipv4"]: db_vm["desired_ipv4"] = db_vm["public_ipv4"] db_vm["current_ipv4"] = virt_vms_by_id[vm_id]['public_ipv4'] if virt_vms_by_id[vm_id]['public_ipv4'] in db_vm_id_by_ipv4: if db_vm["public_ipv4"] not in virt_vm_id_by_ipv4: stole_someone_elses_ip_and_own_ip_avaliable.append(db_vm) else: stole_someone_elses_ip_but_own_ip_also_stolen.append(db_vm) else: has_wrong_ip.append(db_vm) return { "in_db_but_not_in_virt": in_db_but_not_in_virt, "state_not_equal_to_desired_state": state_not_equal_to_desired_state, "has_no_desired_ip_address": has_no_desired_ip_address, "has_not_aquired_ip_address_yet": has_not_aquired_ip_address_yet, "stole_someone_elses_ip_and_own_ip_avaliable": stole_someone_elses_ip_and_own_ip_avaliable, "stole_someone_elses_ip_but_own_ip_also_stolen": stole_someone_elses_ip_but_own_ip_also_stolen, "has_wrong_ip": has_wrong_ip, }