mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-09-29 18:16:43 +00:00
OCI catalog verification, console start marks and log cleanup
This commit is contained in:
@@ -733,6 +733,36 @@ def _lxc_isolated_ips(vmid):
|
||||
return isolated
|
||||
|
||||
|
||||
def _lxc_shares_host_network(vmid):
|
||||
"""Whether the container uses the host's network namespace instead of its
|
||||
own, as a ProxMenux host-monitor (Glances, Netdata) does: lxc-info then
|
||||
lists every address of the host, private bridges included."""
|
||||
try:
|
||||
with open(f"/etc/pve/lxc/{int(vmid)}.conf", encoding="utf-8") as handle:
|
||||
text = handle.read().split("\n[", 1)[0]
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
return ("lxc.include: /etc/pve/lxc/proxmenux-host-monitor" in text
|
||||
or ("lxc.net.0.type: none" in text and not re.search(r"^net\d+:", text, re.M)))
|
||||
|
||||
|
||||
def _host_default_route_ipv4():
|
||||
"""The host's IPv4 address on the interface that carries its default
|
||||
route: the one a reader on the LAN reaches."""
|
||||
try:
|
||||
route = subprocess.run(['ip', '-4', 'route', 'show', 'default'],
|
||||
capture_output=True, text=True, timeout=3).stdout
|
||||
match = re.search(r"\bdev\s+(\S+)", route)
|
||||
if not match:
|
||||
return None
|
||||
addr = subprocess.run(['ip', '-4', '-o', 'addr', 'show', 'dev', match.group(1), 'scope', 'global'],
|
||||
capture_output=True, text=True, timeout=3).stdout
|
||||
found = re.search(r"\binet\s+([0-9.]+)/", addr)
|
||||
return found.group(1) if found else None
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return None
|
||||
|
||||
|
||||
def get_lxc_ip_from_lxc_info(vmid):
|
||||
"""Get LXC IP addresses using lxc-info command (for DHCP containers)
|
||||
Returns a dict with all IPs and classification"""
|
||||
@@ -763,6 +793,10 @@ def get_lxc_ip_from_lxc_info(vmid):
|
||||
isolated = _lxc_isolated_ips(vmid)
|
||||
if isolated:
|
||||
real_ips.sort(key=lambda ip: ip in isolated)
|
||||
if _lxc_shares_host_network(vmid):
|
||||
host_ip = _host_default_route_ipv4()
|
||||
if host_ip in real_ips:
|
||||
real_ips.sort(key=lambda ip: ip != host_ip)
|
||||
|
||||
return {
|
||||
'all_ips': ips,
|
||||
@@ -15349,7 +15383,9 @@ def api_lxc_console_log(vmid):
|
||||
lines = request.args.get('lines', default=200, type=int)
|
||||
offset = request.args.get('offset', type=int)
|
||||
inode = request.args.get('inode', type=int)
|
||||
return jsonify(oci_console_logs.read(vmid, lines=lines, offset=offset, inode=inode))
|
||||
since_start = request.args.get('since_start', '') in ('1', 'true')
|
||||
return jsonify(oci_console_logs.read(vmid, lines=lines, offset=offset, inode=inode,
|
||||
since_start=since_start))
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@@ -78,7 +78,11 @@ def clean(text: str) -> list[str]:
|
||||
return lines
|
||||
|
||||
|
||||
def _read_tail(handle, size: int, lines: int) -> tuple[list[str], bool]:
|
||||
# Written by the container's pre-start hook at every start (oci_console_mark.sh).
|
||||
START_MARK = "=== ProxMenux: container started "
|
||||
|
||||
|
||||
def _read_tail(handle, size: int, lines: int, since_start: bool = False) -> tuple[list[str], bool, bool]:
|
||||
start = max(0, size - TAIL_READ_LIMIT)
|
||||
handle.seek(start)
|
||||
data = handle.read(size - start).decode("utf-8", errors="replace")
|
||||
@@ -87,11 +91,15 @@ def _read_tail(handle, size: int, lines: int) -> tuple[list[str], bool]:
|
||||
result = result[1:] # the first line was cut by the read window
|
||||
if result and result[-1] == "":
|
||||
result = result[:-1]
|
||||
head_cut = start > 0 or len(result) > lines
|
||||
return result[-lines:], head_cut
|
||||
marks = [index for index, line in enumerate(result) if line.startswith(START_MARK)]
|
||||
if since_start and marks:
|
||||
result = result[marks[-1]:]
|
||||
head_cut = (start > 0 and not (since_start and marks)) or len(result) > lines
|
||||
return result[-lines:], head_cut, bool(marks)
|
||||
|
||||
|
||||
def read(vmid: int, lines: int = 200, offset: int | None = None, inode: int | None = None) -> dict:
|
||||
def read(vmid: int, lines: int = 200, offset: int | None = None, inode: int | None = None,
|
||||
since_start: bool = False) -> dict:
|
||||
"""The last `lines` lines, or what was appended after `offset`.
|
||||
|
||||
`lines=0` reads nothing: it only says whether the container has a
|
||||
@@ -115,9 +123,9 @@ def read(vmid: int, lines: int = 200, offset: int | None = None, inode: int | No
|
||||
if offset is None or replaced or offset > size:
|
||||
# First open, or the file the viewer followed is gone: rotated by
|
||||
# copytruncate, or recreated with the container.
|
||||
result, head_cut = _read_tail(handle, size, max(lines, 1))
|
||||
result, head_cut, has_marks = _read_tail(handle, size, max(lines, 1), since_start)
|
||||
return {**base, "lines": result, "offset": size, "reset": offset is not None,
|
||||
"head_truncated": head_cut}
|
||||
"head_truncated": head_cut, "start_marks": has_marks}
|
||||
handle.seek(offset)
|
||||
chunk = handle.read(min(size - offset, FOLLOW_READ_LIMIT))
|
||||
# Only whole lines are returned; an unfinished last line is left for the
|
||||
|
||||
Reference in New Issue
Block a user