| Eduardo Sousa | 7d8d6e7 | 2018-07-25 01:30:14 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| Eduardo Sousa | d795f87 | 2019-02-05 16:05:53 +0000 | [diff] [blame] | 3 | # 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 Sousa | 7d8d6e7 | 2018-07-25 01:30:14 +0100 | [diff] [blame] | 21 | DB_EXISTS="" |
| 22 | |
| 23 | max_attempts=120 |
| 24 | function 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 | |
| 43 | function 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 | |
| 59 | wait_db "$DB_HOST" "$DB_PORT" || exit 1 |
| 60 | |
| 61 | is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y" |
| 62 | |
| 63 | if [ -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'" |
| 67 | fi |
| 68 | |
| 69 | # Setting Keystone database connection |
| 70 | sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf |
| 71 | |
| 72 | # Setting Keystone tokens |
| 73 | sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf |
| 74 | |
| 75 | # Populate Keystone database |
| 76 | if [ -z $DB_EXISTS ]; then |
| 77 | su -s /bin/sh -c "keystone-manage db_sync" keystone |
| 78 | fi |
| 79 | |
| 80 | # Initialize Fernet key repositories |
| 81 | keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone |
| 82 | keystone-manage credential_setup --keystone-user keystone --keystone-group keystone |
| 83 | |
| 84 | # Bootstrap Keystone service |
| 85 | if [ -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 |
| 91 | fi |
| 92 | |
| 93 | # Restart Apache Service |
| 94 | service apache2 restart |
| 95 | |
| 96 | cat << EOF >> setup_env |
| 97 | export OS_PROJECT_DOMAIN_NAME=default |
| 98 | export OS_USER_DOMAIN_NAME=default |
| 99 | export OS_PROJECT_NAME=admin |
| 100 | export OS_USERNAME=admin |
| 101 | export OS_PASSWORD=$ADMIN_PASSWORD |
| 102 | export OS_AUTH_URL=http://keystone:5000/v3 |
| 103 | export OS_IDENTITY_API_VERSION=3 |
| 104 | export OS_IMAGE_API_VERSION=2 |
| 105 | EOF |
| 106 | |
| 107 | source setup_env |
| 108 | |
| 109 | # Create NBI User |
| 110 | if [ -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 |
| 114 | fi |
| 115 | |
| 116 | while ps -ef | grep -v grep | grep -q apache2 |
| 117 | do |
| 118 | sleep 60 |
| 119 | done |
| 120 | |
| 121 | # Only reaches this point if apache2 stops running |
| 122 | # When this happens exits with error code |
| 123 | exit 1 |