blob: d7fa2abd55d8bd21940f92a128953a25d03a34c8 [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
112 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'"
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'@'%' IDENTIFIED BY '$KEYSTONE_DB_PASSWORD'"
114fi
115
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100116# Setting Keystone database connection
David Garcia58b0e322020-03-02 14:17:26 +0100117sed -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 +0100118
119# Setting Keystone tokens
David Garcia58b0e322020-03-02 14:17:26 +0100120sed -i '/^\[token\]$/,/^\[/ s/^.*provider = .*/provider = fernet/' /etc/keystone/keystone.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100121
David Garcia6fff9af2020-03-23 15:32:43 +0100122
123# Use LDAP authentication for Identity
124if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
125 # Enable Keyston domains
126 sed -i "s%.*domain_specific_drivers_enabled =.*%domain_specific_drivers_enabled = true%" /etc/keystone/keystone.conf
127 sed -i "s%.*domain_config_dir =.*%domain_config_dir = /etc/keystone/domains%" /etc/keystone/keystone.conf
128 mkdir -p /etc/keystone/domains
129 # Configure domain for LDAP authentication
130 cat << EOF > /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
131[identity]
132driver = ldap
133[ldap]
134url = $LDAP_URL
135user_allow_create=false
136user_allow_update=false
137user_allow_delete=false
138group_allow_create=false
139group_allow_update=false
140group_allow_delete=false
141query_scope = sub
142EOF
sousaedu66dc9ed2021-06-16 16:17:48 +0100143 if [ "$LDAP_BIND_USER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100144 echo "user = $LDAP_BIND_USER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
145 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100146 if [ "$LDAP_BIND_PASSWORD" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100147 echo "password = $LDAP_BIND_PASSWORD" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
148 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100149 if [ "$LDAP_CHASE_REFERRALS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100150 echo "chase_referrals = $LDAP_CHASE_REFERRALS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
151 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100152 if [ "$LDAP_PAGE_SIZE" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100153 echo "page_size = $LDAP_PAGE_SIZE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
154 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100155 if [ "$LDAP_USER_TREE_DN" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100156 echo "user_tree_dn = $LDAP_USER_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
157 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100158 if [ "$LDAP_USER_OBJECTCLASS" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100159 echo "user_objectclass = $LDAP_USER_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
160 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100161 if [ "$LDAP_USER_ID_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100162 echo "user_id_attribute = $LDAP_USER_ID_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
163 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100164 if [ "$LDAP_USER_NAME_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100165 echo "user_name_attribute = $LDAP_USER_NAME_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
166 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100167 if [ "$LDAP_USER_PASS_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100168 echo "user_pass_attribute = $LDAP_USER_PASS_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
169 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100170 if [ "$LDAP_USER_FILTER" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100171 echo "user_filter = $LDAP_USER_FILTER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
172 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100173 if [ "$LDAP_USER_ENABLED_ATTRIBUTE" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100174 echo "user_enabled_attribute = $LDAP_USER_ENABLED_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
175 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100176 if [ "$LDAP_USER_ENABLED_MASK" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100177 echo "user_enabled_mask = $LDAP_USER_ENABLED_MASK" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
178 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100179 if [ "$LDAP_USER_ENABLED_DEFAULT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100180 echo "user_enabled_default = $LDAP_USER_ENABLED_DEFAULT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
181 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100182 if [ "$LDAP_USER_ENABLED_INVERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100183 echo "user_enabled_invert = $LDAP_USER_ENABLED_INVERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
184 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100185 if [ "$LDAP_GROUP_OBJECTCLASS" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100186 echo "group_objectclass = $LDAP_GROUP_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
187 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100188 if [ "$LDAP_GROUP_TREE_DN" ]; then
sousaedubb631be2020-10-20 01:15:37 +0100189 echo "group_tree_dn = $LDAP_GROUP_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
190 fi
sousaedu79201022021-06-17 11:04:34 +0100191 if [ "$LDAP_TLS_CACERT_BASE64" ]; then
192 mkdir -p /etc/ssl/certs/
193 echo "-----BEGIN CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
194 echo $LDAP_TLS_CACERT_BASE64 >> /etc/ssl/certs/ca-certificates.crt
195 echo "-----END CERTIFICATE-----" >> /etc/ssl/certs/ca-certificates.crt
196 fi
sousaedu66dc9ed2021-06-16 16:17:48 +0100197 if [ "$LDAP_USE_STARTTLS" ] && [ "$LDAP_USE_STARTTLS" == "true" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100198 echo "use_tls = true" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
199 mkdir -p /etc/keystone/ssl/certs/
200 echo "-----BEGIN CERTIFICATE-----" > /etc/keystone/ssl/certs/ca.pem
201 echo $LDAP_TLS_CACERT_BASE64 >> /etc/keystone/ssl/certs/ca.pem
202 echo "-----END CERTIFICATE-----" >> /etc/keystone/ssl/certs/ca.pem
203 echo "tls_cacertfile = /etc/keystone/ssl/certs/ca.pem" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
sousaedu66dc9ed2021-06-16 16:17:48 +0100204 if [ "$LDAP_TLS_REQ_CERT" ]; then
David Garcia6fff9af2020-03-23 15:32:43 +0100205 echo "tls_req_cert = $LDAP_TLS_REQ_CERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
206 fi
207 fi
208fi
209
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100210# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100211if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
sousaedu9d1d0c12022-01-21 14:01:53 +0000212 keystone-manage db_sync
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100213fi
214
215# Initialize Fernet key repositories
216keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
217keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
218
David Garcia58b0e322020-03-02 14:17:26 +0100219wait_keystone_host
220
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100221# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100222if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
223 keystone-manage bootstrap \
224 --bootstrap-username "$ADMIN_USERNAME" \
225 --bootstrap-password "$ADMIN_PASSWORD" \
226 --bootstrap-project "$ADMIN_PROJECT" \
227 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
228 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
229 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
230 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100231fi
232
David Garcia58b0e322020-03-02 14:17:26 +0100233echo "ServerName $KEYSTONE_HOST" >> /etc/apache2/apache2.conf
sousaedu9d1d0c12022-01-21 14:01:53 +0000234
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100235# Restart Apache Service
236service apache2 restart
237
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100238cat << EOF >> setup_env
239export OS_PROJECT_DOMAIN_NAME=default
240export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100241export OS_PROJECT_NAME=$ADMIN_PROJECT
242export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100243export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100244export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100245export OS_IDENTITY_API_VERSION=3
246export OS_IMAGE_API_VERSION=2
247EOF
248
249source setup_env
250
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100251# Create NBI User
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100252if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
253 openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME"
254 openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT"
Eduardo Sousa4222cc92019-05-22 22:58:51 +0100255 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin
David Garcia6fff9af2020-03-23 15:32:43 +0100256fi
257
258if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
259 if !(openstack domain list | grep -q $LDAP_AUTHENTICATION_DOMAIN_NAME); then
260 # Create domain in keystone for LDAP authentication
261 openstack domain create $LDAP_AUTHENTICATION_DOMAIN_NAME
262 # Restart Apache Service
263 service apache2 restart
264 fi
265 # Check periodically LDAP for updates
266 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 +0100267fi
268
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100269while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100270do
271 sleep 60
272done
273
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100274# Only reaches this point if apache2 stops running
275# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100276exit 1