Skip to content

Commit

Permalink
cleaned up __root__ references, fixed bugs, added small features
Browse files Browse the repository at this point in the history
- Fixed pyproject.toml dependencies
- Added model properties to encapsulate `__root__`
- Added online help for commands
- Added `show-config` command
- Added `--basedir` CLI option
- Fixed docker syntax line in base template
- Added support for SHELL in stage template
- Fixed bug with LABEL k/v pair
- Updated test suite
  • Loading branch information
eric-anderton-at-rearc committed Nov 7, 2023
1 parent ef0c5ef commit b0968ea
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 27 deletions.
25 changes: 16 additions & 9 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ on:
- push

jobs:
docs_build:
docs:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- uses: ammaraskar/sphinx-action@master
with:
docs-folder: "docs/"
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip' # caching pip dependencies
- run: pip install -r docs/requirements.txt
- run: cd docs && make html

- uses: actions/upload-artifact@v1
with:
name: DocumentationHTML
path: docs/_build/html/
- uses: actions/upload-artifact@v3
with:
name: DocumentationHTML
path: docs/build/html/
retention-days: 30
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
1. Start virtualenv: `source venv/bin/activate`
1. Install dependencies and this project in editable mode: `pip install --editable .`

To run the examples, use the
To run the examples, use the
5 changes: 4 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- Add `setup` feature for modules
- Add build profiles to control the generation of the bakefile(s)
- Add duplicate name check to config file validation
- Add duplicate name check to config file validation
- Upgrade to pydantic 2.x
- Unit tests
- Integration (CLI) tests
31 changes: 20 additions & 11 deletions docker_printer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def version_callback(value: bool):
if value:
typer.echo(__version__)
raise typer.Exit()


def base_dir_callback(value: str = None):
# Change CWD to target so path resolution works as expected
Expand Down Expand Up @@ -64,7 +64,7 @@ def _synth():
with open(dockerfile_path, "w", newline="\n") as f:
f.write(dockerfile)

for build_config in build_configs.__root__:
for build_config in build_configs.configs:
bakefile_path = base_dir() / f"docker-bake.{build_config.name}.json"
bakefile = build_config.generate_bakefile(targets)
with open(bakefile_path, "w", newline="\n") as f:
Expand All @@ -80,36 +80,42 @@ def build(name: str = "default"):
_, build_configs = _synth()

try:
config = next(cfg for cfg in build_configs.__root__ if cfg.name == name)
config = next(cfg for cfg in build_configs.configs if cfg.name == name)
except StopIteration:
names = ", ".join(set([x.name for x in build_configs.__root__]))
typer.secho(f"Error: No build config found with name '{name}'", fg=typer.colors.RED)
names = ", ".join(set([x.name for x in build_configs.configs]))
typer.secho(
f"Error: No build config found with name '{name}'", fg=typer.colors.RED
)
typer.secho(f"Valid names: {names}", fg=typer.colors.YELLOW)
typer.Exit(1)
else:
typer.echo(config.build_command)
subprocess.run(config.build_command, shell=True)


@app.command()
def show_config():
"""List the current config files and build targets."""
preload_modules()

target_config_file = targets_file()
build_config_file = builds_file()
targets = TargetCollection.parse_obj(yml_load(target_config_file))
target_configs = TargetCollection.parse_obj(yml_load(target_config_file))
build_configs = BuildConfigCollection.parse_obj(yml_load(build_config_file))

typer.secho("Config files", bold=True, fg=typer.colors.GREEN)
typer.echo(str(target_config_file))
typer.echo(str(build_config_file))

typer.secho("\nTargets", bold=True, fg=typer.colors.GREEN)
for target in targets.__root__:
typer.echo(target)
for target in target_configs.targets:
typer.echo(target.name)
for mod in target.all_modules():
typer.echo(f" {mod.name}")
typer.echo()

typer.secho("\nBuilds", bold=True, fg=typer.colors.GREEN)
for build_config in build_configs.__root__:
for build_config in build_configs.configs:
typer.echo(f"{build_config.name} {build_config.image}")


Expand All @@ -124,7 +130,10 @@ def init(
"""Initializes a new project tree."""
base_dir = path / "docker-printer"
if base_dir.exists():
typer.secho(f"Error: {base_dir} already exists, cannot initialize new project", fg=typer.colors.RED)
typer.secho(
f"Error: {base_dir} already exists, cannot initialize new project",
fg=typer.colors.RED,
)
typer.Exit(1)

base_dir.mkdir(exist_ok=False, parents=False)
Expand All @@ -138,4 +147,4 @@ def init(
*.rendered.yml
""".lstrip()
)
)
)
4 changes: 4 additions & 0 deletions docker_printer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,7 @@ def build_command(self):

class BuildConfigCollection(BaseModel):
__root__: List[BuildConfig]

@property
def configs(self):
return [t for t in self.__root__]
2 changes: 1 addition & 1 deletion docker_printer/resources/templates/base.Dockerfile.jinja2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax = docker/dockerfile:1.2
# syntax=docker/dockerfile:1

{% block pre_init %}{% endblock %}

Expand Down
6 changes: 5 additions & 1 deletion docker_printer/resources/templates/stage.Dockerfile.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM {{ base }} AS {{ name }}

{% block labels -%}
{% for key, value in labels.items() -%}
LABEL "{{ key }}"={{ value }}"
LABEL "{{ key }}"="{{ value }}"
{% endfor %}
{%- endblock %}

Expand All @@ -20,6 +20,10 @@ ENV {{ key }}="{{ value }}"
{% endfor %}
{%- endblock %}

{% if shell -%}
SHELL {{ shell|tojson|safe }}
{%- endif %}

{% block instructions -%}
{% for instr in instructions -%}
{{ instr }}
Expand Down
2 changes: 1 addition & 1 deletion examples/fastapi_app/Dockerfile.synth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax = docker/dockerfile:1.2
# syntax=docker/dockerfile:1

ARG PYTHON_VERSION=3.9
ARG BASE_OS=slim
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"pyyaml",
"jinja2",
"click",
"pydantic",
"pydantic==1.*",
"typer",
"rich"
]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ jinja2
click
pydantic==1.*
typer
rich
rich

0 comments on commit b0968ea

Please sign in to comment.