From fcac599385bffe82fd457b29f14d2e887bbd90fb Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Mon, 30 Jan 2017 14:10:09 +0000
Subject: [PATCH] Task #10302: Update to latest chuser mechanism, avoiding use
 of sudo

---
 .gitattributes                    |  1 +
 Docker/lofar-pulp/Dockerfile.tmpl |  2 ++
 Docker/lofar-pulp/chuser.sh       | 26 ++++++++++++++++++++++++++
 3 files changed, 29 insertions(+)
 create mode 100755 Docker/lofar-pulp/chuser.sh

diff --git a/.gitattributes b/.gitattributes
index 2a651908c47..f60cc6e1cb6 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2375,6 +2375,7 @@ Docker/lofar-outputproc/Dockerfile.tmpl -text
 Docker/lofar-pipeline/Dockerfile.tmpl -text
 Docker/lofar-pulp/Dockerfile.tmpl -text
 Docker/lofar-pulp/bashrc -text
+Docker/lofar-pulp/chuser.sh -text
 Docker/lofar-pulp/sudoers -text
 Docker/lofar-tbbwriter/Dockerfile -text
 Docker/lofar-tbbwriter/bashrc -text
diff --git a/Docker/lofar-pulp/Dockerfile.tmpl b/Docker/lofar-pulp/Dockerfile.tmpl
index bc7297dfefa..3be362ba4e4 100644
--- a/Docker/lofar-pulp/Dockerfile.tmpl
+++ b/Docker/lofar-pulp/Dockerfile.tmpl
@@ -61,3 +61,5 @@ RUN apt-get update && apt-get install -y liblog4cplus-dev libhdf5-dev libblitz0-
     apt-get autoremove -y
 
 COPY ["bashrc", "/opt/"]
+COPY ["chuser.sh", "/usr/local/bin"]
+ENTRYPOINT ["/usr/local/bin/chuser.sh"]
diff --git a/Docker/lofar-pulp/chuser.sh b/Docker/lofar-pulp/chuser.sh
new file mode 100755
index 00000000000..46872545a87
--- /dev/null
+++ b/Docker/lofar-pulp/chuser.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+# Configure user
+if [ -z "${USER}" ]; then
+  export USER=${UID}
+fi
+
+# Create home directory
+if [ -z "${HOME}" ]; then
+  export HOME=/home/${USER}
+  mkdir -p $HOME && cd $HOME
+fi
+
+# Add user to system
+fgrep -q ":x:${UID}:" /etc/passwd || echo "${USER}:x:${UID}:${UID}::${HOME}:/bin/bash" >> /etc/passwd
+fgrep -q ":x:${UID}:" /etc/group  || echo "${USER}:x:${UID}:" >> /etc/group
+
+# Set the environment
+[ -e /opt/bashrc ] && source /opt/bashrc
+
+# Run the requested command
+if [ -z "$*" ]; then
+  exec /bin/bash
+else
+  exec "$@"
+fi
-- 
GitLab