This is an automated email from the ASF dual-hosted git repository.

tn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-release.git


The following commit(s) were added to refs/heads/main by this push:
     new 486fcb2  Support multi-layer docker build to only include necessary 
data into the final image, rework Makefile scripts wrt docker
486fcb2 is described below

commit 486fcb2fc552abd1b81679d2cdc5bb6d5ec5e2db
Author: Thomas Neidhart <[email protected]>
AuthorDate: Tue Mar 18 17:37:55 2025 +0100

    Support multi-layer docker build to only include necessary data into the 
final image, rework Makefile scripts wrt docker
---
 .dockerignore        |   1 +
 Dockerfile.alpine    | 102 +++++++++++++++++++++++++++++++++------------------
 Makefile             |  18 ++++++---
 atr/config.py        |   2 +-
 atr/metadata.py      |  37 +++++++++++++------
 scripts/build        |   3 --
 scripts/poetry/build |   5 +--
 scripts/run          |   9 ++++-
 scripts/stop         |   3 ++
 start-atr.sh         |  12 ++++++
 10 files changed, 132 insertions(+), 60 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index 3cb4c14..6744e47 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -9,6 +9,7 @@
 .hypothesis/
 .mypy_cache/
 .nox/
+.poetry
 .pyre/
 .pytest_cache
 .pytype/
diff --git a/Dockerfile.alpine b/Dockerfile.alpine
index 0b4a364..2e98746 100644
--- a/Dockerfile.alpine
+++ b/Dockerfile.alpine
@@ -1,3 +1,30 @@
+FROM python:3.13.2-alpine3.21 AS builder
+
+ENV PIP_DEFAULT_TIMEOUT=100 \
+    PIP_DISABLE_PIP_VERSION_CHECK=1 \
+    PIP_NO_CACHE_DIR=1 \
+    PYTHONUNBUFFERED=1 \
+    POETRY_VERSION=2.1.1
+
+RUN apk update && \
+    apk add --no-cache \
+      make \
+      git
+
+RUN pip3 install poetry==${POETRY_VERSION}
+
+# use the same workdir as in the final image
+# as the created venv will use that path
+WORKDIR /opt/atr
+
+COPY . .
+
+RUN make sync PYTHON="$(which python3)"
+
+# generate a _version.py module from git information
+RUN make generate-version
+
+# final image
 FROM python:3.13.2-alpine3.21
 
 ARG BIND=127.0.0.1:4443
@@ -5,53 +32,58 @@ ENV BIND=${BIND}
 ARG SCRIPTS=scripts/poetry
 ENV SCRIPTS=${SCRIPTS}
 
-# gcompat is required for ruff to work
-# # go is required for syft
-# nodejs is required for pyright to work
-# openjdk8 is required for Apache RAT
-RUN apk update && apk add --no-cache \
-    curl \
-    file \
-    gcompat \
-    git \
-    gpg \
-    make \
-    nix \
-    nodejs \
-    openjdk8 \
-    uv
+ENV PYTHONDONTWRITEBYTECODE=1 \
+    PYTHONUNBUFFERED=1
 
 # Set JAVA_TOOL_OPTIONS to limit memory usage
 ENV JAVA_TOOL_OPTIONS="-XX:MaxMetaspaceSize=32m -Xmx128m -XX:+UseSerialGC 
-XX:MaxRAM=256m -XX:CompressedClassSpaceSize=16m"
 
-ENV PYTHONUNBUFFERED=1
-RUN pip3 install setuptools wheel pip-tools poetry
+# go is required for syft
+# openjdk8 is required for Apache RAT
+RUN apk update && \
+    apk add --no-cache \
+      curl \
+      file \
+      git \
+      gpg \
+      make \
+      nix \
+      openjdk8
+
 WORKDIR /opt/atr
-COPY . /opt/atr
+
+# copy app and wheels from builder
+COPY --from=builder /opt/atr/.venv ./.venv
+COPY --from=builder /opt/atr/atr ./atr
+COPY --from=builder /opt/atr/asfquart ./asfquart
+COPY --from=builder /opt/atr/migrations ./migrations
+COPY --from=builder /opt/atr/scripts ./scripts
+COPY --from=builder /opt/atr/Makefile .
+COPY --from=builder /opt/atr/alembic.ini .
+COPY --from=builder /opt/atr/start-atr.sh .
+
+RUN chmod +x ./start-atr.sh
+
+# install additional tools
+ENV RAT_VERSION=0.16.1
+
+RUN mkdir -p /opt/tools
 RUN mkdir -p /tmp/apache-rat
 WORKDIR /tmp/apache-rat
