Modifies MON deployment to use mysql database 56/7256/11
authorBenjamin Diaz <bdiaz@whitestack.com>
Mon, 25 Feb 2019 20:18:51 +0000 (17:18 -0300)
committerdiazb <bdiaz@whitestack.com>
Fri, 5 Apr 2019 20:58:22 +0000 (21:58 +0100)
Adds script to automatically create database if not existant.
Adds generation of db env var to install script.

Depends on 7255

Change-Id: Ic4da007e5799f8986b27c896b82c278e49e9b7e5
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
docker/MON/Dockerfile
docker/MON/scripts/runInstall.sh [deleted file]
docker/MON/scripts/start.sh [new file with mode: 0644]
installers/docker/docker-compose.yaml
installers/full_install_osm.sh

index c1ab2fa..2fb0800 100644 (file)
@@ -24,11 +24,21 @@ FROM ubuntu:16.04
 
 LABEL authors="Guillermo Calvino"
 
-RUN apt-get update && apt-get -y install curl software-properties-common
-
-RUN apt-get --yes update \
- && apt-get --yes install git python python-pip python3 python3-pip libmysqlclient-dev libssl-dev libffi-dev \
- && pip3 install pip==9.0.3
+RUN apt-get update \
+    && apt-get -y install curl \
+                          software-properties-common \
+                          git \
+                          python \
+                          python-pip \
+                          python3 \
+                          python3-pip \
+                          libmysqlclient-dev \
+                          libssl-dev \
+                          libffi-dev \
+                          mysql-client \
+    && apt-get clean \
+    && rm -rf /var/lib/apt/lists/* \
+    && pip3 install pip==9.0.3
 
 RUN pip3 install  'macaroonbakery>=1.1,<2.0' 'pyRFC3339>=1.0,<2.0' \
       'pyyaml>=3.0,<4.0' 'theblues>=0.3.8,<1.0' 'websockets>=4.0,<5.0' \
@@ -39,15 +49,16 @@ ARG RELEASE=ReleaseFOUR-daily
 ARG REPOSITORY_KEY=OSM%20ETSI%20Release%20Key.gpg
 ARG REPOSITORY=testing
 
-RUN curl ${REPOSITORY_BASE}/${RELEASE}/${REPOSITORY_KEY} | apt-key add -
-RUN add-apt-repository -y "deb ${REPOSITORY_BASE}/${RELEASE} ${REPOSITORY} MON N2VC common" && apt update
-
 ARG MON_VERSION
 ARG COMMON_VERSION
 ARG N2VC_VERSION
 
-RUN apt-get --yes update  && apt-get -y install python3-osm-common${COMMON_VERSION} python3-osm-mon${MON_VERSION} python3-n2vc${N2VC_VERSION}
-
+RUN curl ${REPOSITORY_BASE}/${RELEASE}/${REPOSITORY_KEY} | apt-key add - \
+    && add-apt-repository -y "deb ${REPOSITORY_BASE}/${RELEASE} ${REPOSITORY} MON N2VC common" \
+    && apt update \
+    && apt-get -y install python3-osm-common${COMMON_VERSION} \
+                          python3-osm-mon${MON_VERSION} \
+                          python3-n2vc${N2VC_VERSION}
 COPY scripts/ scripts/
 
 ENV OSMMON_MESSAGE_DRIVER kafka
@@ -74,4 +85,4 @@ EXPOSE 8000
 HEALTHCHECK --interval=5s --timeout=2s --retries=12 \
   CMD osm-mon-healthcheck || exit 1
 
-CMD /bin/bash scripts/runInstall.sh
+CMD /bin/bash scripts/start.sh
diff --git a/docker/MON/scripts/runInstall.sh b/docker/MON/scripts/runInstall.sh
deleted file mode 100755 (executable)
index b3228b6..0000000
+++ /dev/null
@@ -1,25 +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/MON/scripts/start.sh b/docker/MON/scripts/start.sh
new file mode 100644 (file)
index 0000000..59940a4
--- /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 7ac827b..3a879ad 100644 (file)
@@ -135,7 +135,6 @@ services:
     environment:
       OSMMON_MESSAGE_HOST: kafka
       OSMMON_MESSAGE_PORT: 9092
-      OSMMON_SQL_DATABASE_URI: sqlite:///app/database/mon_sqlite.db
     env_file:
       - ./mon.env
     ports:
index 79dc117..a48bd4d 100755 (executable)
@@ -801,6 +801,7 @@ function generate_docker_env_files() {
     # MON
     if [ ! -f $OSM_DOCKER_WORK_DIR/mon.env ]; then
         echo "OSMMON_DATABASE_COMMONKEY=${OSM_DATABASE_COMMONKEY}" | $WORKDIR_SUDO tee -a $OSM_DOCKER_WORK_DIR/mon.env
+        echo "OSMMON_SQL_DATABASE_URI=mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/mon" | $WORKDIR_SUDO tee -a $OSM_DOCKER_WORK_DIR/mon.env
     fi
 
     if ! grep -Fq "OS_NOTIFIER_URI" $OSM_DOCKER_WORK_DIR/mon.env; then