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