Skip to content

Commit

Permalink
feat: Add project management tool
Browse files Browse the repository at this point in the history
This commit replaces the Makefile with a Python CLI tool. The tool
manages the project, including building the Docker image, running the
app in a container, and running the app locally. The tool also builds
the Tailwind CSS stylesheet.

The app always runs with hot template reloading enabled.
In development mode, it also runs with hot code reloading.
  • Loading branch information
jamilraichouni committed Jan 29, 2025
1 parent e0c22bf commit 58964aa
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 24,032 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: CC0-1.0

static/css/main*.css
static/js/tailwind*.js
static/js/typography*.js
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,6 @@ package*.json

# IntelliJ project files
.idea/

# stylesheet generated by tailwindcss cli
static/css/main-*.css
63 changes: 49 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,89 @@
# SPDX-License-Identifier: Apache-2.0

FROM python:3.12-slim-bookworm
WORKDIR /app
ARG TAILWIND_CSS_VERSION
ARG TAILWIND_CSS_CLI_VERSION
ARG TAILWIND_CSS_TYPOGRAPHY_VERSION
USER root
WORKDIR /app
ENV HOME=/home
ENV PATH=$HOME/.local/bin:$PATH
ENV MODEL_ENTRYPOINT=/model
# Expose the port the app runs on
EXPOSE 8000

# install system pkgs {{{
RUN apt-get update && \
apt-get install --yes --no-install-recommends \
curl \
git \
git-lfs \
libgirepository1.0-dev \
libcairo2-dev \
gir1.2-pango-1.0 \
graphviz \
make && \
libcairo2-dev \
libgirepository1.0-dev \
npm && \
apt-get autoremove --yes && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# }}}
# copy needed app assets {{{

# copy files {{{
COPY capella_model_explorer capella_model_explorer
COPY static static
COPY templates templates
COPY Makefile pyproject.toml ./
COPY pyproject.toml ./
COPY .git .git
COPY entrypoint.sh /
# }}}
# Expose the port the app runs on
EXPOSE 8000
ENV HOME=/home
RUN chmod +x /entrypoint.sh
ENV MODEL_ENTRYPOINT=/model
# }}}

# misc setup {{{
RUN git config --global --add safe.directory /model && \
git config --global --add safe.directory /model/.git && \
chmod -R 777 /app && \
chmod -R 777 /home
# }}}

# Run as non-root user per default
USER 1000

# install tailwindcss stuff {{{
RUN npm install -D \
tailwindcss@${TAILWIND_CSS_VERSION} \
@tailwindcss/cli@${TAILWIND_CSS_CLI_VERSION} \
@tailwindcss/typography@${TAILWIND_CSS_TYPOGRAPHY_VERSION}
# }}}

# install uv {{{
RUN curl -Lo /tmp/install.sh https://astral.sh/uv/install.sh && \
chmod +x /tmp/install.sh && \
UV_NO_MODIFY_PATH=1 sh /tmp/install.sh && \
rm /tmp/install.sh
# }}}

RUN $HOME/.local/bin/uv venv && \
# install app incl. its cli, elk.js, and build stylesheet {{{
RUN uv venv && \
# install app
$HOME/.local/bin/uv pip install --no-cache-dir . && \
uv pip install --no-cache-dir . && \
# Install elk.js automatically into a persistent local cache directory
# in order to prepare the elk.js execution environment ahead of time.
$HOME/.local/bin/uv run python3 -c "from capellambse_context_diagrams import install_elk; install_elk()"
uv run python3 -c "from capellambse_context_diagrams import install_elk; install_elk()" && \
# build stylesheet
python3 -m capella_model_explorer build css
# }}}

# clean up as root {{{
USER root
RUN ln -s /home/.local/bin/uv /usr/local/bin && \
apt-get purge --remove --yes curl npm && \
apt-get autoremove --yes && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* \
rm -rf node_modules
# }}}

# Back to non-root user
USER 1000

ENTRYPOINT ["/entrypoint.sh"]
52 changes: 0 additions & 52 deletions Makefile

This file was deleted.

48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,49 @@ There are a few more use cases but we will reveal them a bit later.

# Quick start

Ensure that you enable host networking in Docker settings. This is needed to
access the app from the host machine.
Clone this repository, then build and run a Docker container:

Clone, then build and run locally with Docker:
```bash
python3 -m pip install capella-model-explorer
python3 -m capella_model_explorer build image
python3 -m capella_model_explorer run container # set port via CME_PORT env var
```

Above will start the app with a sample model which can be found here:
[In-Flight Entertainment System](https://github.com/DSD-DBS/Capella-IFE-sample)

To run the application with a custom model in a container:

```bash
docker build -t model-explorer:latest .
docker run --rm --name=cme \
-e MODEL_ENTRYPOINT='{"path": "git+https://github.com/eclipse-capella/capella","revision": "v7.0.0", "entrypoint": "samples/In-Flight Entertainment System/In-Flight Entertainment System.aird"}' \
export CME_VERSION=$(python -c 'import capella_model_explorer as cme; print(cme.__version__)')
docker run --rm \
--name capella-model-explorer \
-e MODEL_ENTRYPOINT='{"path": "git+https://github.com/DSD-DBS/Capella-IFE-sample.git","revision": "master", "entrypoint": "In-Flight Entertainment System.aird"}' \
-p 8000:8000 \
--net=host \
model-explorer
capella-model-explorer:$CME_VERSION
```

More information about the `MODEL_ENTRYPOINT` can be found in the documentation
of capellambse:

https://dsd-dbs.github.io/py-capellambse/start/specifying-models.html

If you want to be able to explore a local Capella model, and/ or live edit and
test local templates, you can mount the model and templates folders into the
container and enable live hot code reloading:
test local templates, you can mount a local model and a templates folder into
the container with automatically enabled live template reloading and rendering:

```bash
docker build -t model-explorer:latest .
docker run --rm --name=cme \
-e LIVE="1" \
-v /absolute/path/to/your/model/folder/on/host:/model \
-v "$PWD/templates:/app/templates" \
export CME_VERSION=$(python -c 'import capella_model_explorer as cme; print(cme.__version__)')
docker run --rm \
--name capella-model-explorer \
-p 8000:8000 \
--net=host \
model-explorer
-v /absolute/path/to/your/model/folder/on/host:/model \ # optional local model
-v "$(pwd)/templates:/app/templates" \
capella-model-explorer:$CME_VERSION
```

If you stop the app via CTRL+C, you must delete the container via `docker rm
cme` before you can start it again.
capella-model-explorer` before you can start it again.

Then open your browser at `http://localhost:8000/` and start exploring
your model.
Expand Down
Loading

0 comments on commit 58964aa

Please sign in to comment.