capsul-flask/capsulflask/admin.py

114 lines
4.1 KiB
Python

import re
import sys
import json
import ipaddress
from datetime import datetime, timedelta
from flask import Blueprint, current_app, render_template, make_response
from werkzeug.exceptions import abort
from nanoid import generate
from capsulflask.metrics import durations as metric_durations
from capsulflask.auth import admin_account_required
from capsulflask.db import get_model
from capsulflask.shared import my_exec_info_message
bp = Blueprint("admin", __name__, url_prefix="/admin")
@bp.route("/")
@admin_account_required
def index():
# first create the hosts list w/ ip allocation visualization
#
hosts = get_model().list_hosts_with_networks(None)
vms_by_host_and_network = get_model().non_deleted_vms_by_host_and_network(None)
network_display_width_px = float(270)
#operations = get_model().list_all_operations()
display_hosts = []
inline_styles = [f"""
.network-display {'{'}
width: {network_display_width_px}px;
{'}'}
"""]
public_ipv4_by_capsul_id = dict()
for kv in hosts.items():
host_id = kv[0]
value = kv[1]
display_host = dict(name=host_id, networks=value['networks'])
for network in display_host['networks']:
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"]))
network['allocations'] = []
network_addresses_width = float((network_end_int-network_start_int)+1)
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']
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'])}"
inline_styles.append(
f"""
.{allocation} {'{'}
left: {(float(ip_address_int-network_start_int)/network_addresses_width)*network_display_width_px}px;
width: {network_display_width_px/network_addresses_width}px;
{'}'}
"""
)
network['allocations'].append(allocation)
else:
current_app.logger.warning(f"/admin: capsul {vm['id']} has public_ipv4 {vm['public_ipv4']} which is out of range for its host network {host_id} {network['network_name']} {network['public_ipv4_cidr_block']}")
display_hosts.append(display_host)
# Now creating the capsuls running status ui
#
db_vms = get_model().all_vm_ids_with_desired_state()
virt_vms = current_app.config["HUB_MODEL"].list_ids_with_desired_state()
virt_vms_dict = dict()
for vm in virt_vms:
virt_vms_dict[vm["id"]] = vm["state"]
in_db_but_not_in_virt = []
needs_to_be_started = []
needs_to_be_started_missing_ipv4 = []
for vm in db_vms:
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"]]})
else:
needs_to_be_started_missing_ipv4.append(vm["id"])
csp_inline_style_nonce = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10)
response_text = render_template(
"admin.html",
display_hosts=display_hosts,
in_db_but_not_in_virt=in_db_but_not_in_virt,
needs_to_be_started=needs_to_be_started,
needs_to_be_started_missing_ipv4=needs_to_be_started_missing_ipv4,
network_display_width_px=network_display_width_px,
csp_inline_style_nonce=csp_inline_style_nonce,
inline_style='\n'.join(inline_styles)
)
response = make_response(response_text)
response.headers.set('Content-Type', 'text/html')
response.headers.set('Content-Security-Policy', f"default-src 'self'; style-src 'self' 'nonce-{csp_inline_style_nonce}'")
return response