blob: dde1b5ae687f701f1d2933aa17eedb0d730dc421 [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=""
sousaedu87c40852021-07-28 12:08:43 +020022USER_DB_EXISTS=""
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010023DB_NOT_EMPTY=""
Eduardo Sousa07e8a242018-10-08 12:49:14 +010024
Eduardo Sousa09a1e972018-09-21 11:06:32 +010025max_attempts=120
26function wait_db(){
27 db_host=$1
28 db_port=$2
29 attempt=0
30 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
31 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
32 #wait 120 sec
33 if [ $attempt -ge $max_attempts ]; then
34 echo
35 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
36 return 1
37 fi
38 attempt=$[$attempt+1]
39 echo -n "."
40 sleep 1
41 done
42 return 0
43}
44
David Garcia58b0e322020-03-02 14:17:26 +010045function wait_keystone_host(){
46 attempt=0
47 timeout=2
48 echo "Wait until Keystone hostname can be resolved "
49 while ! nslookup $KEYSTONE_HOST; do
50 #wait 120 sec
51 if [ $attempt -ge $max_attempts ]; then
52 echo
53 echo "Can not resolve ${KEYSTONE_HOST} during $max_attempts sec"
54 return 1
55 fi
56 attempt=$[$attempt+1]
57 echo -n "."
58 sleep 1
59 done
60 return 0
61}
62
Eduardo Sousa09a1e972018-09-21 11:06:32 +010063function is_db_created() {
64 db_host=$1
65 db_port=$2
66 db_user=$3
67 db_pswd=$4
68 db_name=$5
69
Eduardo Sousa07e8a242018-10-08 12:49:14 +010070 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 +010071 echo "DB $db_name exists"
72 return 0
73 else
74 echo "DB $db_name does not exist"
75 return 1
76 fi
77}
78
sousaedu87c40852021-07-28 12:08:43 +020079function is_user_db_created() {
80 db_host=$1
81 db_port=$2
82 db_user=$3
83 db_pswd=$4
84 db_user_to_check=$5
85
86 user_count=$(mysql -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" --default_character_set utf8 -sse "SELECT COUNT(*) FROM mysql.user WHERE user='$db_user_to_check' AND host='%';")
87
88 if [ $user_count -gt 0 ]; then
89 echo "DB User $db_name exists"
90 return 0
91 else
sousaedu9d1d0c12022-01-21 14:01:53 +000092 echo "DB User $db_name does not exist"
sousaedu87c40852021-07-28 12:08:43 +020093 return 1
94 fi
95}
96
Eduardo Sousa09a1e972018-09-21 11:06:32 +010097wait_db "$DB_HOST" "$DB_PORT" || exit 1
98
99is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
sousaedu87c40852021-07-28 12:08:43 +0200100is_user_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && USER_DB_EXISTS="Y"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100101
102if [ -z $DB_EXISTS ]; then
103 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE keystone"
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100104else
David Garcia58b0e322020-03-02 14:17:26 +0100105 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 +0100106 echo "DB keystone is empty"
107 DB_NOT_EMPTY="y"
108 fi
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100109fi
110
sousaedu87c40852021-07-28 12:08:43 +0200111if [ -z $USER_DB_EXISTS ]; then
garciadeblascca4b8e2023-10-10 11:45:57 +0200112 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE USER 'keystone'@'localhost' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
113 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'"
114 mysql -h"$DB_HOST" -P"$DB_PORT" -u"$ROOT_DB_USER" -p"$ROOT_DB_PASSWORD" --default_character_set utf8 -e "CREATE USER 'keystone'@'%' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
115 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'@'%'"
sousaedu87c40852021-07-28 12:08:43 +0200116fi
117
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100118# Setting Keystone database connection
David Garcia58b0e322020-03-02 14:17:26 +0100119sed -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 +0100120
121# Setting Keystone tokens
David Garcia58b0e322020-03-02 14:17:26 +0100122sed -i '/^\[token\]$/,/^\[/ s/^.*provider = .*/provider = fernet/' /etc/keystone/keystone.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100123
David Garcia6fff9af2020-03-23 15:32:43 +0100124
125# Use LDAP authentication for Identity
126if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
127 # Enable Keyston domains
128 sed -i "s%.*domain_specific_drivers_enabled =.*%domain_specific_drivers_enabled = true%" /etc/keystone/keystone.conf
129 sed -i "s%.*domain_config_dir =.*%domain_config_dir = /etc/keystone/domains%" /etc/keystone/keystone.conf
130 mkdir -p /etc/keystone/domains
131 # Configure domain for LDAP authentication
132 cat << EOF > /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
133[identity]
134driver = ldap
135[ldap]
136url = $LDAP_URL
137user_allow_create=false
138user_allow_update=false
139user_allow_delete=false
140group_allow_create=false
141group_allow_update=false
142group_allow_delete=false
143query_scope = sub
144EOF
sousaedu66dc9ed2021-06-16 16:17:48 +0100145 if [ "$LDAP_BIND_USER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100146 echo "user = $LDAP_BIND_USER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
147 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100148 if [ "$LDAP_BIND_PASSWORD" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100149 echo "password = $LDAP_BIND_PASSWORD" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
150 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100151 if [ "$LDAP_CHASE_REFERRALS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100152 echo "chase_referrals = $LDAP_CHASE_REFERRALS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
153 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100154 if [ "$LDAP_PAGE_SIZE" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100155 echo "page_size = $LDAP_PAGE_SIZE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
156 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100157 if [ "$LDAP_USER_TREE_DN" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100158 echo "user_tree_dn = $LDAP_USER_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
159 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100160 if [ "$LDAP_USER_OBJECTCLASS" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100161 echo "user_objectclass = $LDAP_USER_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
162 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100163 if [ "$LDAP_USER_ID_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100164 echo "user_id_attribute = $LDAP_USER_ID_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
165 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100166 if [ "$LDAP_USER_NAME_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100167 echo "user_name_attribute = $LDAP_USER_NAME_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
168 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100169 if [ "$LDAP_USER_PASS_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100170 echo "user_pass_attribute = $LDAP_USER_PASS_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
171 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100172 if [ "$LDAP_USER_FILTER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100173 echo "user_filter = $LDAP_USER_FILTER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
174 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100175 if [ "$LDAP_USER_ENABLED_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100176 echo "user_enabled_attribute = $LDAP_USER_ENABLED_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
177 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100178 if [ "$LDAP_USER_ENABLED_MASK" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100179 echo "user_enabled_mask = $LDAP_USER_ENABLED_MASK" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
180 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100181 if [ "$LDAP_USER_ENABLED_DEFAULT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100182 echo "user_enabled_default = $LDAP_USER_ENABLED_DEFAULT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
183 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100184 if [ "$LDAP_USER_ENABLED_INVERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100185 echo "user_enabled_invert = $LDAP_USER_ENABLED_INVERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
186 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100187 if [ "$LDAP_GROUP_OBJECTCLASS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100188 echo "group_objectclass = $LDAP_GROUP_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
189 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100190 if [ "$LDAP_GROUP_TREE_DN" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100191 echo "group_tree_dn = $LDAP_GROUP_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
192 fi
sousaedu79201022021-06-17 11:04:34 +0100193 if [ "$LDAP_TLS_CACERT_BASE64" ]; then
194 mkdir -p /etc/ssl/certs/
195 echo "-----BEGIN CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
196 echo $LDAP_TLS_CACERT_BASE64 >> /etc/ssl/certs/ca-certificates.crt
197 echo "-----END CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
198 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100199 if [ "$LDAP_USE_STARTTLS" ] && [ "$LDAP_USE_STARTTLS" == "true" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100200 echo "use_tls = true" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
201 mkdir -p /etc/keystone/ssl/certs/
202 echo "-----BEGIN CERTIFICATE-----" > /etc/keystone/ssl/certs/ca.pem
203 echo $LDAP_TLS_CACERT_BASE64 >> /etc/keystone/ssl/certs/ca.pem
204 echo "-----END CERTIFICATE-----" >> /etc/keystone/ssl/certs/ca.pem
205 echo "tls_cacertfile = /etc/keystone/ssl/certs/ca.pem" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
sousaedu66dc9ed2021-06-16 16:17:48 +0100206 if [ "$LDAP_TLS_REQ_CERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100207 echo "tls_req_cert = $LDAP_TLS_REQ_CERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
208 fi
209 fi
210fi
211
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100212# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100213if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
sousaedu9d1d0c12022-01-21 14:01:53 +0000214 keystone-manage db_sync
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100215fi
216
217# Initialize Fernet key repositories
218keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
219keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
220
David Garcia58b0e322020-03-02 14:17:26 +0100221wait_keystone_host
222
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100223# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100224if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
225 keystone-manage bootstrap \
226 --bootstrap-username "$ADMIN_USERNAME" \
227 --bootstrap-password "$ADMIN_PASSWORD" \
228 --bootstrap-project "$ADMIN_PROJECT" \
229 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
230 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
231 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
232 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100233fi
234
David Garcia58b0e322020-03-02 14:17:26 +0100235echo "ServerName $KEYSTONE_HOST" >> /etc/apache2/apache2.conf
sousaedu9d1d0c12022-01-21 14:01:53 +0000236
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100237# Restart Apache Service
238service apache2 restart
239
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100240cat << EOF >> setup_env
241export OS_PROJECT_DOMAIN_NAME=default
242export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100243export OS_PROJECT_NAME=$ADMIN_PROJECT
244export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100245export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100246export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100247export OS_IDENTITY_API_VERSION=3
248export OS_IMAGE_API_VERSION=2
249EOF
250
251source setup_env
252
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100253# Create NBI User
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100254if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
255 openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME"
256 openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT"
Eduardo Sousa4222cc92019-05-22 22:58:51 +0100257 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin
David Garcia6fff9af2020-03-23 15:32:43 +0100258fi
259
260if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
261 if !(openstack domain list | grep -q $LDAP_AUTHENTICATION_DOMAIN_NAME); then
262 # Create domain in keystone for LDAP authentication
263 openstack domain create $LDAP_AUTHENTICATION_DOMAIN_NAME
264 # Restart Apache Service
265 service apache2 restart
266 fi
267 # Check periodically LDAP for updates
268 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 +0100269fi
270
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100271while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100272do
273 sleep 60
274done
275
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100276# Only reaches this point if apache2 stops running
277# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100278exit 1