Modifies MON to create database if using mysql URI 55/7255/5
authorBenjamin Diaz <bdiaz@whitestack.com>
Mon, 25 Feb 2019 20:19:23 +0000 (17:19 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Wed, 27 Feb 2019 17:05:14 +0000 (14:05 -0300)
Removes default value of config in VimCredentials as MySQL does not support
it.
Adds pymysql as dependency.

Change-Id: Idb60029e027f8abad7de4617c12bde20aa66deab
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Dockerfile
debian/python3-osm-mon.postinst
docker/Dockerfile
docker/scripts/runInstall.sh [deleted file]
docker/scripts/start.sh [new file with mode: 0644]
osm_mon/core/database.py
osm_mon/migrations/001_initial.py
setup.py

index 153a6d2..9392d74 100644 (file)
@@ -25,5 +25,5 @@
 FROM ubuntu:16.04
 RUN  apt-get update && \
   DEBIAN_FRONTEND=noninteractive apt-get --yes install git tox make python-all python3 python3-pip debhelper wget && \
-  DEBIAN_FRONTEND=noninteractive apt-get --yes install libmysqlclient-dev libxml2 python3-all && \
+  DEBIAN_FRONTEND=noninteractive apt-get --yes install libmysqlclient-dev libxml2 python3-all libssl-dev && \
   DEBIAN_FRONTEND=noninteractive pip3 install -U setuptools setuptools-version-command stdeb
index 26ac8d7..fc18a7a 100644 (file)
@@ -37,4 +37,5 @@ pip3 install pyvcloud==19.1.1
 pip3 install python-ceilometerclient==2.9.*
 pip3 install peewee-migrate==1.1.*
 pip3 install python-novaclient==12.0.*
+pip3 install pymysql==0.9.*
 echo "Installation of python dependencies finished"
index dca983c..5271205 100644 (file)
@@ -25,7 +25,7 @@ FROM ubuntu:16.04
 LABEL authors="Benjamín Díaz"
 
 RUN apt-get --yes update \
- && apt-get --yes install git python3 python3-pip libmysqlclient-dev libssl-dev libffi-dev \
+ && apt-get --yes install git python3 python3-pip libmysqlclient-dev libssl-dev libffi-dev mysql-client \
  && pip3 install pip==9.0.3
 
 COPY requirements.txt /mon/requirements.txt
@@ -60,4 +60,4 @@ EXPOSE 8000
 HEALTHCHECK --interval=5s --timeout=2s --retries=12 \
   CMD osm-mon-healthcheck || exit 1
 
-CMD /bin/bash mon/docker/scripts/runInstall.sh
+CMD /bin/bash mon/docker/scripts/start.sh
diff --git a/docker/scripts/runInstall.sh b/docker/scripts/runInstall.sh
deleted file mode 100755 (executable)
index 254decc..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/env bash
-# Copyright 2018 Whitestack, LLC
-# *************************************************************
-
-# This file is part of OSM Monitoring module
-# All Rights Reserved to Whitestack, LLC
-
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-
-#         http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-# For those usages not covered by the Apache License, Version 2.0 please
-# contact: bdiaz@whitestack.com or glavado@whitestack.com
-##
-osm-mon-server &
-osm-mon-evaluator &
-osm-mon-collector
-
diff --git a/docker/scripts/start.sh b/docker/scripts/start.sh
new file mode 100644 (file)
index 0000000..fc0b3c3
--- /dev/null
@@ -0,0 +1,57 @@
+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 [[ $OSMMON_SQL_DATABASE_URI == *'mysql'* ]]; then
+    DB_HOST=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^\w+://.+:.+@(.+):.*$|\1|')
+    DB_PORT=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^\w+://.*:([0-9]+).*$|\1|')
+    DB_USER=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^\w+://(.+):.+@.+$|\1|')
+    DB_PASSWORD=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://.+:(.+)@.*$|\1|')
+    DB_NAME=$(echo $OSMMON_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
+
+osm-mon-server &
+osm-mon-evaluator &
+osm-mon-collector
\ No newline at end of file
index 0886c2b..939abc6 100644 (file)
@@ -53,7 +53,7 @@ class VimCredentials(BaseModel):
     user = CharField()
     password = CharField()
     tenant_name = CharField()
-    config = TextField(default='{}')
+    config = TextField()
 
 
 class Alarm(BaseModel):
index a8b7290..cb5fb41 100644 (file)
@@ -63,7 +63,7 @@ def migrate(migrator, database, fake=False, **kwargs):
         user = pw.CharField(max_length=255)
         password = pw.CharField(max_length=255)
         tenant_name = pw.CharField(max_length=255)
-        config = pw.TextField(constraints=[SQL("DEFAULT '{}'")])
+        config = pw.TextField()
 
         class Meta:
             table_name = "vimcredentials"
index 2126358..15828bb 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -65,6 +65,7 @@ setup(
         "python-ceilometerclient==2.9.*",
         "peewee-migrate==1.1.*",
         "python-novaclient==12.0.*",
+        "pymysql==0.9.*",
         "osm-common",
         "n2vc"
     ],