-RUN curl -L 
https://dlcdn.apache.org/creadur/apache-rat-0.16.1/apache-rat-0.16.1-bin.tar.gz 
-o apache-rat.tar.gz
+RUN curl -L 
https://dlcdn.apache.org/creadur/apache-rat-${RAT_VERSION}/apache-rat-${RAT_VERSION}-bin.tar.gz
 -o apache-rat.tar.gz
 RUN tar -xzf apache-rat.tar.gz
-RUN find apache-rat-0.16.1 -type f -name "*.jar" -exec cp {} . \;
+RUN find apache-rat-${RAT_VERSION} -type f -name "*.jar" -exec cp {} . \;
 # Rename to match expected filename if needed
-RUN [ -f apache-rat-0.16.1.jar ] || mv $(find . -maxdepth 1 -type f -name 
"apache-rat*.jar" | head -1) apache-rat-0.16.1.jar
+RUN [ -f apache-rat-${RAT_VERSION}.jar ] || mv $(find . -maxdepth 1 -type f 
-name "apache-rat*.jar" | head -1) apache-rat-${RAT_VERSION}.jar
+RUN mv apache-rat-${RAT_VERSION}.jar /opt/tools
 RUN java -version
-WORKDIR /opt/atr
+
 # TODO: We should pin the syft version
 # RUN GOPATH=/usr/local go install github.com/anchore/syft/cmd/syft@latest
 RUN curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh 
| sh -s -- -b /usr/local/bin
-RUN rm -rf .venv-*
-RUN make sync PYTHON="$(which python3)"
+
 EXPOSE 4443
 
-RUN printf '#!/bin/sh\n\
-cd /opt/atr\n\
-test -d /opt/atr/state || mkdir -p /opt/atr/state\n\
-make certs\n\
-cp /tmp/apache-rat/apache-rat-0.16.1.jar /opt/atr/state/\n\
-echo "Copied Apache RAT JAR to state directory:"\n\
-ls -la /opt/atr/state/apache-rat-0.16.1.jar\n\
-exec make serve BIND=${BIND}\n\
-' > /usr/local/bin/start-atr.sh
-RUN chmod +x /usr/local/bin/start-atr.sh
-CMD ["/usr/local/bin/start-atr.sh"]
+WORKDIR /opt/atr
+
+CMD ["./start-atr.sh"]
diff --git a/Makefile b/Makefile
index 450f68f..97b87ac 100644
--- a/Makefile
+++ b/Makefile
@@ -1,14 +1,16 @@
-.PHONY: build build-alpine-ubuntu certs check docs report run serve sync 
sync-dev
+.PHONY: build build-alpine build-ubuntu certs check docs generate-version 
report run stop serve sync sync-dev
 
 BIND ?= 127.0.0.1:8080
 PYTHON ?= $(which python3)
 SCRIPTS ?= scripts/poetry
 
-build:
-       scripts/build
+build: build-alpine
 
-build-alpine-ubuntu:
-       $(SCRIPTS)/build
+build-alpine:
+       $(SCRIPTS)/build Dockerfile.alpine
+
+build-ubuntu:
+       $(SCRIPTS)/build Dockerfile.ubuntu
 
 certs:
        if test ! -f state/cert.pem || test ! -f state/key.pem; \
@@ -23,12 +25,18 @@ docs:
        do cmark "$$fn" > "$${fn%.md}.html"; \
        done
 
+generate-version:
+       $(SCRIPTS)/run python atr/metadata.py > atr/_version.py
+
 report:
        @echo SCRIPTS = $(SCRIPTS)
 
 run:
        scripts/run
 
+stop:
+       scripts/stop
+
 serve:
        $(SCRIPTS)/run hypercorn --bind $(BIND) --keyfile key.pem --certfile 
cert.pem atr.server:app --debug --reload
 
diff --git a/atr/config.py b/atr/config.py
index ff663e5..bd522d3 100644
--- a/atr/config.py
+++ b/atr/config.py
@@ -46,7 +46,7 @@ class AppConfig:
     SQLITE_DB_PATH = decouple.config("SQLITE_DB_PATH", default="/atr.db")
 
     # Apache RAT configuration
-    APACHE_RAT_JAR_PATH = decouple.config("APACHE_RAT_JAR_PATH", 
default="state/apache-rat-0.16.1.jar")
+    APACHE_RAT_JAR_PATH = decouple.config("APACHE_RAT_JAR_PATH", 
default="/opt/tools/apache-rat-0.16.1.jar")
     # Maximum size limit for archive extraction
     MAX_EXTRACT_SIZE: int = decouple.config("MAX_EXTRACT_SIZE", default=2 * 
_GB, cast=int)
     # Chunk size for reading files during extraction
