Adding license headers to keystone
[osm/devops.git] / docker / Keystone / scripts / start.sh
1 #!/bin/bash
2
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
21 DB_EXISTS=""
22 DB_NOT_EMPTY=""
23
24 max_attempts=120
25 function 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
44 function is_db_created() {
45 db_host=$1
46 db_port=$2
47 db_user=$3
48 db_pswd=$4
49 db_name=$5
50
51 if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then
52 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
60 wait_db "$DB_HOST" "$DB_PORT" || exit 1
61
62 is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
63
64 if [ -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'"
68 else
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
73 fi
74
75 # Setting Keystone database connection
76 sed -i "721s%.*%connection = mysql+pymysql://keystone:$KEYSTONE_DB_PASSWORD@$DB_HOST:$DB_PORT/keystone%" /etc/keystone/keystone.conf
77
78 # Setting Keystone tokens
79 sed -i "2934s%.*%provider = fernet%" /etc/keystone/keystone.conf
80
81 # Populate Keystone database
82 if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
83 su -s /bin/sh -c "keystone-manage db_sync" keystone
84 fi
85
86 # Initialize Fernet key repositories
87 keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
88 keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
89
90 # Bootstrap Keystone service
91 if [ -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"
100 fi
101
102 # Restart Apache Service
103 service apache2 restart
104
105 cat << EOF >> setup_env
106 export OS_PROJECT_DOMAIN_NAME=default
107 export OS_USER_DOMAIN_NAME=default
108 export OS_PROJECT_NAME=$ADMIN_PROJECT
109 export OS_USERNAME=$ADMIN_USERNAME
110 export OS_PASSWORD=$ADMIN_PASSWORD
111 export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
112 export OS_IDENTITY_API_VERSION=3
113 export OS_IMAGE_API_VERSION=2
114 EOF
115
116 source setup_env
117
118 # Create NBI User
119 if [ -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"
122 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USER" admin
123 openstack role delete _member_
124 fi
125
126 while ps -ef | grep -v grep | grep -q apache2
127 do
128 sleep 60
129 done
130
131 # Only reaches this point if apache2 stops running
132 # When this happens exits with error code
133 exit 1