blob: 4c25e770c1cceef400bdb316b52058ae3f373283 [file] [log] [blame]
Eduardo Sousa7d8d6e72018-07-25 01:30:14 +01001#!/bin/bash
2
Eduardo Sousad795f872019-02-05 16:05:53 +00003# Copyright 2018 Whitestack, LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16#
17# For those usages not covered by the Apache License, Version 2.0 please
18# contact: esousa@whitestack.com or glavado@whitestack.com
19##
20
Eduardo Sousa7d8d6e72018-07-25 01:30:14 +010021DB_EXISTS=""
22
23max_attempts=120
24function wait_db(){
25 db_host=$1
26 db_port=$2
27 attempt=0
28 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
29 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
30 #wait 120 sec
31 if [ $attempt -ge $max_attempts ]; then
32 echo
33 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
34 return 1
35 fi
36 attempt=$[$attempt+1]
37 echo -n "."
38 sleep 1
39 done
40 return 0
41}
42
43function is_db_created() {
44 db_host=$1
45 db_port=$2
46 db_user=$3
47 db_pswd=$4
48 db_name=$5
49
50 if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
51 echo "DB $db_name exists"
52 return 0
53 else
54 echo "DB $db_name does not exist"
55 return 1
56 fi
57}
58
59wait_db "$DB_HOST" "$DB_PORT" || exit 1
60
61is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
62
63if [ -z $DB_EXISTS ]; then
64 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
65 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'"
66 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'"
67fi
68
69# Setting Keystone database connection
70sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf
71
72# Setting Keystone tokens
73sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf
74
75# Populate Keystone database
76if [ -z $DB_EXISTS ]; then
77 su -s /bin/sh -c "keystone-manage db_sync" keystone
78fi
79
80# Initialize Fernet key repositories
81keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
82keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
83
84# Bootstrap Keystone service
85if [ -z $DB_EXISTS ]; then
86 keystone-manage bootstrap --bootstrap-password "$ADMIN_PASSWORD" \
87 --bootstrap-admin-url http://keystone:5000/v3/ \
88 --bootstrap-internal-url http://keystone:5000/v3/ \
89 --bootstrap-public-url http://keystone:5000/v3/ \
90 --bootstrap-region-id RegionOne
91fi
92
93# Restart Apache Service
94service apache2 restart
95
96cat << EOF >> setup_env
97export OS_PROJECT_DOMAIN_NAME=default
98export OS_USER_DOMAIN_NAME=default
99export OS_PROJECT_NAME=admin
100export OS_USERNAME=admin
101export OS_PASSWORD=$ADMIN_PASSWORD
102export OS_AUTH_URL=http://keystone:5000/v3
103export OS_IDENTITY_API_VERSION=3
104export OS_IMAGE_API_VERSION=2
105EOF
106
107source setup_env
108
109# Create NBI User
110if [ -z $DB_EXISTS ]; then
111 openstack user create --domain default --password "$NBI_PASSWORD" nbi
112 openstack project create --domain default --description "Service Project" service
113 openstack role add --project service --user nbi admin
114fi
115
116while ps -ef | grep -v grep | grep -q apache2
117do
118 sleep 60
119done
120
121# Only reaches this point if apache2 stops running
122# When this happens exits with error code
123exit 1