From 320bb81a53ef0f16cb06a11083f1059b9e91b454 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladimi=CC=81r=20Orany=CC=81?= <vladimir@orany.cz>
Date: Mon, 7 Dec 2015 16:02:11 +0100
Subject: [PATCH] start refactoring the shell commands, automatically push the
 new version

---
 .travis.yml                  |  1 +
 bin/docker/notify.sh         | 24 ++++++++++++
 bin/lib/delegate.sh          | 68 +++++++++++++++++++++++++++++++++
 bin/nested.txt               |  1 +
 bin/nested/test.sh           |  3 ++
 bin/test.sh                  |  3 ++
 bin/test.txt                 |  1 +
 catalogue                    |  5 +++
 conf/docker/mc-config.groovy | 73 +++++++++++++++++++++++++++---------
 9 files changed, 161 insertions(+), 18 deletions(-)
 create mode 100755 bin/docker/notify.sh
 create mode 100755 bin/lib/delegate.sh
 create mode 100644 bin/nested.txt
 create mode 100755 bin/nested/test.sh
 create mode 100755 bin/test.sh
 create mode 100644 bin/test.txt
 create mode 100755 catalogue

diff --git a/.travis.yml b/.travis.yml
index 6fa79bacf2..4279b530f6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -32,6 +32,7 @@ after_script:
 
 # deployment
 before_deploy: "./war.sh"
+after_deploy: "./catalogue docker notify"
 deploy:
   provider: releases
   api_key:
