blob: 1e3709e461fd9045c8a04da120eec65b907cc335 [file] [log] [blame]
Eduardo Sousa7d8d6e72018-07-25 01:30:14 +01001#!/bin/bash
2
3DB_EXISTS=""
4
5max_attempts=120
6function 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
25function 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
41wait_db "$DB_HOST" "$DB_PORT" || exit 1
42
43is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
44
45if [ -z $DB_EXISTS ]; then
46 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
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'@'localhost' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
48 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'"
49fi
50
51# Setting Keystone database connection
52sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf
53
54# Setting Keystone tokens
55sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf
56
57# Populate Keystone database
58if [ -z $DB_EXISTS ]; then
59 su -s /bin/sh -c "keystone-manage db_sync" keystone
60fi
61
62# Initialize Fernet key repositories
63keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
64keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
65
66# Bootstrap Keystone service
67if [ -z $DB_EXISTS ]; then
68 keystone-manage bootstrap --bootstrap-password "$ADMIN_PASSWORD" \
69 --bootstrap-admin-url http://keystone:5000/v3/ \
70 --bootstrap-internal-url http://keystone:5000/v3/ \
71 --bootstrap-public-url http://keystone:5000/v3/ \
72 --bootstrap-region-id RegionOne
73fi
74
75# Restart Apache Service
76service apache2 restart
77
78cat << EOF >> setup_env
79export OS_PROJECT_DOMAIN_NAME=default
80export OS_USER_DOMAIN_NAME=default
81export OS_PROJECT_NAME=admin
82export OS_USERNAME=admin
83export OS_PASSWORD=$ADMIN_PASSWORD
84export OS_AUTH_URL=http://keystone:5000/v3
85export OS_IDENTITY_API_VERSION=3
86export OS_IMAGE_API_VERSION=2
87EOF
88
89source setup_env
90
91# Create NBI User
92if [ -z $DB_EXISTS ]; then
93 openstack user create --domain default --password "$NBI_PASSWORD" nbi
94 openstack project create --domain default --description "Service Project" service
95 openstack role add --project service --user nbi admin
96fi
97
98while ps -ef | grep -v grep | grep -q apache2
99do
100 sleep 60
101done
102
103# Only reaches this point if apache2 stops running
104# When this happens exits with error code
105exit 1