Skip to content

Commit

Permalink
Merge pull request #107 from dbnicholson/docker-fixes
Browse files Browse the repository at this point in the history
Docker fixes
  • Loading branch information
rtibbles authored Apr 20, 2022
2 parents a4d44af + 31f33d0 commit e37bff0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
39 changes: 21 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN dpkg --add-architecture i386 && \
libffi-dev \
libltdl-dev\
libncurses5:i386 \
libssl-dev \
libstdc++6:i386 \
libtool \
locales \
Expand Down Expand Up @@ -49,29 +50,31 @@ ENV PATH /usr/local/bin:$PATH
RUN cd /usr/local/bin && \
ln -s $(which python3) python

# install python dependencies
RUN pip install -r requirements.txt && \
useradd -lm kivy

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
locale-gen
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

USER kivy:kivy
WORKDIR /home/kivy

# make sure the build is from scratch on a per-architecture basis
ARG ARCH=$ARCH
# Install Android SDK
ENV ANDROID_HOME=/opt/android
ENV ANDROIDSDK=$ANDROID_HOME/sdk
ENV ANDROIDNDK=$ANDROIDSDK/ndk-bundle
COPY Makefile /tmp/
RUN make -C /tmp setup SDK=$ANDROIDSDK && \
rm -f /tmp/Makefile

# Initializes the directory, owned by new user. Volume mounts adopt existing permissions, etc.
RUN mkdir ~/.local

COPY --chown=kivy:kivy . .

RUN make setup
# install python dependencies
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt && \
rm -f /tmp/requirements.txt

RUN set -a; source .env; set +a
# Configure gradle for use in docker. Disable gradle's automatically
# detected rich console doesn't work in docker. Disable the gradle
# daemon since it will be stopped as soon as the container exits.
ENV GRADLE_OPTS="-Dorg.gradle.console=plain -Dorg.gradle.daemon=false"

ENTRYPOINT [ "make" ]
# Create a mount point for the build cache and make it world writable so
# that the volume can be used by an unprivileged user without additional
# setup.
RUN mkdir /cache && chmod 777 /cache

CMD [ "kolibri.apk" ]
CMD [ "make", "kolibri.apk" ]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ sdk:
yes y | $(SDK)/cmdline-tools/bin/sdkmanager "system-images;android-$(ANDROID_API);default;x86_64" --sdk_root=$(SDK)
yes y | $(SDK)/cmdline-tools/bin/sdkmanager "build-tools;30.0.3" --sdk_root=$(SDK)
yes y | $(SDK)/cmdline-tools/bin/sdkmanager "ndk;$(ANDROIDNDKVER)" --sdk_root=$(SDK)
ln -sfT ndk/$(ANDROIDNDKVER) $(SDK)/ndk-bundle
@echo "Accepting all licenses"
yes | $(SDK)/cmdline-tools/bin/sdkmanager --licenses --sdk_root=$(SDK)

Expand All @@ -142,8 +143,7 @@ setup:
$(MAKE) sdk
@echo "Make sure to set the necessary environment variables"
@echo "export ANDROIDSDK=$(SDK)"
@echo "export ANDROIDNDK=$(SDK)/ndk/$(ANDROIDNDKVER)"
@echo "ANDROIDSDK=$(SDK)\nANDROIDNDK=$(SDK)/ndk/$(ANDROIDNDKVER)" > .env
@echo "export ANDROIDNDK=$(SDK)/ndk-bundle"

clean-tools:
rm -rf ${ANDROID_HOME}
89 changes: 43 additions & 46 deletions scripts/rundocker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,48 @@

set -e

CONTAINER_HOME=/home/kivy

# Specifies the name of the docker volume used to store p4a cache
P4A_CACHE=p4a_cached_dir_$ARCH

CID_FILE=kolibri-android-app-container-id.cid.txt

