blob: 0661049e70cf89872da8b4876ceeab52e63bcdcb [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:
Adam Spierscb961592013-10-05 12:11:07 +010012#
13# - recreate_database_$DATABASE_TYPE
14# - install_database_$DATABASE_TYPE
15# - configure_database_$DATABASE_TYPE
16# - database_connection_url_$DATABASE_TYPE
Terry Wilson428af5a2012-11-01 16:12:39 -040017#
18# and call register_database $DATABASE_TYPE
19
20# Save trace setting
21XTRACE=$(set +o | grep xtrace)
22set +o xtrace
23
Dean Troyercc6b4432013-04-08 15:38:03 -050024
Terry Wilson428af5a2012-11-01 16:12:39 -040025# Register a database backend
Adam Spierscb961592013-10-05 12:11:07 +010026#
27# $1 The name of the database backend
28#
Dean Troyercc6b4432013-04-08 15:38:03 -050029# This is required to be defined before the specific database scripts are sourced
Terry Wilson428af5a2012-11-01 16:12:39 -040030function register_database {
31 [ -z "$DATABASE_BACKENDS" ] && DATABASE_BACKENDS=$1 || DATABASE_BACKENDS+=" $1"
32}
33
Dean Troyerafc29fe2013-02-07 15:56:24 -060034# Sourcing the database libs sets DATABASE_BACKENDS with the available list
Dean Troyercc6b4432013-04-08 15:38:03 -050035for f in $TOP_DIR/lib/databases/*; do
36 source $f;
37done
Terry Wilson428af5a2012-11-01 16:12:39 -040038
Dean Troyerafc29fe2013-02-07 15:56:24 -060039# ``DATABASE_BACKENDS`` now contains a list of the supported databases
40# Look in ``ENABLED_SERVICES`` to see if one has been selected
41for db in $DATABASE_BACKENDS; do
42 # Set the type for the rest of the backend to use
43 if is_service_enabled $db; then
Tal Kain0729d062013-04-22 17:50:27 +030044 # Set this now for the rest of the database functions
Dean Troyerafc29fe2013-02-07 15:56:24 -060045 DATABASE_TYPE=$db
46 fi
47done
48# If ``DATABASE_TYPE`` is unset here no database was selected
49# This is not an error as multi-node installs will do this on the compute nodes
50
51
Dean Troyercc6b4432013-04-08 15:38:03 -050052# Functions
53# ---------
54
Dean Troyer995eb922013-03-07 16:11:40 -060055# Get rid of everything enough to cleanly change database backends
56function cleanup_database {
57 cleanup_database_$DATABASE_TYPE
58}
59
Terry Wilson428af5a2012-11-01 16:12:39 -040060# Set the database type based on the configuration
61function initialize_database_backends {
62 for backend in $DATABASE_BACKENDS; do
63 is_service_enabled $backend && DATABASE_TYPE=$backend
64 done
65
66 [ -z "$DATABASE_TYPE" ] && return 1
67
68 # For backward-compatibility, read in the MYSQL_HOST/USER variables and use
69 # them as the default values for the DATABASE_HOST/USER variables.
Matt Riedemann96ba6ec2013-10-02 11:08:56 -070070 MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
Terry Wilson428af5a2012-11-01 16:12:39 -040071 MYSQL_USER=${MYSQL_USER:-root}
72
73 DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}}
74 DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}}
75
76 if [ -n "$MYSQL_PASSWORD" ]; then
77 DATABASE_PASSWORD=$MYSQL_PASSWORD
78 else
79 read_password DATABASE_PASSWORD "ENTER A PASSWORD TO USE FOR THE DATABASE."
80 fi
81
82 # We configure Nova, Horizon, Glance and Keystone to use MySQL as their
83 # database server. While they share a single server, each has their own
84 # database and tables.
85
86 # By default this script will install and configure MySQL. If you want to
87 # use an existing server, you can pass in the user/password/host parameters.
88 # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing
89 # a multi-node DevStack installation.
90
91 # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services
92 BASE_SQL_CONN=${BASE_SQL_CONN:-${DATABASE_TYPE}://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
93
94 return 0
95}
96
Terry Wilson428af5a2012-11-01 16:12:39 -040097# Recreate a given database
98# $1 The name of the database
99# $2 The character set/encoding of the database
100function recreate_database {
101 local db=$1
102 local charset=$2
103 recreate_database_$DATABASE_TYPE $db $charset
104}
105
106# Install the database
107function install_database {
108 install_database_$DATABASE_TYPE
109}
110
111# Configure and start the database
112function configure_database {
113 configure_database_$DATABASE_TYPE
114}
115
Tal Kain0729d062013-04-22 17:50:27 +0300116# Generate an SQLAlchemy connection URL and output it using echo
117# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400118function database_connection_url {
Tal Kain0729d062013-04-22 17:50:27 +0300119 local db=$1
120 database_connection_url_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400121}
122
Dean Troyercc6b4432013-04-08 15:38:03 -0500123
Terry Wilson428af5a2012-11-01 16:12:39 -0400124# Restore xtrace
125$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400126
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100127# Tell emacs to use shell-script-mode
128## Local variables:
129## mode: shell-script
130## End: