From e1a65ff94b74bc81baf08905613db8ea0fbdb8e2 Mon Sep 17 00:00:00 2001
From: Stefano Alberto Russo <stefano.russo@gmail.com>
Date: Tue, 28 Sep 2021 21:51:05 +0200
Subject: [PATCH] Added the entrypoint base container.

---
 base/build.sh                 |   3 +-
 base/entrypoint/Dockerfile    |  25 +++++++
 base/entrypoint/build.sh      |   2 +
 base/entrypoint/entrypoint.sh | 124 ++++++++++++++++++++++++++++++++++
 base/entrypoint/pull.sh       |  11 +++
 base/entrypoint/push.sh       |   2 +
 base/entrypoint/tag.sh        |   2 +
 7 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100644 base/entrypoint/Dockerfile
 create mode 100755 base/entrypoint/build.sh
 create mode 100644 base/entrypoint/entrypoint.sh
 create mode 100755 base/entrypoint/pull.sh
 create mode 100755 base/entrypoint/push.sh
 create mode 100755 base/entrypoint/tag.sh

diff --git a/base/build.sh b/base/build.sh
index b875725..5e1d680 100755
--- a/base/build.sh
+++ b/base/build.sh
@@ -5,6 +5,7 @@ set -e
 cd system && ./build.sh && cd ..
 cd extras && ./build.sh && cd ..
 cd astro && ./build.sh && cd ..
+cd entrypoint && ./build.sh && cd ..
 
 # Use the astro image as lofar base
-docker tag lofarit_base_astro lofarit_base
+docker tag lofarit_base_entrypoint lofarit_base
diff --git a/base/entrypoint/Dockerfile b/base/entrypoint/Dockerfile
new file mode 100644
index 0000000..e43f636
--- /dev/null
+++ b/base/entrypoint/Dockerfile
@@ -0,0 +1,25 @@
+FROM lofarit_base_astro:latest
+MAINTAINER Giuliano Taffoni <giuliano.taffoni@inaf.it>
+ENV CONTAINER_NAME='lofarit_base_entrypoint'
+
+#=============================
+# Switch to root for install
+#=============================
+USER root
+
+
+# Copy entrypoint
+COPY entrypoint.sh /
+
+# Give right permissions
+RUN chmod 755 /entrypoint.sh
+
+# Set entrypoint
+ENTRYPOINT ["/entrypoint.sh"]
+
+#=============================
+# Switch to metauser
+#=============================
+
+USER metauser
+
diff --git a/base/entrypoint/build.sh b/base/entrypoint/build.sh
new file mode 100755
index 0000000..590dd82
--- /dev/null
+++ b/base/entrypoint/build.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+docker build ./ -t lofarit_base_entrypoint
diff --git a/base/entrypoint/entrypoint.sh b/base/entrypoint/entrypoint.sh
new file mode 100644
index 0000000..1196598
--- /dev/null
+++ b/base/entrypoint/entrypoint.sh
@@ -0,0 +1,124 @@
+#!/bin/bash
+
+# Exit on any error. More complex stuff could be done in future
+# (see https://stackoverflow.com/questions/4381618/exit-a-script-on-error)
+set -e
+
+
+if [ "x$SAFE_MODE" == "xTrue" ]; then
+
+    echo ""
+    echo "[INFO] Not executing entrypoint as we are in safe mode, just opening a Bash shell."
+    exec /bin/bash
+
+else
+
+    echo ""
+    echo "[INFO] Executing entrypoint..."
+    
+    if [ "x$GUI" == "xTrue" ]; then
+        if [ "x$BASE_PORT" == "x" ]; then
+            echo "[INFO] No task base port set, will set noVNC port 8590 and VNC port 5900 with desktop id \"0\""  
+        else 
+            echo "[INFO] Task base port set, will set noVNC port $BASE_PORT and noVNC port $(($BASE_PORT+1)) with desktop id \"$(($BASE_PORT-5900+1))\""
+        fi
+    fi
+    
+    #---------------------
+    #   Setup home
+    #---------------------
+
+    if [ -f "/home/metauser/.initialized" ]; then
+        :
+    else
+        echo "[INFO] Setting up home"
+        mkdir -p /home/metauser
+
+        # Copy over vanilla home contents
+        for x in /metauser_home_vanilla/* /metauser_home_vanilla/.[!.]* /metauser_home_vanilla/..?*; do
+            if [ -e "$x" ]; then cp -a "$x" /home/metauser/; fi
+        done
+        
+        # Mark as initialized
+        touch /home/metauser/.initialized
+    fi
+    
+
+    #---------------------
+    #   Save env
+    #---------------------
+    echo "[INFO] Dumping env"
+    
+    # Save env vars for later usage (e.g. ssh)
+    
+    env | \
+    while read env_var; do
+      if [[ $env_var == HOME\=* ]]; then
+          : # Skip HOME var
+      elif [[ $env_var == PWD\=* ]]; then
+          : # Skip PWD var
+      else
+          echo "export $env_var" >> /tmp/env.sh
+      fi
+    done
+    
+    #---------------------
+    #   VNC Password
+    #---------------------
+    if [ "x$GUI" == "xTrue" ]; then
+        if [ "x$AUTH_PASS" != "x" ]; then
+            echo "[INFO] Setting up VNC password..."
+            mkdir -p /home/metauser/.vnc
+            /opt/tigervnc/usr/bin/vncpasswd -f <<< $AUTH_PASS > /home/metauser/.vnc/passwd
+            chmod 600 /home/metauser/.vnc/passwd
+            export VNC_AUTH=True
+        else
+            echo "[INFO] Not setting up any VNC password"
+                
+        fi
+    fi
+    
+    echo "[INFO] Creating /tmp/metauserhome to be used as metauser home"
+    mkdir /tmp/metauserhome
+    
+    echo "[INFO] Initializing /tmp/metauserhome with configuration files"
+    cp -aT /metauser_home_vanilla /tmp/metauserhome
+    
+    echo "[INFO] Moving to /home/metauser and setting as home"
+    cd /home/metauser
+    export HOME=/home/metauser
+    
+    echo "[INFO] Setting new prompt @$CONTAINER_NAME container"
+    echo 'export PS1="${debian_chroot:+($debian_chroot)}\u@$CONTAINER_NAME@\h:\w\$ "' >> /tmp/metauserhome/.bashrc
+
+    echo "[INFO] Sourcing env in /opt/lofar/init.sh..."
+    source /opt/lofar/init.sh
+          
+    # Set entrypoint command
+    if [ "x$@" == "x" ]; then
+        COMMAND="/bin/bash"
+    else
+        COMMAND="$@"
+    fi
+    
+
+    # Start!
+    echo -n "[INFO] Will execute entrypoint command: "
+    echo $COMMAND
+    echo ""
+    echo "=============================================================="
+    echo ""
+    echo "      Welcome to the LOFAR-IT $CONTAINER_NAME container!"
+    echo ""
+    echo "=============================================================="
+    echo ""
+    echo "You are now in /home/metauser with write access as user \"$(whoami)\"."
+    echo ""
+    echo "Remember that contents inside this container, unless stored"
+    echo "on a persistent volume mounted from you host machine, will"
+    echo "be wiped out when exiting the container."
+    echo ""
+    
+    exec $COMMAND
+
+fi
diff --git a/base/entrypoint/pull.sh b/base/entrypoint/pull.sh
new file mode 100755
index 0000000..4eb8d22
--- /dev/null
+++ b/base/entrypoint/pull.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+docker pull git.ia2.inaf.it:5050/lofarit/containers/lofarit_base_entrypoint
+
+if [ $? -eq 0 ] 
+then 
+  echo ""
+  echo "Pulled lofarit/containers/lofarit_base_entrypoint. To use it for building other images in this repository, retag it as:"
+  echo " $ docker tag git.ia2.inaf.it:5050/lofarit/containers/lofarit_base_entrypoint lofarit_base_entrypoint"
+  echo ""
+fi
+
diff --git a/base/entrypoint/push.sh b/base/entrypoint/push.sh
new file mode 100755
index 0000000..33a6636
--- /dev/null
+++ b/base/entrypoint/push.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+docker push git.ia2.inaf.it:5050/lofarit/containers/lofarit_base_entrypoint
diff --git a/base/entrypoint/tag.sh b/base/entrypoint/tag.sh
new file mode 100755
index 0000000..42fe214
--- /dev/null
+++ b/base/entrypoint/tag.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+docker tag lofarit_base_entrypoint git.ia2.inaf.it:5050/lofarit/containers/lofarit_base_entrypoint
-- 
GitLab