blob: 8dd61ad6ae0ef4ce50c071cca730f6b16e4cd568 [file] [log] [blame]
Eduardo Sousa09a1e972018-09-21 11:06:32 +01001#!/bin/bash
2
Eduardo Sousa3c761742019-02-05 16:19:31 +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 Sousa07e8a242018-10-08 12:49:14 +010021DB_EXISTS=""
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010022DB_NOT_EMPTY=""
Eduardo Sousa07e8a242018-10-08 12:49:14 +010023
Eduardo Sousa09a1e972018-09-21 11:06:32 +010024max_attempts=120
25function wait_db(){
26 db_host=$1
27 db_port=$2
28 attempt=0
29 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
30 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
31 #wait 120 sec
32 if [ $attempt -ge $max_attempts ]; then
33 echo
34 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
35 return 1
36 fi
37 attempt=$[$attempt+1]
38 echo -n "."
39 sleep 1
40 done
41 return 0
42}
43
44function is_db_created() {
45 db_host=$1
46 db_port=$2
47 db_user=$3
48 db_pswd=$4
49 db_name=$5
50
Eduardo Sousa07e8a242018-10-08 12:49:14 +010051 if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
Eduardo Sousa09a1e972018-09-21 11:06:32 +010052 echo "DB $db_name exists"
53 return 0
54 else
55 echo "DB $db_name does not exist"
56 return 1
57 fi
58}
59
Eduardo Sousa09a1e972018-09-21 11:06:32 +010060wait_db "$DB_HOST" "$DB_PORT" || exit 1
61
62is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
63
64if [ -z $DB_EXISTS ]; then
65 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
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'@'localhost' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
67 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'"
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010068else
69 if [ $(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -sse "SELECT COUNT(*) FROM keystone;") -gt 0 ]; then
70 echo "DB keystone is empty"
71 DB_NOT_EMPTY="y"
72 fi
Eduardo Sousa09a1e972018-09-21 11:06:32 +010073fi
74
75# Setting Keystone database connection
76sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf
77
78# Setting Keystone tokens
79sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf
80
81# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010082if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
Eduardo Sousa09a1e972018-09-21 11:06:32 +010083 su -s /bin/sh -c "keystone-manage db_sync" keystone
84fi
85
86# Initialize Fernet key repositories
87keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
88keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
89
90# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010091if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
92 keystone-manage bootstrap \
93 --bootstrap-username "$ADMIN_USERNAME" \
94 --bootstrap-password "$ADMIN_PASSWORD" \
95 --bootstrap-project "$ADMIN_PROJECT" \
96 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
97 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
98 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
99 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100100fi
101
102# Restart Apache Service
103service apache2 restart
104
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100105cat << EOF >> setup_env
106export OS_PROJECT_DOMAIN_NAME=default
107export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100108export OS_PROJECT_NAME=$ADMIN_PROJECT
109export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100110export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100111export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100112export OS_IDENTITY_API_VERSION=3
113export OS_IMAGE_API_VERSION=2
114EOF
115
116source setup_env
117
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100118# Create NBI User
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100119if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
120 openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME"
121 openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT"
Eduardo Sousa4222cc92019-05-22 22:58:51 +0100122 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100123 openstack role delete _member_
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100124fi
125
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100126while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100127do
128 sleep 60
129done
130
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100131# Only reaches this point if apache2 stops running
132# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100133exit 1