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 |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 23 | _XTRACE_LIB_DB=$(set +o | grep xtrace) |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 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. |
Brian Haley | 180f5eb | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 73 | MYSQL_HOST=${MYSQL_HOST:-$SERVICE_LOCAL_HOST} |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 74 | MYSQL_USER=${MYSQL_USER:-root} |
| 75 | |
Brian Haley | 180f5eb | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 76 | # Set DATABASE_HOST equal to MYSQL_HOST. If SERVICE_IP_VERSION is equal to 6, |
| 77 | # set DATABASE_HOST equal to [MYSQL_HOST]. MYSQL_HOST cannot use brackets due |
| 78 | # to mysql not using bracketing for IPv6 addresses. DATABASE_HOST must have brackets |
| 79 | # due to sqlalchemy only reading IPv6 addresses with brackets. |
| 80 | if [[ "$SERVICE_IP_VERSION" == 6 ]]; then |
| 81 | DATABASE_HOST=${DATABASE_HOST:-[$MYSQL_HOST]} |
| 82 | else |
| 83 | DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}} |
| 84 | fi |
| 85 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 86 | DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}} |
| 87 | |
| 88 | if [ -n "$MYSQL_PASSWORD" ]; then |
| 89 | DATABASE_PASSWORD=$MYSQL_PASSWORD |
| 90 | else |
| 91 | read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE." |
| 92 | fi |
| 93 | |
| 94 | # We configure Nova, Horizon, Glance and Keystone to use MySQL as their |
| 95 | # database server. While they share a single server, each has their own |
| 96 | # database and tables. |
| 97 | |
| 98 | # By default this script will install and configure MySQL. If you want to |
| 99 | # use an existing server, you can pass in the user/password/host parameters. |
| 100 | # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing |
| 101 | # a multi-node DevStack installation. |
| 102 | |
| 103 | # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services |
Julien Danjou | 0eec4f8 | 2015-09-08 10:45:06 +0000 | [diff] [blame] | 104 | BASE_SQL_CONN=${BASE_SQL_CONN:-$(get_database_type_$DATABASE_TYPE)://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST} |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 105 | |
| 106 | return 0 |
| 107 | } |
| 108 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 109 | # Recreate a given database |
| 110 | # $1 The name of the database |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 111 | function recreate_database { |
| 112 | local db=$1 |
Ihar Hrachyshka | 157c84b | 2014-10-06 13:29:39 +0200 | [diff] [blame] | 113 | recreate_database_$DATABASE_TYPE $db |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | # Install the database |
| 117 | function install_database { |
| 118 | install_database_$DATABASE_TYPE |
| 119 | } |
| 120 | |
Dean Troyer | 5686dbc | 2015-03-09 14:27:51 -0500 | [diff] [blame] | 121 | # Install the database Python packages |
| 122 | function install_database_python { |
| 123 | install_database_python_$DATABASE_TYPE |
| 124 | } |
| 125 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 126 | # Configure and start the database |
| 127 | function configure_database { |
| 128 | configure_database_$DATABASE_TYPE |
| 129 | } |
| 130 | |
Tal Kain | 0729d06 | 2013-04-22 17:50:27 +0300 | [diff] [blame] | 131 | # Generate an SQLAlchemy connection URL and output it using echo |
| 132 | # $1 The name of the database |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 133 | function database_connection_url { |
Tal Kain | 0729d06 | 2013-04-22 17:50:27 +0300 | [diff] [blame] | 134 | local db=$1 |
| 135 | database_connection_url_$DATABASE_TYPE $db |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 138 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 139 | # Restore xtrace |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 140 | $_XTRACE_LIB_DB |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 141 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 142 | # Tell emacs to use shell-script-mode |
| 143 | ## Local variables: |
| 144 | ## mode: shell-script |
| 145 | ## End: |