From 326c481dbf3ae4b1a42bb8fac893df95b76ae6d9 Mon Sep 17 00:00:00 2001
From: Dimitar Nedev <dimitar.nedev@monetdbsolutions.com>
Date: Fri, 25 Sep 2015 11:57:46 +0200
Subject: [PATCH] Migrate to CentOS 7 and upgrade to MonetDB July2015 Also: -
 create a monetdb user/group with id 5000 - install the GLS extension -
 provide more robust database init process

---
 Dockerfile                                    | 99 ++++++++++++-------
 README.md                                     | 11 ++-
 configs/.monetdb                              |  2 +
 supervisord.ini => configs/supervisord.conf   |  0
 scripts/init-db.sh                            | 49 +++++++++
 .../set-monetdb-password.sh                   |  0
 6 files changed, 118 insertions(+), 43 deletions(-)
 create mode 100644 configs/.monetdb
 rename supervisord.ini => configs/supervisord.conf (100%)
 create mode 100644 scripts/init-db.sh
 rename set-monetdb-password.sh => scripts/set-monetdb-password.sh (100%)

diff --git a/Dockerfile b/Dockerfile
index ff4316b..3a9ad87 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,68 +1,91 @@
 ############################################################
-# Dockerfile to build MonetDB container images
-# Based on Fedora (latest)
+# Dockerfile to build MonetDB and R images
+# Based on CentOS 7
 ############################################################
-
-FROM fedora:latest
+FROM centos:7
 MAINTAINER Dimitar Nedev, dimitar.nedev@monetdbsolutions.com
 
+#######################################################
+# Expose ports
+#######################################################
+EXPOSE 50000
+
+#######################################################
+# Setup supervisord
+#######################################################
+# Install supervisor
+RUN yum install -y python-setuptools
+RUN easy_install supervisor
+# Create a log dir for the supervisor
+RUN mkdir -p /var/log/supervisor
+# Copy the config
+COPY configs/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
+
 #############################################################
 # Enables repos, update system, install packages and clean up
 #############################################################
-# Enable MonetDB repo
-RUN yum install -y https://dev.monetdb.org/downloads/Fedora/MonetDB-release-1.1-1.monetdb.noarch.rpm
+RUN yum install -y \
+    wget \
+    nano
+
+RUN wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
+RUN rpm -ivh epel-release-7-5.noarch.rpm
+
 # Update & upgrade
 RUN yum update -y && \
     yum upgrade -y
 
-# Install supervisor
-RUN yum install -y supervisor
+#############################################################
+# MonetDB installation
+#############################################################
+# Create users and groups
+RUN groupadd -g 5000 monetdb && \
+    useradd -u 5000 -g 5000 monetdb
+
+# Enable MonetDB repo
+RUN yum install -y http://dev.monetdb.org/downloads/epel/MonetDB-release-epel-1.1-1.monetdb.noarch.rpm
+
+# Update & upgrade
+RUN yum update -y && \
+    yum upgrade -y
 
 # Install MonetDB
-RUN yum install -y MonetDB-SQL-server5 MonetDB-client
+RUN yum install -y MonetDB-SQL-server5-hugeint-11.21.5
+RUN yum install -y MonetDB-client
 # Install the MonetDB/GEOM module
 RUN yum install -y MonetDB-geom-MonetDB5
 # Install MonetDB/R (R is installed as a dependency)
 RUN yum install -y MonetDB-R
+# Install MonetDB/GSL module
+RUN yum install -y MonetDB-gsl-MonetDB5
 
 # Clean up
 RUN yum -y clean all
 
-#######################################################
-# Setup supervisord
-#######################################################
-# Create a log dir for the supervisor
-RUN mkdir -p /var/log/supervisor
-# Copy the config
-COPY supervisord.ini /etc/supervisord.d/supervisord.ini
-
 #######################################################
 # Setup MonetDB
 #######################################################
-# Setup using the monetdb user
-USER monetdb
-# Start the dbfarm, create a new database, enable R integration and release it
-RUN monetdbd start /var/monetdb5/dbfarm && \
-    monetdb create db && \
-    monetdb set embedr=true db && \
-    monetdb start db && \
-    monetdb release db && \
-    monetdb stop db
-
-#######################################################
-# Helper scripts
-#######################################################
-COPY set-monetdb-password.sh /home/monetdb/set-monetdb-password.sh
-# Switch back to root for the rest
-USER root
+# Add helper scripts    
+COPY scripts/set-monetdb-password.sh /home/monetdb/set-monetdb-password.sh
 RUN chmod +x /home/monetdb/set-monetdb-password.sh
 
