From 2fe7c87bd10516afe0fa298abe0bfb5216d20ad6 Mon Sep 17 00:00:00 2001 From: Adam King Date: Fri, 15 Sep 2023 09:44:58 -0400 Subject: [PATCH] cephadm: start decorators.py in cephadmlib Originally, wanted to move all the decorators into their own files. Unfortunately, that isn't possible at this time as most of them depend on things that are still within cephadm.py This includes list_daemons _rm_cluster is_fsid termcolor ContainerInfo Ceph and I'm sure I'm missing some others. We'll have to revisit this again later when more of these things have moved, or they can be slowly moved as their dependencies are. Signed-off-by: Adam King --- src/cephadm/cephadm.py | 40 +++-------------------- src/cephadm/cephadmlib/decorators.py | 47 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 src/cephadm/cephadmlib/decorators.py diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index ecbe6dcd46b..041babd683b 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -77,7 +77,6 @@ from cephadmlib.constants import ( LOGROTATE_DIR, LOG_DIR, LOG_DIR_MODE, - NO_DEPRECATED, PIDS_LIMIT_UNLIMITED_PODMAN_VERSION, SYSCTL_DIR, UNIT_DIR, @@ -144,6 +143,11 @@ from cephadmlib.container_types import ( InitContainer, is_container_running, ) +from cephadmlib.decorators import ( + deprecated_command, + executes_early, + require_image +) FuncT = TypeVar('FuncT', bound=Callable) @@ -1636,19 +1640,6 @@ def infer_image(func: FuncT) -> FuncT: return cast(FuncT, _infer_image) -def require_image(func: FuncT) -> FuncT: - """ - Require the global --image flag to be set - """ - @wraps(func) - def _require_image(ctx: CephadmContext) -> Any: - if not ctx.image: - raise Error('This command requires the global --image option to be set') - return func(ctx) - - return cast(FuncT, _require_image) - - def default_image(func: FuncT) -> FuncT: @wraps(func) def _default_image(ctx: CephadmContext) -> Any: @@ -1683,27 +1674,6 @@ def update_default_image(ctx: CephadmContext) -> None: ctx.image = _get_default_image(ctx) -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 deprecated_command(func: FuncT) -> FuncT: - @wraps(func) - def _deprecated_command(ctx: CephadmContext) -> Any: - logger.warning(f'Deprecated command used: {func}') - if NO_DEPRECATED: - raise Error('running deprecated commands disabled') - return func(ctx) - - return cast(FuncT, _deprecated_command) - - def get_container_info(ctx: CephadmContext, daemon_filter: str, by_name: bool) -> Optional[ContainerInfo]: """ :param ctx: Cephadm context diff --git a/src/cephadm/cephadmlib/decorators.py b/src/cephadm/cephadmlib/decorators.py new file mode 100644 index 00000000000..73d81d34f33 --- /dev/null +++ b/src/cephadm/cephadmlib/decorators.py @@ -0,0 +1,47 @@ +# decorators.py - decorators used by cephadm and their helpers + +import logging +from functools import wraps +from typing import Any, Callable, TypeVar, cast + +from .context import CephadmContext +from .constants import NO_DEPRECATED +from .exceptions import Error + +FuncT = TypeVar('FuncT', bound=Callable) + +logger = logging.getLogger() + + +def require_image(func: FuncT) -> FuncT: + """ + Require the global --image flag to be set + """ + @wraps(func) + def _require_image(ctx: CephadmContext) -> Any: + if not ctx.image: + raise Error('This command requires the global --image option to be set') + return func(ctx) + + return cast(FuncT, _require_image) + + +def deprecated_command(func: FuncT) -> FuncT: + @wraps(func) + def _deprecated_command(ctx: CephadmContext) -> Any: + logger.warning(f'Deprecated command used: {func}') + if NO_DEPRECATED: + raise Error('running deprecated commands disabled') + return func(ctx) + + return cast(FuncT, _deprecated_command) + + +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 -- 2.39.5