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