From 0f850692ea51257f8292986bac5fbd2b6de605ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Laurent?= <francois.laurent@posteo.net>
Date: Wed, 8 Jun 2022 08:25:28 +0200
Subject: [PATCH] bootstrap slightly accelerated

---
 Project.toml             |  1 -
 recipes/Dockerfile       | 51 +++++++++++++++++++++-------------------
 recipes/Dockerfile.local | 15 +++++-------
 scripts/larvatagger.sh   | 28 ++++++++++++----------
 test/precompile.jl       | 24 +++++++++++++++++++
 test/precompile.sh       | 27 +++++++++++++++++++++
 6 files changed, 100 insertions(+), 46 deletions(-)
 create mode 100644 test/precompile.jl
 create mode 100755 test/precompile.sh

diff --git a/Project.toml b/Project.toml
index 1065fe0..1b5dd3d 100644
--- a/Project.toml
+++ b/Project.toml
@@ -8,7 +8,6 @@ Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
 Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
 DocOpt = "968ba79b-81e4-546f-ab3a-2eecfa62a9db"
 Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
-JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
 JSServe = "824d6782-a2ef-11e9-3a09-e5662e0c26f9"
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
 Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
diff --git a/recipes/Dockerfile b/recipes/Dockerfile
index 86a4d58..e4f2cdf 100644
--- a/recipes/Dockerfile
+++ b/recipes/Dockerfile
@@ -1,33 +1,36 @@
-FROM ubuntu:focal
-
-RUN apt-get update &&\
-    apt-get install -y wget git &&\
-    wget https://raw.githubusercontent.com/abelsiqueira/jill/main/jill.sh &&\
-    bash jill.sh -y &&\
-    rm jill.sh
+FROM julia:1.7.3-bullseye
 
 ENV DIRNAME app
-
 ARG BRANCH=main
