Skip to content

Commit

Permalink
Generalising the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Dec 7, 2023
1 parent 98b2591 commit 7d4f31b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
8 changes: 7 additions & 1 deletion exasol_transformers_extension/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
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()
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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="")
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7d4f31b

Please sign in to comment.