# create the container to be used throughout the script
# creates a volume for reuse between builds, holding p4a's android distro
docker create -it \
--mount type=volume,src=${P4A_CACHE},dst=${CONTAINER_HOME}/.local \
--env BUILDKITE_BUILD_NUMBER \
--env P4A_RELEASE_KEYSTORE=${CONTAINER_HOME}/$(basename "${P4A_RELEASE_KEYSTORE}") \
--env P4A_RELEASE_KEYALIAS \
--env P4A_RELEASE_KEYSTORE_PASSWD \
--env P4A_RELEASE_KEYALIAS_PASSWD \
--env ARCH \
--env ANDROID_HOME=${CONTAINER_HOME}/.local/android \
--cidfile ${CID_FILE} \
android_kolibri

# Used to reference container, rather than name. For concurrency.
# Removing immediately, but could use this to check if running.
CONTAINER_ID=$(more ${CID_FILE} && rm ${CID_FILE})

echo -ne "Creating android build container, ID: ${CONTAINER_ID}"

# make sure the environment variable is defined
if [ "${P4A_RELEASE_KEYSTORE}" ]; then
# make sure the directory is valid
if [ -a ${P4A_RELEASE_KEYSTORE} ]; then
echo -e "Copying the signing key \n\t From ${P4A_RELEASE_KEYSTORE} to ${CONTAINER_ID}:${CONTAINER_HOME}"
# copy keystore to same location on the container
docker cp ${P4A_RELEASE_KEYSTORE} ${CONTAINER_ID}:${CONTAINER_HOME}
fi
SCRIPTDIR=$(realpath "$(dirname "$0")")
SRCDIR=$(dirname "$SCRIPTDIR")

BUILD_CACHE_VOLUME="kolibri-android-cache-$ARCH"
BUILD_CACHE_PATH=/cache
BUILD_UID=$(id -u)
BUILD_GID=$(id -g)

# Build array of options to pass to docker run.
RUN_OPTS=(
-it --rm

# Mount the cache volume.
--mount "type=volume,src=${BUILD_CACHE_VOLUME},dst=${BUILD_CACHE_PATH}"

# Bind mount the source directory into the container and make it the
# working dirctory.
--mount "type=bind,src=${SRCDIR},dst=${SRCDIR}"
--workdir "${SRCDIR}"

# Run as the calling user and make the cache volume the user's home
# directory so all the intermediate build outputs (e.g.,
# ~/.local/share/python-for-android and ~/.gradle) are stored.
--user "${BUILD_UID}:${BUILD_GID}"
--env HOME="${BUILD_CACHE_PATH}"

# Pass through other environment variables.
--env BUILDKITE_BUILD_NUMBER
--env P4A_RELEASE_KEYALIAS
--env P4A_RELEASE_KEYSTORE_PASSWD
--env P4A_RELEASE_KEYALIAS_PASSWD
--env ARCH
)

# If the release signing key has been specified and exists, ensure the
# path is absolute and bind mount it readonly into the container.
if [ -e "${P4A_RELEASE_KEYSTORE}" ]; then
P4A_RELEASE_KEYSTORE=$(realpath "${P4A_RELEASE_KEYSTORE}")
RUN_OPTS+=(
--env P4A_RELEASE_KEYSTORE
--volume "${P4A_RELEASE_KEYSTORE}:${P4A_RELEASE_KEYSTORE}:ro"
)
fi

# run the container, generating the apk
echo "Starting ${CONTAINER_ID}"
docker start -i ${CONTAINER_ID}

# copy the apk to our host. Handles permissions.
echo -e "Copying APK \n\t From ${CONTAINER_ID}:${CONTAINER_HOME}/dist/ to ${PWD}"
docker cp ${CONTAINER_ID}:${CONTAINER_HOME}/dist/ .

# manually remove the container afterward
echo -n "Removing "
docker rm ${CONTAINER_ID}
exec docker run "${RUN_OPTS[@]}" android_kolibri "$@"

0 comments on commit e37bff0

Please sign in to comment.