From: John Mulligan Date: Sat, 20 May 2023 17:42:02 +0000 (-0400) Subject: cephadm: split command_deploy_from into mockable parts X-Git-Tag: v19.0.0~982^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3684cbbfe2add10de42794aa0eeef9b13897804b;p=ceph.git cephadm: split command_deploy_from into mockable parts Split up command_deploy_from so that the part that reads the JSON from an input source is moved into read_configuration_source and the part that applied the configuration data to the ctx is moved into apply_deploy_config_to_ctx. This will make these sections reusable and, more importantly, make it easier to mock/monkeypatch these functions in tests. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 847bb3f8e63b..c2d1e8f0e2f8 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -6259,10 +6259,8 @@ def command_deploy(ctx): _common_deploy(ctx, daemon_type, daemon_id, daemon_ports, deployment_type) -def command_deploy_from(ctx: CephadmContext) -> None: - """The deploy-from command is similar to deploy but sources nearly all - configuration parameters from an input JSON configuration file. - """ +def read_configuration_source(ctx: CephadmContext) -> Dict[str, Any]: + """Read a JSON configuration based on the `ctx.source` value.""" source = '-' if 'source' in ctx and ctx.source: source = ctx.source @@ -6272,9 +6270,16 @@ def command_deploy_from(ctx: CephadmContext) -> None: with open(source, 'rb') as fh: config_data = json.load(fh) logger.debug('Loaded deploy configuration: %r', config_data) + return config_data - # Bind properties taken from the json to our ctx, similar to how - # cli options on `deploy` are bound to the context. + +def apply_deploy_config_to_ctx( + config_data: Dict[str, Any], + ctx: CephadmContext, +) -> None: + """Bind properties taken from the config_data dictionary to our ctx, + similar to how cli options on `deploy` are bound to the context. + """ ctx.name = config_data['name'] if 'image' in config_data: ctx.image = config_data['image'] @@ -6306,6 +6311,14 @@ def command_deploy_from(ctx: CephadmContext) -> None: update_default_image(ctx) logger.debug('Determined image: %r', ctx.image) + +def command_deploy_from(ctx: CephadmContext) -> None: + """The deploy-from command is similar to deploy but sources nearly all + configuration parameters from an input JSON configuration file. + """ + config_data = read_configuration_source(ctx) + apply_deploy_config_to_ctx(config_data, ctx) + daemon_type, daemon_id = ctx.name.split('.', 1) lock = FileLock(ctx, ctx.fsid)