]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm: add executes_early decorator to version command
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 27 Apr 2023 17:39:02 +0000 (13:39 -0400)
committerAdam King <adking@redhat.com>
Thu, 31 Aug 2023 17:35:12 +0000 (13:35 -0400)
Add the executes_early decorator and apply it to the version command
such that the version command will function without the uaual
requirements and set up steps that a cephadm command needs.
This allows anyone with a binary, root or not, to check the version.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 20e38136d7baafdf74a43c09effeffec3040f952)

src/cephadm/cephadm.py

index e6575edbd1b45bd141ddec33dc1bd01d2f465eab..c6f6b694afcd5bbb6d194bc1976104286d5c67c4 100755 (executable)
@@ -2284,6 +2284,16 @@ def default_image(func: FuncT) -> FuncT:
     return cast(FuncT, _default_image)
 
 
+def executes_early(func: FuncT) -> FuncT:
+    """Decorator that indicates the command function is meant to have no
+    dependencies and no environmental requirements and can therefore be
+    executed as non-root and with no logging, etc. Commands that have this
+    decorator applied must be simple and self-contained.
+    """
+    cast(Any, func)._execute_early = True
+    return func
+
+
 def get_container_info(ctx: CephadmContext, daemon_filter: str, by_name: bool) -> Optional[ContainerInfo]:
     """
     :param ctx: Cephadm context
@@ -4845,6 +4855,7 @@ def command_agent(ctx: CephadmContext) -> None:
 
 ##################################
 
+@executes_early
 def command_version(ctx):
     # type: (CephadmContext) -> int
     import importlib
@@ -10124,6 +10135,15 @@ def main() -> None:
         sys.stderr.write('No command specified; pass -h or --help for usage\n')
         sys.exit(1)
 
+    if ctx.has_function() and getattr(ctx.func, '_execute_early', False):
+        try:
+            sys.exit(ctx.func(ctx))
+        except Error as e:
+            if ctx.verbose:
+                raise
+            logger.error('ERROR: %s' % e)
+            sys.exit(1)
+
     cephadm_require_root()
     cephadm_init_logging(ctx, av)
     try: