clan-lib: Rename check_machine_online to can_ssh_login. Move to Remote object

This commit is contained in:
Qubasa
2025-06-23 14:21:51 +02:00
parent 533e404886
commit bc6b7e4ae9
3 changed files with 58 additions and 53 deletions

View File

@@ -1,41 +0,0 @@
import logging
import time
from dataclasses import dataclass
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.ssh.remote import Remote
log = logging.getLogger(__name__)
@dataclass
class ConnectionOptions:
timeout: int = 2
retries: int = 10
@API.register
def check_machine_online(
remote: Remote, opts: ConnectionOptions | None = None
) -> Literal["Online", "Offline"]:
timeout = opts.timeout if opts and opts.timeout else 2
for _ in range(opts.retries if opts and opts.retries else 10):
with remote.ssh_control_master() as ssh:
res = ssh.run(
["true"],
RunOpts(timeout=timeout, check=False, needs_user_terminal=True),
)
if res.returncode == 0:
return "Online"
if "Host key verification failed." in res.stderr:
raise ClanError(res.stderr.strip())
time.sleep(timeout)
return "Offline"