blob: e4bb5f27fbd339e03385a45c2bb873c5668cd7b6 [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
David Garcia58b0e322020-03-02 14:17:26 +010044function wait_keystone_host(){
45 attempt=0
46 timeout=2
47 echo "Wait until Keystone hostname can be resolved "
48 while ! nslookup $KEYSTONE_HOST; do
49 #wait 120 sec
50 if [ $attempt -ge $max_attempts ]; then
51 echo
52 echo "Can not resolve ${KEYSTONE_HOST} during $max_attempts sec"
53 return 1
54 fi
55 attempt=$[$attempt+1]
56 echo -n "."
57 sleep 1
58 done
59 return 0
60}
61
Eduardo Sousa09a1e972018-09-21 11:06:32 +010062function is_db_created() {
63 db_host=$1
64 db_port=$2
65 db_user=$3
66 db_pswd=$4
67 db_name=$5
68
Eduardo Sousa07e8a242018-10-08 12:49:14 +010069 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 +010070 echo "DB $db_name exists"
71 return 0
72 else
73 echo "DB $db_name does not exist"
74 return 1
75 fi
76}
77
Eduardo Sousa09a1e972018-09-21 11:06:32 +010078wait_db "$DB_HOST" "$DB_PORT" || exit 1
79
80is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
81
82if [ -z $DB_EXISTS ]; then
83 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
84 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'"
85 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 +010086else
David Garcia58b0e322020-03-02 14:17:26 +010087 if [ $(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -sse "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'keystone';") -gt 0 ]; then
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010088 echo "DB keystone is empty"
89 DB_NOT_EMPTY="y"
90 fi
Eduardo Sousa09a1e972018-09-21 11:06:32 +010091fi
92
93# Setting Keystone database connection
David Garcia58b0e322020-03-02 14:17:26 +010094sed -i '/^\[database\]$/,/^\[/ s/^connection = .*/connection = mysql+pymysql:\/\/keystone:'$KEYSTONE_DB_PASSWORD'@'$DB_HOST':'$DB_PORT'\/keystone/' /etc/keystone/keystone.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +010095
96# Setting Keystone tokens
David Garcia58b0e322020-03-02 14:17:26 +010097sed -i '/^\[token\]$/,/^\[/ s/^.*provider = .*/provider = fernet/' /etc/keystone/keystone.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +010098
David Garcia6fff9af2020-03-23 15:32:43 +010099
100# Use LDAP authentication for Identity
101if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
102 # Enable Keyston domains
103 sed -i "s%.*domain_specific_drivers_enabled =.*%domain_specific_drivers_enabled = true%" /etc/keystone/keystone.conf
104 sed -i "s%.*domain_config_dir =.*%domain_config_dir = /etc/keystone/domains%" /etc/keystone/keystone.conf
105 mkdir -p /etc/keystone/domains
106 # Configure domain for LDAP authentication
107 cat << EOF > /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
108[identity]
109driver = ldap
110[ldap]
111url = $LDAP_URL
112user_allow_create=false
113user_allow_update=false
114user_allow_delete=false
115group_allow_create=false
116group_allow_update=false
117group_allow_delete=false
118query_scope = sub
119EOF
sousaedu66dc9ed2021-06-16 16:17:48 +0100120 if [ "$LDAP_BIND_USER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100121 echo "user = $LDAP_BIND_USER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
122 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100123 if [ "$LDAP_BIND_PASSWORD" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100124 echo "password = $LDAP_BIND_PASSWORD" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
125 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100126 if [ "$LDAP_CHASE_REFERRALS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100127 echo "chase_referrals = $LDAP_CHASE_REFERRALS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
128 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100129 if [ "$LDAP_PAGE_SIZE" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100130 echo "page_size = $LDAP_PAGE_SIZE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
131 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100132 if [ "$LDAP_USER_TREE_DN" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100133 echo "user_tree_dn = $LDAP_USER_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
134 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100135 if [ "$LDAP_USER_OBJECTCLASS" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100136 echo "user_objectclass = $LDAP_USER_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
137 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100138 if [ "$LDAP_USER_ID_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100139 echo "user_id_attribute = $LDAP_USER_ID_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
140 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100141 if [ "$LDAP_USER_NAME_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100142 echo "user_name_attribute = $LDAP_USER_NAME_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
143 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100144 if [ "$LDAP_USER_PASS_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100145 echo "user_pass_attribute = $LDAP_USER_PASS_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
146 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100147 if [ "$LDAP_USER_FILTER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100148 echo "user_filter = $LDAP_USER_FILTER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
149 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100150 if [ "$LDAP_USER_ENABLED_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100151 echo "user_enabled_attribute = $LDAP_USER_ENABLED_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
152 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100153 if [ "$LDAP_USER_ENABLED_MASK" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100154 echo "user_enabled_mask = $LDAP_USER_ENABLED_MASK" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
155 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100156 if [ "$LDAP_USER_ENABLED_DEFAULT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100157 echo "user_enabled_default = $LDAP_USER_ENABLED_DEFAULT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
158 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100159 if [ "$LDAP_USER_ENABLED_INVERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100160 echo "user_enabled_invert = $LDAP_USER_ENABLED_INVERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
161 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100162 if [ "$LDAP_GROUP_OBJECTCLASS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100163 echo "group_objectclass = $LDAP_GROUP_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
164 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100165 if [ "$LDAP_GROUP_TREE_DN" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100166 echo "group_tree_dn = $LDAP_GROUP_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
167 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100168 if [ "$LDAP_USE_STARTTLS" ] && [ "$LDAP_USE_STARTTLS" == "true" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100169 echo "use_tls = true" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
170 mkdir -p /etc/keystone/ssl/certs/
171 echo "-----BEGIN CERTIFICATE-----" > /etc/keystone/ssl/certs/ca.pem
172 echo $LDAP_TLS_CACERT_BASE64 >> /etc/keystone/ssl/certs/ca.pem
173 echo "-----END CERTIFICATE-----" >> /etc/keystone/ssl/certs/ca.pem
174 echo "tls_cacertfile = /etc/keystone/ssl/certs/ca.pem" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
sousaedu66dc9ed2021-06-16 16:17:48 +0100175 if [ "$LDAP_TLS_REQ_CERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100176 echo "tls_req_cert = $LDAP_TLS_REQ_CERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
177 fi
178 fi
179fi
180
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100181# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100182if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100183 su -s /bin/sh -c "keystone-manage db_sync" keystone
184fi
185
186# Initialize Fernet key repositories
187keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
188keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
189
David Garcia58b0e322020-03-02 14:17:26 +0100190wait_keystone_host
191
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100192# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100193if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
194 keystone-manage bootstrap \
195 --bootstrap-username "$ADMIN_USERNAME" \
196 --bootstrap-password "$ADMIN_PASSWORD" \
197 --bootstrap-project "$ADMIN_PROJECT" \
198 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
199 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
200 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
201 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100202fi
203
David Garcia58b0e322020-03-02 14:17:26 +0100204echo "ServerName $KEYSTONE_HOST" >> /etc/apache2/apache2.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100205# Restart Apache Service
206service apache2 restart
207
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100208cat << EOF >> setup_env
209export OS_PROJECT_DOMAIN_NAME=default
210export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100211export OS_PROJECT_NAME=$ADMIN_PROJECT
212export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100213export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100214export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100215export OS_IDENTITY_API_VERSION=3
216export OS_IMAGE_API_VERSION=2
217EOF
218
219source setup_env
220
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100221# Create NBI User
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100222if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
223 openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME"
224 openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT"
Eduardo Sousa4222cc92019-05-22 22:58:51 +0100225 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin
David Garcia6fff9af2020-03-23 15:32:43 +0100226fi
227
228if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
229 if !(openstack domain list | grep -q $LDAP_AUTHENTICATION_DOMAIN_NAME); then
230 # Create domain in keystone for LDAP authentication
231 openstack domain create $LDAP_AUTHENTICATION_DOMAIN_NAME
232 # Restart Apache Service
233 service apache2 restart
234 fi
235 # Check periodically LDAP for updates
236 echo "0 1 * * * keystone-manage mapping_purge --domain-name $LDAP_AUTHENTICATION_DOMAIN_NAME; keystone-manage mapping_populate --domain-name $LDAP_AUTHENTICATION_DOMAIN_NAME" >> /var/spool/cron/crontabs/root
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100237fi
238
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100239while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100240do
241 sleep 60
242done
243
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100244# Only reaches this point if apache2 stops running
245# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100246exit 1