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