add --port flag
This commit is contained in:
1 parent
2d564a1546
commit
9a818fce9f
10 files changed
+294
-14
No files matched your search
+41
-2
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Iterable
|
||||
|
||||
@@ -10,6 +11,11 @@ from .config import ProxmoxConfig
|
||||
from .errors import ApiError, ProxdomainError
|
||||
from .http import JsonClient, Transport, UrllibTransport
|
||||
|
||||
# `proxdomain-port=8080` on its own or inside a sentence, in the guest Notes field.
|
||||
PORT_NOTE_RE = re.compile(r"proxdomain-port\s*[:=]\s*(\d{1,5})", re.IGNORECASE)
|
||||
# Equivalent shorthand as a Proxmox tag: `proxdomain-port-8080`.
|
||||
PORT_TAG_RE = re.compile(r"^proxdomain-port-(\d{1,5})$", re.IGNORECASE)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Guest:
|
||||
@@ -110,6 +116,25 @@ class ProxmoxClient:
|
||||
result.append(guest)
|
||||
return result
|
||||
|
||||
# -- per-guest settings ---------------------------------------------
|
||||
def guest_port(self, guest: Guest) -> tuple[int | None, str]:
|
||||
"""Port the guest declares for itself, plus where it was found.
|
||||
|
||||
Checked in this order: the Notes field (`proxdomain-port=8080`), then a
|
||||
tag (`proxdomain-port-8080`). Returns (None, "") when the guest says
|
||||
nothing, so the caller can fall back to its own default.
|
||||
"""
|
||||
notes = self._config_document(guest).get("description")
|
||||
if isinstance(notes, str):
|
||||
match = PORT_NOTE_RE.search(notes)
|
||||
if match:
|
||||
return _checked_port(match.group(1), guest, "its Notes field"), "guest notes"
|
||||
for tag in guest.tags:
|
||||
match = PORT_TAG_RE.match(tag.strip())
|
||||
if match:
|
||||
return _checked_port(match.group(1), guest, f"the tag {tag!r}"), "guest tag"
|
||||
return None, ""
|
||||
|
||||
# -- addressing -----------------------------------------------------
|
||||
def guest_ip(self, guest: Guest) -> str | None:
|
||||
want = self.config.ip_preference
|
||||
@@ -188,17 +213,21 @@ class ProxmoxClient:
|
||||
found.append(value)
|
||||
return found
|
||||
|
||||
def _config_ips(self, guest: Guest) -> list[str]:
|
||||
def _config_document(self, guest: Guest) -> dict:
|
||||
endpoint = "qemu" if guest.kind == "qemu" else "lxc"
|
||||
try:
|
||||
data = self.http.get(f"/nodes/{guest.node}/{endpoint}/{guest.vmid}/config")
|
||||
except ApiError as exc:
|
||||
if exc.is_auth_error:
|
||||
raise
|
||||
return []
|
||||
return {}
|
||||
config = data if isinstance(data, dict) else {}
|
||||
if "data" in config and isinstance(config["data"], dict):
|
||||
config = config["data"]
|
||||
return config
|
||||
|
||||
def _config_ips(self, guest: Guest) -> list[str]:
|
||||
config = self._config_document(guest)
|
||||
found: list[str] = []
|
||||
for key, value in sorted(config.items()):
|
||||
if not (key.startswith("net") or key.startswith("ipconfig")):
|
||||
@@ -251,6 +280,16 @@ def _split_tags(value: object) -> tuple[str, ...]:
|
||||
return tuple(tag.strip() for tag in normalized.split(";") if tag.strip())
|
||||
|
||||
|
||||
def _checked_port(raw: str, guest: Guest, where: str) -> int:
|
||||
port = int(raw)
|
||||
if not 1 <= port <= 65535:
|
||||
raise ProxdomainError(
|
||||
f"{guest.label} declares the port {port} in {where}, which is not a "
|
||||
"valid TCP port (1-65535)"
|
||||
)
|
||||
return port
|
||||
|
||||
|
||||
def _version(address: str) -> int:
|
||||
try:
|
||||
return ipaddress.ip_address(address).version
|
||||
|
||||
Reference in new issue
Block a user