From de3cd0a17a285afa0dd783ec11024c992f4aa3ff Mon Sep 17 00:00:00 2001
From: startxfr <clarue@startx.fr>
Date: Sat, 29 Nov 2014 04:11:44 +0100
Subject: =?UTF-8?q?modif=20pour=20mise=20en=20place=20avanc=C3=A9=20de=20m?=
 =?UTF-8?q?ariadb?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 OS/Centos/Dockerfile               |   3 +-
 OS/Fedora/Dockerfile               |   3 +-
 README.md                          |  76 ++++++++--------
 Services/mariadb/Dockerfile        |  22 +++--
 Services/mariadb/README.md         |   7 +-
 Services/mariadb/mariadb_run.sh    |  56 ------------
 Services/mariadb/sx/mariadb.sh     | 174 +++++++++++++++++++++++++++++++++++++
 Services/mariadb/sx/mariadb_run.sh |  22 +++++
 8 files changed, 258 insertions(+), 105 deletions(-)
 delete mode 100644 Services/mariadb/mariadb_run.sh
 create mode 100644 Services/mariadb/sx/mariadb.sh
 create mode 100644 Services/mariadb/sx/mariadb_run.sh

diff --git a/OS/Centos/Dockerfile b/OS/Centos/Dockerfile
index 70b83d9..28df09d 100644
--- a/OS/Centos/Dockerfile
+++ b/OS/Centos/Dockerfile
@@ -1,7 +1,8 @@
 FROM centos:latest
 MAINTAINER Christophe LARUE <dev@startx.fr>
 
+USER root
 # Install minimal packages
-RUN yum -y install deltarpm pwgen tar \
+RUN yum -y install deltarpm pwgen tar psmisc \
     && yum -y update \
     && yum clean all
diff --git a/OS/Fedora/Dockerfile b/OS/Fedora/Dockerfile
index 0570fd1..b100398 100644
--- a/OS/Fedora/Dockerfile
+++ b/OS/Fedora/Dockerfile
@@ -1,7 +1,8 @@
 FROM fedora:latest
 MAINTAINER Christophe LARUE <dev@startx.fr>
 
+USER root
 # Install minimal packages
-RUN yum -y install deltarpm pwgen tar \
+RUN yum -y install deltarpm pwgen tar psmisc \
     && yum -y update \
     && yum clean all
diff --git a/README.md b/README.md
index bf84dc0..a64c099 100644
--- a/README.md
+++ b/README.md
@@ -5,58 +5,58 @@ Docker container images all based on red hat like distributions. Actually you ca
 ## Running container from docker registry images
 
 ### Operating Systems
-| OS      | Command |
-|---------|-----|
-| Fedora Core | `docker run -it --name="fedora" startx/fedora bash` | 
-| CentOS | `docker run -it --name="centos" startx/centos bash` |
+| OS                  | Command                                                              |
+|---------------------|----------------------------------------------------------------------|
+| Fedora Core         | `docker run -it --name="fedora" startx/fedora bash`                  | 
+| CentOS              | `docker run -it --name="centos" startx/centos bash`                  |
 
 ### Services
-| Services      | Command |
-|---------|-----|
-| **Apache** | `docker run -d -p 80:80 --name="apache" startx/sv-apache` | 
-| **MariaDB** | `docker run -d -p 3306:3306 --name="mariadb" startx/sv-mariadb` | 
-| **Memcache** | `docker run -d -p 11211:11211 --name="memcache" startx/sv-memcache` | 
-| **MongoDB** | `docker run -d -p 27017:27017 --name="mongo" startx/sv-mongo` | 
-| **nodejs** | `docker run -d -p 8000:8000 --name="nodejs" startx/sv-nodejs` | 
-| **ooconv** | `docker run -d -p XXX:XXX --name="ooconv" startx/sv-ooconv` | 
-| **php** | `docker run -d -p 80:80 --name="php" startx/sv-php` | 
-| **postgreSQL** | `docker run -d -p 5432:5432 --name="postgres" startx/sv-postgres` | 
-| **SSH** | `docker run -d -p 22:22 --name="ssh" startx/sv-ssh` | 
+| Services            | Command                                                              |
+|---------------------|----------------------------------------------------------------------|
+| **Apache**          | `docker run -d -p 80:80 --name="apache" startx/sv-apache`            | 
+| **MariaDB**         | `docker run -d -p 3306:3306 --name="mariadb" startx/sv-mariadb`      | 
+| **Memcache**        | `docker run -d -p 11211:11211 --name="memcache" startx/sv-memcache`  | 
+| **MongoDB**         | `docker run -d -p 27017:27017 --name="mongo" startx/sv-mongo`        | 
+| **nodejs**          | `docker run -d -p 8000:8000 --name="nodejs" startx/sv-nodejs`        | 
+| **ooconv**          | `docker run -d -p XXX:XXX --name="ooconv" startx/sv-ooconv`          | 
+| **php**             | `docker run -d -p 80:80 --name="php" startx/sv-php`                  | 
+| **postgreSQL**      | `docker run -d -p 5432:5432 --name="postgres" startx/sv-postgres`    | 
+| **SSH**             | `docker run -d -p 22:22 --name="ssh" startx/sv-ssh`                  | 
 
 ### Applications
-| Applications      | Command |
-|---------|-----|
-| **PhpMyAdmin** | `docker run -d -p 80:80 --name="pma" startx/app-pma` | 
-| **RockMongo** | `docker run -d -p 80:80 --name="rockmongo" startx/app-rockmongo` | 
+| Applications        | Command                                                              |
+|---------------------|----------------------------------------------------------------------|
+| **PhpMyAdmin**      | `docker run -d -p 80:80 --name="pma" startx/app-pma`                 | 
+| **RockMongo**       | `docker run -d -p 80:80 --name="rockmongo" startx/app-rockmongo`     | 
 
 ## Flavors (tags) for Red Hat like container's
 
 This repository offer various flavour of Red Hat like distribution. Tag `:latest` is actually pointing to fedora core 20
 
 ### Operating Systems
-| OS      | Docker Hub repository | Flavour 1 | Flavour 2 | Flavour 3 |
-|---------|-----------------------|---------|-------|-------|
-| Fedora Core | `startx/fedora` | `:latest` | `:20` | `:21` | 
-| CentOS | `startx/centos` | `:6` | `:7` |  |
+| OS            | Docker Hub repository          | Flavour                |
+|---------------|--------------------------------|------------------------|
+| Fedora Core   | `startx/fedora`                | `:latest` `:20` `:21`  | 
+| CentOS        | `startx/centos`                | `:6` `:7`              |
 
 ### Services
-| Service | Docker Hub repository | latest | Fedora | Fedora | CentOS | CentOS |
-|---------|-----------------------|---------|-------|-------|----------|----------|
-| Apache | `startx/sv-apache` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| MariaDB | `startx/sv-mariadb` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| Memcache | `startx/sv-memcache` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| MongoDB | `startx/sv-mongo` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| nodejs | `startx/sv-nodejs` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| ooconv | `startx/sv-ooconv` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| php | `startx/sv-php` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| postgreSQL | `startx/sv-postgres` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| SSH | `startx/sv-ssh` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
+| Service    | Docker Hub repository | Fedora                    | CentOS                |
+|------------|-----------------------|---------------------------|-----------------------|
+| Apache     | `startx/sv-apache`    | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| MariaDB    | `startx/sv-mariadb`   | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| Memcache   | `startx/sv-memcache`  | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| MongoDB    | `startx/sv-mongo`     | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| nodejs     | `startx/sv-nodejs`    | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| ooconv     | `startx/sv-ooconv`    | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| php        | `startx/sv-php`       | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| postgreSQL | `startx/sv-postgres`  | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| SSH        | `startx/sv-ssh`       | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
 
 ### Applications
-| Service | Docker Hub repository | latest | Fedora | Fedora | CentOS | CentOS |
-|---------|-----------------------|---------|-------|-------|----------|----------|
-| PhpMyAdmin | `startx/sv-pma` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
-| RockMongo | `startx/sv-rockmongo` | `:latest` | `:fc20` | `:fc21` | `:centos6` | `:centos7` |
+| Service    | Docker Hub repository | Fedora                    | CentOS                |
+|------------|-----------------------|---------------------------|-----------------------|
+| PhpMyAdmin | `startx/sv-pma`       | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
+| RockMongo  | `startx/sv-rockmongo` | `:latest` `:fc20` `:fc21` | `:centos6` `:centos7` |
 
 ### Running sample container's
 
