Merge "Disable parallel make for easier debugging"
[osm/devops.git] / docker / Keystone / scripts / start.sh
1 #!/bin/bash
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 RESULT=`mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -o $db_name`
31 if [ "$RESULT" == "$db_name" ]; then
32 echo "DB $db_name exists"
33 return 0
34 else
35 echo "DB $db_name does not exist"
36 return 1
37 fi
38 }
39
40 wait_db "$DB_HOST" "$DB_PORT" || exit 1
41
42 is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
43
44 if [ -z $DB_EXISTS ]; then
45 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
46 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
47 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
48 fi
49
50 # Setting Keystone database connection
51 sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf
52
53 # Setting Keystone tokens
54 sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf
55
56 # Populate Keystone database
57 if [ -z $DB_EXISTS ]; then
58 su -s /bin/sh -c "keystone-manage db_sync" keystone
59 fi
60
61 # Initialize Fernet key repositories
62 keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
63 keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
64
65 # Bootstrap Keystone service
66 if [ -z $DB_EXISTS ]; then
67 keystone-manage bootstrap --bootstrap-password "$ADMIN_PASSWORD" \
68 --bootstrap-admin-url http://keystone:5000/v3/ \
69 --bootstrap-internal-url http://keystone:5000/v3/ \
70 --bootstrap-public-url http://keystone:5000/v3/ \
71 --bootstrap-region-id RegionOne
72 fi
73
74 # Restart Apache Service
75 service apache2 restart
76
77 cat << EOF >> setup_env
78 export OS_PROJECT_DOMAIN_NAME=default
79 export OS_USER_DOMAIN_NAME=default
80 export OS_PROJECT_NAME=admin
81 export OS_USERNAME=admin
82 export OS_PASSWORD=$ADMIN_PASSWORD
83 export OS_AUTH_URL=http://keystone:5000/v3
84 export OS_IDENTITY_API_VERSION=3
85 export OS_IMAGE_API_VERSION=2
86 EOF
87
88 source setup_env
89
90 # Create NBI User
91 if [ -z $DB_EXISTS ]; then
92 openstack user create --domain default --password "$NBI_PASSWORD" nbi
93 openstack project create --domain default --description "Service Project" service
94 openstack role add --project service --user nbi admin
95 fi
96
97 while [ $(ps -ef | grep -v grep | grep apache2 | wc -l) -ne 0 ]
98 do
99 sleep 60
100 done
101
102 exit 1