Modifies POL to create database if using mysql URI
[osm/POL.git] / docker / scripts / start.sh
1 DB_EXISTS=""
2
3 max_attempts=120
4 function wait_db(){
5 db_host=$1
6 db_port=$2
7 attempt=0
8 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
9 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
10 #wait 120 sec
11 if [ $attempt -ge $max_attempts ]; then
12 echo
13 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
14 return 1
15 fi
16 attempt=$[$attempt+1]
17 echo -n "."
18 sleep 1
19 done
20 return 0
21 }
22
23 function is_db_created() {
24 db_host=$1
25 db_port=$2
26 db_user=$3
27 db_pswd=$4
28 db_name=$5
29
30 if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
31 echo "DB $db_name exists"
32 return 0
33 else
34 echo "DB $db_name does not exist"
35 return 1
36 fi
37 }
38
39 if [[ $OSMPOL_SQL_DATABASE_URI == *'mysql'* ]]; then
40 DB_HOST=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.+:.+@(.+):.*$|\1|')
41 DB_PORT=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.*:([0-9]+).*$|\1|')
42 DB_USER=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://(.+):.+@.+$|\1|')
43 DB_PASSWORD=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^.+://.+:(.+)@.*$|\1|')
44 DB_NAME=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.+:.+@.+:.*/(.+)$|\1|')
45
46 wait_db "$DB_HOST" "$DB_PORT" || exit 1
47
48 is_db_created "$DB_HOST" "$DB_PORT" "$DB_USER" "$DB_PASSWORD" "$DB_NAME" && DB_EXISTS="Y"
49
50 if [ -z $DB_EXISTS ]; then
51 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE $DB_NAME"
52 fi
53 fi
54
55 osm-policy-agent