From 1e106d3fee4a8e33e478f67f041d4c71996c2a1c Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Wed, 3 Feb 2016 21:23:48 +0000
Subject: [PATCH] Task #9048: Added Docker package. Generate Dockerfiles from
 templates to build this branch and revision in the Docker image

---
 .gitattributes                                |  4 +-
 CMake/LofarPackageList.cmake                  |  1 +
 CMakeLists.txt                                |  1 +
 Docker/CMakeLists.txt                         | 42 ++++++++++++++
 .../{Dockerfile => Dockerfile.tmpl}           |  8 ++-
 .../{Dockerfile => Dockerfile.tmpl}           |  8 ++-
 Docker/template.in                            | 57 +++++++++++++++++++
 7 files changed, 113 insertions(+), 8 deletions(-)
 create mode 100644 Docker/CMakeLists.txt
 rename Docker/lofar-outputproc/{Dockerfile => Dockerfile.tmpl} (93%)
 rename Docker/lofar-pipeline/{Dockerfile => Dockerfile.tmpl} (94%)
 create mode 100755 Docker/template.in

diff --git a/.gitattributes b/.gitattributes
index 9b5ccd5bb7a..770d78c0436 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2349,8 +2349,8 @@ Docker/lofar-base/Dockerfile -text
 Docker/lofar-base/bashrc -text
 Docker/lofar-base/chuser.sh -text
 Docker/lofar-base/subversion_servers -text
-Docker/lofar-outputproc/Dockerfile -text
-Docker/lofar-pipeline/Dockerfile -text
+Docker/lofar-outputproc/Dockerfile.tmpl -text
+Docker/lofar-pipeline/Dockerfile.tmpl -text
 JAVA/GUI/Plotter/dist/lib/sgt.jar -text svneol=unset#unset
 JAVA/GUI/Plotter/doc/Plotter.EAP -text
 JAVA/GUI/Plotter/doc/javadoc/resources/inherit.gif -text
diff --git a/CMake/LofarPackageList.cmake b/CMake/LofarPackageList.cmake
index a4692663891..140d0210870 100644
--- a/CMake/LofarPackageList.cmake
+++ b/CMake/LofarPackageList.cmake
@@ -16,6 +16,7 @@ if(NOT DEFINED LOFAR_PACKAGE_LIST_INCLUDED)
   set(LOFAR_PACKAGE_LIST_INCLUDED TRUE)
   set(Calibration_SOURCE_DIR ${CMAKE_SOURCE_DIR}/CEP/Calibration)
   set(DP3_SOURCE_DIR ${CMAKE_SOURCE_DIR}/CEP/DP3)
+  set(Docker_SOURCE_DIR ${CMAKE_SOURCE_DIR}/Docker)
   set(GSM_SOURCE_DIR ${CMAKE_SOURCE_DIR}/CEP/GSM)
   set(Imager_SOURCE_DIR ${CMAKE_SOURCE_DIR}/CEP/Imager)
   set(LMWCommon_SOURCE_DIR ${CMAKE_SOURCE_DIR}/CEP/LMWCommon)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ccb788fe33c..ea914605bb0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,7 @@ include(LofarGeneral)
 ## ---------------------------------------------------------------------------
 include(LofarPackage)
 if(NOT DEFINED BUILD_PACKAGES)
+  lofar_add_package(Docker)
   lofar_add_package(LCS)
   lofar_add_package(CEP)
   lofar_add_package(RTCP)
diff --git a/Docker/CMakeLists.txt b/Docker/CMakeLists.txt
new file mode 100644
index 00000000000..547058cdff2
--- /dev/null
+++ b/Docker/CMakeLists.txt
@@ -0,0 +1,42 @@
+# $Id$
+
+lofar_package(Docker 1.0)
+
+include(LofarFindPackage)
+lofar_find_package(Subversion REQUIRED)
+
+# Create our template engine, using build-specific info
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/template.in
+  ${CMAKE_CURRENT_BINARY_DIR}/template @ONLY)
+
+# Directories with Dockerfile.tmpl to parse
+set(DOCKER_TEMPLATE_DIRS
+  lofar-pipeline
+  lofar-outputproc)
+
+# Convert Dockerfile.tmpl -> Dockerfile in ${DOCKER_TEMPLATE_DIRS}
+foreach(_dir ${DOCKER_TEMPLATE_DIRS})
+  set(_src ${CMAKE_CURRENT_SOURCE_DIR}/${_dir}/Dockerfile.tmpl)
+  set(_dst ${CMAKE_CURRENT_BINARY_DIR}/${_dir}_Dockerfile)
+  add_custom_command(
+    OUTPUT ${_dst}
+    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/template < ${_src} > ${_dst}
+    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/template ${_src}
+  )
+  add_custom_target(${_dir}_Dockerfile_target ALL DEPENDS ${_dst})
+
+  install(FILES
+    ${_dst}
+    DESTINATION share/docker/${_dir}
+    RENAME Dockerfile
+  )
+endforeach()
+
+# Install everything
+install(DIRECTORY
+  lofar-base
+  lofar-pipeline
+  lofar-outputproc
+  DESTINATION share/docker
+  PATTERN Dockerfile.tmpl EXCLUDE)
diff --git a/Docker/lofar-outputproc/Dockerfile b/Docker/lofar-outputproc/Dockerfile.tmpl
similarity index 93%
rename from Docker/lofar-outputproc/Dockerfile
rename to Docker/lofar-outputproc/Dockerfile.tmpl
index e59b087f8d0..d2abbda5575 100644
--- a/Docker/lofar-outputproc/Dockerfile
+++ b/Docker/lofar-outputproc/Dockerfile.tmpl
@@ -9,16 +9,18 @@ FROM lofar-base
 # *******************
 #
 
