[openstack-nova] Switch default db config from sqlite to mysql.

Russell Bryant russellb at fedoraproject.org
Tue Nov 29 16:41:06 UTC 2011


commit 730f662e1df92fa74b8a9b250b1db58381a13910
Author: Russell Bryant <rbryant at redhat.com>
Date:   Wed Nov 23 14:47:35 2011 -0500

    Switch default db config from sqlite to mysql.

 nova.conf               |    1 +
 openstack-nova-db-setup |  170 +++++++++++++++++++++++++++++++++++++++++++++++
 openstack-nova.spec     |   19 +++---
 3 files changed, 180 insertions(+), 10 deletions(-)
---
diff --git a/nova.conf b/nova.conf
index d088827..b647725 100644
--- a/nova.conf
+++ b/nova.conf
@@ -9,3 +9,4 @@
 --credentials_template=/usr/share/nova/novarc.template
 --network_manager=nova.network.manager.FlatDHCPManager
 --iscsi_helper=tgtadm
+--sql_connection=mysql://nova:nova@localhost/nova
diff --git a/openstack-nova-db-setup b/openstack-nova-db-setup
new file mode 100755
index 0000000..961ccdc
--- /dev/null
+++ b/openstack-nova-db-setup
@@ -0,0 +1,170 @@
+#!/bin/bash
+#
+# Copyright (C) 2011, Red Hat, Inc.
+# Russell Bryant <rbryant at redhat.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+usage() {
+	echo "Set up a local MySQL database for use with openstack-nova."
+	echo "This script will create a 'nova' database that is accessible"
+	echo "only on localhost by user 'nova' with password 'nova'."
+	echo "The setup of MySQL with a multi-server OpenStack installation"
+	echo "is outside of the scope of this simple helper script."
+	echo ""
+	echo "Usage: openstack-nova-db-setup [options]"
+	echo "Options:"
+	echo "    --help|-h : Print usage information."
+	exit 0
+}
+
+while [ $# -gt 0 ]
+do
+	case "$1" in
+		-h|--help)
+			usage
+			;;
+		*)
+			# ignore
+			shift
+			;;
+	esac
+done
+
+
+# Make sure MySQL is installed.
+
+NEW_MYSQL_INSTALL=0
+if ! rpm -q mysql-server > /dev/null
+then
+	printf "mysql-server is not installed.  Would you like to install it now? (y/n): "
+	read response
+	case "$response" in
+		y|Y)
+			yum install mysql-server
+			NEW_MYSQL_INSTALL=1
+			;;
+		n|N)
+			echo "mysql-server must be installed.  Please install it before proceeding."
+			exit 0
+			;;
+		*)
+			echo "Invalid response."
+			exit 1
+	esac
+fi
+
+
+# Make sure mysqld is running.
+
+if ! systemctl status mysqld.service > /dev/null
+then
+	printf "mysqld is not running.  Would you like to start it now? (y/n): "
+	read response
+	case "$response" in
+		y|Y)
+			systemctl start mysqld.service
+			;;
+		n|N)
+			echo "mysqld must be running.  Please start it before proceeding."
+			exit 0
+			;;
+		*)
+			echo "Invalid response."
+			exit 1
+	esac
+
+fi
+
+
+# Get MySQL root access.
+
+PW=""
+if [ $NEW_MYSQL_INSTALL -eq 1 ]
+then
+	echo "Since this is a fresh installation of MySQL, please set a password for the 'root' mysql user."
+
+	PW_MATCH=0
+	while [ $PW_MATCH -eq 0 ]
+	do
+		printf "Enter new password for 'root' mysql user: "
+		read -s PW
+		echo
+		printf "Enter new password again: "
+		read -s PW2
+		echo
+		if [ "$PW" = "$PW2" ]
+		then
+			PW_MATCH=1
+		else
+			echo "Passwords did not match."
+		fi
+	done
+
+	echo "UPDATE mysql.user SET password = password('$PW') WHERE user = 'root'; DELETE FROM mysql.user WHERE user = ''; flush privileges;" | mysql -u root
+	if ! [ $? -eq 0 ] ; then
+		echo "Failed to set password for 'root' MySQL user."
+		exit 1
+	fi
+else
+	printf "Please enter the password for the 'root' MySQL user: "
+	read -s PW
+	echo
+fi
+
+
+# Sanity check MySQL credentials.
+
+PW_ARG=""
+if [ -n "$PW" ]
+then
+	PW_ARG="--password=$PW"
+fi
+echo "SELECT 1;" | mysql -u root $PW_ARG > /dev/null
+if ! [ $? -eq 0 ]
+then
+	echo "Failed to connect to the MySQL server.  Please check your root user credentials."
+	exit 1
+fi
+echo "Verified connectivity to MySQL."
+
+
+# Now create the db.
+
+echo "Creating 'nova' database."
+cat << EOF | mysql -u root $PW_ARG
+CREATE DATABASE nova;
+CREATE USER 'nova'@'localhost' IDENTIFIED BY 'nova';
+CREATE USER 'nova'@'%' IDENTIFIED BY 'nova';
+GRANT ALL ON nova.* TO 'nova'@'%';
+flush privileges;
+EOF
+
+
+# Ask openstack-nova to sync the db.
+
+echo "Asking openstack-nova to sync the databse."
+nova-manage db sync
+
+
+# Do a final sanity check on the database.
+
+echo "SELECT * FROM migrate_version;" | mysql -u nova --password=nova nova > /dev/null
+if ! [ $? -eq 0 ]
+then
+	echo "Final sanity check failed.  File a bug report on bugzilla.redhat.com against the openstack-nova package."
+	exit 1
+fi
+
+echo "Complete!"
diff --git a/openstack-nova.spec b/openstack-nova.spec
index cf20fbb..57e7cf5 100644
--- a/openstack-nova.spec
+++ b/openstack-nova.spec
@@ -2,7 +2,7 @@
 
 Name:             openstack-nova
 Version:          2011.3