diff --git a/bin/docker/notify.sh b/bin/docker/notify.sh
new file mode 100755
index 0000000000..320e27776c
--- /dev/null
+++ b/bin/docker/notify.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+echo "Travis reported branch $TRAVIS_BRANCH"
+
+#go to home and setup git
+cd $HOME
+git config --global user.email "travis@travis-ci.org"
+git config --global user.name "Travis"
+
+git clone --quiet --branch=2.x https://${GH_TOKEN}@github.com/MetadataRegistry/registry.git registry > /dev/null
+
+cd registry
+
+sed "s/ENV MC_VERSION.*/ENV MC_VERSION $TRAVIS_TAG/" Dockerfile > TemporaryDockerfile
+rm Dockerfile
+mv TemporaryDockerfile Dockerfile
+
+git add -A .
+git commit -m "Travis MetadataRegistry/ModelCataloguePlugin build $TRAVIS_BUILD_NUMBER pushed MetadataRegistry/registry"
+git tag "$TRAVIS_TAG"
+git push -fq origin 2.x > /dev/null
+git push -fq origin "$TRAVIS_TAG" > /dev/null
+
+echo "Metadata Registry notified of successful build"
\ No newline at end of file
diff --git a/bin/lib/delegate.sh b/bin/lib/delegate.sh
new file mode 100755
index 0000000000..3e59f55b7b
--- /dev/null
+++ b/bin/lib/delegate.sh
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+###
+# prints single command with it's description using the first line of the text file named same as the command
+# but having .txt extension.
+###
+function print_command() {
+    local PADDING='               '
+    local FOLDER=$1
+    local COMMAND=$2
+    local DESC="(no description provided)"
+
+    if [ -f "$FOLDER/$COMMAND.txt" ] ; then
+        DESC=$(head -n 1 "$FOLDER/$COMMAND.txt")
+    fi
+
+    printf "%s %s %s\n" "$COMMAND" "${PADDING:${#COMMAND}}" "$DESC"
+}
+
+###
+# prints usage for folder listing all *.sh files and directories (except lib directory)
+# @param FOLDER folder to be listed
+###
+function print_usage_for_folder(){
+    local ORIGINAL_DIRECTORY=$(pwd)
+    cd "$1"
+    echo "Usage:"
+    for f in * ; do
+        if [ "$f" == "lib" ] ; then
+            continue
+        elif [ -d "$f" ]; then
+            print_command $(pwd) "$f"
+        elif [ "$f" == *.sh ]; then
+            local COMMAND_NAME="${f/.sh/}"
+            print_command $(pwd) "$COMMAND_NAME"
+        fi
+    done
+    cd "$ORIGINAL_DIRECTORY"
+}
+
+###
+# delegates to scripts inside folder
+# @param FOLDER folder to be delegated to
+# @param COMMAND name of the script to be delegated to
+###
+function delegate_to_folder() {
+    local FOLDER="$1"
+    local SCRIPT="$2"
+    local COMMAND="$FOLDER""/""$SCRIPT"
+
+    shift 2
+
+
+    if [ -d "$COMMAND" ]; then
+        delegate_to_folder "$COMMAND" "$@"
+    elif [ -f "$COMMAND.sh" ]; then
+        "$COMMAND.sh" "$@"
+    else
+        COMMAND=${COMMAND//bin\///}
+        COMMAND=${COMMAND//\// }
+        echo
+        echo "Unknown command:$COMMAND"
+        echo
+        print_usage_for_folder "$FOLDER"
+        echo
+        exit 2
+    fi
+}
\ No newline at end of file
diff --git a/bin/nested.txt b/bin/nested.txt
new file mode 100644
index 0000000000..a178c7585f
--- /dev/null
+++ b/bin/nested.txt
@@ -0,0 +1 @@
+tests the command
\ No newline at end of file
diff --git a/bin/nested/test.sh b/bin/nested/test.sh
new file mode 100755
index 0000000000..07ba20af21
--- /dev/null
+++ b/bin/nested/test.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+echo "nested"
diff --git a/bin/test.sh b/bin/test.sh
new file mode 100755
index 0000000000..f91c83c2c2
--- /dev/null
+++ b/bin/test.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+echo "test"
\ No newline at end of file
diff --git a/bin/test.txt b/bin/test.txt
new file mode 100644
index 0000000000..a178c7585f
--- /dev/null
+++ b/bin/test.txt
@@ -0,0 +1 @@
+tests the command
\ No newline at end of file
diff --git a/catalogue b/catalogue
new file mode 100755
index 0000000000..88e8b15591
--- /dev/null
+++ b/catalogue
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+source bin/lib/delegate.sh
+
+delegate_to_folder "bin" "$@"
\ No newline at end of file
diff --git a/conf/docker/mc-config.groovy b/conf/docker/mc-config.groovy
index e55dd69fa1..7370bb1eae 100644
--- a/conf/docker/mc-config.groovy
+++ b/conf/docker/mc-config.groovy
@@ -1,29 +1,66 @@
 // config
 grails.logging.jul.usebridge = false
-grails.serverURL =  "http://${System.getenv('VIRTUAL_HOST')}"
+grails.serverURL =  "http://${System.getenv('VIRTUAL_HOST') ?: System.getenv('METADATA_URL')}"
 
 // datasource
 dataSource {
-    driverClassName = "com.mysql.jdbc.Driver"
-    dialect='org.hibernate.dialect.MySQL5InnoDBDialect'
-    url = "jdbc:mysql://mc-mysql:3306/metadata?autoReconnect=true&useUnicode=yes"
-    username = "metadata"
-    password = "metadata"
-    dbCreate = "update"
-    properties {
-        maxActive = -1
-        minEvictableIdleTimeMillis=1800000
-        timeBetweenEvictionRunsMillis=1800000
-        numTestsPerEvictionRun=3
-        testOnBorrow=true
-        testWhileIdle=true
-        testOnReturn=false
-        validationQuery="SELECT 1"
-        jdbcInterceptors="ConnectionState"
+
+    def metadataDbPassword = System.getenv('METADATA_PASSWORD')
+    def metadataDbUsername = System.getenv('METADATA_USERNAME')
+    def metadataJdbcString = System.getenv('METADATA_JDBC_URL')
+
+    if (!metadataJdbcString && System.getenv("MC_MYSQL_NAME")) {
+        metadataJdbcString = "jdbc:mysql://mc-mysql:3306/metadata?autoReconnect=true&useUnicode=yes"
     }
+
+    if (metadataJdbcString) {
+        driverClassName = "com.mysql.jdbc.Driver"
+        dialect='org.hibernate.dialect.MySQL5InnoDBDialect'
+        url = metadataJdbcString
+        username = metadataDbUsername
+        password = metadataDbPassword
+        dbCreate = "update"
+        properties {
+            maxActive = -1
+            minEvictableIdleTimeMillis=1800000
+            timeBetweenEvictionRunsMillis=1800000
+            numTestsPerEvictionRun=3
+            testOnBorrow=true
+            testWhileIdle=true
+            testOnReturn=false
+            validationQuery="SELECT 1"
+            jdbcInterceptors="ConnectionState"
+        }
+    } else {
+        def dbdir = "${System.properties['catalina.base']}/db"
+
+        dbCreate = "update"
+        url = "jdbc:h2:file:${dbdir};MVCC=TRUE;LOCK_TIMEOUT=10000"
+        pooled = true
+        username = "sa"
+        password = "db!admin"
+
+        properties {
+            maxActive = -1
+            minEvictableIdleTimeMillis=1800000
+            timeBetweenEvictionRunsMillis=1800000
+            numTestsPerEvictionRun=3
+            testOnBorrow=true
+            testWhileIdle=true
+            testOnReturn=true
+            validationQuery="SELECT 1"
+        }
+    }
+
+
 }
 
-mc.search.elasticsearch.host='mc-es'
+if (System.getenv("MC_ES_NAME")) {
+    // mc-es is bound
+    mc.search.elasticsearch.host='mc-es'
+} else {
+    mc.search.elasticsearch.local="${System.properties['catalina.base']}/es"
+}
 
 grails.plugin.console.enabled=true