-RUN git clone --depth 1 --single-branch -b $BRANCH https://gitlab.pasteur.fr/nyx/larvatagger.jl $DIRNAME &&\
-    cd $DIRNAME &&\
-    julia --project=. -e 'using Pkg; Pkg.instantiate()' &&\
-    ln -s $(pwd)/scripts/larvatagger.jl /bin
+RUN apt-get update && apt-get install -y \
+    git \
+ && rm -rf /var/lib/apt/lists/* \
+ && git clone --depth 1 --single-branch -b $BRANCH https://gitlab.pasteur.fr/nyx/larvatagger.jl $DIRNAME \
+ && julia --project=$DIRNAME -e 'using Pkg; Pkg.instantiate()' \
+ && ln -s /$DIRNAME/scripts/larvatagger.jl /bin
+
+COPY test/precompile.* test/data_sample* $DIRNAME/test/
 
-COPY ./extra $DIRNAME/
+ARG TIMEZONE=UTC
+RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime \
+ && echo $TIMEZONE > /etc/timezone \
+ && $DIRNAME/test/precompile.sh --shallow \
+ && rm -f test/data_sample*
+
+COPY external $DIRNAME/
 
 ARG BACKEND
-RUN if [ "$BACKEND" = "MaggotUBA_20220418" ]; then \
-    apt-get install -y python3-pip &&\
-    python3 -m pip install poetry &&\
-    cd $DIRNAME &&\
-    git clone --depth 1 --single-branch -b 20220418 https://gitlab.pasteur.fr/nyx/MaggotUBA-adapter MaggotUBA &&\
-    cd MaggotUBA &&\
-    python3 -m poetry remove structured-temporal-convolution &&\
-    python3 -m poetry add ../structured-temporal-convolution &&\
-    python3 -m poetry install; \
+RUN if [ "$BACKEND" = "MaggotUBA/20220418" ]; then \
+    apt-get update && apt-get install -y \
+    python3-pip \
+ && python3 -m pip install poetry \
+ && cd $DIRNAME \
+ && git clone --depth 1 --single-branch -b 20220418 https://gitlab.pasteur.fr/nyx/MaggotUBA-adapter MaggotUBA \
+ && cd MaggotUBA \
+ && python3 -m poetry remove structured-temporal-convolution \
+ && python3 -m poetry add ../structured-temporal-convolution \
+ && python3 -m poetry install \
+ && rm -rf /var/lib/apt/lists/*; \
     fi
 
-WORKDIR $DIRNAME
-
 ENTRYPOINT ["larvatagger.jl"]
diff --git a/recipes/Dockerfile.local b/recipes/Dockerfile.local
index 62e6538..2e44d25 100644
--- a/recipes/Dockerfile.local
+++ b/recipes/Dockerfile.local
@@ -1,17 +1,14 @@
-FROM ubuntu:jammy
-
-RUN apt-get update &&\
-    apt-get install -y wget git &&\
-    wget https://raw.githubusercontent.com/abelsiqueira/jill/main/jill.sh &&\
-    bash jill.sh -y &&\
-    rm jill.sh
+FROM julia:1.7.3-bullseye
 
 COPY src /app/src/
 COPY scripts /app/scripts/
 COPY Project.toml Manifest.toml /app/
 
-RUN cd /app &&\
-    julia --project=. -e 'using Pkg; Pkg.instantiate()' &&\
+RUN apt-get update && apt-get install -y \
+    git \
+ && rm -rf /var/lib/apt/lists/* \
+ && cd /app \
+ && julia --project=. -e 'using Pkg; Pkg.instantiate()' \
     ln -s /app/scripts/larvatagger.jl /bin
 
 ENTRYPOINT ["larvatagger.jl"]
diff --git a/scripts/larvatagger.sh b/scripts/larvatagger.sh
index d8a4c38..f7ad157 100755
--- a/scripts/larvatagger.sh
+++ b/scripts/larvatagger.sh
@@ -12,13 +12,13 @@ if ! [ -f recipes/Dockerfile ]; then
 echo "the build command can only be run from the project root directory"
 exit 1
 fi
-mkdir -p ./extra
+mkdir -p ./external
 while ! [ -z "$1" ]; do
 if [ "$1" == "--dev" -o "$1" == "--stable" ]; then
 BUILD=$1; shift
 elif [ "$1" == "--get-backend" ]; then
-BACKEND="--build-arg BACKEND=MaggotUBA_20220418"; shift
-cd ./extra
+DOCKER_ARGS="--build-arg BACKEND=MaggotUBA/20220418 "; shift
+cd ./external
 if [ -d structured-temporal-convolution ]; then
 cd structured-temporal-convolution; git pull; cd ..
 else
@@ -27,28 +27,32 @@ fi
 cd ..
 else
 echo "argument not supported: $1"; shift
-#rm -rf ./extra
+#rm -rf ./external
 exit 1
 fi
 done
+DOCKER_ARGS="--build-arg TIMEZONE=$(cat /etc/timezone) $DOCKER_ARGS"
 if [ "$BUILD" == "--dev" ]; then
-docker build -t "${LARVATAGGER_IMAGE}:dev" -f recipes/Dockerfile.local $BACKEND .
+docker build -t "${LARVATAGGER_IMAGE}:dev" -f recipes/Dockerfile.local ${DOCKER_ARGS}.
 elif [ "$BUILD" == "--stable" ]; then
-docker build -t "$LARVATAGGER_IMAGE:stable" -f recipes/Dockerfile $BACKEND .
+docker build -t "${LARVATAGGER_IMAGE}:stable" -f recipes/Dockerfile ${DOCKER_ARGS}.
 else
-docker build -t "$LARVATAGGER_IMAGE:latest" -f recipes/Dockerfile $BACKEND --build-arg BRANCH=dev .
+docker build -t "${LARVATAGGER_IMAGE}:latest" -f recipes/Dockerfile ${DOCKER_ARGS}--build-arg BRANCH=dev .
 fi
-#rm -rf ./extra
+#rm -rf ./external
 ;;
 
 	open)
 
-ARGS="-p 9284:9284"
-if ! [ -z "$LARVATAGGER_PORT" -o "$LARVATAGGER_PORT" == "9284" ]; then
-ARGS="-p $LARVATAGGER_PORT:9284 $ARGS"
+if [ -z "$LARVATAGGER_PORT" -o "$LARVATAGGER_PORT" == "9284" ]; then
+DOCKER_ARGS="-p 9284:9284"
+TAGGER_ARGS=
+else
+DOCKER_ARGS="-p $LARVATAGGER_PORT:$LARVATAGGER_PORT"
+TAGGER_ARGS="--port=$LARVATAGGER_PORT"
 fi
 file=$1; shift
-exec docker run -iv $(pwd):/data $ARGS "$LARVATAGGER_IMAGE" open "/data/$file" $@
+exec docker run -iv $(pwd):/data $DOCKER_ARGS "$LARVATAGGER_IMAGE" open "/data/$file" $TAGGER_ARGS $@
 ;;
 
 	import)
diff --git a/test/precompile.jl b/test/precompile.jl
new file mode 100644
index 0000000..00f2210
--- /dev/null
+++ b/test/precompile.jl
@@ -0,0 +1,24 @@
+using LarvaTagger
+using JSServe: Server
+
+datafile = joinpath(@__DIR__, "data_sample")
+ispath(datafile) || exit()
+
+app = larvaeditor(datafile)
+srv = Server(app, "0.0.0.0", 9284)
+
+if isempty(ARGS)
+
+    using HTTP
+    HTTP.request(:GET, "http://127.0.0.1:9284")
+
+elseif ARGS == ["--electron"]
+
+    using Blink
+    win = Window(async=false)
+    loadurl(win, "http://127.0.0.1:9284")
+    close(win)
+
+end
+
+close(srv)
diff --git a/test/precompile.sh b/test/precompile.sh
new file mode 100755
index 0000000..4bda55d
--- /dev/null
+++ b/test/precompile.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+root=$(dirname $(dirname $0))
+if [ "$1" = "--shallow" ]; then
+shift
+echo "Project root: $root"
+julia --project=$root -e 'using Pkg; Pkg.add("HTTP")'
+echo "[$(date +%T)] Further precompiling..."
+julia --project=$root $root/test/precompile.jl
+echo "[$(date +%T)] Precompilation done"
+julia --project=$root -e 'using Pkg; Pkg.rm("HTTP")'
+else
+packages="unzip xvfb libgtk-3-0 libnss3 libxss1 libasound2"
+apt-get update
+apt-get install -y $packages
+echo "Project root: $root"
+julia --project=$root -e 'using Pkg; Pkg.add("Blink")'
+# to find out missing dependencies:
+# electron=$(julia --project=$root -e 'using Blink; println(Blink.AtomShell.electron())')
+# echo $electron
+# $electron
+echo "[$(date +%T)] Further precompiling..."
+xvfb-run julia --project=$root $root/test/precompile.jl --electron
+echo "[$(date +%T)] Precompilation done"
+julia --project=$root -e 'using Pkg; Pkg.rm("Blink")'
+apt-get autoremove -y $packages
+rm -rf /var/lib/apt/lists/*
+fi
-- 
GitLab