1

I have a problem with my SH script, I'm writing a SH script that automates the installation of OpenStack Keystone, and also uses a small configuration file, everything works fine but due to a syntax error on my part OpenStack command, script fails when something has already been created, example RabbitMQ user, Project or OpenStack User

The contents of my SH script is this:

#!/usr/bin/env bash

set -e set -o xtrace

source "$PWD/install-keystone.conf"

conf=/etc/keystone/keystone.conf

if [ "$EUID" -ne 0 ]; then echo "To proceed with the installation of OpenStack Keystone you must be root user" echo "Please run as root" exit 1 fi

config_mysql() { apt install mariadb-server python3-pymysql -y

if ! test -f /etc/mysql/mariadb.conf.d/99-openstack.cnf; then cat >> /etc/mysql/mariadb.conf.d/99-openstack.cnf << EOF [mysqld] bind-address = $HOST_IP

default-storage-engine = innodb innodb_file_per_table = on max_connections = 4096 collation-server = utf8_general_ci character-set-server = utf8 EOF fi

service mysql restart

}

config_rabbitmq() { apt install rabbitmq-server -y

rabbitmqctl add_user openstack $RABBITMQ_PASSWORD

rabbitmqctl set_permissions openstack "." "." ".*"

}

install_pkgs() { apt install keystone crudini python3-openstackclient -y }

create_keystone_database() {

mysql -u root -e "CREATE DATABASE IF NOT EXISTS keystone";

mysql -u root -e "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY '$DATABASE_PASSWORD';"; mysql -u root -e "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' IDENTIFIED BY '$DATABASE_PASSWORD';";

}

conf_keystone() {

crudini --set $conf database connection mysql+pymysql://keystone:$DATABASE_PASSWORD@$HOST_IP/keystone crudini --set $conf token provider fernet

su -s /bin/sh -c "keystone-manage db_sync" keystone

keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone keystone-manage credential_setup --keystone-user keystone --keystone-group keystone

keystone-manage bootstrap --bootstrap-password $ADMIN_PASSWORD --bootstrap-admin-url http://$HOST_IP:5000/v3/ --bootstrap-internal-url http://$HOST_IP:5000/v3/ --bootstrap-public-url http://$HOST_IP:5000/v3/ --bootstrap-region-id RegionOne

service apache2 restart }

create_projects_users() { export OS_USERNAME=admin export OS_PASSWORD=$ADMIN_PASSWORD export OS_PROJECT_NAME=admin export OS_USER_DOMAIN_NAME=Default export OS_PROJECT_DOMAIN_NAME=Default export OS_AUTH_URL=http://$HOST_IP:5000/v3 export OS_IDENTITY_API_VERSION=3

openstack project create --domain default --description "Service Project" service

openstack project create --domain default --description "Demo Project" demo

openstack user create --domain default --password $DEMO_PASSWORD demo

openstack role create user

openstack role add --project demo --user demo user

}

request_auth_token() {

openstack --os-auth-url http://$HOST_IP:5000/v3
--os-project-domain-name Default --os-user-domain-name Default
--os-project-name admin --os-username admin --os-password $ADMIN_PASSWORD admin token issue

openstack --os-auth-url http://$HOST_IP:5000/v3
--os-project-domain-name Default --os-user-domain-name Default
--os-project-name demo --os-username demo --os-password $DEMO_PASSWORD token issue

}

create_cli_environment_scripts() {

if ! test -f /root/admin-openrc.sh; then cat >> /root/admin-openrc.sh << EOF export OS_PROJECT_DOMAIN_NAME=Default export OS_USER_DOMAIN_NAME=Default export OS_PROJECT_NAME=admin export OS_USERNAME=admin export OS_PASSWORD=$ADMIN_PASSWORD export OS_AUTH_URL=http://$HOST_IP:5000/v3 export OS_IDENTITY_API_VERSION=3 export OS_IMAGE_API_VERSION=2 EOF fi

if ! test -f /root/demo-openrc.sh; then cat >> /root/demo-openrc.sh << EOF export OS_PROJECT_DOMAIN_NAME=Default export OS_USER_DOMAIN_NAME=Default export OS_PROJECT_NAME=demo export OS_USERNAME=demo export OS_PASSWORD=$DEMO_PASSWORD export OS_AUTH_URL=http://$HOST_IP:5000/v3 export OS_IDENTITY_API_VERSION=3 export OS_IMAGE_API_VERSION=2 EOF fi

}

config_mysql config_rabbitmq install_pkgs create_keystone_database conf_keystone create_projects_users request_auth_token create_cli_environment_scripts

echo "" echo "" echo "" echo "This is your host IP address: $HOST_IP" echo "Keystone URL is available at http://$HOST_IP:5000/" echo "The default users are: admin and demo" echo "The password of administrator is '$ADMIN_PASSWORD'" echo "" echo "OpenStack Keystone has been successfully installed!" echo "Learn more about Keystone in guides and more from : https://docs.openstack.org/keystone/latest/" echo "" echo "Now connect this Keystone installation to other OpenStack services" echo "It is recommended to subsequently install Glance (Image Service) on your controller node" exit

My custom config file:

HOST_IP=
ADMIN_PASSWORD=
DEMO_PASSWORD=
DATABASE_PASSWORD=
RABBITMQ_PASSWORD=

I realized that it initially failed because I made a mistake in the --os-username section I didn't enter the username and the script failed because of me

now as soon as it comes to OpenStack user creation on RabbitMQ it says the user already exists but the script fails without ignoring that message

Is there a way to ignore the message?

And I use set -e, to avoid having the script continue due to an error, but even if something exists it still marks it as an error and the script doesn't proceed

  • 1
    Thank you very much, I think this question is a duplicate, the question you sent me is useful to me! – Steforgame 910 Oct 31 '23 at 09:25
  • If you want to rerun the script without modifications, you could delete the openstack user with rabbitmqctl. – ctx Oct 31 '23 at 11:55

0 Answers0