From 7d4f31b5e810475c8c29630accb4b7c80a678b45 Mon Sep 17 00:00:00 2001 From: mibe Date: Thu, 7 Dec 2023 14:40:16 +0000 Subject: [PATCH] Generalising the cli --- exasol_transformers_extension/deploy.py | 8 ++- .../language_container_deployer_cli.py | 55 +++++++++++++++---- .../te_language_container_deployer.py | 12 ++-- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/exasol_transformers_extension/deploy.py b/exasol_transformers_extension/deploy.py index 7757b639..9281552d 100644 --- a/exasol_transformers_extension/deploy.py +++ b/exasol_transformers_extension/deploy.py @@ -3,7 +3,8 @@ from exasol_transformers_extension.deployment.scripts_deployer_cli import \ scripts_deployer_main from exasol_transformers_extension.deployment.language_container_deployer_cli \ - import language_container_deployer_main + import language_container_deployer_main, slc_parameter_formatters, CustomizableParameters +from exasol_transformers_extension.deployment.te_language_container_deployer import TeLanguageContainerDeployer @click.group() @@ -11,6 +12,11 @@ def main(): pass +slc_parameter_formatters.set_formatter(CustomizableParameters.container_url, + TeLanguageContainerDeployer.SLC_URL_FORMATTER) +slc_parameter_formatters.set_formatter(CustomizableParameters.container_name, + TeLanguageContainerDeployer.SLC_NAME) + main.add_command(scripts_deployer_main) main.add_command(language_container_deployer_main) diff --git a/exasol_transformers_extension/deployment/language_container_deployer_cli.py b/exasol_transformers_extension/deployment/language_container_deployer_cli.py index 971c1c05..99b8dd8a 100644 --- a/exasol_transformers_extension/deployment/language_container_deployer_cli.py +++ b/exasol_transformers_extension/deployment/language_container_deployer_cli.py @@ -1,8 +1,39 @@ import os import click +from enum import Enum from pathlib import Path from exasol_transformers_extension.deployment import deployment_utils as utils -from exasol_transformers_extension.deployment.te_language_container_deployer import TeLanguageContainerDeployer +from exasol_transformers_extension.deployment.language_container_deployer import LanguageContainerDeployer + + +class CustomizableParameters(Enum): + container_url = 1 + container_name = 2 + + +class _ParameterFormatters: + def __init__(self): + self._formatters = {} + + def __call__(self, ctx, param, value): + + def update_parameter(parameter_name: str, formatter: str) -> None: + param_formatter = ctx.params.get(parameter_name, formatter) + if param_formatter: + kwargs = {param.name: value} + ctx.params[parameter_name] = param_formatter.format(**kwargs) + + if value: + for prm_name, prm_formatter in self._formatters.items(): + update_parameter(prm_name, prm_formatter) + + return value + + def set_formatter(self, custom_parameter: CustomizableParameters, formatter: str) -> None: + self._formatters[custom_parameter.name] = formatter + + +slc_parameter_formatters = _ParameterFormatters() @click.command(name="language-container") @@ -12,18 +43,17 @@ @click.option('--bucketfs-use-https', type=bool, default=False) @click.option('--bucketfs-user', type=str, required=True, default="w") @click.option('--bucketfs-password', prompt='bucketFS password', hide_input=True, - default=lambda: os.environ.get( - utils.BUCKETFS_PASSWORD_ENVIRONMENT_VARIABLE, "")) + default=lambda: os.environ.get(utils.BUCKETFS_PASSWORD_ENVIRONMENT_VARIABLE, "")) @click.option('--bucket', type=str, required=True) @click.option('--path-in-bucket', type=str, required=True, default=None) @click.option('--container-file', type=click.Path(exists=True, file_okay=True), default=None) -@click.option('--version', type=str, default=None) +@click.option('--version', type=str, default=None, expose_value=False, + callback=slc_parameter_formatters) @click.option('--dsn', type=str, required=True) @click.option('--db-user', type=str, required=True) @click.option('--db-pass', prompt='db password', hide_input=True, - default=lambda: os.environ.get( - utils.DB_PASSWORD_ENVIRONMENT_VARIABLE, "")) + default=lambda: os.environ.get(utils.DB_PASSWORD_ENVIRONMENT_VARIABLE, "")) @click.option('--language-alias', type=str, default="PYTHON3_TE") @click.option('--ssl-cert-path', type=str, default="") @click.option('--ssl-client-cert-path', type=str, default="") @@ -42,7 +72,6 @@ def language_container_deployer_main( bucket: str, path_in_bucket: str, container_file: str, - version: str, dsn: str, db_user: str, db_pass: str, @@ -53,9 +82,11 @@ def language_container_deployer_main( use_ssl_cert_validation: bool, upload_container: bool, alter_system: bool, - allow_override: bool): + allow_override: bool, + container_url: str = None, + container_name: str = None): - deployer = TeLanguageContainerDeployer.create( + deployer = LanguageContainerDeployer.create( bucketfs_name=bucketfs_name, bucketfs_host=bucketfs_host, bucketfs_port=bucketfs_port, @@ -77,9 +108,11 @@ def language_container_deployer_main( deployer.run(alter_system=alter_system, allow_override=allow_override) elif container_file: deployer.run(container_file=Path(container_file), alter_system=alter_system, allow_override=allow_override) - elif version: - deployer.download_from_git_and_run(version, alter_system=alter_system, allow_override=allow_override) + elif container_url and container_name: + deployer.download_and_run(container_url, container_name, alter_system=alter_system, + allow_override=allow_override) else: + # The error message should mention the parameters which the callback is specified for being missed. raise ValueError("To upload a language container you should specify either its " "release version or a path of the already downloaded container file.") diff --git a/exasol_transformers_extension/deployment/te_language_container_deployer.py b/exasol_transformers_extension/deployment/te_language_container_deployer.py index e652cd29..834938ff 100644 --- a/exasol_transformers_extension/deployment/te_language_container_deployer.py +++ b/exasol_transformers_extension/deployment/te_language_container_deployer.py @@ -6,14 +6,14 @@ class TeLanguageContainerDeployer(LanguageContainerDeployer): SLC_NAME = "exasol_transformers_extension_container_release.tar.gz" - GH_RELEASE_URL = "https://github.com/exasol/transformers-extension/releases/download" + SLC_URL_FORMATTER = "https://github.com/exasol/transformers-extension/releases/download/{version}/" + SLC_NAME - def download_from_git_and_run(self, version: str, - alter_system: bool = True, - allow_override: bool = False) -> None: + def download_from_github_and_run(self, version: str, + alter_system: bool = True, + allow_override: bool = False) -> None: - url = "/".join((self.GH_RELEASE_URL, version, self.SLC_NAME)) - self.download_and_run(url, self.SLC_NAME, alter_system=alter_system, allow_override=allow_override) + self.download_and_run(self.SLC_URL_FORMATTER.format(version=version), self.SLC_NAME, + alter_system=alter_system, allow_override=allow_override) def run(self, container_file: Optional[Path] = None, bucket_file_path: Optional[str] = None,