Skip to content

Commit

Permalink
feat(cli): check env vars and prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
rubencabrera committed Aug 13, 2024
1 parent f6aa061 commit 452c8b6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
91 changes: 79 additions & 12 deletions odoo_docker_cli.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"
Expand All @@ -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"),
Expand All @@ -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.
6 changes: 4 additions & 2 deletions templates/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% endif %}
---
version: '3.8'
name: {{ compose_project_name }}
name: {{ odoo_docker_project_name }}
services:
db:
environment:
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 452c8b6

Please sign in to comment.