-#######################################################
-# Expose ports
-#######################################################
-EXPOSE 50000
+# Add a monetdb config file to avoid prompts for username/password
+# We will need this one to authenticate when running init-db.sh, as well
+COPY configs/.monetdb /home/monetdb/.monetdb
+
+# Copy the database init scripts
+COPY scripts/init-db.sh /home/monetdb/init-db.sh
+RUN chmod +x /home/monetdb/init-db.sh
+
+# Set the owner to monetdbs
+RUN chown -R monetdb:monetdb /home/monetdb
+
+# Init the db in a scipt to allow more than one process to run in the container
+# We need two: one for monetdbd and one for mserver
+# The sript will init the database with using the unpreveledged user monetdb
+RUN sh /home/monetdb/init-db.sh
 
 #######################################################
 # Startup scripts
 #######################################################
-CMD ["/usr/bin/supervisord", "-n"]
+CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
diff --git a/README.md b/README.md
index 4641e80..a70f47b 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@ monetdb-r-docker
 ===========================
 Docker container for [MonetDB with R](https://www.monetdb.org/content/embedded-r-monetdb)
 
-Based on Fedora (latest)
+Based on CentOS 7
 
 ![docker-badge](http://dockeri.co/image/monetdb/monetdb-r-docker)
 
@@ -46,13 +46,14 @@ docker build --rm -t <yourname>/monetdb-r-docker .
 
 # Details
 ## Base image
-The MonetDB image is based on the latest Fedora (at the time of image generation). There are plans to migrate to CentOS (latest) once certain system.d issue is resolved
+The MonetDB image is based on the CentOS 7. We migrated from Fedora (latest).
 ## Software
-The image includes the latest stable version (at the time of image generation, again) of:
+The image includes the latest stable version (July2015 or 11.21.5) of:
 * MonetDB
+* R module for embedded R support
+ * R
 * GEOS module
-* R integration module
-* R
+* GSL module
 
 The default database on the image has R integration enabled.
 
diff --git a/configs/.monetdb b/configs/.monetdb
new file mode 100644
index 0000000..4539760
--- /dev/null
+++ b/configs/.monetdb
@@ -0,0 +1,2 @@
+user=monetdb
+password=monetdb
diff --git a/supervisord.ini b/configs/supervisord.conf
similarity index 100%
rename from supervisord.ini
rename to configs/supervisord.conf
diff --git a/scripts/init-db.sh b/scripts/init-db.sh
new file mode 100644
index 0000000..bf73571
--- /dev/null
+++ b/scripts/init-db.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+function test_monetdb_connection() {
+  runuser -l  monetdb -c "mclient -d db -s 'SELECT 1'" &> /dev/null
+  local status=$?
+  if [ $status -ne 0 ]; then
+    return 0
+  fi
+  return 1
+}
+
+chown -R monetdb:monetdb /var/monetdb5
+cd /home/monetdb
+
+if [ ! -d "/var/monetdb5/dbfarm" ]; then
+    runuser -l  monetdb -c 'monetdbd create /var/monetdb5/dbfarm'
+else
+    echo "Existing dbfarm found in '/var/monetdb5/dbfarm'"
+fi
+runuser -l  monetdb -c 'monetdbd start /var/monetdb5/dbfarm'
+sleep 5
+if [ ! -d "/var/monetdb5/dbfarm/db" ]; then
+    runuser -l  monetdb -c 'monetdb create db && \
+        monetdb set embedr=true db && \
+        monetdb release db'
+else
+    echo "Existing database found in '/var/monetdb5/dbfarm/db'"
+fi
+runuser -l  monetdb -c 'monetdb start db'
+
+for i in {30..0}; do
+  echo 'Testing MonetDB connection ' $i
+	if test_monetdb_connection ; then
+		echo 'Waiting for MonetDB to start...'
+  	sleep 1
+  else
+    echo 'MonetDB is running'
+    break
+	fi
+done
+if [ "$i" = 0 ]; then
+	echo >&2 'MonetDB startup failed'
+	exit 1
+fi
+
+mkdir -p /var/log/monetdb
+chown -R monetdb:monetdb /var/log/monetdb
+
+echo "Initialization done"
diff --git a/set-monetdb-password.sh b/scripts/set-monetdb-password.sh
similarity index 100%
rename from set-monetdb-password.sh
rename to scripts/set-monetdb-password.sh
-- 
GitLab