Adds deps to support mysql session backend in LWUI 88/7488/9
authorBenjamin Diaz <bdiaz@whitestack.com>
Wed, 22 May 2019 15:21:31 +0000 (12:21 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Thu, 23 May 2019 23:07:09 +0000 (20:07 -0300)
This is an initial requirement to add support in LWUI for mysql backend, so CI
pipeline passes.

In a future commit the changes to Dockerfile to enable this feature will be
added.

Change-Id: I81330f99d2df85ba4813db130bddab92d3c9574b
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
build-debpkg.sh
docker/Dockerfile
entrypoint.sh [new file with mode: 0644]
requirements.txt

index 6f05b94..19565e1 100755 (executable)
@@ -16,7 +16,7 @@
 
 
 PKG_DIRECTORIES="authosm descriptorhandler instancehandler lib projecthandler sdnctrlhandler sf_t3d static template userhandler vimhandler packagehandler netslicehandler wimhandler"
-PKG_FILES="bower.json django.ini LICENSE manage.py nginx-app.conf README.md requirements.txt supervisor-app.conf .bowerrc"
+PKG_FILES="bower.json django.ini LICENSE manage.py nginx-app.conf README.md requirements.txt supervisor-app.conf .bowerrc entrypoint.sh"
 MDG_NAME=lightui
 DEB_INSTALL=debian/osm-${MDG_NAME}.install
 export DEBEMAIL="gerardo.garciadeblas@telefonica.com"
@@ -25,10 +25,11 @@ export DEBFULLNAME="Gerardo Garcia"
 PKG_VERSION=$(git describe --match "v*" --tags --abbrev=0)
 PKG_VERSION_PREFIX=$(echo $PKG_VERSION | sed -e 's/v//g')
 PKG_VERSION_POST=$(git rev-list $PKG_VERSION..HEAD | wc -l)
+PKG_VERSION_HASH=$(git describe --match "v*" --tags | awk '{print $3}' FS=-)
 if [ "$PKG_VERSION_POST" -eq 0 ]; then
     PKG_DIR="deb_dist/osm-${MDG_NAME}-${PKG_VERSION_PREFIX}"
 else
-    PKG_DIR="deb_dist/osm-${MDG_NAME}-$PKG_VERSION_PREFIX.post${PKG_VERSION_POST}"
+    PKG_DIR="deb_dist/osm-${MDG_NAME}-$PKG_VERSION_PREFIX.post${PKG_VERSION_POST}+${PKG_VERSION_HASH}"
 fi
 
 rm -rf $PKG_DIR
index 5c0aec0..932f31e 100644 (file)
@@ -4,7 +4,7 @@ WORKDIR /usr/share/osm-lightui
 COPY . /usr/share/osm-lightui
 
 RUN apt-get update
-RUN apt-get install -y npm git python-pip nginx supervisor
+RUN apt-get install -y npm git python-pip nginx supervisor libmysqlclient-dev mysql-client
 RUN npm install -g bower
 RUN ln -s /usr/bin/nodejs /usr/bin/node
 RUN bower install --allow-root
@@ -19,11 +19,8 @@ COPY supervisor-app.conf /etc/supervisor/conf.d/
 RUN rm -f db.sqlite3
 
 ENV DJANGO_ENV=prod
-RUN python manage.py makemigrations authosm
-RUN python manage.py migrate
 RUN python manage.py collectstatic --noinput
 
-
 EXPOSE 80
 
-CMD ["supervisord", "-n"]
+CMD ["/usr/share/osm-lightui/entrypoint.sh"]
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100644 (file)
index 0000000..a70ab27
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+DB_EXISTS=""
+
+max_attempts=120
+function wait_db(){
+    db_host=$1
+    db_port=$2
+    attempt=0
+    echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
+    while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
+        #wait 120 sec
+        if [ $attempt -ge $max_attempts ]; then
+            echo
+            echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
+            return 1
+        fi
+        attempt=$[$attempt+1]
+        echo -n "."
+        sleep 1
+    done
+    return 0
+}
+
+function is_db_created() {
+    db_host=$1
+    db_port=$2
+    db_user=$3
+    db_pswd=$4
+    db_name=$5
+
+    if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
+        echo "DB $db_name exists"
+        return 0
+    else
+        echo "DB $db_name does not exist"
+        return 1
+    fi
+}
+
+if [[ $OSMUI_SQL_DATABASE_URI == *'mysql'* ]]; then
+    DB_HOST=$(echo $OSMUI_SQL_DATABASE_URI | sed -r 's|^.+://.+:.+@(.+):.*$|\1|')
+    DB_PORT=$(echo $OSMUI_SQL_DATABASE_URI | sed -r 's|^.+://.*:([0-9]+).*$|\1|')
+    DB_USER=$(echo $OSMUI_SQL_DATABASE_URI | sed -r 's|^.+://(.+):.+@.+$|\1|')
+    DB_PASSWORD=$(echo $OSMUI_SQL_DATABASE_URI | sed -r 's|^.+://.+:(.+)@.*$|\1|')
+    DB_NAME=$(echo $OSMUI_SQL_DATABASE_URI | sed -r 's|^.+://.+:.+@.+:.*/(\w+)(\?.*)?$|\1|')
+    
+    wait_db "$DB_HOST" "$DB_PORT" || exit 1
+
+    is_db_created "$DB_HOST" "$DB_PORT" "$DB_USER" "$DB_PASSWORD" "$DB_NAME" && DB_EXISTS="Y"
+
+    if [ -z $DB_EXISTS ]; then
+        mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE $DB_NAME"
+    fi
+fi
+
+python manage.py makemigrations authosm
+python manage.py migrate
+
+supervisord -n
index d8f43b7..cdc3eb7 100644 (file)
@@ -13,3 +13,5 @@ six==1.10.0
 smmap2==2.0.3
 url==0.2.0
 wheel==0.24.0
+mysql-python
+sqlalchemy