From: John Mulligan Date: Fri, 23 Jun 2023 15:49:57 +0000 (-0400) Subject: cephadm: add DaemonIdentity type X-Git-Tag: v19.0.0~711^2~19 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=0aba240abd5f931ce741e84ba5db6e9336f7eede;p=ceph.git cephadm: add DaemonIdentity type It's an extremely common pattern in cephadm to need a triple of (fsid, daemon_type, daemon_id). These triples are used to generate various names and paths etc. Add a new type, DaemonIdentity, that can represent these values in a more convient package. It has an additional optional field "subcomponent" that can represent a part of a daemon/service. It is expected to be used in an immutable manner like a namedtuple, but I didn't end using a namedtuple because cephadm doesn't make much use of them and the syntax to combine namedtuple and type hints is awkward. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 6f1eb99a9c274..d3b3ba50f1f6b 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -4396,6 +4396,75 @@ WantedBy=ceph-{fsid}.target ################################## +class DaemonIdentity: + def __init__( + self, + fsid: str, + daemon_type: str, + daemon_id: Union[int, str], + subcomponent: str = '', + ) -> None: + self._fsid = fsid + self._daemon_type = daemon_type + self._daemon_id = str(daemon_id) + self._subcomponent = subcomponent + + @property + def fsid(self) -> str: + return self._fsid + + @property + def daemon_type(self) -> str: + return self._daemon_type + + @property + def daemon_id(self) -> str: + return self._daemon_id + + @property + def subcomponent(self) -> str: + return self._subcomponent + + @property + def legacy_container_name(self) -> str: + return 'ceph-%s-%s.%s' % (self.fsid, self.daemon_type, self.daemon_id) + + @property + def container_name(self) -> str: + name = f'ceph-{self.fsid}-{self.daemon_type}-{self.daemon_id}' + if self.subcomponent: + name = f'{name}-{self.subcomponent}' + return name.replace('.', '-') + + def _replace( + self, + *, + fsid: Optional[str] = None, + daemon_type: Optional[str] = None, + daemon_id: Union[None, int, str] = None, + subcomponent: Optional[str] = None, + ) -> 'DaemonIdentity': + return self.__class__( + fsid=self.fsid if fsid is None else fsid, + daemon_type=( + self.daemon_type if daemon_type is None else daemon_type + ), + daemon_id=self.daemon_id if daemon_id is None else daemon_id, + subcomponent=( + self.subcomponent if subcomponent is None else subcomponent + ), + ) + + @classmethod + def from_name(cls, fsid: str, name: str) -> 'DaemonIdentity': + daemon_type, daemon_id = name.split('.', 1) + return cls(fsid, daemon_type, daemon_id) + + @classmethod + def from_context(cls, ctx: 'CephadmContext') -> 'DaemonIdentity': + return cls.from_name(ctx.fsid, ctx.name) + + class BasicContainer: def __init__( self,