-ENV LOFAR_BRANCH=trunk
-
 # Run-time dependencies
 RUN sudo apt-get install -y liblog4cplus-1.0-4 libxml2 libboost-thread${BOOST_VERSION}.0 libboost-filesystem${BOOST_VERSION}.0 libboost-date-time${BOOST_VERSION}.0 libpng12-0 libsigc++-2.0-dev libxml++2.6-2 libboost-regex${BOOST_VERSION}.0
 
+# Tell image build information
+ENV LOFAR_BRANCH=${BRANCH_NAME} \
+    LOFAR_REVISION=${REVISION}
+
 # Install
 RUN sudo apt-get install -y subversion cmake g++ gfortran bison flex autogen liblog4cplus-dev libhdf5-dev libblitz0-dev libboost-dev libboost-python${BOOST_VERSION}-dev libxml2-dev pkg-config libpng12-dev libfftw3-dev libunittest++-dev libxml++2.6-dev libboost-filesystem${BOOST_VERSION}-dev libboost-date-time${BOOST_VERSION}-dev libboost-thread${BOOST_VERSION}-dev libboost-regex${BOOST_VERSION} binutils-dev && \
     mkdir -p ${INSTALLDIR}/lofar/build/gnu_opt && \
     cd ${INSTALLDIR}/lofar && \
-    svn --non-interactive -q --username lofar-guest --password lofar-guest co -N https://svn.astron.nl/LOFAR/${LOFAR_BRANCH} src; \
+    svn --non-interactive -q --username lofar-guest --password lofar-guest co -r ${REVISION} -N ${BRANCH_URL} src; \
     svn --non-interactive -q up src/CMake && \
     cd ${INSTALLDIR}/lofar/build/gnu_opt && cmake -DBUILD_PACKAGES=Online_OutputProc -DCMAKE_INSTALL_PREFIX=${INSTALLDIR}/lofar/ -DCASACORE_ROOT_DIR=${INSTALLDIR}/casacore/ -DLOG4CPLUS_ROOT_DIR=${INSTALLDIR}/log4cplus/ -DQPID_ROOT_DIR=/opt/qpid/ -DDAL_ROOT_DIR=${INSTALLDIR}/DAL -DUSE_OPENMP=True ${INSTALLDIR}/lofar/src/ && \
     cd ${INSTALLDIR}/lofar/build/gnu_opt && sed -i '29,31d' include/ApplCommon/PosixTime.h && \
diff --git a/Docker/lofar-pipeline/Dockerfile b/Docker/lofar-pipeline/Dockerfile.tmpl
similarity index 94%
rename from Docker/lofar-pipeline/Dockerfile
rename to Docker/lofar-pipeline/Dockerfile.tmpl
index 18ccf92aee9..4385395312d 100644
--- a/Docker/lofar-pipeline/Dockerfile
+++ b/Docker/lofar-pipeline/Dockerfile.tmpl
@@ -9,8 +9,6 @@ FROM lofar-base
 # *******************
 #
 
-ENV LOFAR_BRANCH=trunk
-
 # Run-time dependencies
 RUN sudo apt-get install -y python-xmlrunner python-scipy liblog4cplus-1.0-4 libxml2 libboost-thread${BOOST_VERSION}.0 libboost-filesystem${BOOST_VERSION}.0 libboost-date-time${BOOST_VERSION}.0 libpng12-0 libsigc++-2.0-dev libxml++2.6-2 libgsl0ldbl openssh-client libboost-regex${BOOST_VERSION}.0 && \
     sudo apt-get -y install python-pip python-dev && \
@@ -18,11 +16,15 @@ RUN sudo apt-get install -y python-xmlrunner python-scipy liblog4cplus-1.0-4 lib
     sudo apt-get -y purge python-pip python-dev && \
     sudo apt-get -y autoremove
 
