From 8b05891b78ca1d714639029320741f4f0809a537 Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Mon, 2 Mar 2020 08:09:53 +0100
Subject: [PATCH] Add a development docker build image

The produced image contains the required tools to build a fairly reasonable
environment.  The image is built and run in order to integrate smoothly the
developer's environment.

Obviously a proper installation of Docker is required.
---
 CONTRIBUTING.md      | 12 ++++++-
 tools/docker-pugs.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100755 tools/docker-pugs.sh

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 1cf5b7bfb..156e50f34 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -13,7 +13,7 @@
 
 ### Running tests
 Tests are built automatically and are run typing
-> `make test`
+> `make; make test`
 
 **all tests should be running correctly before any merge request**
 
@@ -26,6 +26,16 @@ Preferably, one should install [gcovr](http://www.gcovr.com) and build `pugs` sp
 
 However coverage is computed at each `push` by the `gitlab-ci`.
 
+----
+## Update build environment using [Docker](http://www.docker.com)
+This is the easiest way to keep your environment update to build `pugs`.
+
+Running the [docker-pugs.sh](tools/docker-pugs.sh) script creates the image and
+runs it in interactive mode. The image will runs the user's permissions and his
+home directory is mounted.
+
+**keep in mind that the produced executable will only run inside docker**
+
 ----
 ## Coding
 
diff --git a/tools/docker-pugs.sh b/tools/docker-pugs.sh
new file mode 100755
index 000000000..af54df67c
--- /dev/null
+++ b/tools/docker-pugs.sh
@@ -0,0 +1,76 @@
+#! /bin/sh
+
+DOCKER=$(command -v docker 2>/dev/null)
+
+if [ "${DOCKER}" = "" ]
+then
+    echo Could not find Docker on your system. Check your installation.
+    exit 1
+fi
+
+echo "Using docker: ${DOCKER}"
+
+USABLE_DOCKER=$(${DOCKER} info >/dev/null 2>&1 && echo yes)
+
+if [ "${USABLE_DOCKER}" != "yes" ]
+then
+   echo "################### ABORTING ######################"
+   echo "Cannot use Docker!"
+   echo " - check that Docker server is running"
+   echo " - check that you have permissions to use it"
+   echo "   (usually user must belong to the 'docker' group)"
+   echo "###################################################"
+
+   exit 1
+fi
+
+USER=$(id -un)
+USER_ID=$(id -u)
+USER_GID=$(id -g)
+DOCKER_HOSTNAME="$(hostname)-docker"
+
+DOCKERFILE_DIR=/tmp/${USER}/pugs.docker
+mkdir -p ${DOCKERFILE_DIR}
+
+DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile"
+
+cat > ${DOCKERFILE} <<EOF
+FROM ubuntu:bionic
+
+ENV USER="${USER}" USER_ID="${USER_ID}" USER_GID="${USER_GID}" HOSTNAME="${DOCKER_HOSTNAME}"
+
+RUN echo "${DOCKER_HOSTNAME}" > /etc/hostname
+RUN groupadd --gid "${USER_GID}" "${USER}"
+RUN useradd --uid "${USER_ID}" --gid "${USER_GID}" --create-home --shell /bin/bash "${USER}"
+
+RUN apt-get update && apt-get -y upgrade && apt-get -y remove g++ gcc  && apt-get -y install cmake git make lcov bc gnupg gnupg2 gnupg1 wget
+
+RUN echo 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main' > /etc/apt/sources.list.d/backports.list
+RUN wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
+
+RUN apt-get update && apt-get -y upgrade && apt-get -y install clang-8 clang-tools-8 clang-8-doc libclang-common-8-dev libclang-8-dev libclang1-8 clang-format-8 python-clang-8
+
+RUN apt-get -y install libparmetis-dev sudo
+RUN apt-get clean
+
+RUN rm /usr/bin/cc
+RUN echo "${USER} ALL=(ALL:ALL) NOPASSWD:ALL" > "/etc/sudoers.d/${USER}"
+
+RUN ln -s /usr/bin/clang-format-8 /usr/bin/clang-format
+
+ENV CC="clang-8" CXX="clang++-8"
+
+EOF
+
+if [ -e "${DOCKERFILE}" ]
+then
+    echo "Successfully built: ${DOCKERFILE}"
+else
+    echo "Aborting: unable to build ${DOCKERFILE}"
+    exit 1
+fi
+
+IMAGE_NAME="pugs-docker-${USER}"
+${DOCKER} build -t "${IMAGE_NAME}:latest" ${DOCKERFILE_DIR}
+
+${DOCKER} run --volume=${HOME}:${HOME} -w $(pwd) --user ${USER_ID}:${USER_GID} -ti --entrypoint /bin/bash --hostname="${DOCKER_HOSTNAME}" "${IMAGE_NAME}"
-- 
GitLab