-Release:          7%{?dist}
+Release:          8%{?dist}
 Summary:          OpenStack Compute (nova)
 
 Group:            Applications/System
@@ -25,6 +25,7 @@ Source19:         openstack-nova-vncproxy.service
 Source20:         nova-sudoers
 Source21:         nova-polkit.pkla
 Source22:         nova-ifc-template
+Source23:         openstack-nova-db-setup
 
 #
 # Patches managed here: https://github.com/markmc/nova/tree/fedora-patches
@@ -286,9 +287,6 @@ install -d -m 755 %{buildroot}%{_sharedstatedir}/nova/networks
 install -d -m 755 %{buildroot}%{_sharedstatedir}/nova/tmp
 install -d -m 755 %{buildroot}%{_localstatedir}/log/nova
 
-# Setup ghost sqlite DB
-touch %{buildroot}%{_sharedstatedir}/nova/nova.sqlite
-
 # Setup ghost CA cert
 install -d -m 755 %{buildroot}%{_sharedstatedir}/nova/CA
 install -p -m 755 nova/CA/*.sh %{buildroot}%{_sharedstatedir}/nova/CA
@@ -332,6 +330,9 @@ install -p -D -m 644 %{SOURCE22} %{buildroot}%{_datarootdir}/nova/interfaces.tem
 install -d -m 755 %{buildroot}%{_sysconfdir}/polkit-1/localauthority/50-local.d
 install -p -D -m 644 %{SOURCE21} %{buildroot}%{_sysconfdir}/polkit-1/localauthority/50-local.d/50-nova.pkla
 
+# Install database setup helper script.
+install -p -D -m 755 %{SOURCE23} %{buildroot}%{_bindir}/openstack-nova-db-setup
+
 # Remove ajaxterm and various other tools
 rm -fr %{buildroot}%{_datarootdir}/nova/{ajaxterm,euca-get-ajax-console,install_venv.py,nova-debug,pip-requires,clean-vlans,with_venv.sh,esx}
 
@@ -348,11 +349,6 @@ useradd --uid 162 -r -g nova -G nova,nobody,qemu -d %{_sharedstatedir}/nova -s /
 exit 0
 
 %post
-# Initialize the DB
-if [ ! -f %{_sharedstatedir}/nova/nova.sqlite ]; then
-    runuser -l -s /bin/bash -c 'nova-manage --flagfile=/dev/null --logdir=%{_localstatedir}/log/nova --state_path=%{_sharedstatedir}/nova db sync' nova
-    chmod 600 %{_sharedstatedir}/nova/nova.sqlite
-fi
 if [ $1 -eq 1 ] ; then
     # Initial installation
     /bin/systemctl daemon-reload >/dev/null 2>&1 || :
@@ -389,6 +385,7 @@ fi
 %dir %attr(0755, nova, root) %{_localstatedir}/run/nova
 
 %{_bindir}/nova-*
+%{_bindir}/openstack-nova-db-setup
 %{_unitdir}/openstack-nova-*.service
 %{_datarootdir}/nova
 %{_mandir}/man1/nova*.1.gz
@@ -401,7 +398,6 @@ fi
 %dir %{_sharedstatedir}/nova/keys
 %dir %{_sharedstatedir}/nova/networks
 %dir %{_sharedstatedir}/nova/tmp
-%ghost %config(missingok,noreplace) %verify(not md5 size mtime) %{_sharedstatedir}/nova/nova.sqlite
 
 %dir %{_sharedstatedir}/nova/CA/
 %dir %{_sharedstatedir}/nova/CA/certs
@@ -431,6 +427,9 @@ fi
 %endif
 
 %changelog
+* Tue Nov 29 2011 Russell Bryant <rbryant at redhat.com> - 2011.3-8
+- Change default database to mysql. (#735012)
+
 * Mon Nov 14 2011 Mark McLoughlin <markmc at redhat.com> - 2011.3-7
 - Add ~20 significant fixes from upstream stable branch
 


More information about the scm-commits mailing list