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