fix: WinRM RDP update via run_ps, strip CLIXML noise (0.20.11)

This commit is contained in:
2026-06-20 19:41:05 +10:00
parent d3517a5706
commit 05408e17c3
7 changed files with 294 additions and 83 deletions
+60 -29
View File
@@ -8,6 +8,7 @@ from sqlalchemy import select
from sqlalchemy.orm import Session
from app.models import Host
from app.services.agent_update_types import AgentUpdateFallbackResult
from app.services.agent_update_settings import (
PRODUCT_RDP,
PRODUCT_SSH,
@@ -147,18 +148,21 @@ def run_linux_agent_update_fallback(
*,
repo_url: str | None = None,
git_branch: str | None = None,
) -> tuple[bool, str, str | None]:
) -> AgentUpdateFallbackResult:
if not is_linux_host(host):
return False, "Host is not Linux", None
return AgentUpdateFallbackResult(ok=False, message="Host is not Linux", product_version=None)
if not cfg_linux.configured:
return False, "Linux SSH admin is not configured", None
return AgentUpdateFallbackResult(
ok=False,
message="Linux SSH admin is not configured",
product_version=None,
)
last_message = "SSH fallback failed"
version: str | None = None
last = AgentUpdateFallbackResult(ok=False, message="SSH fallback failed", product_version=None)
try:
targets = iter_ssh_targets(host)
except (HostNotLinuxError, HostTargetMissingError) as exc:
return False, str(exc), None
return AgentUpdateFallbackResult(ok=False, message=str(exc), product_version=None)
effective_repo = (repo_url or "").strip() or None
effective_branch = (git_branch or "main").strip() or "main"
@@ -170,23 +174,38 @@ def run_linux_agent_update_fallback(
repo_url=effective_repo,
git_branch=effective_branch,
)
last_message = result.message
last = AgentUpdateFallbackResult(
ok=result.ok,
message=result.message,
product_version=result.agent_version,
target=result.target,
stdout=result.stdout,
stderr=result.stderr,
exit_code=result.exit_code,
)
if result.ok:
version = result.agent_version
return True, result.message, version
return False, last_message, version
return last
return last
def run_windows_agent_update_fallback(host: Host, cfg_win, script_path: str) -> tuple[bool, str, str | None]:
def run_windows_agent_update_fallback(
host: Host,
cfg_win,
script_path: str,
) -> AgentUpdateFallbackResult:
if not is_windows_host(host):
return False, "Host is not Windows", None
return AgentUpdateFallbackResult(ok=False, message="Host is not Windows", product_version=None)
if not cfg_win.configured:
return False, "Windows domain admin is not configured", None
return AgentUpdateFallbackResult(
ok=False,
message="Windows domain admin is not configured",
product_version=None,
)
try:
targets = iter_winrm_targets(host)
except (HostNotWindowsError, WinRmHostTargetMissingError) as exc:
return False, str(exc), None
return AgentUpdateFallbackResult(ok=False, message=str(exc), product_version=None)
result, attempts = run_winrm_on_host_targets(
host,
@@ -200,7 +219,7 @@ def run_windows_agent_update_fallback(host: Host, cfg_win, script_path: str) ->
),
)
if result is None:
return False, "WinRM fallback failed", None
return AgentUpdateFallbackResult(ok=False, message="WinRM fallback failed", product_version=None)
message = result.message
if attempts:
message = f"{message} (пробовали: {', '.join(attempts)})"
@@ -209,10 +228,18 @@ def run_windows_agent_update_fallback(host: Host, cfg_win, script_path: str) ->
from app.services.ssh_connect import parse_ssh_monitor_version_text
version = parse_ssh_monitor_version_text(result.stdout)
return result.ok, message, version
return AgentUpdateFallbackResult(
ok=result.ok,
message=message,
product_version=version,
target=result.target,
stdout=result.stdout,
stderr=result.stderr,
exit_code=result.exit_code,
)
def execute_agent_update_fallback(db: Session, host: Host) -> tuple[bool, str, str | None]:
def execute_agent_update_fallback(db: Session, host: Host) -> AgentUpdateFallbackResult:
cfg = get_effective_agent_update_config(db)
product = (host.product or "").strip()
@@ -221,7 +248,7 @@ def execute_agent_update_fallback(db: Session, host: Host) -> tuple[bool, str, s
agent_cfg = get_effective_agent_update_config(db)
repo_url = (agent_cfg.ssh_git_repo_url or "").strip() or None
branch = (agent_cfg.git_branch or "main").strip() or "main"
ok, message, version = run_linux_agent_update_fallback(
result = run_linux_agent_update_fallback(
host,
linux_cfg,
repo_url=repo_url,
@@ -229,27 +256,31 @@ def execute_agent_update_fallback(db: Session, host: Host) -> tuple[bool, str, s
)
elif product == PRODUCT_RDP or is_windows_host(host):
win_cfg = get_effective_win_admin_config(db)
ok, message, version = run_windows_agent_update_fallback(host, win_cfg, cfg.win_agent_update_script)
result = run_windows_agent_update_fallback(host, win_cfg, cfg.win_agent_update_script)
else:
return False, f"Unsupported product for fallback update: {product}", None
return AgentUpdateFallbackResult(
ok=False,
message=f"Unsupported product for fallback update: {product}",
product_version=None,
)
now = datetime.now(timezone.utc)
host.agent_update_last_at = now
if ok:
if result.ok:
host.agent_update_state = "success"
host.agent_update_last_error = None
_clear_pending(host)
if version:
host.product_version = version
if result.product_version:
host.product_version = result.product_version
if is_linux_host(host):
host.ssh_admin_ok = True
host.ssh_admin_checked_at = now
else:
host.agent_update_state = "failed"
host.agent_update_last_error = message[:2000]
host.agent_update_last_error = result.message[:2000]
db.flush()
return ok, message, version
return result
def process_agent_update_fallbacks(db: Session) -> list[dict]:
@@ -270,14 +301,14 @@ def process_agent_update_fallbacks(db: Session) -> list[dict]:
results: list[dict] = []
for host in rows:
ok, message, version = execute_agent_update_fallback(db, host)
result = execute_agent_update_fallback(db, host)
results.append(
{
"host_id": host.id,
"hostname": host.hostname,
"ok": ok,
"message": message,
"product_version": version,
"ok": result.ok,
"message": result.message,
"product_version": result.product_version,
}
)
return results