From 452c8b612c9b235eeb9d18f5b9d25b427587c3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Cabrera?= Date: Tue, 13 Aug 2024 22:29:12 +0100 Subject: [PATCH] feat(cli): check env vars and prompt --- odoo_docker_cli.py | 91 +++++++++++++++++++++++++++++++----- templates/docker-compose.yml | 6 ++- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/odoo_docker_cli.py b/odoo_docker_cli.py index 5888fd2..bdde83c 100644 --- a/odoo_docker_cli.py +++ b/odoo_docker_cli.py @@ -1,7 +1,25 @@ import click +import os from jinja2 import Environment, FileSystemLoader, select_autoescape -from os import environ +from pathlib import Path + + +def check_environment_variable( + variable_name: str, + default_value: str, + help: str +) -> str: + """Check if an env var used in the template is defined and prompt for + confirmation if it isn't. + """ + return os.environ.get(variable_name, False) or click.prompt( + f"{variable_name} environment variable is not defined," + f" default value is: {default_value} . Introduce your own" + " value or press ENTER to use the default", + default=default_value, + type=str, + ) @click.command() @@ -14,6 +32,7 @@ prompt="Leave help comments in the compose file?", ) @click.option( + "-d", "--db-filter", default=".*", help="db-filter to use. Defaults to .* but you should use a more" @@ -22,16 +41,68 @@ show_default=True, ) @click.option( + "-o", # as in Odoo "--mount-upstream", default=False, help="Mount the upstream code of Odoo/OCB as a host volume " - "in ${ODOO_DOCKER_PROJECT_NAME}_upstream", + "in ${ODOO_DOCKER_UPSTREAM_HOST_PATH}. If the variable is not" + " defined, you'll be prompted for a value or confirmation to use" + " a sensible default.", is_flag=True, prompt="Mount the upstream code of Odoo/OCB as a host volume " - "in ${ODOO_DOCKER_PROJECT_NAME}_upstream", + "in ${ODOO_DOCKER_UPSTREAM_HOST_PATH}? (If the variable is not" + " defined, you'll be prompted for a value or confirmation to use" + " a sensible default)", show_default=True, ) -def compose(comments, db_filter, mount_upstream): +@click.option( + "-p", + "--pudb/--no-pudb", + default=True, + help="Expose pudb port for console debugging option.", + prompt="Expose 6899 port for pudb debugger sessions.", +) +def compose(comments, db_filter, mount_upstream, pudb): + """Generate a docker compose yaml file to run the image built from + this repository. + + Default values are oriented towards local development but you can + get a production ready compose file too. + """ + + try: + # All this crap and the check function must be doable with click: + compose_env_vars = { + "odoo_docker_project_name": { + "default": "odoo_docker", + "help": "Docker Compose project name, used as a base for" + "other defaults.", + }, + "odoo_docker_repos_host_path": { + "default": os.path.join( + os.environ["HOME"], + "." + os.environ.get( + "ODOO_DOCKER_PROJECT_NAME", + "odoo_docker" + ) + "_repos", + ), + } + } + + except KeyError: + raise RuntimeError("No $HOME env var defined.") + + # Call the check variables for prompts + processed_variables = { + variable_name: check_environment_variable( + variable_name=variable_name, + default_value=variable_props.get("default"), + help=variable_props.get("help"), + ) for variable_name, variable_props in compose_env_vars.items() + } + + # Make this a function? + # if any(lambda x: not Path(x).is_dir(), processed_variables.keys()): env = Environment( autoescape=select_autoescape(), loader=FileSystemLoader("templates"), @@ -40,15 +111,11 @@ def compose(comments, db_filter, mount_upstream): template = env.get_template("docker-compose.yml") print( template.render( - compose_project_name=environ.get( - "ODOO_DOCKER_PROJECT_NAME", - "odoo_docker" - ), + # compose_project_name=odoo_docker_project_name, comments=comments, db_filter=db_filter, + # odoo_docker_repos_host_path=odoo_docker_repos_host_path, + pudb=pudb, + **processed_variables, ) ) -# TODO: command to generate the docker compose file for local -# development. -# - Get env vars with values, get from args or use defaults in -# working directory. diff --git a/templates/docker-compose.yml b/templates/docker-compose.yml index d66402d..7c028f2 100644 --- a/templates/docker-compose.yml +++ b/templates/docker-compose.yml @@ -13,7 +13,7 @@ {% endif %} --- version: '3.8' -name: {{ compose_project_name }} +name: {{ odoo_docker_project_name }} services: db: environment: @@ -62,7 +62,9 @@ services: - DB_FILTER={{ db_filter }} image: "rubencabrera/odoo-docker:16.2.0" ports: +{% if pudb %} - "6899:6899"{% if comments %} # pudb telnet port{% endif %} +{% endif +%} - "8069:8069" - "8071:8071" - "8072:8072" @@ -85,7 +87,7 @@ volumes: # FIXME: The local paths need to exist for volumes to be mounted. # For prod environments, this might be better inside a dir in `/opt` {% endif %} - device: ${ODOO_DOCKER_REPOS_HOST_PATH:-${HOME}/.${COMPOSE_PROJECT_NAME}_repos} + device: {{ odoo_docker_repos_host_path }} name: ${COMPOSE_PROJECT_NAME}_code data_storage: name: ${COMPOSE_PROJECT_NAME}_data_storage