From d6a7215ba718b55012871bafa6b40bdeefa45854 Mon Sep 17 00:00:00 2001 From: konstantin Date: Thu, 10 Oct 2024 10:28:55 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20Dockerize=20Toolchain=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/docker-publish.yml | 30 ++++++++++++++++ Dockerfile | 10 ++++++ README.md | 53 +++++++++++++++++++++++++--- docker-compose.yaml | 24 ++++++++++--- env.example | 4 +-- 5 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/docker-publish.yml create mode 100644 Dockerfile diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..77ed4a0 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,30 @@ +name: Publish Docker image to Github Container Registry GHCR +on: + release: + types: + - created + +jobs: + push_to_registry: + name: Push Docker image to GHCR + runs-on: ubuntu-latest + + steps: + - name: Check out the repo + uses: actions/checkout@v4 + # with: # we don't need this + # submodules: "recursive" + - name: get version tag + run: | + VERSION=$(echo ${GITHUB_REF#refs/tags/}) + echo "VERSION=$VERSION" >> $GITHUB_ENV + - name: Log in to GHCR + # the token has repo:write and package scope and expires on 2025-10-08 + # https://github.com/Hochfrequenz/ebd_toolchain/settings/secrets/actions/GHCR_PUSH_TOKEN + run: echo "${{ secrets.GHCR_PUSH_TOKEN }}" | docker login ghcr.io -u hf-kklein --password-stdin + + - name: Build and push + run: | + docker build -t ebd_toolchain:$VERSION . + docker tag ebd_toolchain:$VERSION ghcr.io/hochfrequenz/ebd_toolchain:$VERSION + docker push ghcr.io/hochfrequenz/ebd_toolchain:$VERSION diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..602c479 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.12-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY src . + +CMD ["python", "ebd_toolchain/main.py", "-i", "/container/ebd.docx", "-o", "/container/output", "-t", "json", "-t", "dot", "-t", "svg", "-t", "puml"] diff --git a/README.md b/README.md index e2f62a2..f82cc13 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,22 @@ Diese Entscheidungsbäume sind Teil eines regulatorischen Regelwerks für die de 🇺🇸 This repository provides a Python script combining the libraries [ebdamame](https://github.com/Hochfrequenz/ebdamame) and [rebdhuhn](https://github.com/Hochfrequenz/rebdhuhn) in order to render [edi@energy](https://www.edi-energy.de) _Entscheidungsbaumdiagramme_ (EBD) as both machine-readable tables as well as corresponding graphs in `.svg` and `.uml` format. ## How to use EBD Toolchain +You can either run the toolchain via Python (requires Docker AND Python to be installed on your machine). +Or you can run the entire toolchain in a docker container (requires only Docker). -### Install both libraries from PiPy: +### Option A: Via Python + Kroki in a Docker Container + +#### Install both libraries from PiPy: ```bash pip install -r requirements.txt ``` -Further, make sure to have a local instance of [kroki](https://kroki.io) up and running via docker (localhost:8126) as described in the [rebdhuhn](https://github.com/Hochfrequenz/rebdhuhn) readme. Run the `docker-desktop` app on your local maschine and start the local kroki container via +Further, make sure to have a local instance of [kroki](https://kroki.io) up and running via docker (localhost:8125) as described in the [rebdhuhn](https://github.com/Hochfrequenz/rebdhuhn) readme. +Run the `docker-desktop` app on your local maschine and start the local kroki container via ```bash docker-compose up -d ``` -### Execute the EBD toolchain script: - +#### Execute the EBD toolchain script: + Run `main.py` using your IDE or inside a terminal session via ```bash python main.py @@ -39,6 +44,46 @@ main.py -i -o -t json -t dot -t svg ``` where `-i`, `-o` and `-t` denote the input directory path, the output directory path and the supported data format, respectively. +### Option B: Docker only (no Python required) +In this repository: +1. create an `.env` file with a structure similar to [`env.example`](env.example). +2. set the environment variables to meaningful values. +3. Create a `docker-compose.yml` with the following content: +```yaml +services: + kroki: + image: yuzutech/kroki:0.24.1 + ports: + - "8125:8000" # Expose Kroki on port 8125 for rendering diagrams + # we hardcode 8125 because of https://github.com/Hochfrequenz/rebdhuhn/issues/205 + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:8000/health" ] + interval: 10s + timeout: 5s + retries: 3 + + scrape-and-plot: + image: ghcr.io/hochfrequenz/ebd_toolchain:latest + # If you run into 'manifest unknown' during docker pull, try replacing `:latest` with `:v1.2.3`. + # where v1.2.3 is the latest version of the GHCR image, which can be found here: + # https://github.com/Hochfrequenz/ebd_toolchain/pkgs/container/ebd_toolchain + depends_on: + kroki: + condition: service_healthy + volumes: + - ${EBD_DOCX_FILE}:/container/ebd.docx + - ${OUTPUT_DIR}:/container/output + network_mode: host +``` +4. Login to GitHub Container Registry (GHCR); Use a [Personal Access Token](https://github.com/settings/tokens/new) (PAT) to login that has access to this repository and at least `read:packages` scope +```bash +docker login ghcr.io -u YOUR_GITHUB_USERNAME +``` +5. then run: +```bash +docker-compose up +``` + ## How to use this Repository on Your Machine (for development) Please follow the instructions in our diff --git a/docker-compose.yaml b/docker-compose.yaml index 266dfdc..3b71139 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,23 @@ -version: "3.8" - services: - kroki: # see https://docs.kroki.io/kroki/setup/use-docker-or-podman/#_run_multiple_kroki_containers_together + kroki: image: yuzutech/kroki:0.24.1 ports: - - "8126:8000" + - "8125:8000" + # hardcoded 8125 because: https://github.com/Hochfrequenz/rebdhuhn/issues/205 + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:8000/health" ] + interval: 10s + timeout: 5s + retries: 3 + + scrape-and-plot: + build: . + depends_on: + kroki: + condition: service_healthy + volumes: + - ${EBD_DOCX_FILE}:/container/ebd.docx + - ${OUTPUT_DIR}:/container/output + network_mode: host # Allow the container to use the host's network + # this is also a side effect of https://github.com/Hochfrequenz/rebdhuhn/issues/205 + # this prevents: requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8125): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused')) diff --git a/env.example b/env.example index f3924c6..48c1c96 100644 --- a/env.example +++ b/env.example @@ -1,2 +1,2 @@ -#Environment for Kroki -KROKI_HOST=http://localhost:8126/ +EBD_DOCX_FILE=./edi_energy_mirror/edi_energy_de/FV2410/Entscheidungsbaum-DiagrammeundCodelisten-informatorischeLesefassung3.5KonsolidierteLesefassungmitFehlerkorrekturenStand31.07.2024_20250403_20240403.docx +OUTPUT_DIR=./ebd_toolchain/machine-readable_entscheidungsbaumdiagramme/FV2404