[openstack-nova/el6: 1/2] Add --yes, --rootpw, and --novapw options to openstack-nova-db-setup.

Russell Bryant russellb at fedoraproject.org
Tue Dec 6 20:48:22 UTC 2011


commit e34870e9a5dc6488d683dc23fe0743ce424e59dd
Author: Russell Bryant <rbryant at redhat.com>
Date:   Tue Dec 6 15:42:13 2011 -0500

    Add --yes, --rootpw, and --novapw options to openstack-nova-db-setup.

 openstack-nova-db-setup |  205 +++++++++++++++++++++++++++++++----------------
 openstack-nova.spec     |    5 +-
 2 files changed, 140 insertions(+), 70 deletions(-)
---
diff --git a/openstack-nova-db-setup b/openstack-nova-db-setup
index 961ccdc..1c5a3a5 100755
--- a/openstack-nova-db-setup
+++ b/openstack-nova-db-setup
@@ -16,30 +16,83 @@
 # under the License.
 #
 
+#
+# Print --help output and exit.
+#
 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."
+
+cat << EOF
+Set up a local MySQL database for use with openstack-nova.
+This script will create a 'nova' database that is accessible
+only on localhost by user 'nova' with password 'nova'.
+The setup of MySQL with a multi-server OpenStack installation
+is outside of the scope of this simple helper script.
+
+Usage: openstack-nova-db-setup [options]
+Options:
+	--help        | -h
+		Print usage information.
+	--novapw <pw> | -n <pw>
+		Specify the password for the 'nova' MySQL user that nova will
+		use to connect to the 'nova' MySQL database.  By default,
+		the password 'nova' will be used.
+	--rootpw <pw> | -r <pw>
+		Specify the root MySQL password.  If the script installs
+		the MySQL server, it will set the root password to this value
+		instead of prompting for a password.  If the MySQL server is
+		already installed, this password will be used to connect to the
+		database instead of having to prompt for it.
+	--yes         | -y
+		In cases where the script would normally ask for confirmation
+		before doing something, such as installing mysql-server,
+		just assume yes.  This is useful if you want to run the script
+		non-interactively.
+EOF
+
 	exit 0
 }
 
+install_mysql_server() {
+	if [ -z "${ASSUME_YES}" ] ; then
+		yum install mysql-server
+	else
+		yum install -y mysql-server
+	fi
+}
+
+start_mysql_server() {
+	systemctl start mysqld.service
+}
+
+MYSQL_ROOT_PW=""
+MYSQL_NOVA_PW_DEFAULT="nova"
+MYSQL_NOVA_PW=${MYSQL_NOVA_PW_DEFAULT}
+NOVA_CONFIG="/etc/nova/nova.conf"
+ASSUME_YES=""
+
 while [ $# -gt 0 ]
 do
 	case "$1" in
 		-h|--help)
 			usage
 			;;
+		-n|--novapw)
+			shift
+			MYSQL_NOVA_PW=${1}
+			;;
+		-r|--rootpw)
+			shift
+			MYSQL_ROOT_PW=${1}
+			;;
+		-y|--yes)
+			ASSUME_YES="yes"
+			;;
 		*)
 			# ignore
 			shift
 			;;
 	esac
+	shift
 done
 
 
@@ -48,21 +101,24 @@ done
 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
+	if [ -z "${ASSUME_YES}" ] ; then
+		printf "mysql-server is not installed.  Would you like to install it now? (y/n): "
+		read response
+		case "$response" in
+			y|Y)
+				;;
+			n|N)
+				echo "mysql-server must be installed.  Please install it before proceeding."
+				exit 0
+				;;
+			*)
+				echo "Invalid response."
+				exit 1
+		esac
+	fi
+
+	NEW_MYSQL_INSTALL=1
+	install_mysql_server
 fi
 
 
@@ -70,68 +126,70 @@ fi
 
 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
+	if [ -z "${ASSUME_YES}" ] ; then
+		printf "mysqld is not running.  Would you like to start it now? (y/n): "
+		read response
+		case "$response" in
+			y|Y)
+				;;
+			n|N)
+				echo "mysqld must be running.  Please start it before proceeding."
+				exit 0
+				;;
+			*)
+				echo "Invalid response."
+				exit 1
+		esac
+	fi
 
