]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add DaemonIdentity type
authorJohn Mulligan <jmulligan@redhat.com>
Fri, 23 Jun 2023 15:49:57 +0000 (11:49 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Wed, 9 Aug 2023 17:48:07 +0000 (13:48 -0400)
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 <jmulligan@redhat.com>
src/cephadm/cephadm.py

index 6f1eb99a9c274da338e57467d4e7cc073c0272a7..d3b3ba50f1f6bf57815abb4549bf4746eb640363 100755 (executable)
@@ -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,