lib/database
e263c82e
 #!/bin/bash
 #
428af5a2
 # lib/database
 # Interface for interacting with different database backends
 
 # Dependencies:
afc29fe5
 # ``ENABLED_SERVICES`` must be defined
428af5a2
 
afc29fe5
 # ``DATABASE_BACKENDS`` will contain a list of available database backends
 # after sourcing this file.
 
 # This is a wrapper for the specific database backends available.
428af5a2
 # Each database must implement four functions:
cb961597
 #
 # - recreate_database_$DATABASE_TYPE
 # - install_database_$DATABASE_TYPE
 # - configure_database_$DATABASE_TYPE
 # - database_connection_url_$DATABASE_TYPE
428af5a2
 #
 # and call register_database $DATABASE_TYPE
 
 # Save trace setting
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
 
53753293
 DATABASE_BACKENDS=""
cc6b4435
 
428af5a2
 # Register a database backend
cb961597
 #
 #   $1 The name of the database backend
 #
cc6b4435
 # This is required to be defined before the specific database scripts are sourced
428af5a2
 function register_database {
53753293
     DATABASE_BACKENDS+=" $1"
428af5a2
 }
 
afc29fe5
 # Sourcing the database libs sets DATABASE_BACKENDS with the available list
cc6b4435
 for f in $TOP_DIR/lib/databases/*; do
     source $f;
 done
428af5a2
 
afc29fe5
 # ``DATABASE_BACKENDS`` now contains a list of the supported databases
 # Look in ``ENABLED_SERVICES`` to see if one has been selected
 for db in $DATABASE_BACKENDS; do
     # Set the type for the rest of the backend to use
     if is_service_enabled $db; then
0729d06f
         # Set this now for the rest of the database functions
afc29fe5
         DATABASE_TYPE=$db
     fi
 done
 # If ``DATABASE_TYPE`` is unset here no database was selected
 # This is not an error as multi-node installs will do this on the compute nodes
 
 
cc6b4435
 # Functions
 # ---------
 
995eb927
 # Get rid of everything enough to cleanly change database backends
 function cleanup_database {
     cleanup_database_$DATABASE_TYPE
 }
 
428af5a2
 # Set the database type based on the configuration
 function initialize_database_backends {
     for backend in $DATABASE_BACKENDS; do
         is_service_enabled $backend && DATABASE_TYPE=$backend
     done
 
     [ -z "$DATABASE_TYPE" ] && return 1
 
     # For backward-compatibility, read in the MYSQL_HOST/USER variables and use
     # them as the default values for the DATABASE_HOST/USER variables.
180f5eb6
     MYSQL_HOST=${MYSQL_HOST:-$SERVICE_LOCAL_HOST}
428af5a2
     MYSQL_USER=${MYSQL_USER:-root}
 
180f5eb6
     # Set DATABASE_HOST equal to MYSQL_HOST. If SERVICE_IP_VERSION is equal to 6,
     # set DATABASE_HOST equal to [MYSQL_HOST]. MYSQL_HOST cannot use brackets due
     # to mysql not using bracketing for IPv6 addresses. DATABASE_HOST must have brackets
     # due to sqlalchemy only reading IPv6 addresses with brackets.
     if [[ "$SERVICE_IP_VERSION" == 6 ]]; then
         DATABASE_HOST=${DATABASE_HOST:-[$MYSQL_HOST]}
     else
         DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}}
     fi
 
428af5a2
     DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}}
 
     if [ -n "$MYSQL_PASSWORD" ]; then
         DATABASE_PASSWORD=$MYSQL_PASSWORD
     else
         read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE."
     fi
 
     # We configure Nova, Horizon, Glance and Keystone to use MySQL as their
     # database server.  While they share a single server, each has their own
     # database and tables.
 
     # By default this script will install and configure MySQL.  If you want to
     # use an existing server, you can pass in the user/password/host parameters.
     # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing
     # a multi-node DevStack installation.
 
     # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services
0eec4f86
     BASE_SQL_CONN=${BASE_SQL_CONN:-$(get_database_type_$DATABASE_TYPE)://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
428af5a2
 
     return 0
 }
 
 # Recreate a given database
 #  $1 The name of the database
 function recreate_database {
     local db=$1
157c84b8
     recreate_database_$DATABASE_TYPE $db
428af5a2
 }
 
 # Install the database
 function install_database {
     install_database_$DATABASE_TYPE
 }
 
5686dbc4
 # Install the database Python packages
 function install_database_python {
     install_database_python_$DATABASE_TYPE
 }
 
428af5a2
 # Configure and start the database
 function configure_database {
     configure_database_$DATABASE_TYPE
 }
 
0729d06f
 # Generate an SQLAlchemy connection URL and output it using echo
 #  $1 The name of the database
428af5a2
 function database_connection_url {
0729d06f
     local db=$1
     database_connection_url_$DATABASE_TYPE $db
428af5a2
 }
 
cc6b4435
 
428af5a2
 # Restore xtrace
 $XTRACE
584d90ec
 
6a5aa7c6
 # Tell emacs to use shell-script-mode
 ## Local variables:
 ## mode: shell-script
 ## End: