blob: ff1fafee26f42955543dc05ad2b9a0dc96e16f86 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Terry Wilson428af5a2012-11-01 16:12:39 -04003# lib/database
4# Interface for interacting with different database backends
5
6# Dependencies:
Dean Troyerafc29fe2013-02-07 15:56:24 -06007# ``ENABLED_SERVICES`` must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04008
Dean Troyerafc29fe2013-02-07 15:56:24 -06009# ``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 Wilson428af5a2012-11-01 16:12:39 -040013# Each database must implement four functions:
Adam Spierscb961592013-10-05 12:11:07 +010014#
15# - recreate_database_$DATABASE_TYPE
16# - install_database_$DATABASE_TYPE
17# - configure_database_$DATABASE_TYPE
18# - database_connection_url_$DATABASE_TYPE
Terry Wilson428af5a2012-11-01 16:12:39 -040019#
20# and call register_database $DATABASE_TYPE
21
22# Save trace setting
23XTRACE=$(set +o | grep xtrace)
24set +o xtrace
25
Sean Dague53753292014-12-04 19:38:15 -050026DATABASE_BACKENDS=""
Dean Troyercc6b4432013-04-08 15:38:03 -050027
Terry Wilson428af5a2012-11-01 16:12:39 -040028# Register a database backend
Adam Spierscb961592013-10-05 12:11:07 +010029#
30# $1 The name of the database backend
31#
Dean Troyercc6b4432013-04-08 15:38:03 -050032# This is required to be defined before the specific database scripts are sourced
Terry Wilson428af5a2012-11-01 16:12:39 -040033function register_database {
Sean Dague53753292014-12-04 19:38:15 -050034 DATABASE_BACKENDS+=" $1"
Terry Wilson428af5a2012-11-01 16:12:39 -040035}
36
Dean Troyerafc29fe2013-02-07 15:56:24 -060037# Sourcing the database libs sets DATABASE_BACKENDS with the available list
Dean Troyercc6b4432013-04-08 15:38:03 -050038for f in $TOP_DIR/lib/databases/*; do
39 source $f;
40done
Terry Wilson428af5a2012-11-01 16:12:39 -040041
Dean Troyerafc29fe2013-02-07 15:56:24 -060042# ``DATABASE_BACKENDS`` now contains a list of the supported databases
43# Look in ``ENABLED_SERVICES`` to see if one has been selected
44for 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 Kain0729d062013-04-22 17:50:27 +030047 # Set this now for the rest of the database functions
Dean Troyerafc29fe2013-02-07 15:56:24 -060048 DATABASE_TYPE=$db
49 fi
50done
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 Troyercc6b4432013-04-08 15:38:03 -050055# Functions
56# ---------
57
Dean Troyer995eb922013-03-07 16:11:40 -060058# Get rid of everything enough to cleanly change database backends
59function cleanup_database {
60 cleanup_database_$DATABASE_TYPE
61}
62
Terry Wilson428af5a2012-11-01 16:12:39 -040063# Set the database type based on the configuration
64function 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 Riedemann96ba6ec2013-10-02 11:08:56 -070073 MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
Terry Wilson428af5a2012-11-01 16:12:39 -040074 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
ihrachyshkacbea3442014-06-28 18:09:54 +020095 BASE_SQL_CONN=${BASE_SQL_CONN:-$(get_database_type)://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
Terry Wilson428af5a2012-11-01 16:12:39 -040096
97 return 0
98}
99
Terry Wilson428af5a2012-11-01 16:12:39 -0400100# Recreate a given database
101# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400102function recreate_database {
103 local db=$1
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +0200104 recreate_database_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400105}
106
107# Install the database
108function install_database {
109 install_database_$DATABASE_TYPE
110}
111
Dean Troyer5686dbc2015-03-09 14:27:51 -0500112# Install the database Python packages
113function install_database_python {
114 install_database_python_$DATABASE_TYPE
115}
116
Terry Wilson428af5a2012-11-01 16:12:39 -0400117# Configure and start the database
118function configure_database {
119 configure_database_$DATABASE_TYPE
120}
121
Tal Kain0729d062013-04-22 17:50:27 +0300122# Generate an SQLAlchemy connection URL and output it using echo
123# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400124function database_connection_url {
Tal Kain0729d062013-04-22 17:50:27 +0300125 local db=$1
126 database_connection_url_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400127}
128
ihrachyshkacbea3442014-06-28 18:09:54 +0200129function 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 Troyercc6b4432013-04-08 15:38:03 -0500137
Terry Wilson428af5a2012-11-01 16:12:39 -0400138# Restore xtrace
139$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400140
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100141# Tell emacs to use shell-script-mode
142## Local variables:
143## mode: shell-script
144## End: