Skip to content

Commit

Permalink
feat(cli): get values from env vars and conditionally prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
rubencabrera committed Aug 14, 2024
1 parent 452c8b6 commit 2f491e8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 59 deletions.
106 changes: 55 additions & 51 deletions odoo_docker_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
import os

from jinja2 import Environment, FileSystemLoader, select_autoescape
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,
)
def mount_upstream_callback(ctx, param, value):
if param and not os.environ.get("ODOO_DOCKER_UPSTREAM_HOST_PATH"):
ctx.params["upstream_path"] = click.prompt(
"Where the main odoo/OCB code will be mounted"
" in the host",
default=os.environ.get(
"ODOO_DOCKER_UPSTREAM_HOST_PATH",
os.path.join(
os.path.expanduser("~"),
"." + ctx.params.get(
"project_name",
"odoo_docker",
) + "_upstream"
),
),
)
return value


@click.command()
Expand All @@ -43,6 +44,7 @@ def check_environment_variable(
@click.option(
"-o", # as in Odoo
"--mount-upstream",
callback=mount_upstream_callback,
default=False,
help="Mount the upstream code of Odoo/OCB as a host volume "
"in ${ODOO_DOCKER_UPSTREAM_HOST_PATH}. If the variable is not"
Expand All @@ -55,52 +57,53 @@ def check_environment_variable(
" a sensible default)",
show_default=True,
)
@click.option(
"--project-name",
default=os.environ.get("ODOO_DOCKER_PROJECT_NAME", "odoo_docker"),
help="Docker Compose project name, used as a base for"
"other defaults.",
prompt=True, # make conditional if set via env var
)
@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):
@click.option(
"--repos-path",
default=os.environ.get(
"ODOO_DOCKER_REPOS_HOST_PATH",
lambda: os.path.join(
os.path.expanduser("~"),
"." + click.get_current_context().params.get(
"project_name",
"odoo_docker"
) + "_repos"
),
),
prompt="Where the modules repos code will be mounted"
" in the host." if not os.environ.get(
"ODOO_DOCKER_REPOS_HOST_PATH"
) else False,
)
def compose(
comments,
db_filter,
mount_upstream,
project_name,
pudb,
repos_path,
upstream_path,
):
"""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(
Expand All @@ -111,11 +114,12 @@ def compose(comments, db_filter, mount_upstream, pudb):
template = env.get_template("docker-compose.yml")
print(
template.render(
# compose_project_name=odoo_docker_project_name,
comments=comments,
db_filter=db_filter,
# odoo_docker_repos_host_path=odoo_docker_repos_host_path,
mount_upstream=mount_upstream,
project_name=project_name,
pudb=pudb,
**processed_variables,
repos_host_path=repos_path,
upstream_path=upstream_path,
)
)
18 changes: 10 additions & 8 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: {{ odoo_docker_project_name }}
name: {{ project_name }}
services:
db:
environment:
Expand All @@ -32,7 +32,7 @@ services:
"CMD-SHELL",
'psql -U odoo -d postgres -h 127.0.0.1 -p 5432 -c "select 1"'
]
start_period: 5s{% if comments %} # This might be too long after first run.{% endif %}
start_period: 5s{% if comments %} # This might be too long after first run.{% endif +%}
retries: 5
image: postgres:16.2
ports:
Expand Down Expand Up @@ -87,24 +87,26 @@ 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 }}
device: {{ repos_host_path }}
name: ${COMPOSE_PROJECT_NAME}_code
data_storage:
name: ${COMPOSE_PROJECT_NAME}_data_storage
odoo_data:
name: ${COMPOSE_PROJECT_NAME}_odoo_data
{% if mount_upstream %}
upstream_volume:
{% if comments %}
{% if comments %}
# Odoo main code. This volume should only be needed for upstream
# contributions.
{% endif %}
{% endif %}
driver: local
driver_opts:
o: bind
type: none
{% if comments %}
{% if comments %}
# 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_UPSTREAM_HOST_PATH:-${HOME}/.${COMPOSE_PROJECT_NAME}_upstream}
{% endif %}
device: {{ upstream_path }}
name: ${COMPOSE_PROJECT_NAME}_upstream
{% endif %}

0 comments on commit 2f491e8

Please sign in to comment.