blob: 7b4e008a20a9b60ca9ebff5be22a9b9475e36f8e [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
vegall7badcae2024-07-02 15:30:30 +000021set -e
22
Eduardo Sousa07e8a242018-10-08 12:49:14 +010023DB_EXISTS=""
sousaedu87c40852021-07-28 12:08:43 +020024USER_DB_EXISTS=""
Eduardo Sousac50ed8f2019-04-08 17:17:54 +010025DB_NOT_EMPTY=""
Eduardo Sousa07e8a242018-10-08 12:49:14 +010026
Eduardo Sousa09a1e972018-09-21 11:06:32 +010027max_attempts=120
28function wait_db(){
29 db_host=$1
30 db_port=$2
31 attempt=0
32 echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} "
33 while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do
34 #wait 120 sec
35 if [ $attempt -ge $max_attempts ]; then
36 echo
37 echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec"
38 return 1
39 fi
40 attempt=$[$attempt+1]
41 echo -n "."
42 sleep 1
43 done
44 return 0
45}
46
David Garcia58b0e322020-03-02 14:17:26 +010047function wait_keystone_host(){
48 attempt=0
49 timeout=2
50 echo "Wait until Keystone hostname can be resolved "
51 while ! nslookup $KEYSTONE_HOST; do
52 #wait 120 sec
53 if [ $attempt -ge $max_attempts ]; then
54 echo
55 echo "Can not resolve ${KEYSTONE_HOST} during $max_attempts sec"
56 return 1
57 fi
58 attempt=$[$attempt+1]
59 echo -n "."
60 sleep 1
61 done
62 return 0
63}
64
Eduardo Sousa09a1e972018-09-21 11:06:32 +010065function is_db_created() {
66 db_host=$1
67 db_port=$2
68 db_user=$3
69 db_pswd=$4
70 db_name=$5
71
Eduardo Sousa07e8a242018-10-08 12:49:14 +010072 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 +010073 echo "DB $db_name exists"
74 return 0
75 else
76 echo "DB $db_name does not exist"
77 return 1
78 fi
79}
80
sousaedu87c40852021-07-28 12:08:43 +020081function is_user_db_created() {
82 db_host=$1
83 db_port=$2
84 db_user=$3
85 db_pswd=$4
86 db_user_to_check=$5
87
88 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='%';")
89
90 if [ $user_count -gt 0 ]; then
91 echo "DB User $db_name exists"
92 return 0
93 else
sousaedu9d1d0c12022-01-21 14:01:53 +000094 echo "DB User $db_name does not exist"
sousaedu87c40852021-07-28 12:08:43 +020095 return 1
96 fi
97}
98
Eduardo Sousa09a1e972018-09-21 11:06:32 +010099wait_db "$DB_HOST" "$DB_PORT" || exit 1
100
101is_db_created "$DB_HOST" "$DB_PORT" "$ROOT_DB_USER" "$ROOT_DB_PASSWORD" "keystone" && DB_EXISTS="Y"
sousaedu87c40852021-07-28 12:08:43 +0200102is_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 +0100103
104if [ -z $DB_EXISTS ]; then
105 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 +0100106else
David Garcia58b0e322020-03-02 14:17:26 +0100107 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 +0100108 echo "DB keystone is empty"
109 DB_NOT_EMPTY="y"
110 fi
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100111fi
112
sousaedu87c40852021-07-28 12:08:43 +0200113if [ -z $USER_DB_EXISTS ]; then
garciadeblascca4b8e2023-10-10 11:45:57 +0200114 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'"
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'@'localhost'"
116 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'"
117 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 +0200118fi
119
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100120# Setting Keystone database connection
David Garcia58b0e322020-03-02 14:17:26 +0100121sed -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 +0100122
123# Setting Keystone tokens
David Garcia58b0e322020-03-02 14:17:26 +0100124sed -i '/^\[token\]$/,/^\[/ s/^.*provider = .*/provider = fernet/' /etc/keystone/keystone.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100125
vegall7badcae2024-07-02 15:30:30 +0000126# Setting Keystone for the stderr
127sed -i '/\[DEFAULT\]/a use_stderr = true' /etc/keystone/keystone.conf
David Garcia6fff9af2020-03-23 15:32:43 +0100128
129# Use LDAP authentication for Identity
130if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
131 # Enable Keyston domains
132 sed -i "s%.*domain_specific_drivers_enabled =.*%domain_specific_drivers_enabled = true%" /etc/keystone/keystone.conf
133 sed -i "s%.*domain_config_dir =.*%domain_config_dir = /etc/keystone/domains%" /etc/keystone/keystone.conf
134 mkdir -p /etc/keystone/domains
135 # Configure domain for LDAP authentication
136 cat << EOF > /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
137[identity]
138driver = ldap
139[ldap]
140url = $LDAP_URL
141user_allow_create=false
142user_allow_update=false
143user_allow_delete=false
144group_allow_create=false
145group_allow_update=false
146group_allow_delete=false
147query_scope = sub
148EOF
sousaedu66dc9ed2021-06-16 16:17:48 +0100149 if [ "$LDAP_BIND_USER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100150 echo "user = $LDAP_BIND_USER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
151 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100152 if [ "$LDAP_BIND_PASSWORD" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100153 echo "password = $LDAP_BIND_PASSWORD" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
154 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100155 if [ "$LDAP_CHASE_REFERRALS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100156 echo "chase_referrals = $LDAP_CHASE_REFERRALS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
157 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100158 if [ "$LDAP_PAGE_SIZE" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100159 echo "page_size = $LDAP_PAGE_SIZE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
160 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100161 if [ "$LDAP_USER_TREE_DN" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100162 echo "user_tree_dn = $LDAP_USER_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
163 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100164 if [ "$LDAP_USER_OBJECTCLASS" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100165 echo "user_objectclass = $LDAP_USER_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
166 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100167 if [ "$LDAP_USER_ID_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100168 echo "user_id_attribute = $LDAP_USER_ID_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
169 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100170 if [ "$LDAP_USER_NAME_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100171 echo "user_name_attribute = $LDAP_USER_NAME_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
172 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100173 if [ "$LDAP_USER_PASS_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100174 echo "user_pass_attribute = $LDAP_USER_PASS_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
175 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100176 if [ "$LDAP_USER_FILTER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100177 echo "user_filter = $LDAP_USER_FILTER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
178 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100179 if [ "$LDAP_USER_ENABLED_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100180 echo "user_enabled_attribute = $LDAP_USER_ENABLED_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
181 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100182 if [ "$LDAP_USER_ENABLED_MASK" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100183 echo "user_enabled_mask = $LDAP_USER_ENABLED_MASK" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
184 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100185 if [ "$LDAP_USER_ENABLED_DEFAULT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100186 echo "user_enabled_default = $LDAP_USER_ENABLED_DEFAULT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
187 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100188 if [ "$LDAP_USER_ENABLED_INVERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100189 echo "user_enabled_invert = $LDAP_USER_ENABLED_INVERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
190 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100191 if [ "$LDAP_GROUP_OBJECTCLASS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100192 echo "group_objectclass = $LDAP_GROUP_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
193 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100194 if [ "$LDAP_GROUP_TREE_DN" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100195 echo "group_tree_dn = $LDAP_GROUP_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
196 fi
sousaedu79201022021-06-17 11:04:34 +0100197 if [ "$LDAP_TLS_CACERT_BASE64" ]; then
198 mkdir -p /etc/ssl/certs/
199 echo "-----BEGIN CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
200 echo $LDAP_TLS_CACERT_BASE64 >> /etc/ssl/certs/ca-certificates.crt
201 echo "-----END CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
202 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100203 if [ "$LDAP_USE_STARTTLS" ] && [ "$LDAP_USE_STARTTLS" == "true" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100204 echo "use_tls = true" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
205 mkdir -p /etc/keystone/ssl/certs/
206 echo "-----BEGIN CERTIFICATE-----" > /etc/keystone/ssl/certs/ca.pem
207 echo $LDAP_TLS_CACERT_BASE64 >> /etc/keystone/ssl/certs/ca.pem
208 echo "-----END CERTIFICATE-----" >> /etc/keystone/ssl/certs/ca.pem
209 echo "tls_cacertfile = /etc/keystone/ssl/certs/ca.pem" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
sousaedu66dc9ed2021-06-16 16:17:48 +0100210 if [ "$LDAP_TLS_REQ_CERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100211 echo "tls_req_cert = $LDAP_TLS_REQ_CERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
212 fi
213 fi
214fi
215
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100216# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100217if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
sousaedu9d1d0c12022-01-21 14:01:53 +0000218 keystone-manage db_sync
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100219fi
220
221# Initialize Fernet key repositories
222keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
223keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
224
David Garcia58b0e322020-03-02 14:17:26 +0100225wait_keystone_host
226
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100227# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100228if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
vegall7badcae2024-07-02 15:30:30 +0000229 echo "Bootstraping keystone"
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100230 keystone-manage bootstrap \
231 --bootstrap-username "$ADMIN_USERNAME" \
232 --bootstrap-password "$ADMIN_PASSWORD" \
233 --bootstrap-project "$ADMIN_PROJECT" \
234 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
235 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
236 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
237 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100238fi
239
David Garcia58b0e322020-03-02 14:17:26 +0100240echo "ServerName $KEYSTONE_HOST" >> /etc/apache2/apache2.conf
sousaedu9d1d0c12022-01-21 14:01:53 +0000241
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100242# Restart Apache Service
243service apache2 restart
244
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100245cat << EOF >> setup_env
246export OS_PROJECT_DOMAIN_NAME=default
247export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100248export OS_PROJECT_NAME=$ADMIN_PROJECT
249export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100250export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100251export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100252export OS_IDENTITY_API_VERSION=3
253export OS_IMAGE_API_VERSION=2
254EOF
255
256source setup_env
257
vegall7badcae2024-07-02 15:30:30 +0000258# Function to retry a command up to 5 times
259retry() {
260 local n=1
261 local max=5
262 local delay=5
263 while true; do
264 "$@" && break || {
265 if [[ $n -lt $max ]]; then
266 ((n++))
267 echo "Command failed. Attempt $n/$max:"
268 sleep $delay;
269 else
270 echo "The command has failed after $n attempts."
271 return 1
272 fi
273 }
274 done
275}
276
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100277# Create NBI User
vegall7badcae2024-07-02 15:30:30 +0000278if ! openstack user show nbi --domain default; then
279 echo "NBI user does not exist. Creating nbi user"
280 retry openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME" || exit 1
281 retry openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT" || exit 1
282 retry openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin || exit 1
David Garcia6fff9af2020-03-23 15:32:43 +0100283fi
vegall7badcae2024-07-02 15:30:30 +0000284echo "Done creating the NBI user"
David Garcia6fff9af2020-03-23 15:32:43 +0100285
286if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
287 if !(openstack domain list | grep -q $LDAP_AUTHENTICATION_DOMAIN_NAME); then
288 # Create domain in keystone for LDAP authentication
289 openstack domain create $LDAP_AUTHENTICATION_DOMAIN_NAME
290 # Restart Apache Service
291 service apache2 restart
292 fi
293 # Check periodically LDAP for updates
294 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 +0100295fi
296
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100297while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100298do
vegall7badcae2024-07-02 15:30:30 +0000299 tail -f /var/log/keystone/keystone-manage.log
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100300done
301
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100302# Only reaches this point if apache2 stops running
303# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100304exit 1