blob: 78563f6f6d85f2e64f03cde1eb1a572843dc3d8c [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
Ian Wienand523f4882015-10-13 11:03:03 +110023_XTRACE_LIB_DB=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040024set +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.
Brian Haley180f5eb2015-06-16 13:14:31 -040073 MYSQL_HOST=${MYSQL_HOST:-$SERVICE_LOCAL_HOST}
Terry Wilson428af5a2012-11-01 16:12:39 -040074 MYSQL_USER=${MYSQL_USER:-root}
75
Brian Haley180f5eb2015-06-16 13:14:31 -040076 # Set DATABASE_HOST equal to MYSQL_HOST. If SERVICE_IP_VERSION is equal to 6,
77 # set DATABASE_HOST equal to [MYSQL_HOST]. MYSQL_HOST cannot use brackets due
78 # to mysql not using bracketing for IPv6 addresses. DATABASE_HOST must have brackets
79 # due to sqlalchemy only reading IPv6 addresses with brackets.
80 if [[ "$SERVICE_IP_VERSION" == 6 ]]; then
81 DATABASE_HOST=${DATABASE_HOST:-[$MYSQL_HOST]}
82 else
83 DATABASE_HOST=${DATABASE_HOST:-${MYSQL_HOST}}
84 fi
85
Terry Wilson428af5a2012-11-01 16:12:39 -040086 DATABASE_USER=${DATABASE_USER:-${MYSQL_USER}}
87
88 if [ -n "$MYSQL_PASSWORD" ]; then
89 DATABASE_PASSWORD=$MYSQL_PASSWORD
Terry Wilson428af5a2012-11-01 16:12:39 -040090 fi
91
Dr. Jens Harbott353c3f92021-12-23 12:01:44 +010092 return 0
93}
94
95function define_database_baseurl {
Terry Wilson428af5a2012-11-01 16:12:39 -040096 # We configure Nova, Horizon, Glance and Keystone to use MySQL as their
97 # database server. While they share a single server, each has their own
98 # database and tables.
99
100 # By default this script will install and configure MySQL. If you want to
101 # use an existing server, you can pass in the user/password/host parameters.
102 # You will need to send the same ``DATABASE_PASSWORD`` to every host if you are doing
103 # a multi-node DevStack installation.
104
105 # NOTE: Don't specify ``/db`` in this string so we can use it for multiple services
Julien Danjou0eec4f82015-09-08 10:45:06 +0000106 BASE_SQL_CONN=${BASE_SQL_CONN:-$(get_database_type_$DATABASE_TYPE)://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST}
Terry Wilson428af5a2012-11-01 16:12:39 -0400107}
108
Terry Wilson428af5a2012-11-01 16:12:39 -0400109# Recreate a given database
110# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400111function recreate_database {
112 local db=$1
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +0200113 recreate_database_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400114}
115
116# Install the database
117function install_database {
118 install_database_$DATABASE_TYPE
119}
120
Dean Troyer5686dbc2015-03-09 14:27:51 -0500121# Install the database Python packages
122function install_database_python {
123 install_database_python_$DATABASE_TYPE
124}
125
Terry Wilson428af5a2012-11-01 16:12:39 -0400126# Configure and start the database
127function configure_database {
128 configure_database_$DATABASE_TYPE
129}
130
Tal Kain0729d062013-04-22 17:50:27 +0300131# Generate an SQLAlchemy connection URL and output it using echo
132# $1 The name of the database
Terry Wilson428af5a2012-11-01 16:12:39 -0400133function database_connection_url {
Tal Kain0729d062013-04-22 17:50:27 +0300134 local db=$1
135 database_connection_url_$DATABASE_TYPE $db
Terry Wilson428af5a2012-11-01 16:12:39 -0400136}
137
Dean Troyercc6b4432013-04-08 15:38:03 -0500138
Terry Wilson428af5a2012-11-01 16:12:39 -0400139# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100140$_XTRACE_LIB_DB
Sean Dague584d90e2013-03-29 14:34:53 -0400141
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100142# Tell emacs to use shell-script-mode
143## Local variables:
144## mode: shell-script
145## End: