From e76bea06013af723164213c122121b50930aaa70 Mon Sep 17 00:00:00 2001
From: Vincent Massol <vincent@massol.net>
Date: Fri, 24 Feb 2017 14:55:42 +0100
Subject: [PATCH] XDOCKER-15: Reorganize directory structure to remove the need
 for branches * Implement a simple Gradle build to generate the various
 versions/variants.

---
 8/mysql-tomcat/.dockerignore        |   1 -
 9/mysql-tomcat/.dockerignore        |   1 -
 build.gradle                        |  59 ++++++++++++++++
 template/.env                       |   7 ++
 template/Dockerfile                 |  93 +++++++++++++++++++++++++
 template/docker-compose.yml         |  72 ++++++++++++++++++++
 template/mysql/xwiki.cnf            |  11 +++
 template/tomcat/setenv.sh           |   1 +
 template/xwiki/docker-entrypoint.sh |  85 +++++++++++++++++++++++
 template/xwiki/hibernate.cfg.xml    | 101 ++++++++++++++++++++++++++++
 10 files changed, 429 insertions(+), 2 deletions(-)
 delete mode 100644 8/mysql-tomcat/.dockerignore
 delete mode 100644 9/mysql-tomcat/.dockerignore
 create mode 100644 build.gradle
 create mode 100644 template/.env
 create mode 100644 template/Dockerfile
 create mode 100644 template/docker-compose.yml
 create mode 100644 template/mysql/xwiki.cnf
 create mode 100755 template/tomcat/setenv.sh
 create mode 100755 template/xwiki/docker-entrypoint.sh
 create mode 100644 template/xwiki/hibernate.cfg.xml

diff --git a/8/mysql-tomcat/.dockerignore b/8/mysql-tomcat/.dockerignore
deleted file mode 100644
index f4df651..0000000
--- a/8/mysql-tomcat/.dockerignore
+++ /dev/null
@@ -1 +0,0 @@
-docker-compose-using.yml
diff --git a/9/mysql-tomcat/.dockerignore b/9/mysql-tomcat/.dockerignore
deleted file mode 100644
index f4df651..0000000
--- a/9/mysql-tomcat/.dockerignore
+++ /dev/null
@@ -1 +0,0 @@
-docker-compose-using.yml
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..9acbab6
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,59 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+import org.apache.tools.ant.filters.ReplaceTokens
+
+// Run this Gradle build with 'gradle' to generate the various versions and variants from the template directory.
+// Whenever a new version of XWiki is out, update this file to update the token values, run gradle and commit the
+// result.
+//
+// Note: As a consequence only update the template files and never the generated files!
+
+defaultTasks 'generate'
+
+def dockerVersions = ['8', '9']
+def dockerVariants = ['mysql-tomcat']
+
+def tokens = [
+    '8' : [
+        xwikiVersion: '8.4.4',
+        xwikiSha256: 'b414edb4527e3d8b27c40a8c3f2f09423980de7963207b7dc89da71d14e7fb23'
+    ],
+    '9' : [
+        xwikiVersion: '9.0',
+        xwikiSha256: 'faaca2aa1ade07448be944feb39db22131accfe82658463abfd55f93f859cc25'
+    ]
+]
+
+task generate() {
+    doLast {
+        // Copy the template for all versions and variants
+        dockerVersions.each() { version ->
+            dockerVariants.each() { variant ->
+                copy {
+                    from 'template'
+                    into "${version}/${variant}"
+                    include '**/*'
+                    filter(ReplaceTokens, tokens: tokens[version])
+                    filteringCharset = 'UTF-8'
+                }
+            }
+        }
+    }
+}
diff --git a/template/.env b/template/.env
new file mode 100644
index 0000000..b5b614d
--- /dev/null
+++ b/template/.env
@@ -0,0 +1,7 @@
+# Default environment values
+XWIKI_VERSION=@xwikiVersion@
+MYSQL_DRIVER_VERSION=5.1.38
+MYSQL_USER=xwiki
+MYSQL_PASSWORD=xwiki
+MYSQL_DATABASE=xwiki
+MYSQL_ROOT_PASSWORD=xwiki
diff --git a/template/Dockerfile b/template/Dockerfile
new file mode 100644
index 0000000..ca41aac
--- /dev/null
+++ b/template/Dockerfile
@@ -0,0 +1,93 @@
+# ---------------------------------------------------------------------------
+# See the NOTICE file distributed with this work for additional
+# information regarding copyright ownership.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of
+# the License, or (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this software; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+# ---------------------------------------------------------------------------
+FROM tomcat:8-jre8
+
+MAINTAINER Vincent Massol <vincent@massol.net>
+
+# Note: when using docker-compose, the ENV values below are overridden from the .env file.
+
+# Install LibreOffice + other tools
+RUN apt-get update && \
+  apt-get --no-install-recommends -y install \
+    curl \
+    libreoffice \
+    unzip \
+    libmysql-java && \
+  rm -rf /var/lib/apt/lists/*
+
+# Install XWiki as the ROOT webapp context in Tomcat
+# Create the Tomcat temporary directory
+# Configure the XWiki permanent directory
+ENV XWIKI_VERSION=@xwikiVersion@
+ENV XWIKI_URL_PREFIX "http://maven.xwiki.org/releases/org/xwiki/enterprise/xwiki-enterprise-web/${XWIKI_VERSION}"
+# Note: To compute the sha256, download the binary and issue:
+# - Unix: sha256sum <binary name>
+# - Mac: shasum --algorithm 25 <binary name>
+ENV XWIKI_DOWNLOAD_SHA256 @xwikiSha256@
+RUN rm -rf /usr/local/tomcat/webapps/* && \
+  mkdir -p /usr/local/tomcat/temp && \
+  mkdir -p /usr/local/xwiki/data && \
+  curl -fSL "${XWIKI_URL_PREFIX}/xwiki-enterprise-web-${XWIKI_VERSION}.war" -o xwiki.war && \
+  echo "$XWIKI_DOWNLOAD_SHA256 xwiki.war" | sha256sum -c - && \
+  unzip -d /usr/local/tomcat/webapps/ROOT xwiki.war && \
+  rm -f xwiki.war
+
+# Copy the MySQL JDBC driver in the XWiki webapp
+RUN cp /usr/share/java/mysql-connector-java-*.jar /usr/local/tomcat/webapps/ROOT/WEB-INF/lib/
+
+# Configure Tomcat. For example set the memory for the Tomcat JVM since the default value is too small for XWiki
+COPY tomcat/setenv.sh /usr/local/tomcat/bin/
+
+# Setup the XWiki Hibernate configuration
+ENV MYSQL_DATABASE=xwiki
+COPY xwiki/hibernate.cfg.xml /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+
+# Set a specific distribution id in XWiki for this docker packaging.
+RUN sed -i 's/<id>org.xwiki.enterprise:xwiki-enterprise-web/<id>org.xwiki.enterprise:xwiki-enterprise-docker/' \
+    /usr/local/tomcat/webapps/ROOT/META-INF/extension.xed
+
+# Add scripts required to make changes to XWiki configuration files at execution time
+# Note: we don't run CHMOD since 1) it's not required since the executabe bit is already set in git and 2) running
+# CHMOD after a COPY will sometimes fail, depending on different host-specific factors (especially on AUFS).
+COPY xwiki/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
+
+# Make the XWiki directory (the permanent directory is included in it) persist on the host (so that it's not recreated
+# across runs)
+VOLUME /usr/local/xwiki
+
+# At this point the image is done and what remains below are the runtime configuration used by the user to configure
+# the container that will be created out of the image. Namely the user can override some environment variables with
+#   docker run -e "var1=val1" -e "var2=val2" ...
+# The supported environment variables that can be overridden are:
+# - MYSQL_USER: the name of the user configured for XWiki in the DB. Default is "xwiki". This is used to configure
+#               xwiki's hibernate.cfg.xml file.
+# - MYSQL_PASSWORD: the password for the user configured for XWiki in the DB. Default is "xwiki". This is used to
+#                   configure xwiki's hibernate.cfg.xml file.
+# Example:
+#   docker run -it -e "MYSQL_USER=xwiki" -e "MYSQL_PASSWORD=xwiki" <imagename>
+
+# Starts XWiki by starting Tomcat. All options passed to "docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]"
+# are also passed to docker-entrypoint.sh. If "xwiki" is passed then XWiki will be configured the first time the
+# container executes and Tomcat will be started. If some other parameter is passed then it'll be executed to comply
+# with best practices defined at https://github.com/docker-library/official-images#consistency.
+ENV MYSQL_USER=xwiki \
+    MYSQL_PASSWORD=xwiki
+ENTRYPOINT ["docker-entrypoint.sh"]
+CMD ["xwiki"]
diff --git a/template/docker-compose.yml b/template/docker-compose.yml
new file mode 100644
index 0000000..c7bd535
--- /dev/null
+++ b/template/docker-compose.yml
@@ -0,0 +1,72 @@
+# ---------------------------------------------------------------------------
+# See the NOTICE file distributed with this work for additional
+# information regarding copyright ownership.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of
+# the License, or (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this software; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+# ---------------------------------------------------------------------------
+version: '2'
+networks:
+  bridge:
+    driver: bridge
+services:
+  # The container that runs XWiki + Tomcat
+  web:
+    build: .
+    container_name: xwiki
+    depends_on:
+      - db
+    ports:
+      - "8080:8080"
+    # Default values defined in .env file.
+    # The MYSQL_USER/MYSQL_PASSWORD/MYSQL_DATABASE/DB_CONTAINER_NAME variables are used in the hibernate.cfg.xml file.
+    environment:
+      - XWIKI_VERSION=${XWIKI_VERSION}
+      - MYSQL_DRIVER_VERSION=${MYSQL_DRIVER_VERSION}
+      - MYSQL_USER=${MYSQL_USER}
+      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
+      - MYSQL_DATABASE=${MYSQL_DATABASE}
+      - DB_CONTAINER_NAME=xwiki-mysql
+    # Provide a name instead of an auto-generated id for xwiki data (the permanent directory in included in it)
+    # configured in the Dockerfile, to make it simpler to identify in 'docker volume ls'.
+    volumes:
+      - xwiki-data:/usr/local/xwiki
+    networks:
+      - bridge
+  # The container that runs MySQL
+  db:
+    image: "mysql:5.7"
+    container_name: xwiki-mysql
+    # - We provide a xwiki.cnf file in order to configure the mysql db to support UTF8 and be case-insensitive
+    # We have to do it here since we use an existing image and that's how this image allows customizations.
+    # See https://hub.docker.com/_/mysql/ for more details.
+    # - Provide a name instead of an auto-generated id for the mysql data, to make it simpler to identify in
+    # 'docker volume ls'
+    volumes:
+      - ./mysql/xwiki.cnf:/etc/mysql/conf.d/xwiki.cnf
+      - mysql-data:/var/lib/mysql
+    # Configure the MySQL database and create a user with provided name/password.
+    # See https://hub.docker.com/_/mysql/ for more details.
+    # Default values defined in .env file.
+    environment:
+      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
+      - MYSQL_USER=${MYSQL_USER}
+      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
+      - MYSQL_DATABASE=${MYSQL_DATABASE}
+    networks:
+      - bridge
+volumes:
+  mysql-data: {}
+  xwiki-data: {}
diff --git a/template/mysql/xwiki.cnf b/template/mysql/xwiki.cnf
new file mode 100644
index 0000000..73e4844
--- /dev/null
+++ b/template/mysql/xwiki.cnf
@@ -0,0 +1,11 @@
+[client]
+default-character-set = utf8
+
+[mysqld]
+character-set-server = utf8
+collation-server = utf8_bin
+explicit_defaults_for_timestamp = 1
+
+[mysql]
+default-character-set = utf8
+
diff --git a/template/tomcat/setenv.sh b/template/tomcat/setenv.sh
new file mode 100755
index 0000000..9e4977b
--- /dev/null
+++ b/template/tomcat/setenv.sh
@@ -0,0 +1 @@
+export CATALINA_OPTS="-Xmx1024m"
diff --git a/template/xwiki/docker-entrypoint.sh b/template/xwiki/docker-entrypoint.sh
new file mode 100755
index 0000000..586f4ab
--- /dev/null
+++ b/template/xwiki/docker-entrypoint.sh
@@ -0,0 +1,85 @@
+#!/bin/bash
+# ---------------------------------------------------------------------------
+# See the NOTICE file distributed with this work for additional
+# information regarding copyright ownership.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of
+# the License, or (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this software; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+# ---------------------------------------------------------------------------
+
+set -e
+
+function first_start() {
+  configure
+  touch /usr/local/xwiki/.first_start_completed
+}
+
+# $1 - the path to xwiki.[cfg|properties]
+# $2 - the setting/property to set
+# $3 - the new value
+function xwiki_replace() {
+  sed -i s~"\#\? \?$2 \?=.*"~"$2=$3"~g "$1"
+}
+
+# $1 - the setting/property to set
+# $2 - the new value
+function xwiki_set_cfg() {
+  xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.cfg "$1" "$2"
+}
+
+# $1 - the setting/property to set
+# $2 - the new value
+function xwiki_set_properties() {
+  xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "$1" "$2"
+}
+
+function configure() {
+  echo 'Configuring XWiki...'
+  sed -i "s/replacemysqluser/${MYSQL_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  sed -i "s/replacemysqlpassword/${MYSQL_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  sed -i "s/replacemysqlcontainername/${DB_CONTAINER_NAME:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  sed -i "s/replacemysqldatabase/${MYSQL_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+
+  echo '  Using filesystem-based attachments...'
+  xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
+  xwiki_set_cfg 'xwiki.store.attachment.versioning.hint' 'file'
+  xwiki_set_cfg 'xwiki.store.attachment.recyclebin.hint' 'file'
+  echo '  Generating authentication validation and encryption keys...'
+  xwiki_set_cfg 'xwiki.authentication.validationKey' "$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)"
+  xwiki_set_cfg 'xwiki.authentication.encryptionKey' "$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)"
+
+  echo '  Setting permanent directory...'
+  xwiki_set_properties 'environment.permanentDirectory' '/usr/local/xwiki/data'
+  echo '  Configure libreoffice...'
+  xwiki_set_properties 'openoffice.autoStart' 'true'
+}
+
+# This if will check if the first argument is a flag but only works if all arguments require a hyphenated flag
+# -v; -SL; -f arg; etc will work, but not arg1 arg2
+if [ "${1:0:1}" = '-' ]; then
+    set -- xwiki "$@"
+fi
+
+# Check for the expected command
+if [ "$1" = 'xwiki' ]; then
+  if [[ ! -f /usr/local/xwiki/.first_start_completed ]]; then
+    first_start
+  fi
+  shift
+  set -- catalina.sh run "$@"
+fi
+
+# Else default to run whatever the user wanted like "bash"
+exec "$@"
diff --git a/template/xwiki/hibernate.cfg.xml b/template/xwiki/hibernate.cfg.xml
new file mode 100644
index 0000000..bdd419b
--- /dev/null
+++ b/template/xwiki/hibernate.cfg.xml
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+-->
+
+<!DOCTYPE hibernate-configuration PUBLIC
+  "-//Hibernate/Hibernate Configuration DTD//EN"
+  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+  <session-factory>
+
+    <!-- Please refer to the installation guide on
+         http://platform.xwiki.org/xwiki/bin/view/AdminGuide/Installation for configuring your
+         database. You'll need to do 2 things:
+         1) Copy your database driver JAR in WEB-INF/lib or in some shared lib directory
+         2) Uncomment the properties below for your specific DB (and comment the default
+            database configuration if it doesn't match your DB)
+    -->
+
+    <!-- Generic parameters common to all Databases -->
+
+    <property name="show_sql">false</property>
+    <property name="use_outer_join">true</property>
+
+    <!-- Without it, some queries fail in MS SQL. XWiki doesn't need scrollable result sets, anyway. -->
+    <property name="jdbc.use_scrollable_resultset">false</property>
+
+    <!-- DBCP Connection Pooling configuration. Only some properties are shown. All available properties can be found
+         at http://commons.apache.org/proper/commons-dbcp/configuration.html
+    -->
+    <property name="dbcp.defaultAutoCommit">false</property>
+    <property name="dbcp.maxTotal">50</property>
+    <property name="dbcp.maxIdle">5</property>
+    <property name="dbcp.maxWaitMillis">30000</property>
+    <property name="connection.provider_class">com.xpn.xwiki.store.DBCPConnectionProvider</property>
+
+    <!-- Setting "dbcp.poolPreparedStatements" to true and "dbcp.maxOpenPreparedStatements" will tell DBCP to cache
+         Prepared Statements (it's off by default). Note that for backward compatibility the "dbcp.ps.maxActive" is also
+         supported and when set it'll set "dbcp.poolPreparedStatements" to true and "dbcp.maxOpenPreparedStatements" to
+         value of "dbcp.ps.maxActive".
+
+         Note 1: When using HSQLDB for example, it's important to NOT cache prepared statements because HSQLDB
+         Prepared Statements (PS) contain the schema on which they were initially created and thus when switching
+         schema if the same PS is reused it'll execute on the wrong schema! Since HSQLDB does internally cache
+         prepared statement there's no performance loss by not caching Prepared Statements at the DBCP level.
+         See http://jira.xwiki.org/browse/XWIKI-1740.
+         Thus we recommend not turning on this configuration for HSQLDB unless you know what you're doing :)
+
+         Note 2: The same applies to PostGreSQL.
+    -->
+
+    <!-- BoneCP Connection Pooling configuration.
+    <property name="bonecp.idleMaxAgeInMinutes">240</property>
+    <property name="bonecp.idleConnectionTestPeriodInMinutes">60</property>
+    <property name="bonecp.partitionCount">3</property>
+    <property name="bonecp.acquireIncrement">10</property>
+    <property name="bonecp.maxConnectionsPerPartition">60</property>
+    <property name="bonecp.minConnectionsPerPartition">20</property>
+    <property name="bonecp.statementsCacheSize">50</property>
+    <property name="bonecp.releaseHelperThreads">3</property>
+    <property name="connection.provider_class">com.xpn.xwiki.store.DBCPConnectionProvider</property>
+    -->
+
+    <!-- MySQL configuration.
+         Uncomment if you want to use MySQL and comment out other database configurations.
+         Notes:
+           - if you want the main wiki database to be different than "xwiki"
+             you will also have to set the property xwiki.db in xwiki.cfg file
+    -->
+    <property name="connection.url">jdbc:mysql://replacemysqlcontainername/replacemysqldatabase?useSSL=false</property>
+    <property name="connection.username">replacemysqluser</property>
+    <property name="connection.password">replacemysqlpassword</property>
+    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
+    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
+    <property name="dbcp.poolPreparedStatements">true</property>
+    <property name="dbcp.maxOpenPreparedStatements">20</property>
+    <mapping resource="xwiki.hbm.xml"/>
+    <mapping resource="feeds.hbm.xml"/>
+    <mapping resource="activitystream.hbm.xml"/>
+    <mapping resource="instance.hbm.xml"/>
+    <mapping resource="mailsender.hbm.xml"/>
+
+  </session-factory>
+</hibernate-configuration>
-- 
GitLab