save unsaved files

This commit is contained in:
forest 2021-12-05 16:50:55 -06:00
parent cc349d266f
commit a93a97c66c
4 changed files with 21 additions and 15 deletions

View File

@ -33,7 +33,7 @@ def index():
{'}'} {'}'}
"""] """]
public_ipv4_by_capsul_id = dict() vm_by_id = dict()
for kv in hosts.items(): for kv in hosts.items():
host_id = kv[0] host_id = kv[0]
@ -51,7 +51,10 @@ def index():
if host_id in vms_by_host_and_network: if host_id in vms_by_host_and_network:
if network['network_name'] in vms_by_host_and_network[host_id]: 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']]: 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'])) ip_address_int = int(ipaddress.ip_address(vm['public_ipv4']))
if network_start_int <= ip_address_int and ip_address_int <= network_end_int: if network_start_int <= ip_address_int and ip_address_int <= network_end_int:
allocation = f"{host_id}_{network['network_name']}_{len(network['allocations'])}" 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() 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() virt_vms_dict = dict()
for vm in virt_vms: for vm in virt_vms:
@ -88,10 +92,11 @@ def index():
if vm["id"] not in virt_vms_dict: if vm["id"] not in virt_vms_dict:
in_db_but_not_in_virt.append(vm["id"]) in_db_but_not_in_virt.append(vm["id"])
elif vm["desired_state"] == "running" and virt_vms_dict[vm["id"]] != "running": elif vm["desired_state"] == "running" and virt_vms_dict[vm["id"]] != "running":
if vm["id"] in public_ipv4_by_capsul_id: if vm["id"] in vm_by_id:
needs_to_be_started.append({"id": vm["id"], "ipv4": public_ipv4_by_capsul_id[vm["id"]]}) needs_to_be_started.append(vm_by_id[vm["id"]])
else: else:
needs_to_be_started_missing_ipv4.append(vm["id"]) needs_to_be_started_missing_ipv4.append(vm["id"])
elif vm["ipv4"] != current_ipv4
csp_inline_style_nonce = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10) csp_inline_style_nonce = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10)
response_text = render_template( response_text = render_template(

View File

@ -269,7 +269,8 @@ def notify_users_about_account_balance():
def ensure_vms_and_db_are_synced(): def ensure_vms_and_db_are_synced():
db_vms = get_model().all_vm_ids_with_desired_state() 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() db_ids_dict = dict()
virt_ids_dict = dict() virt_ids_dict = dict()

View File

@ -42,8 +42,8 @@ def get_all_vms_from_db() -> dict:
return db_vms_by_id return db_vms_by_id
def get_all_vms_from_hosts() -> dict: def get_all_vms_from_hosts() -> dict:
virt_vms = current_app.config["HUB_MODEL"].virsh_list() virt_vms = current_app.config["HUB_MODEL"].get_all_by_host_and_network()
virt_networks = current_app.config["HUB_MODEL"].virsh_netlist() #virt_networks = current_app.config["HUB_MODEL"].virsh_netlist()
db_hosts = get_model().list_hosts_with_networks(None) db_hosts = get_model().list_hosts_with_networks(None)
virt_vms_by_id = dict() virt_vms_by_id = dict()

View File

@ -59,7 +59,7 @@ class DBModel:
# ------ VM & ACCOUNT MANAGEMENT --------- # ------ VM & ACCOUNT MANAGEMENT ---------
def non_deleted_vms_by_host_and_network(self, host_id): 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: if host_id is None:
self.cursor.execute(query) self.cursor.execute(query)
else: else:
@ -73,22 +73,22 @@ class DBModel:
hosts = dict() hosts = dict()
for row in self.cursor.fetchall(): for row in self.cursor.fetchall():
host_id = row[1] host_id = row[2]
network_name = row[2] network_name = row[3]
if host_id not in hosts: if host_id not in hosts:
hosts[host_id] = dict() hosts[host_id] = dict()
if network_name not in hosts[host_id]: if network_name not in hosts[host_id]:
hosts[host_id][network_name] = [] hosts[host_id][network_name] = []
hosts[host_id][network_name].append( 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 return hosts
def all_vm_ids_with_desired_state(self): # def all_vm_ids_with_desired_state(self):
self.cursor.execute("SELECT id, desired_state FROM vms WHERE deleted IS NULL") # 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())) # return list(map(lambda x: {"id": x[0], "desired_state": x[1]}, self.cursor.fetchall()))
def operating_systems_dict(self): def operating_systems_dict(self):
self.cursor.execute("SELECT id, template_image_file_name, description FROM os_images WHERE deprecated = FALSE") self.cursor.execute("SELECT id, template_image_file_name, description FROM os_images WHERE deprecated = FALSE")