From: Michael Fritch Date: Mon, 24 Jan 2022 19:39:15 +0000 (-0700) Subject: mgr/cephadm: improve asyncssh type checking X-Git-Tag: v18.0.0~1492^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=dd09127ec4979ee27708d1aab44cc13a7bb3b2e6;p=ceph.git mgr/cephadm: improve asyncssh type checking asyncssh 2.9.0 introduces additional type hints Fixes: https://tracker.ceph.com/issues/54003 Signed-off-by: Michael Fritch --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index b45f4a2c3e52f..f59ade2c79af7 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -63,7 +63,7 @@ from .configchecks import CephadmConfigChecks try: import asyncssh except ImportError as e: - asyncssh = None + asyncssh = None # type: ignore asyncssh_import_error = str(e) logger = logging.getLogger(__name__) diff --git a/src/pybind/mgr/cephadm/ssh.py b/src/pybind/mgr/cephadm/ssh.py index 7c438e897ac23..034688023e9bf 100644 --- a/src/pybind/mgr/cephadm/ssh.py +++ b/src/pybind/mgr/cephadm/ssh.py @@ -6,13 +6,13 @@ from threading import Thread from contextlib import contextmanager from io import StringIO from shlex import quote -from typing import TYPE_CHECKING, Optional, List, Tuple, Dict, Iterator, TypeVar, Awaitable +from typing import TYPE_CHECKING, Optional, List, Tuple, Dict, Iterator, TypeVar, Awaitable, Union from orchestrator import OrchestratorError try: import asyncssh except ImportError: - asyncssh = None + asyncssh = None # type: ignore if TYPE_CHECKING: from cephadm.module import CephadmOrchestrator @@ -85,8 +85,8 @@ class SSHManager: self.cons[host] = conn self.mgr.offline_hosts_remove(host) - conn = self.cons.get(host) - return conn + + return self.cons[host] @contextmanager def redirect_log(self, host: str, addr: str) -> Iterator[None]: @@ -144,9 +144,22 @@ class SSHManager: await self._reset_con(host) self.mgr.offline_hosts.add(host) raise OrchestratorError(f'Unable to reach remote host {host}. {str(e)}') - out = r.stdout.rstrip('\n') - err = r.stderr.rstrip('\n') - return out, err, r.returncode + + def _rstrip(v: Union[bytes, str, None]) -> str: + if not v: + return '' + if isinstance(v, str): + return v.rstrip('\n') + if isinstance(v, bytes): + return v.decode().rstrip('\n') + raise OrchestratorError( + f'Unable to parse ssh output with type {type(v)} from remote host {host}') + + out = _rstrip(r.stdout) + err = _rstrip(r.stderr) + rc = r.returncode if r.returncode else 0 + + return out, err, rc def execute_command(self, host: str, diff --git a/src/pybind/mgr/requirements.txt b/src/pybind/mgr/requirements.txt index fd6d938a01e85..9ee93f481be38 100644 --- a/src/pybind/mgr/requirements.txt +++ b/src/pybind/mgr/requirements.txt @@ -1,3 +1,3 @@ -rrequirements-required.txt -asyncssh==2.8 +asyncssh==2.9 kubernetes==11.0.0