From: John Mulligan Date: Thu, 27 Apr 2023 17:39:02 +0000 (-0400) Subject: cephadm: add executes_early decorator to version command X-Git-Tag: v18.2.1~326^2~90 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=f2b71fa334d47989a79dd654455203e8e1be2752;p=ceph-ci.git cephadm: add executes_early decorator to version command 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 (cherry picked from commit 20e38136d7baafdf74a43c09effeffec3040f952) --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index e6575edbd1b..c6f6b694afc 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -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: