blob: 78c7820a1de4d9f41282062bcc3bbd34ecb9c81b [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
120 if [ $LDAP_BIND_USER ]; then
121 echo "user = $LDAP_BIND_USER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
122 fi
123 if [ $LDAP_BIND_PASSWORD ]; then
124 echo "password = $LDAP_BIND_PASSWORD" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
125 fi
126 if [ $LDAP_USER_TREE_DN ]; then
127 echo "user_tree_dn = $LDAP_USER_TREE_DN" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
128 fi
129 if [ $LDAP_USER_OBJECTCLASS ]; then
130 echo "user_objectclass = $LDAP_USER_OBJECTCLASS" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
131 fi
132 if [ $LDAP_USER_ID_ATTRIBUTE ]; then
133 echo "user_id_attribute = $LDAP_USER_ID_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
134 fi
135 if [ $LDAP_USER_NAME_ATTRIBUTE ]; then
136 echo "user_name_attribute = $LDAP_USER_NAME_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
137 fi
138 if [ $LDAP_USER_PASS_ATTRIBUTE ]; then
139 echo "user_pass_attribute = $LDAP_USER_PASS_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
140 fi
141 if [ $LDAP_USER_FILTER ]; then
142 echo "user_filter = $LDAP_USER_FILTER" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
143 fi
144 if [ $LDAP_USER_ENABLED_ATTRIBUTE ]; then
145 echo "user_enabled_attribute = $LDAP_USER_ENABLED_ATTRIBUTE" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
146 fi
147 if [ $LDAP_USER_ENABLED_MASK ]; then
148 echo "user_enabled_mask = $LDAP_USER_ENABLED_MASK" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
149 fi
150 if [ $LDAP_USER_ENABLED_DEFAULT ]; then
151 echo "user_enabled_default = $LDAP_USER_ENABLED_DEFAULT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
152 fi
153 if [ $LDAP_USER_ENABLED_INVERT ]; then
154 echo "user_enabled_invert = $LDAP_USER_ENABLED_INVERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
155 fi
156 if [ $LDAP_USE_STARTTLS ] && [ "$LDAP_USE_STARTTLS" == "true" ]; then
157 echo "use_tls = true" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
158 mkdir -p /etc/keystone/ssl/certs/
159 echo "-----BEGIN CERTIFICATE-----" > /etc/keystone/ssl/certs/ca.pem
160 echo $LDAP_TLS_CACERT_BASE64 >> /etc/keystone/ssl/certs/ca.pem
161 echo "-----END CERTIFICATE-----" >> /etc/keystone/ssl/certs/ca.pem
162 echo "tls_cacertfile = /etc/keystone/ssl/certs/ca.pem" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
163 if [ $LDAP_TLS_REQ_CERT ]; then
164 echo "tls_req_cert = $LDAP_TLS_REQ_CERT" >> /etc/keystone/domains/keystone.$LDAP_AUTHENTICATION_DOMAIN_NAME.conf
165 fi
166 fi
167fi
168
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100169# Populate Keystone database
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100170if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100171 su -s /bin/sh -c "keystone-manage db_sync" keystone
172fi
173
174# Initialize Fernet key repositories
175keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
176keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
177
David Garcia58b0e322020-03-02 14:17:26 +0100178wait_keystone_host
179
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100180# Bootstrap Keystone service
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100181if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
182 keystone-manage bootstrap \
183 --bootstrap-username "$ADMIN_USERNAME" \
184 --bootstrap-password "$ADMIN_PASSWORD" \
185 --bootstrap-project "$ADMIN_PROJECT" \
186 --bootstrap-admin-url "http://$KEYSTONE_HOST:5000/v3/" \
187 --bootstrap-internal-url "http://$KEYSTONE_HOST:5000/v3/" \
188 --bootstrap-public-url "http://$KEYSTONE_HOST:5000/v3/" \
189 --bootstrap-region-id "$REGION_ID"
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100190fi
191
David Garcia58b0e322020-03-02 14:17:26 +0100192echo "ServerName $KEYSTONE_HOST" >> /etc/apache2/apache2.conf
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100193# Restart Apache Service
194service apache2 restart
195
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100196cat << EOF >> setup_env
197export OS_PROJECT_DOMAIN_NAME=default
198export OS_USER_DOMAIN_NAME=default
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100199export OS_PROJECT_NAME=$ADMIN_PROJECT
200export OS_USERNAME=$ADMIN_USERNAME
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100201export OS_PASSWORD=$ADMIN_PASSWORD
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100202export OS_AUTH_URL=http://$KEYSTONE_HOST:5000/v3
Eduardo Sousae193dfd2018-09-21 11:37:49 +0100203export OS_IDENTITY_API_VERSION=3
204export OS_IMAGE_API_VERSION=2
205EOF
206
207source setup_env
208
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100209# Create NBI User
Eduardo Sousac50ed8f2019-04-08 17:17:54 +0100210if [ -z $DB_EXISTS ] || [ -z $DB_NOT_EMPTY ]; then
211 openstack user create --domain default --password "$SERVICE_PASSWORD" "$SERVICE_USERNAME"
212 openstack project create --domain default --description "Service Project" "$SERVICE_PROJECT"
Eduardo Sousa4222cc92019-05-22 22:58:51 +0100213 openstack role add --project "$SERVICE_PROJECT" --user "$SERVICE_USERNAME" admin
David Garcia6fff9af2020-03-23 15:32:43 +0100214fi
215
216if [ $LDAP_AUTHENTICATION_DOMAIN_NAME ]; then
217 if !(openstack domain list | grep -q $LDAP_AUTHENTICATION_DOMAIN_NAME); then
218 # Create domain in keystone for LDAP authentication
219 openstack domain create $LDAP_AUTHENTICATION_DOMAIN_NAME
220 # Restart Apache Service
221 service apache2 restart
222 fi
223 # Check periodically LDAP for updates
224 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 +0100225fi
226
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100227while ps -ef | grep -v grep | grep -q apache2
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100228do
229 sleep 60
230done
231
Eduardo Sousa07e8a242018-10-08 12:49:14 +0100232# Only reaches this point if apache2 stops running
233# When this happens exits with error code
Eduardo Sousa09a1e972018-09-21 11:06:32 +0100234exit 1