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