Skip to content

Commit

Permalink
riotdocker-base: Split out build logic
Browse files Browse the repository at this point in the history
This splits out all the build logic into the bash script build.sh. This
has two advantages:

- Only a single layer is added for this Dockerfile
    - This reduces overhead, especially with the VFS storage driver
    - Still takes full advantage of de-duplication of the layers
      concept: No image is based on intermediate steps anyway
- Improves maintainability
    - Strict split of meta data (--> Dockerfile) and build commands
      (--> build.sh)
    - No need for long `cmd_a && cmd_b && cmd_c && cmd_d` stuff anymore
  • Loading branch information
maribu committed Feb 22, 2023
1 parent c26aa61 commit 706e704
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 29 deletions.
33 changes: 4 additions & 29 deletions riotdocker-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,11 @@ FROM ubuntu:jammy
LABEL maintainer="Kaspar Schleiser <[email protected]>"

RUN \
echo 'Update the package index files to latest available versions' >&2 && \
apt-get update && \
echo 'Install GCC' >&2 && \
apt-get -y --no-install-recommends install \
gcc \
git \
python3 \
python3-dev \
python3-pip \
&& \
echo 'Clean up installation files' >&2 && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
--mount=type=bind,source=build.sh,target=/root/build.sh \
--mount=type=bind,source=run.sh,target=/root/run.sh \
--mount=type=bind,source=create_user.c,target=/root/create_user.c \
cd /root && ./build.sh

# compile suid create_user binary
COPY create_user.c /tmp/create_user.c
RUN gcc -DHOMEDIR=\"/data/riotbuild\" -DUSERNAME=\"riotbuild\" /tmp/create_user.c -o /usr/local/bin/create_user \
&& chown root:root /usr/local/bin/create_user \
&& chmod u=rws,g=x,o=- /usr/local/bin/create_user \
&& rm /tmp/create_user.c

# Create working directory for mounting the RIOT sources
RUN mkdir -m 777 -p /data/riotbuild

# Set a global system-wide git user and email address
RUN git config --system user.name "riot" && \
git config --system user.email "[email protected]" && \
git config --system --add safe.directory /data/riotbuild

# Copy our entry point script (signal wrapper)
COPY run.sh /run.sh
ENTRYPOINT ["/bin/bash", "/run.sh"]

# By default, run a shell when no command is specified on the docker command line
Expand Down
112 changes: 112 additions & 0 deletions riotdocker-base/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash

# Automatically exit on error
set -e

COUNTER_STEP=0
COUNTER_SUBSTEP=0
BLUE="\e[34m"
BOLD="\e[1m"
NORMAL="\e[0m"

step() {
COUNTER_SUBSTEP=0
COUNTER_STEP=$(("$COUNTER_STEP" + 1))
printf "::group::%s" "$1"
printf "${BLUE}${BOLD}==>${NORMAL}${BOLD} Step %d:${NORMAL} %s\n" "$COUNTER_STEP" "$1"
}

endstep() {
echo "::endgroup::"
}

substep() {
COUNTER_SUBSTEP=$(("$COUNTER_SUBSTEP" + 1))
printf "::group::%s" "$1"
printf "${BLUE}${BOLD} -->${NORMAL}${BOLD} Step %d.%d:${NORMAL} %s\n" \
"$COUNTER_STEP" "$COUNTER_SUBSTEP" "$1"
}

endsubstep() {
echo "::endgroup::"
}

step_install_dev_tools() {
step "Installing development tools"

substep "Updating package index"
apt-get update
endsubstep

substep "Installing GCC"
apt-get -y --no-install-recommends install gcc
endsubstep

substep "Installing git"
apt-get -y --no-install-recommends install git
endsubstep

substep "Installing Python"
apt-get -y --no-install-recommends install \
python3 \
python3-dev \
python3-pip
endsubstep

substep "Clean up installation files"
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
endsubstep

endstep
}

step_provide_create_user_cmd() {
step "Providing create_user binary"

substep "Compiling create_user from source"
gcc -DHOMEDIR=\"/data/riotbuild\" -DUSERNAME=\"riotbuild\" create_user.c -o /usr/local/bin/create_user
endsubstep

substep "Updating file attributes of create_user"
chown root:root /usr/local/bin/create_user
chmod u=rws,g=x,o=- /usr/local/bin/create_user
endsubstep

endstep
}

step_setup_dirs() {
step "Setting up folders and files"

substep "Creating /data/riotbuild"
mkdir -m 777 -p /data/riotbuild
endsubstep

substep "Creating /run.sh"
cp run.sh /run.sh
endsubstep

endstep
}

step_setup_git() {
step "Setting up git"

substep "Configuring user and email"
git config --system user.name "riot"
git config --system user.email "[email protected]"
endsubstep

substep "Setting up safe directories"
git config --system --add safe.directory /data/riotbuild
endsubstep

endstep
}

step_install_dev_tools
step_provide_create_user_cmd
step_setup_dirs
step_setup_git
exit 0

0 comments on commit 706e704

Please sign in to comment.