From 78a79e69e948c66b9242530a6812f95e25aeef74 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 24 Jan 2024 01:05:37 +0100 Subject: [PATCH] preserve docker build cache on orchestra musician produce docker build speedup - git checkout produce docker cache invalidation, do it when needed - use /opt/app PATH for the two containers - use two different layers: one to do the pip requirements, the other for the rest of the sourcecode --- docker/musician.Dockerfile | 6 ++++-- docker/orchestra.Dockerfile | 27 ++++++++++++++------------- pull-repos.sh | 12 +++++++++--- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/docker/musician.Dockerfile b/docker/musician.Dockerfile index 37a5765..b539024 100644 --- a/docker/musician.Dockerfile +++ b/docker/musician.Dockerfile @@ -8,14 +8,16 @@ RUN apt update && apt-get install -y \ python3-dev \ python-is-python3 -WORKDIR /home +WORKDIR /opt/musician RUN python3 -m pip install --upgrade pip RUN pip install wheel -COPY django-musician . +COPY django-musician/requirements.txt . RUN pip install -r requirements.txt +COPY django-musician . + COPY docker/musician.entrypoint.sh . ENTRYPOINT sh ./musician.entrypoint.sh diff --git a/docker/orchestra.Dockerfile b/docker/orchestra.Dockerfile index d68d160..c7232ee 100644 --- a/docker/orchestra.Dockerfile +++ b/docker/orchestra.Dockerfile @@ -3,12 +3,12 @@ FROM debian:bullseye-slim # based on https://github.com/glic3rinu/django-orchestra/blob/master/INSTALL.md # HOW TO RUN THIS # -# 0. rsync -avhP root@109.69.8.140:/home/orchestra orchestra +# 0. rsync -avhP root@109.69.8.140:/opt/orchestra orchestra # 1. copy this Dockerfile into a dir containing orchestra, like: # $ ls # Dockerfile orchestra # 2. docker build -f orchestra . -# 3. docker rm orchestra; docker run -v /Users/maymerichgubern/orchestra/orchestra/:/home/orchestra -p 8443:443 -p 8080:80 -it --name orchestra orchestra bash +# 3. docker rm orchestra; docker run -v /Users/maymerichgubern/orchestra/orchestra/:/opt/orchestra -p 8443:443 -p 8080:80 -it --name orchestra orchestra bash # 4. sed -i "s/ALLOWED_HOSTS = .*/ALLOWED_HOSTS = ('orchestra.pangea.org', 'localhost')/" panel/settings.py # 5. service postgresql start; service uwsgi start; service nginx start # 6. goto https://localhost:8443/admin/ @@ -78,31 +78,32 @@ RUN apt clean && rm -rf /var/lib/apt/lists/* # cracklib \ # lxml +# this is to ensure django project is created on top of this working directory +WORKDIR /opt/orchestra/ + # TODO maybe from here goes to docker entrypoint? # TODO assumes that the project already exists, and in some cases that would be interesting -#COPY django-orchestra /home/orchestra -COPY django-orchestra /home/orchestra - -# this is to ensure django project is created on top of this working directory -WORKDIR /home/orchestra/ +COPY django-orchestra/requirements.txt . # TODO fix this better in the repo itself RUN pip3 install -r requirements.txt RUN pip3 install lxml==4.9.3 + +COPY django-orchestra . #RUN sed -i 's/lxml==3.3.5/lxml==4.9.3/' requirements.txt # solves "No module named 'orchestra'" RUN pip install -e . -RUN django-admin startproject panel --template=/home/orchestra/orchestra/conf/project_template/ +RUN django-admin startproject panel --template=/opt/orchestra/orchestra/conf/project_template/ RUN adduser orchestra \ && sudo adduser orchestra sudo \ && su - orchestra -ENV PATH=$PATH:/home/orchestra/django-orchestra/orchestra/bin +ENV PATH=$PATH:/opt/orchestra/django-orchestra/orchestra/bin -WORKDIR /home/orchestra/panel -COPY docker/orchestra.migrate.exp /home/orchestra/panel +WORKDIR /opt/orchestra/panel +COPY docker/orchestra.migrate.exp /opt/orchestra/panel COPY docker/orchestra.entrypoint.sh . ENTRYPOINT sh ./orchestra.entrypoint.sh @@ -114,10 +115,10 @@ ENTRYPOINT sh ./orchestra.entrypoint.sh # CMD ./manage.py runserver 0.0.0.0:9080 # EXPOSE 9080:9080 -#RUN echo /home/orchestra/django-orchestra/ > /usr/local/lib/python3.9/dist-packages/orchestra.pth +#RUN echo /opt/orchestra/django-orchestra/ > /usr/local/lib/python3.9/dist-packages/orchestra.pth # TODO move this to entrypoint, with fakedata -# && su postgres bash -c 'psql -f <(zcat /home/orchestra/orchestra_db_20230907.sql)' \ +# && su postgres bash -c 'psql -f <(zcat /opt/orchestra/orchestra_db_20230907.sql)' \ #RUN service postgresql start \ # && python3 manage.py setupnginx --user orchestra \ diff --git a/pull-repos.sh b/pull-repos.sh index b6d1641..b00f9c3 100755 --- a/pull-repos.sh +++ b/pull-repos.sh @@ -8,22 +8,28 @@ set -x # clone or pull git repo _pull() { myurl="${1}" + expected_branch="${2}" mydir_raw="$(basename "${myurl}")" mydir="${mydir_raw%.git}" - mybranch="${2}" if [ -d "${mydir}" ]; then cd "${mydir}" - git checkout "${mybranch}" + current_branch="$(git branch --show-current)" + # ensure we are in the right branch without invalidating docker build cache + # yes, even without no branch switch and no new commits + if [ ! "${current_branch}" = "${expected_branch}" ]; then + git checkout "${expected_branch}" + fi git pull cd - else - git clone -b "${mybranch}" "${myurl}" "${mydir}" + git clone -b "${expected_branch}" "${myurl}" "${mydir}" fi } main() { + figlet 'pull repositories' _pull 'https://gitea.pangea.org/pangea/django-orchestra' 'sso' _pull 'https://gitea.pangea.org/pangea/django-musician' 'sso' _pull 'git@gitea.pangea.org:trustchain-oc1-orchestral/IdHub.git' 'main'