|
|
|
|
@ -591,13 +591,13 @@ def create_app(
|
|
|
|
|
return f"{address}/{guest_prefix}"
|
|
|
|
|
raise VMError("公网地址池中没有可分配的 IP,请检查已用和保留地址", 409)
|
|
|
|
|
|
|
|
|
|
def public_network_options(profile: dict, service: VMService) -> dict:
|
|
|
|
|
"""Return a bounded, non-secret snapshot for the admin IP selector.
|
|
|
|
|
def public_network_inventory(profile: dict, service: VMService) -> dict:
|
|
|
|
|
"""Build one bounded public-pool inventory without exposing claims.
|
|
|
|
|
|
|
|
|
|
The list is advisory only. The network mutation endpoint still
|
|
|
|
|
performs its own validation and atomic claim immediately before
|
|
|
|
|
changing libvirt, so a stale selector can never allocate an address
|
|
|
|
|
twice.
|
|
|
|
|
The supported /24 through /30 pools contain at most 254 host
|
|
|
|
|
addresses, so materializing these sets is intentionally bounded. VM
|
|
|
|
|
metadata and durable claims are merged because an uncertain create
|
|
|
|
|
must keep its address unavailable even when no domain is visible yet.
|
|
|
|
|
"""
|
|
|
|
|
pool_value = profile.get("public_pool_cidr", "")
|
|
|
|
|
gateway_value = profile.get("public_gateway", "")
|
|
|
|
|
@ -616,9 +616,10 @@ def create_app(
|
|
|
|
|
"guest_prefix": None,
|
|
|
|
|
"gateway": "",
|
|
|
|
|
"dns_servers": [],
|
|
|
|
|
"available_ips": [],
|
|
|
|
|
"available_count": 0,
|
|
|
|
|
"message": "宿主机尚未配置公网地址池,暂时无法更换公网 IP。",
|
|
|
|
|
"usable_count": 0,
|
|
|
|
|
"reserved_count": 0,
|
|
|
|
|
"allocated_count": 0,
|
|
|
|
|
"available": [],
|
|
|
|
|
}
|
|
|
|
|
try:
|
|
|
|
|
network = ipaddress.IPv4Network(
|
|
|
|
|
@ -648,17 +649,20 @@ def create_app(
|
|
|
|
|
or address in (network.network_address, network.broadcast_address)
|
|
|
|
|
for address in reserved):
|
|
|
|
|
raise ValueError
|
|
|
|
|
blocked = {network.network_address, network.broadcast_address, gateway,
|
|
|
|
|
*dns, *reserved}
|
|
|
|
|
statically_blocked = {
|
|
|
|
|
network.network_address, network.broadcast_address, gateway,
|
|
|
|
|
*dns, *reserved,
|
|
|
|
|
}
|
|
|
|
|
try:
|
|
|
|
|
host_address = ipaddress.IPv4Address(profile.get("host", ""))
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
host_address = None
|
|
|
|
|
if host_address is not None:
|
|
|
|
|
blocked.add(host_address)
|
|
|
|
|
statically_blocked.add(host_address)
|
|
|
|
|
except (KeyError, TypeError, ValueError) as exc:
|
|
|
|
|
raise VMError("宿主机公网地址池配置损坏,请管理员重新保存设置", 500) from exc
|
|
|
|
|
|
|
|
|
|
allocated: set[ipaddress.IPv4Address] = set()
|
|
|
|
|
for existing_vm in service.list_vms():
|
|
|
|
|
configured = existing_vm.get("public_ip_cidr")
|
|
|
|
|
if not isinstance(configured, str):
|
|
|
|
|
@ -668,7 +672,7 @@ def create_app(
|
|
|
|
|
except ValueError:
|
|
|
|
|
continue
|
|
|
|
|
if address in network:
|
|
|
|
|
blocked.add(address)
|
|
|
|
|
allocated.add(address)
|
|
|
|
|
|
|
|
|
|
scope_prefix = f"{host_scope(profile)}."
|
|
|
|
|
if data_store is not None:
|
|
|
|
|
@ -680,21 +684,69 @@ def create_app(
|
|
|
|
|
except ValueError:
|
|
|
|
|
continue
|
|
|
|
|
if address in network:
|
|
|
|
|
blocked.add(address)
|
|
|
|
|
allocated.add(address)
|
|
|
|
|
|
|
|
|
|
available = [f"{address}/{guest_prefix}" for address in network.hosts()
|
|
|
|
|
if address not in blocked]
|
|
|
|
|
usable = set(network.hosts())
|
|
|
|
|
reserved_in_pool = usable & statically_blocked
|
|
|
|
|
allocated_in_pool = (usable - reserved_in_pool) & allocated
|
|
|
|
|
available = sorted(usable - reserved_in_pool - allocated_in_pool)
|
|
|
|
|
return {
|
|
|
|
|
"configuration_state": "ready",
|
|
|
|
|
"pool_cidr": str(network),
|
|
|
|
|
"guest_prefix": guest_prefix,
|
|
|
|
|
"gateway": str(gateway),
|
|
|
|
|
"dns_servers": [str(value) for value in dns],
|
|
|
|
|
"available_ips": available,
|
|
|
|
|
"available_count": len(available),
|
|
|
|
|
"usable_count": len(usable),
|
|
|
|
|
"reserved_count": len(reserved_in_pool),
|
|
|
|
|
"allocated_count": len(allocated_in_pool),
|
|
|
|
|
"available": available,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def public_network_options(profile: dict, service: VMService) -> dict:
|
|
|
|
|
"""Return a bounded, non-secret snapshot for the admin IP selector.
|
|
|
|
|
|
|
|
|
|
The list is advisory only. The network mutation endpoint still
|
|
|
|
|
performs its own validation and atomic claim immediately before
|
|
|
|
|
changing libvirt, so a stale selector can never allocate an address
|
|
|
|
|
twice.
|
|
|
|
|
"""
|
|
|
|
|
inventory = public_network_inventory(profile, service)
|
|
|
|
|
if inventory["configuration_state"] == "not_configured":
|
|
|
|
|
return {
|
|
|
|
|
"configuration_state": "not_configured",
|
|
|
|
|
"pool_cidr": "",
|
|
|
|
|
"guest_prefix": None,
|
|
|
|
|
"gateway": "",
|
|
|
|
|
"dns_servers": [],
|
|
|
|
|
"available_ips": [],
|
|
|
|
|
"available_count": 0,
|
|
|
|
|
"message": "宿主机尚未配置公网地址池,暂时无法更换公网 IP。",
|
|
|
|
|
}
|
|
|
|
|
available_ips = [
|
|
|
|
|
f"{address}/{inventory['guest_prefix']}"
|
|
|
|
|
for address in inventory["available"]
|
|
|
|
|
]
|
|
|
|
|
return {
|
|
|
|
|
"configuration_state": "ready",
|
|
|
|
|
"pool_cidr": inventory["pool_cidr"],
|
|
|
|
|
"guest_prefix": inventory["guest_prefix"],
|
|
|
|
|
"gateway": inventory["gateway"],
|
|
|
|
|
"dns_servers": inventory["dns_servers"],
|
|
|
|
|
"available_ips": available_ips,
|
|
|
|
|
"available_count": len(available_ips),
|
|
|
|
|
"message": "目标地址仍会由引擎在提交时再次校验并原子占用。",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def public_ip_capacity(profile: dict, service: VMService) -> dict:
|
|
|
|
|
inventory = public_network_inventory(profile, service)
|
|
|
|
|
return {
|
|
|
|
|
"configured": inventory["configuration_state"] == "ready",
|
|
|
|
|
"total": inventory["usable_count"],
|
|
|
|
|
"reserved": inventory["reserved_count"],
|
|
|
|
|
"allocated": inventory["allocated_count"],
|
|
|
|
|
"available": len(inventory["available"]),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def claim_explicit_public_ip(profile: dict, public_ip_cidr: str,
|
|
|
|
|
vm_id: str) -> None:
|
|
|
|
|
if data_store is None:
|
|
|
|
|
@ -1605,6 +1657,19 @@ def create_app(
|
|
|
|
|
profile = current_profile()
|
|
|
|
|
return public_network_options(profile, current_service())
|
|
|
|
|
|
|
|
|
|
@app.get("/api/v1/host/capacity")
|
|
|
|
|
def host_capacity(_session: Session = Depends(require_admin)):
|
|
|
|
|
"""Return the scheduler's non-secret observation for this host."""
|
|
|
|
|
profile = current_profile()
|
|
|
|
|
service = new_service(profile)
|
|
|
|
|
capacity = service.host_capacity()
|
|
|
|
|
return {
|
|
|
|
|
"health": "healthy",
|
|
|
|
|
"observed_at": int(time.time()),
|
|
|
|
|
**capacity,
|
|
|
|
|
"public_ipv4": public_ip_capacity(profile, service),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@app.post("/api/v1/host/scan")
|
|
|
|
|
def scan_host(body: HostScanBody, _session: Session = Depends(require_admin_write)):
|
|
|
|
|
return manager.scan(body.host, body.port)
|
|
|
|
|
|