diff --git a/Services/mariadb/Dockerfile b/Services/mariadb/Dockerfile
index 1bdf0ea..9140c96 100644
--- a/Services/mariadb/Dockerfile
+++ b/Services/mariadb/Dockerfile
@@ -1,22 +1,30 @@
 FROM startx/fedora
 MAINTAINER Christophe LARUE <dev@startx.fr>
 
-COPY mariadb_run.sh /bin/
+COPY sx/* /sx/
 RUN yum -y install \
     mariadb-libs \
     mariadb-server \
     mariadb \
+        \
+    psmisc \
+        \
     && yum clean all \
     && mkdir -p /var/log/mysql \
+    && mkdir -p /sx \
     && touch /var/log/mysql/.keep /var/lib/mysql/.keep \
     && chown -R mysql:mysql /var/log/mysql /var/lib/mysql \
-    && chmod ug+rx /bin/mariadb_*
+    && chmod ug+rx /sx/mariadb* \
+    && chown -R mysql:mysql /sx/mariadb*
 
-VOLUME ["/var/lib/mysql", "/var/log/mysql"]
 USER mysql
-
 EXPOSE 3306
+VOLUME ["/var/lib/mysql", "/var/log/mysql"]
+
+# if you wan't to set root password, otherwise auto-generated
+# see docker logs <containerId>
+ENV mysql_newadminpwd "newRootPassword"
+
 
-#CMD ["/usr/libexec/mysqld"]
-CMD ["/bin/mariadb_run.sh"]
-ONBUILD CMD ["/bin/mariadb_run.sh"]
\ No newline at end of file
+#ENDPOINT ["/sx/mariadb_run.sh"]
+CMD ["/sx/mariadb_run.sh"]
\ No newline at end of file
diff --git a/Services/mariadb/README.md b/Services/mariadb/README.md
index d9436cd..0d4dad8 100644
--- a/Services/mariadb/README.md
+++ b/Services/mariadb/README.md
@@ -4,8 +4,11 @@ This container run mariadb on fedora server.
 ## Running from docker registry
 
 	docker run -d -p 3306:3306 --name="mariadb" startx/sv-mariadb
-	when linked to another container
-	docker run -d --name="mariadb" startx/sv-mariadb
+	# when used with a volume container
+	docker run -d -v /var/lib/mysql -v /var/log/mysql --name mariadb-data startx/sv-mariadb
+	docker run -d --volumes-from mariadb-data -p 3306:3306 --name="mariadb" startx/sv-mariadb
+	# when used in a linked container
+	docker run -d -p 3306:3306 --name="mariadb" startx/sv-mariadb
 	docker run -d --name="php" --link mariadb:mariadb startx/sv-php
 
 ## Build and run from local Dockerfile
diff --git a/Services/mariadb/mariadb_run.sh b/Services/mariadb/mariadb_run.sh
deleted file mode 100644
index b5b1421..0000000
--- a/Services/mariadb/mariadb_run.sh
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/bin/bash
-ln -s /dev/stderr /var/log/mysql/mysqld.log
-if [ ! -f /var/lib/mysql/.created ]; then
-  function wait_for_mysqld_start {
-    for i in {1..30}; do
-      if echo 'select 1' | mysql -u root > /dev/null 2>&1; then
-        return 0
-      fi
-      sleep 1
-    done
-
-    echo "MariaDB did not start in time"
-    exit 1
-  }
-
-
-
-  password=${DB_PASSWORD:-password}
-  dbname=${DB_NAME:-master}
-
-  /usr/bin/mysql_install_db -u mysql
-
-  /usr/libexec/mysqld &
-  pid=$!
-
-  wait_for_mysqld_start
-
-  echo "Creating database $dbname ..."
-
-  sql=$(cat <<SQL
-  drop database if exists test;
-  create database \`$dbname\`
-     DEFAULT CHARACTER SET utf8 DEFAULT
-     COLLATE utf8_general_ci;
-SQL
-)
-  echo $sql | mysql -u root
-
-  #delete from user;
-
-  sql=$(cat <<SQL
-    delete from user where user='';
-    grant all on *.* to 'mysql'@'localhost' identified by '$password' with grant option;
-    grant all on *.* to 'mysql'@'%' identified by '$password' with grant option;
-    flush privileges;
-SQL
-)
-  echo $sql | mysql -u root mysql
-
-  touch /var/lib/mysql/.created
-  kill -TERM $pid
-
-  echo "Starting mysqld ..."
-fi
-
-exec /usr/libexec/mysqld
\ No newline at end of file
diff --git a/Services/mariadb/sx/mariadb.sh b/Services/mariadb/sx/mariadb.sh
new file mode 100644
index 0000000..af6124b
--- /dev/null
+++ b/Services/mariadb/sx/mariadb.sh
@@ -0,0 +1,174 @@
+#!/bin/bash
+
+export TERM=dumb
+export logfile="/var/log/mysql/mysqld.log"
+
+# Begin configuration before starting daemonized process
+# redirect mysql.log to /dev/stderr
+function begin_config {
+    echo "=> Begin mariadb configuration for host $HOSTNAME"
+    ln -s /dev/stderr $logfile
+    if [ "$(ls -1 /var/lib/mysql | wc -l)" -le "3"  ]; then
+        echo "=> directory /var/lib/mysql is empty, start mysql installation ..."
+        install_db
+    else 
+        echo "=> data found in /var/lib/mysql, skip mysql installation ..."
+    fi;
+    if [ ! -f /var/lib/mysql/mysql.sock ]; then
+        echo "=> mysqld is not running, start server ..."
+        start_server
+    else
+        echo "=> mysqld is already running ..."
+    fi;
+    update_rootuser
+}
+
+# End configuration process just before starting daemon
+# stop output of mysql.log to /dev/stderr and create mysql.log file
+function end_config {
+    stop_server
+    rm $logfile
+    touch $logfile
+    echo "=> End mariadb configuration ..."
+}
+
+# Preform installation of database structure. Must be used when no
+# database is already set
+function install_db {
+    echo "===> Installing mariadb databases ..."
+    mysql_install_db -u mysql > /dev/null 2>&1
+    chown -R mysql:mysql /var/lib/mysql
+}
+
+# Start the mysqld server in background. Used to perform config
+# against the database structure such as user creation
+function start_server {
+    echo "===> Starting mariadb server ..."
+    /usr/bin/mysqld_safe > /dev/null 2>&1 &
+    sleep 8
+}
+
+# Stop the mysqld server running in background. 
+function stop_server {
+    echo "===> Stopping mariadb server ..."
+    killall mysqld mysqld_safe
+    sleep 8
+}
+
+# Start the mysqld server as a deamon and execute it inside 
+# the running shell
+function start_daemon {
+    echo "=> Starting mariadb daemon ..."
+    exec /usr/libexec/mysqld
+}
+
+# Set new root password and grant permissions to all databases
+function update_rootuser {
+    if [ "$mysql_newadminpwd" = "" ]; then
+       export mysql_newadminpwd=$(pwgen 13 1);
+    fi
+    local n=$mysql_newadminpwd;
+    echo "===> Update root user password and permission"
+    mysqladmin -u root password $n
+    mysql -u root -p$n -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$n' WITH GRANT OPTION; FLUSH PRIVILEGES;"
+    unset mysql_newadminpwd;
+    export MARIADB_ROOTPWD=$n;
+    echo "========================================================================";
+    echo "You can now connect to this MariaDB Server using the following credentials:";
+    echo " ";
+    echo "        user type : administrator";
+    echo "        username  : root";
+    echo "        password  : $n";
+    echo " ";
+    echo "        mysql -u root -p$n";
+    echo "========================================================================";
+    echo " ";
+    return 0
+}
+
+# Find all sqlfiles in /tmp/ and import then using admin user
+function import_sqlfiles {
+    local filedir=$1; local p=$MARIADB_ROOTPWD;
+    if [ "$(ls -1 $filedir | wc -l)" -ge "1"  ]; then
+        echo "=> Found SQL files to import ..."
+        for filename in "$filedir"; do
+                import_sqlfile $filename
+        done;
+    fi;
+    return 0
+}
+
+# Find all sqlfiles in /tmp/ and import then using admin user
+function import_sqlfile {
+    local filename=$1; local p=$MARIADB_ROOTPWD;
+    if [ -f "$filename" ]; then
+        echo "===> Importing sql file : $filename"
+        mysql -u root -p$p < $filename
+    else 
+        echo "====> Could not find sql file $filename. Skip import..."
+    fi;
+    return 0
+}
+
+# Set new root password and grant permissions to all databases
+function create_userdb {
+    local userdb="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD;
+    if [ "$pass" = "" ]; then
+       local pass=$(pwgen 13 1);
+    fi
+    echo "===> Create new user $userdb with database $userdb"
+    mysql -u root -p$p -e "CREATE USER '$userdb'@'%';SET PASSWORD FOR '$userdb'@'%' = PASSWORD('$pass');\
+                           CREATE USER '$userdb'@'localhost';SET PASSWORD FOR '$userdb'@'localhost' = PASSWORD('$pass');\
+                           DROP DATABASE IF EXISTS $userdb; \
+                           CREATE DATABASE $userdb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; \
+                           GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'%' IDENTIFIED BY '$pass'; \
+                           GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'localhost' IDENTIFIED BY '$pass'; \
+                           FLUSH PRIVILEGES;"
+    echo "========================================================================";
+    echo "You can now connect to this MariaDB Server using the following credentials:";
+    echo " ";
+    echo "        user type : user ";
+    echo "        username  : $userdb ";
+    echo "        password  : $pass";
+    echo "        database  : $userdb ";
+    echo " ";
+    echo "        mysql -u $userdb -p$pass $userdb";
+    echo "========================================================================";
+    echo " ";
+    return 0
+}
+
+# Set new root password and grant permissions to all databases
+function create_user {
+    local user="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD;
+    if [ "$pass" = "" ]; then
+       local pass=$(pwgen 13 1);
+    fi
+    echo "===> Create new user $user"
+    mysql -u root -p$p -e "CREATE USER '$user'@'%';SET PASSWORD FOR '$user'@'%' = PASSWORD('$pass');\
+                           CREATE USER '$user'@'localhost';SET PASSWORD FOR '$user'@'localhost' = PASSWORD('$pass');"
+    echo "========================================================================";
+    echo "You can now connect to this MariaDB Server using the following credentials:";
+    echo " ";
+    echo "        user type : user ";
+    echo "        username  : $user ";
+    echo "        password  : $pass ";
+    echo " ";
+    echo "        mysql -u $user -p$pass";
+    echo "========================================================================";
+    echo " ";
+    return 0
+}
+
+# Create a new database
+function create_db {
+    local db=$1; local p=$MARIADB_ROOTPWD;
+    echo "===> Create new database $db"
+    mysql -u root -p$p -e "CREATE DATABASE $db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
+    echo "===> New database $db CREATED"
+    return 0
+}
+
+if [[ "$0" == *"mariadb.sh" && ! $1 = "" ]];then
+    eval "$@"; 
+fi
\ No newline at end of file
diff --git a/Services/mariadb/sx/mariadb_run.sh b/Services/mariadb/sx/mariadb_run.sh
new file mode 100644
index 0000000..07a4c03
--- /dev/null
+++ b/Services/mariadb/sx/mariadb_run.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+source /sx/mariadb.sh
+
+begin_config
+
+## if you wan't to add a new user with database
+#create_userdb 'dbuser1' 'password'
+## if you wan't to add a new user with database (generated password)
+#create_userdb 'dbuser2'
+## if you wan't to add a new user
+#create_user 'username' 'password'
+## if you wan't to add a new database
+#create_db 'dbname'
+## Execute an SQL request to get the user list
+#mysql -u root -p$MARIADB_ROOTPWD -e 'select user, host FROM mysql.user;'
+## Execute SQL scripts located into a directory
+#import_sqlfiles /tmp/sql/*.sql
+## Execute a single SQL script
+#import_sqlfile /tmp/sql/example.sql
+
+end_config
+start_daemon
\ No newline at end of file
-- 
cgit v1.2.3