+	start_mysql_server
 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 [ -z "${MYSQL_ROOT_PW}" ] ; 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 MYSQL_ROOT_PW
+			echo
+			printf "Enter new password again: "
+			read -s PW2
+			echo
+			if [ "${MYSQL_ROOT_PW}" = "${PW2}" ] ; then
+				PW_MATCH=1
+			else
+				echo "Passwords did not match."
+			fi
+		done
+	fi
+
+	echo "UPDATE mysql.user SET password = password('${MYSQL_ROOT_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
+elif [ -z "${MYSQL_ROOT_PW}" ] ; then
 	printf "Please enter the password for the 'root' MySQL user: "
-	read -s PW
+	read -s MYSQL_ROOT_PW
 	echo
 fi
 
 
 # Sanity check MySQL credentials.
 
-PW_ARG=""
-if [ -n "$PW" ]
+MYSQL_ROOT_PW_ARG=""
+if [ -n "${MYSQL_ROOT_PW}" ]
 then
-	PW_ARG="--password=$PW"
+	MYSQL_ROOT_PW_ARG="--password=${MYSQL_ROOT_PW}"
 fi
-echo "SELECT 1;" | mysql -u root $PW_ARG > /dev/null
+echo "SELECT 1;" | mysql -u root ${MYSQL_ROOT_PW_ARG} > /dev/null
 if ! [ $? -eq 0 ]
 then
 	echo "Failed to connect to the MySQL server.  Please check your root user credentials."
@@ -143,15 +201,24 @@ echo "Verified connectivity to MySQL."
 # Now create the db.
 
 echo "Creating 'nova' database."
-cat << EOF | mysql -u root $PW_ARG
+cat << EOF | mysql -u root ${MYSQL_ROOT_PW_ARG}
 CREATE DATABASE nova;
-CREATE USER 'nova'@'localhost' IDENTIFIED BY 'nova';
-CREATE USER 'nova'@'%' IDENTIFIED BY 'nova';
+CREATE USER 'nova'@'localhost' IDENTIFIED BY '${MYSQL_NOVA_PW}';
+CREATE USER 'nova'@'%' IDENTIFIED BY '${MYSQL_NOVA_PW}';
+GRANT ALL ON nova.* TO 'nova'@'localhost';
 GRANT ALL ON nova.* TO 'nova'@'%';
 flush privileges;
 EOF
 
 
+# Make sure nova configuration has the right MySQL password.
+
+if [ "${MYSQL_NOVA_PW}" != "${MYSQL_NOVA_PW_DEFAULT}" ] ; then
+	echo "Updating 'nova' database password in ${NOVA_CONFIG}"
+	sed -i -e "s/mysql:\/\/nova:\(.*\)@/mysql:\/\/nova:${MYSQL_NOVA_PW}@/" ${NOVA_CONFIG}
+fi
+
+
 # Ask openstack-nova to sync the db.
 
 echo "Asking openstack-nova to sync the databse."
@@ -160,7 +227,7 @@ 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
+echo "SELECT * FROM migrate_version;" | mysql -u nova --password=${MYSQL_NOVA_PW} 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."
diff --git a/openstack-nova.spec b/openstack-nova.spec
index cb19199..4088749 100644
--- a/openstack-nova.spec
+++ b/openstack-nova.spec
@@ -2,7 +2,7 @@
 
 Name:             openstack-nova
 Version:          2011.3
-Release:          10%{?dist}
+Release:          11%{?dist}
 Summary:          OpenStack Compute (nova)
 
 Group:            Applications/System
@@ -444,6 +444,9 @@ fi
 %endif
 
 %changelog
+* Tue Dec 06 2011 Russell Bryant <rbryant at redhat.com> - 2011.3-11
+- Add --yes, --rootpw, and --novapw options to openstack-nova-db-setup.
+
 * Wed Nov 30 2011 Pádraig Brady <P at draigBrady.com> - 2011.3-10
 - Add libguestfs support
 


More information about the scm-commits mailing list