0b0cb79ff38f6a70b1f3e8280e07b5d5e6abe28c
[osm/MON.git] / docker / scripts / start.sh
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: glavado@whitestack.com
22 ##
23 DB_EXISTS=""
24
25 max_attempts=120
26 function wait_db(){
27 db_host=$1
28 db_port=$2
29 attempt=0
30 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
31 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
32 #wait 120 sec
33 if [ $attempt -ge $max_attempts ]; then
34 echo
35 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
36 return 1
37 fi
38 attempt=$[$attempt+1]
39 echo -n "."
40 sleep 1
41 done
42 return 0
43 }
44
45 function is_db_created() {
46 db_host=$1
47 db_port=$2
48 db_user=$3
49 db_pswd=$4
50 db_name=$5
51
52 if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
53 echo "DB $db_name exists"
54 return 0
55 else
56 echo "DB $db_name does not exist"
57 return 1
58 fi
59 }
60
61 if [[ $OSMMON_SQL_DATABASE_URI == *'mysql'* ]]; then
62 DB_HOST=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://.+:.+@(.+):.*$|\1|')
63 DB_PORT=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://.*:([0-9]+).*$|\1|')
64 DB_USER=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://(.+):.+@.+$|\1|')
65 DB_PASSWORD=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://.+:(.+)@.*$|\1|')
66 DB_NAME=$(echo $OSMMON_SQL_DATABASE_URI | sed -r 's|^.+://.+:.+@.+:.*/(\w+)(\?.*)?$|\1|')
67
68 wait_db "$DB_HOST" "$DB_PORT" || exit 1
69
70 is_db_created "$DB_HOST" "$DB_PORT" "$DB_USER" "$DB_PASSWORD" "$DB_NAME" && DB_EXISTS="Y"
71
72 if [ -z $DB_EXISTS ]; then
73 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE $DB_NAME"
74 fi
75 fi
76
77 osm-mon-server &
78 osm-mon-evaluator &
79 osm-mon-collector &
80 osm-mon-dashboarder