blob: 366d2b3d2c994c7542c641f29c1684f4238b042b [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
Dean Troyercc6b4432013-04-08 15:38:03 -050026
Terry Wilson428af5a2012-11-01 16:12:39 -040027# Register a database backend
Adam Spierscb961592013-10-05 12:11:07 +010028#
29# $1 The name of the database backend
30#
Dean Troyercc6b4432013-04-08 15:38:03 -050031# This is required to be defined before the specific database scripts are sourced
Terry Wilson428af5a2012-11-01 16:12:39 -040032function register_database {
33 [ -z "$DATABASE_BACKENDS" ] && DATABASE_BACKENDS=$1 || DATABASE_BACKENDS+=" $1"
34}
35
Dean Troyerafc29fe2013-02-07 15:56:24 -060036# Sourcing the database libs sets DATABASE_BACKENDS with the available list
Dean Troyercc6b4432013-04-08 15:38:03 -050037for f in $TOP_DIR/lib/databases/*; do
38 source $f;
39done
Terry Wilson428af5a2012-11-01 16:12:39 -040040
Dean Troyerafc29fe2013-02-07 15:56:24 -060041# ``DATABASE_BACKENDS`` now contains a list of the supported databases
42# Look in ``ENABLED_SERVICES`` to see if one has been selected
43for db in $DATABASE_BACKENDS; do
44 # Set the type for the rest of the backend to use
45 if is_service_enabled $db; then
Tal Kain0729d062013-04-22 17:50:27 +030046 # Set this now for the rest of the database functions
Dean Troyerafc29fe2013-02-07 15:56:24 -060047 DATABASE_TYPE=$db
48 fi
49done
50# If ``DATABASE_TYPE`` is unset here no database was selected
51# This is not an error as multi-node installs will do this on the compute nodes
52
53
Dean Troyercc6b4432013-04-08 15:38:03 -050054# Functions
55# ---------
56
Dean Troyer995eb922013-03-07 16:11:40 -060057# Get rid of everything enough to cleanly change database backends
58function cleanup_database {
59 cleanup_database_$DATABASE_TYPE
60}
61
Terry Wilson428af5a2012-11-01 16:12:39 -040062# Set the database type based on the configuration
63function initialize_database_backends {
64 for backend in $DATABASE_BACKENDS; do
65 is_service_enabled $backend && DATABASE_TYPE=$backend
66 done
67
68 [ -z "$DATABASE_TYPE" ] && return 1
69
70 # For backward-compatibility, read in the MYSQL_HOST/USER variables and use
71 # them as the default values for the DATABASE_HOST/USER variables.
Matt Riedemann96ba6ec2013-10-02 11:08:56 -070072 MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
Terry Wilson428af5a2012-11-01 16:12:39 -040073 MYSQL_USER=${MYSQL_USER:-root}
74
75 DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}}
76 DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}}
77
78 if [ -n "$MYSQL_PASSWORD" ]; then
79 DATABASE_PASSWORD=$MYSQL_PASSWORD
80 else
81 read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE."
82 fi
83
84 # We configure Nova, Horizon, Glance and Keystone to use MySQL as their
85 # database server. While they share a single server, each has their own
86 # database and tables.
87
88 # By default this script will install and configure MySQL. If you want to
89 # use an existing server, you can pass in the user/password/host parameters.
90 # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing
91 # a multi-node DevStack installation.
92
93 # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services
ihrachyshkacbea3442014-06-28 18:09:54 +020094 BASE_SQL_CONN=${BASE_SQL_CONN:-$(get_database_type)://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
Terry Wilson428af5a2012-11-01 16:12:39 -040095
96 return 0
97}
98
Terry Wilson428af5a2012-11-01 16:12:39 -040099# Recreate a given database
100# $1 The name of the database
101# $2 The character set/encoding of the database
102function recreate_database {
103 local db=$1
104 local charset=$2
105 recreate_database_$DATABASE_TYPE $db $charset
106}
107
108# Install the database
109function install_database {
110 install_database_$DATABASE_TYPE
111}
112
113# Configure and start the database
114function configure_database {
115 configure_database_$DATABASE_TYPE
116}
117
Tal Kain0729d062013-04-22 17:50:27 +0300118# Generate an SQLAlchemy connection URL and output it using echo
119# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400120function database_connection_url {
Tal Kain0729d062013-04-22 17:50:27 +0300121 local db=$1
122 database_connection_url_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400123}
124
ihrachyshkacbea3442014-06-28 18:09:54 +0200125function get_database_type {
126 if [[ -n "${SQLALCHEMY_DATABASE_DRIVER}" ]]; then
127 echo "${DATABASE_TYPE}+${SQLALCHEMY_DATABASE_DRIVER}"
128 else
129 echo "${DATABASE_TYPE}"
130 fi
131}
132
Dean Troyercc6b4432013-04-08 15:38:03 -0500133
Terry Wilson428af5a2012-11-01 16:12:39 -0400134# Restore xtrace
135$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400136
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100137# Tell emacs to use shell-script-mode
138## Local variables:
139## mode: shell-script
140## End: