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