blob: 4fba7c21403ed5fa66ffcc3dc9d3373273344751 [file] [log] [blame]
Terry Wilson428af5a2012-11-01 16:12:39 -04001# lib/database
2# Interface for interacting with different database backends
3
4# Dependencies:
Dean Troyerafc29fe2013-02-07 15:56:24 -06005# ``ENABLED_SERVICES`` must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04006
Dean Troyerafc29fe2013-02-07 15:56:24 -06007# ``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 Wilson428af5a2012-11-01 16:12:39 -040011# 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
20XTRACE=$(set +o | grep xtrace)
21set +o xtrace
22
23# Register a database backend
24# $1 The name of the database backend
25function register_database {
26 [ -z "$DATABASE_BACKENDS" ] && DATABASE_BACKENDS=$1 || DATABASE_BACKENDS+=" $1"
27}
28
Dean Troyerafc29fe2013-02-07 15:56:24 -060029# Sourcing the database libs sets DATABASE_BACKENDS with the available list
Terry Wilson428af5a2012-11-01 16:12:39 -040030for f in $TOP_DIR/lib/databases/*; do source $f; done
31
Dean Troyerafc29fe2013-02-07 15:56:24 -060032# If ``DATABASE_TYPE`` is defined here it's because the user has it in ``localrc``
33# or has called ``use_database``. Both are deprecated so let's fix it up for now.
34if [[ -n $DATABASE_TYPE ]]; then
35 # This is now deprecated usage, set up a warning and try to be
36 # somewhat backward compatible for now.
37 DEPRECATED_TEXT="$DEPRECATED_TEXT\nThe database backend needs to be properly set in ENABLED_SERVICES; DATABASE_TYPE or use_database is deprecated localrc\n"
38 if [[ ! $ENABLED_SERVICES =~ $DATABASE_TYPE ]]; then
39 # It's not in enabled services but user has attempted to select a
40 # database, so just add it now
41 ENABLED_SERVICES+=,$DATABASE_TYPE
42 unset DATABASE_TYPE
43 fi
44fi
45
46# ``DATABASE_BACKENDS`` now contains a list of the supported databases
47# Look in ``ENABLED_SERVICES`` to see if one has been selected
48for db in $DATABASE_BACKENDS; do
49 # Set the type for the rest of the backend to use
50 if is_service_enabled $db; then
51 # Set this now for the rest of the database funtions
52 DATABASE_TYPE=$db
53 fi
54done
55# If ``DATABASE_TYPE`` is unset here no database was selected
56# This is not an error as multi-node installs will do this on the compute nodes
57
58
Terry Wilson428af5a2012-11-01 16:12:39 -040059# Set the database type based on the configuration
60function initialize_database_backends {
61 for backend in $DATABASE_BACKENDS; do
62 is_service_enabled $backend && DATABASE_TYPE=$backend
63 done
64
65 [ -z "$DATABASE_TYPE" ] && return 1
66
67 # For backward-compatibility, read in the MYSQL_HOST/USER variables and use
68 # them as the default values for the DATABASE_HOST/USER variables.
69 MYSQL_HOST=${MYSQL_HOST:-localhost}
70 MYSQL_USER=${MYSQL_USER:-root}
71
72 DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}}
73 DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}}
74
75 if [ -n "$MYSQL_PASSWORD" ]; then
76 DATABASE_PASSWORD=$MYSQL_PASSWORD
77 else
78 read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE."
79 fi
80
81 # We configure Nova, Horizon, Glance and Keystone to use MySQL as their
82 # database server. While they share a single server, each has their own
83 # database and tables.
84
85 # By default this script will install and configure MySQL. If you want to
86 # use an existing server, you can pass in the user/password/host parameters.
87 # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing
88 # a multi-node DevStack installation.
89
90 # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services
91 BASE_SQL_CONN=${BASE_SQL_CONN:-${DATABASE_TYPE}://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
92
93 return 0
94}
95
Terry Wilson428af5a2012-11-01 16:12:39 -040096# Recreate a given database
97# $1 The name of the database
98# $2 The character set/encoding of the database
99function recreate_database {
100 local db=$1
101 local charset=$2
102 recreate_database_$DATABASE_TYPE $db $charset
103}
104
105# Install the database
106function install_database {
107 install_database_$DATABASE_TYPE
108}
109
110# Configure and start the database
111function configure_database {
112 configure_database_$DATABASE_TYPE
113}
114
115# Generate an SQLAlchemy connection URL and store it in a variable
116# $1 The variable name in which to store the connection URL
117# $2 The name of the database
118function database_connection_url {
119 local var=$1
120 local db=$2
121 database_connection_url_$DATABASE_TYPE $var $db
122}
123
124# Restore xtrace
125$XTRACE