From 7c3c74080ac6074e9bb906a56cbb302a415955b4 Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Mon, 28 Jul 2014 14:41:45 +0200 Subject: [PATCH] functions.guest: add iniset_sudo, a root wrapper around iniset This changeset introduces iniset_sudo, a root wrapper around the devstack function, iniset. The wrapper function makes it easier to have comments explaining lines changes to configuration files. Instead of sourcing the devstack library in the root environment as well, iniset_sudo works on a temporary copy of the configuration file and uses root privileges only to copy the result back into place. It would have to be modified before it could edit files that the script user has no permission to read. The changeset illustrates the use of the function with a patched apt_install_mysql.sh Change-Id: I416c0d14280e774a939d7bebaf7d45c3a488e763 --- labs/lib/functions.guest | 14 ++++++++++++++ labs/scripts/apt_install_mysql.sh | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/labs/lib/functions.guest b/labs/lib/functions.guest index 495e1026..2131b466 100644 --- a/labs/lib/functions.guest +++ b/labs/lib/functions.guest @@ -128,6 +128,20 @@ function as_root_exec_script { echo "$(date) done" } +#------------------------------------------------------------------------------- +# Root wrapper around devstack function for manipulating config files +#------------------------------------------------------------------------------- + +function iniset_sudo { + local file=$1 + shift + local tmpfile=$(mktemp) + # Create a temporary copy, work on it, and copy it back into place + cp -fv "$file" "$tmpfile" + iniset "$tmpfile" "$@" + cat "$tmpfile" | sudo tee "$file" >/dev/null +} + #------------------------------------------------------------------------------- # Network configuration #------------------------------------------------------------------------------- diff --git a/labs/scripts/apt_install_mysql.sh b/labs/scripts/apt_install_mysql.sh index 0fa016dd..7853a952 100755 --- a/labs/scripts/apt_install_mysql.sh +++ b/labs/scripts/apt_install_mysql.sh @@ -28,13 +28,18 @@ echo "Installing MySQL." sudo apt-get install -y mysql-server python-mysqldb echo "Configuring MySQL to accept requests by other nodes." -sudo bash -c "source $LIB_DIR/functions-common-devstack && \ - iniset /etc/mysql/my.cnf mysqld bind-address $DB_IP && \ - iniset /etc/mysql/my.cnf mysqld default-storage-engine innodb && \ - iniset /etc/mysql/my.cnf mysqld innodb_file_per_table 1 && \ - iniset /etc/mysql/my.cnf mysqld collation-server utf8_general_ci && \ - iniset /etc/mysql/my.cnf mysqld init-connect \"'SET NAMES utf8'\" && \ - iniset /etc/mysql/my.cnf mysqld character-set-server utf8" + +# Enable access by other nodes via the management network +iniset_sudo /etc/mysql/my.cnf mysqld bind-address "$DB_IP" + +# Enable InnoDB +iniset_sudo /etc/mysql/my.cnf mysqld default-storage-engine innodb +iniset_sudo /etc/mysql/my.cnf mysqld innodb_file_per_table 1 + +# Enable UTF-8 character set and UTF-8 collation by default +iniset_sudo /etc/mysql/my.cnf mysqld collation-server utf8_general_ci +iniset_sudo /etc/mysql/my.cnf mysqld init-connect "'SET NAMES utf8'" +iniset_sudo /etc/mysql/my.cnf mysqld character-set-server utf8 echo "Restarting MySQL service." sudo service mysql restart