+# Tell image build information
+ENV LOFAR_BRANCH=${BRANCH_NAME} \
+    LOFAR_REVISION=${REVISION}
+
 # Install
 RUN sudo apt-get install -y subversion cmake g++ gfortran bison flex liblog4cplus-dev libhdf5-dev libblitz0-dev libboost-dev libboost-python-dev python-dev libxml2-dev pkg-config libpng12-dev libfftw3-dev libunittest++-dev libxml++2.6-dev libgsl0-dev libboost-filesystem${BOOST_VERSION}-dev libboost-date-time${BOOST_VERSION}-dev libboost-thread${BOOST_VERSION}-dev libboost-regex${BOOST_VERSION} binutils-dev && \
     mkdir -p ${INSTALLDIR}/lofar/build/gnu_opt && \
     cd ${INSTALLDIR}/lofar && \
-    svn --non-interactive -q --username lofar-guest --password lofar-guest co -N https://svn.astron.nl/LOFAR/${LOFAR_BRANCH} src; \
+    svn --non-interactive -q --username lofar-guest --password lofar-guest co -r ${REVISION} -N ${BRANCH_URL} src; \
     svn --non-interactive -q up src/CMake && \
     cd ${INSTALLDIR}/lofar/build/gnu_opt && cmake -DBUILD_PACKAGES=Offline -DCMAKE_INSTALL_PREFIX=${INSTALLDIR}/lofar/ -DWCSLIB_ROOT_DIR=${INSTALLDIR}/wcslib/ -DCFITSIO_ROOT_DIR=${INSTALLDIR}/cfitsio/ -DCASAREST_ROOT_DIR=${INSTALLDIR}/casarest/ -DCASACORE_ROOT_DIR=${INSTALLDIR}/casacore/ -DLOG4CPLUS_ROOT_DIR=${INSTALLDIR}/log4cplus/ -DQPID_ROOT_DIR=/opt/qpid/ -DUSE_OPENMP=True ${INSTALLDIR}/lofar/src/ && \
     cd ${INSTALLDIR}/lofar/build/gnu_opt && sed -i '29,31d' include/ApplCommon/PosixTime.h && \
diff --git a/Docker/template.in b/Docker/template.in
new file mode 100755
index 00000000000..37b5dea9e7a
--- /dev/null
+++ b/Docker/template.in
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+#
+# Template engine for LOFAR
+#
+#   Reads stdin, replaces variables, writes stdout.
+#
+#   Variables replaced:
+#       ${BRANCH_NAME}  = Name of this branch, relative to root
+#       ${BRANCH_URL}   = Full subversion URL for this branch
+#       ${REVISION}     = SVN revision number of this checkout
+#       ${NOW}          = now in UTC (format: 2016-01-01 10:11:12)
+#
+
+# ----- BRANCH_NAME = branches/LOFAR-Task1234 -----
+# ----- BRANCH_NAME = trunk -----
+# ----- BRANCH_NAME = tags/LOFAR-Release-2_15_1 -----
+# ----- BRANCH_NAME = UNKNOWN -----
+
+# Make sure we obtain info about the project source!
+SVN_INFO=`@Subversion_SVN_EXECUTABLE@ info @PROJECT_SOURCE_DIR@`
+
+# Extract repository root, e.g. https://svn.astron.nl/LOFAR
+REPO=`echo "$SVN_INFO" | perl -ne 'print "$1" if /Repository Root: +(.+)/;'`
+
+# Extract branch URL, e.g. https://svn.astron.nl/LOFAR/branches/LOFAR-Task1234
+URL=`echo "$SVN_INFO" | perl -ne 'print "$1" if /URL: +(.+)/;'`
+
+# Extract branch name w.r.t. repository root, e.g. branches/LOFAR-Task1234
+BRANCH=`echo "$URL" | perl -ne 'print "$1" if m|^\Q'"$REPO"'\E/?(.+)|;'`
+
+# Define $BRANCH_NAME if all the above succeeded
+if [ -n "$REPO" -a -n "$BRANCH" ]; then
+  export BRANCH_NAME=${BRANCH}
+else
+  export BRANCH_NAME=UNKNOWN
+fi
+
+# ----- BRANCH_URL = https://svn.astron.nl/LOFAR/branches/LOFAR-Task1234 -----
+# ----- BRANCH_URL = https://svn.astron.nl/LOFAR/trunk -----
+# ----- BRANCH_URL = https://svn.astron.nl/LOFAR/tags/LOFAR-Release-2_15_1 -----
+
+export BRANCH_URL="$URL"
+
+# ----- REVISION = 12345 -----
+
+export REVISION=`echo "$SVN_INFO" | perl -ne 'print "$1" if /Revision: +(.+)/;'`
+
+# ----- NOW = 2016-01-01 10:11:12 -----
+
+export NOW="`date -u +'%F %T'`"
+
+# ----- Process input -----
+
+# Insert our knowledge when processing stdin -> stdout
+envsubst '$BRANCH_NAME $BRANCH_URL $REVISION $NOW'
+
-- 
GitLab