From: John Mulligan Date: Thu, 11 May 2023 14:25:38 +0000 (-0400) Subject: cephadm: split default_image decorator into two functions X-Git-Tag: v18.2.1~326^2~74 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8a1186e3d65bd1fbab6bc42a7c680e131fb03367;p=ceph.git cephadm: split default_image decorator into two functions Keep default_image as a decorator for functions that will only ever need to update an image passed by the CLI. For other future functions that want to execute code prior to assigning an image from CLI parameters add `update_default_image` which takes a CephadmContext and updates it and can be used by the caller at an arbitrary point in the code flow. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index e4d172df2ddd..18a900ca074f 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -2303,29 +2303,35 @@ def require_image(func: FuncT) -> FuncT: def default_image(func: FuncT) -> FuncT: @wraps(func) def _default_image(ctx: CephadmContext) -> Any: - if not ctx.image: - if 'name' in ctx and ctx.name: - type_ = ctx.name.split('.', 1)[0] - if type_ in Monitoring.components: - ctx.image = Monitoring.components[type_]['image'] - if type_ == 'haproxy': - ctx.image = HAproxy.default_image - if type_ == 'keepalived': - ctx.image = Keepalived.default_image - if type_ == SNMPGateway.daemon_type: - ctx.image = SNMPGateway.default_image - if type_ in Tracing.components: - ctx.image = Tracing.components[type_]['image'] - if not ctx.image: - ctx.image = os.environ.get('CEPHADM_IMAGE') - if not ctx.image: - ctx.image = _get_default_image(ctx) - + update_default_image(ctx) return func(ctx) return cast(FuncT, _default_image) +def update_default_image(ctx: CephadmContext) -> None: + if getattr(ctx, 'image', None): + return + ctx.image = None # ensure ctx.image exists to avoid repeated `getattr`s + name = getattr(ctx, 'name', None) + if name: + type_ = name.split('.', 1)[0] + if type_ in Monitoring.components: + ctx.image = Monitoring.components[type_]['image'] + if type_ == 'haproxy': + ctx.image = HAproxy.default_image + if type_ == 'keepalived': + ctx.image = Keepalived.default_image + if type_ == SNMPGateway.daemon_type: + ctx.image = SNMPGateway.default_image + if type_ in Tracing.components: + ctx.image = Tracing.components[type_]['image'] + if not ctx.image: + ctx.image = os.environ.get('CEPHADM_IMAGE') + if not ctx.image: + 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