Update pve RustDesk GA sync scripts
This commit is contained in:
@@ -14,6 +14,7 @@ fi
|
||||
|
||||
exec python3 - <<'PY'
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -173,8 +174,30 @@ def guest_exec_text(vmid: int, command: list[str], timeout: int = 90) -> tuple[s
|
||||
return None, 'guest exec timeout'
|
||||
|
||||
|
||||
def decode_agent_file_content(raw) -> str | None:
|
||||
if raw is None:
|
||||
return None
|
||||
if isinstance(raw, bytes):
|
||||
return raw.decode('utf-8-sig', errors='replace')
|
||||
text = str(raw).strip()
|
||||
if not text:
|
||||
return None
|
||||
# pvesh may return decoded file body (JSON/TOML/plain text).
|
||||
if text[0] in '{[#' or 'rustdesk_id' in text or re.search(r'(?m)^\s*id\s*=', text):
|
||||
return text.lstrip('\ufeff')
|
||||
ascii_blob = ''.join(text.split())
|
||||
if not ascii_blob:
|
||||
return None
|
||||
try:
|
||||
decoded = base64.b64decode(ascii_blob, validate=True)
|
||||
return decoded.decode('utf-8-sig', errors='replace')
|
||||
except (ValueError, binascii.Error):
|
||||
return text.lstrip('\ufeff')
|
||||
|
||||
|
||||
def guest_file_read(vmid: int, guest_path: str) -> tuple[str | None, str | None]:
|
||||
for path in {guest_path, guest_path.replace('\\', '/')}:
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[
|
||||
'pvesh', 'get', f'/nodes/{NODE}/qemu/{vmid}/agent/file-read',
|
||||
@@ -184,15 +207,18 @@ def guest_file_read(vmid: int, guest_path: str) -> tuple[str | None, str | None]
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
continue
|
||||
try:
|
||||
payload = json.loads(proc.stdout)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
data = payload.get('data', payload)
|
||||
content_b64 = data.get('content') if isinstance(data, dict) else None
|
||||
if not content_b64:
|
||||
if not isinstance(data, dict):
|
||||
continue
|
||||
raw = data.get('content')
|
||||
if raw is None:
|
||||
continue
|
||||
text = decode_agent_file_content(raw)
|
||||
if text:
|
||||
return text, None
|
||||
except (json.JSONDecodeError, ValueError, UnicodeError):
|
||||
continue
|
||||
return base64.b64decode(content_b64).decode('utf-8', errors='replace'), None
|
||||
return None, f'file-read failed for {guest_path}'
|
||||
|
||||
|
||||
@@ -230,6 +256,7 @@ def guest_exec_powershell(vmid: int) -> tuple[str | None, str | None]:
|
||||
def read_rustdesk_id(vmid: int) -> tuple[str | None, str | None]:
|
||||
errors: list[str] = []
|
||||
for path in GUEST_READ_PATHS:
|
||||
try:
|
||||
text, err = guest_file_read(vmid, path)
|
||||
if not text:
|
||||
if VERBOSE:
|
||||
@@ -241,6 +268,9 @@ def read_rustdesk_id(vmid: int) -> tuple[str | None, str | None]:
|
||||
log(f'VM {vmid}: ID {rid} from file-read {path}')
|
||||
return rid, None
|
||||
errors.append(f'{path}: no id in file')
|
||||
except Exception as exc: # noqa: BLE001 — keep sync loop alive
|
||||
errors.append(f'{path}: {exc}')
|
||||
continue
|
||||
|
||||
rid, err = guest_exec_powershell(vmid)
|
||||
if rid:
|
||||
|
||||
Reference in New Issue
Block a user