|
| 1 | +# lib/database |
| 2 | +# Interface for interacting with different database backends |
| 3 | + |
| 4 | +# Dependencies: |
| 5 | +# DATABASE_BACKENDS variable must contain a list of available database backends |
| 6 | +# DATABASE_TYPE variable must be set |
| 7 | + |
| 8 | +# Each database must implement four functions: |
| 9 | +# recreate_database_$DATABASE_TYPE |
| 10 | +# install_database_$DATABASE_TYPE |
| 11 | +# configure_database_$DATABASE_TYPE |
| 12 | +# database_connection_url_$DATABASE_TYPE |
| 13 | +# |
| 14 | +# and call register_database $DATABASE_TYPE |
| 15 | + |
| 16 | +# Save trace setting |
| 17 | +XTRACE=$(set +o | grep xtrace) |
| 18 | +set +o xtrace |
| 19 | + |
| 20 | +# Register a database backend |
| 21 | +# $1 The name of the database backend |
| 22 | +function register_database { |
| 23 | + [ -z "$DATABASE_BACKENDS" ] && DATABASE_BACKENDS=$1 || DATABASE_BACKENDS+=" $1" |
| 24 | +} |
| 25 | + |
| 26 | +for f in $TOP_DIR/lib/databases/*; do source $f; done |
| 27 | + |
| 28 | +# Set the database type based on the configuration |
| 29 | +function initialize_database_backends { |
| 30 | + for backend in $DATABASE_BACKENDS; do |
| 31 | + is_service_enabled $backend && DATABASE_TYPE=$backend |
| 32 | + done |
| 33 | + |
| 34 | + [ -z "$DATABASE_TYPE" ] && return 1 |
| 35 | + |
| 36 | + # For backward-compatibility, read in the MYSQL_HOST/USER variables and use |
| 37 | + # them as the default values for the DATABASE_HOST/USER variables. |
| 38 | + MYSQL_HOST=${MYSQL_HOST:-localhost} |
| 39 | + MYSQL_USER=${MYSQL_USER:-root} |
| 40 | + |
| 41 | + DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}} |
| 42 | + DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}} |
| 43 | + |
| 44 | + if [ -n "$MYSQL_PASSWORD" ]; then |
| 45 | + DATABASE_PASSWORD=$MYSQL_PASSWORD |
| 46 | + else |
| 47 | + read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE." |
| 48 | + fi |
| 49 | + |
| 50 | + # We configure Nova, Horizon, Glance and Keystone to use MySQL as their |
| 51 | + # database server. While they share a single server, each has their own |
| 52 | + # database and tables. |
| 53 | + |
| 54 | + # By default this script will install and configure MySQL. If you want to |
| 55 | + # use an existing server, you can pass in the user/password/host parameters. |
| 56 | + # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing |
| 57 | + # a multi-node DevStack installation. |
| 58 | + |
| 59 | + # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services |
| 60 | + BASE_SQL_CONN=${BASE_SQL_CONN:-${DATABASE_TYPE}://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST} |
| 61 | + |
| 62 | + return 0 |
| 63 | +} |
| 64 | + |
| 65 | +# Set the database backend to use |
| 66 | +# $1 The name of the database backend to use (mysql, postgresql, ...) |
| 67 | +function use_database { |
| 68 | + use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1 && return 0 |
| 69 | + ret=$? |
| 70 | + echo "Invalid database '$1'" |
| 71 | + return $ret |
| 72 | +} |
| 73 | + |
| 74 | +# Recreate a given database |
| 75 | +# $1 The name of the database |
| 76 | +# $2 The character set/encoding of the database |
| 77 | +function recreate_database { |
| 78 | + local db=$1 |
| 79 | + local charset=$2 |
| 80 | + recreate_database_$DATABASE_TYPE $db $charset |
| 81 | +} |
| 82 | + |
| 83 | +# Install the database |
| 84 | +function install_database { |
| 85 | + install_database_$DATABASE_TYPE |
| 86 | +} |
| 87 | + |
| 88 | +# Configure and start the database |
| 89 | +function configure_database { |
| 90 | + configure_database_$DATABASE_TYPE |
| 91 | +} |
| 92 | + |
| 93 | +# Generate an SQLAlchemy connection URL and store it in a variable |
| 94 | +# $1 The variable name in which to store the connection URL |
| 95 | +# $2 The name of the database |
| 96 | +function database_connection_url { |
| 97 | + local var=$1 |
| 98 | + local db=$2 |
| 99 | + database_connection_url_$DATABASE_TYPE $var $db |
| 100 | +} |
| 101 | + |
| 102 | +# Restore xtrace |
| 103 | +$XTRACE |
0 commit comments