clan-cli: Fix check_machine_online to use Remote object instead of machine, this makes it possible to override HostKeyCheck properly

This commit is contained in:
Qubasa
2025-06-08 17:20:38 +02:00
parent 68b7c22faf
commit 87f4fbfcbf
3 changed files with 42 additions and 16 deletions

View File

@@ -6,7 +6,7 @@ from typing import Literal
from clan_lib.api import API
from clan_lib.cmd import RunOpts
from clan_lib.errors import ClanError
from clan_lib.machines.machines import Machine
from clan_lib.ssh.remote import Remote
log = logging.getLogger(__name__)
@@ -19,19 +19,12 @@ class ConnectionOptions:
@API.register
def check_machine_online(
machine: Machine, opts: ConnectionOptions | None = None
remote: Remote, opts: ConnectionOptions | None = None
) -> Literal["Online", "Offline"]:
hostname = machine.target_host().target
if not hostname:
msg = f"Machine {machine.name} does not specify a targetHost"
raise ClanError(msg)
timeout = opts.timeout if opts and opts.timeout else 2
for _ in range(opts.retries if opts and opts.retries else 10):
host = machine.target_host()
with host.ssh_control_master() as ssh:
with remote.ssh_control_master() as ssh:
res = ssh.run(
["true"],
RunOpts(timeout=timeout, check=False, needs_user_terminal=True),
@@ -39,6 +32,10 @@ def check_machine_online(
if res.returncode == 0:
return "Online"
if "Host key verification failed." in res.stderr:
raise ClanError(res.stderr.strip())
time.sleep(timeout)
return "Offline"