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