diff --git a/atr/metadata.py b/atr/metadata.py
index 9e1d846..72c0841 100644
--- a/atr/metadata.py
+++ b/atr/metadata.py
@@ -15,10 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-import os
 
-
-def _get_development_version() -> tuple[str, str] | None:
+def _get_version_from_git() -> tuple[str, str] | None:
     """Returns the version when within a development environment."""
 
     try:
@@ -29,10 +27,8 @@ def _get_development_version() -> tuple[str, str] | None:
         return None
 
     try:
-        from pathlib import Path
-
         # We start in state/, so we need to go up one level
-        version = Version.from_git(path=Path(".."))
+        version = Version.from_git()
         if version.distance > 0:
             # The development version number should reflect the next release 
that is going to be cut,
             # indicating how many commits have already going into that since 
the last release.
@@ -54,15 +50,32 @@ def _get_development_version() -> tuple[str, str] | None:
         return None
 
 
-def _get_version_from_env() -> tuple[str, str | None]:
-    """Returns the version from an environment variable."""
+def _get_version_from_version_module() -> tuple[str, str] | None:
+    """Returns the version from _version module if it exists."""
+
+    try:
+        from atr._version import ATR_COMMIT, ATR_VERSION  # pyright: ignore 
[reportMissingImports]
 
-    # Use the commit where dunamai was added by default
-    # TODO: Use a better default value
-    return os.environ.get("VERSION", "undefined"), os.environ.get("COMMIT", 
"4e5bff1")
+        return ATR_VERSION, ATR_COMMIT
+    except ImportError:
+        return None
+
+
+def _get_undefined() -> tuple[str, str]:
+    return "undefined", "undefined"
 
 
 # Try to determine the version from a development environment first.
 # If this fails, try to get it from environment variables that are set when 
building a docker image.
 # We don't use __version__ and __commit__ as these are not reserved words in 
Python
-version, commit = _get_development_version() or _get_version_from_env()
+version, commit = _get_version_from_git() or 
_get_version_from_version_module() or _get_undefined()
+
+
+if __name__ == "__main__":
+    """Will output version / commit info from git tags if available."""
+    version, commit = _get_version_from_git() or _get_undefined()
+
+    print(f"""
+ATR_VERSION = '{version}'
+ATR_COMMIT = '{commit}'
+""")
diff --git a/scripts/build b/scripts/build
deleted file mode 100755
index 8fcb780..0000000
--- a/scripts/build
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-set -eu
-docker build -t tooling-trusted-release --build-arg BIND=0.0.0.0:4443 -f 
Dockerfile.alpine .
diff --git a/scripts/poetry/build b/scripts/poetry/build
index d28e415..8b5e04d 100755
--- a/scripts/poetry/build
+++ b/scripts/poetry/build
@@ -1,5 +1,4 @@
 #!/bin/sh
-# Just to check that the containers build successfully
 set -eu
-docker build -t tooling-trusted-release -f Dockerfile.alpine .
-docker build -t tooling-trusted-release -f Dockerfile.ubuntu .
+DOCKERFILE="${1:-Dockerfile.alpine}"
+docker build -t tooling-trusted-release --build-arg BIND=0.0.0.0:4443 -f 
${DOCKERFILE} .
diff --git a/scripts/run b/scripts/run
index 4537c66..c3c5aa0 100755
--- a/scripts/run
+++ b/scripts/run
@@ -1,2 +1,9 @@
 #!/bin/sh
-sudo docker run --rm -d -v 
"$HOME/tooling-trusted-release/state:/opt/atr/state" --network host 
tooling-trusted-release
+
+if [ -d "${HOME}/tooling-trusted-release" ]; then
+  MOUNT="${HOME}/tooling-trusted-release/state"
+else
+  MOUNT="./state"
+fi
+
+docker run --rm -d -v "${MOUNT}:/opt/atr/state" --network host --name 
tooling-trusted-release tooling-trusted-release
diff --git a/scripts/stop b/scripts/stop
new file mode 100755
index 0000000..8598b19
--- /dev/null
+++ b/scripts/stop
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+docker stop tooling-trusted-release || true
diff --git a/start-atr.sh b/start-atr.sh
new file mode 100644
index 0000000..d9f346e
--- /dev/null
+++ b/start-atr.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+set -e
+
+source .venv/bin/activate
+
+test -d /opt/atr/state || mkdir -p /opt/atr/state
+
+if [ ! -f state/cert.pem ] || [ ! -f state/key.pem ]; then
+       python3 scripts/generate-certificates
+fi
+
+exec hypercorn --bind ${BIND} --keyfile key.pem --certfile cert.pem 
atr.server:app


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to