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