Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 1 | # lib/database |
| 2 | # Interface for interacting with different database backends |
| 3 | |
| 4 | # Dependencies: |
Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 5 | # ``ENABLED_SERVICES`` must be defined |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 6 | |
Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 7 | # ``DATABASE_BACKENDS`` will contain a list of available database backends |
| 8 | # after sourcing this file. |
| 9 | |
| 10 | # This is a wrapper for the specific database backends available. |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 11 | # Each database must implement four functions: |
| 12 | # recreate_database_$DATABASE_TYPE |
| 13 | # install_database_$DATABASE_TYPE |
| 14 | # configure_database_$DATABASE_TYPE |
| 15 | # database_connection_url_$DATABASE_TYPE |
| 16 | # |
| 17 | # and call register_database $DATABASE_TYPE |
| 18 | |
| 19 | # Save trace setting |
| 20 | XTRACE=$(set +o | grep xtrace) |
| 21 | set +o xtrace |
| 22 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 23 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 24 | # Register a database backend |
| 25 | # $1 The name of the database backend |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 26 | # This is required to be defined before the specific database scripts are sourced |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 27 | function register_database { |
| 28 | [ -z "$DATABASE_BACKENDS" ] && DATABASE_BACKENDS=$1 || DATABASE_BACKENDS+=" $1" |
| 29 | } |
| 30 | |
Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 31 | # Sourcing the database libs sets DATABASE_BACKENDS with the available list |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 32 | for f in $TOP_DIR/lib/databases/*; do |
| 33 | source $f; |
| 34 | done |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 35 | |
Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 36 | # ``DATABASE_BACKENDS`` now contains a list of the supported databases |
| 37 | # Look in ``ENABLED_SERVICES`` to see if one has been selected |
| 38 | for db in $DATABASE_BACKENDS; do |
| 39 | # Set the type for the rest of the backend to use |
| 40 | if is_service_enabled $db; then |
Tal Kain | 0729d06 | 2013-04-22 17:50:27 +0300 | [diff] [blame^] | 41 | # Set this now for the rest of the database functions |
Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 42 | DATABASE_TYPE=$db |
| 43 | fi |
| 44 | done |
| 45 | # If ``DATABASE_TYPE`` is unset here no database was selected |
| 46 | # This is not an error as multi-node installs will do this on the compute nodes |
| 47 | |
| 48 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 49 | # Functions |
| 50 | # --------- |
| 51 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 52 | # Get rid of everything enough to cleanly change database backends |
| 53 | function cleanup_database { |
| 54 | cleanup_database_$DATABASE_TYPE |
| 55 | } |
| 56 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 57 | # Set the database type based on the configuration |
| 58 | function initialize_database_backends { |
| 59 | for backend in $DATABASE_BACKENDS; do |
| 60 | is_service_enabled $backend && DATABASE_TYPE=$backend |
| 61 | done |
| 62 | |
| 63 | [ -z "$DATABASE_TYPE" ] && return 1 |
| 64 | |
| 65 | # For backward-compatibility, read in the MYSQL_HOST/USER variables and use |
| 66 | # them as the default values for the DATABASE_HOST/USER variables. |
| 67 | MYSQL_HOST=${MYSQL_HOST:-localhost} |
| 68 | MYSQL_USER=${MYSQL_USER:-root} |
| 69 | |
| 70 | DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}} |
| 71 | DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}} |
| 72 | |
| 73 | if [ -n "$MYSQL_PASSWORD" ]; then |
| 74 | DATABASE_PASSWORD=$MYSQL_PASSWORD |
| 75 | else |
| 76 | read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE." |
| 77 | fi |
| 78 | |
| 79 | # We configure Nova, Horizon, Glance and Keystone to use MySQL as their |
| 80 | # database server. While they share a single server, each has their own |
| 81 | # database and tables. |
| 82 | |
| 83 | # By default this script will install and configure MySQL. If you want to |
| 84 | # use an existing server, you can pass in the user/password/host parameters. |
| 85 | # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing |
| 86 | # a multi-node DevStack installation. |
| 87 | |
| 88 | # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services |
| 89 | BASE_SQL_CONN=${BASE_SQL_CONN:-${DATABASE_TYPE}://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST} |
| 90 | |
| 91 | return 0 |
| 92 | } |
| 93 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 94 | # Recreate a given database |
| 95 | # $1 The name of the database |
| 96 | # $2 The character set/encoding of the database |
| 97 | function recreate_database { |
| 98 | local db=$1 |
| 99 | local charset=$2 |
| 100 | recreate_database_$DATABASE_TYPE $db $charset |
| 101 | } |
| 102 | |
| 103 | # Install the database |
| 104 | function install_database { |
| 105 | install_database_$DATABASE_TYPE |
| 106 | } |
| 107 | |
| 108 | # Configure and start the database |
| 109 | function configure_database { |
| 110 | configure_database_$DATABASE_TYPE |
| 111 | } |
| 112 | |
Tal Kain | 0729d06 | 2013-04-22 17:50:27 +0300 | [diff] [blame^] | 113 | # Generate an SQLAlchemy connection URL and output it using echo |
| 114 | # $1 The name of the database |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 115 | function database_connection_url { |
Tal Kain | 0729d06 | 2013-04-22 17:50:27 +0300 | [diff] [blame^] | 116 | local db=$1 |
| 117 | database_connection_url_$DATABASE_TYPE $db |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 120 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 121 | # Restore xtrace |
| 122 | $XTRACE |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 123 | |
| 124 | # Local variables: |
| 125 | # mode: shell-script |
| 126 | # End: |