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
This commit is contained in:
Roger Luethi 2014-07-28 14:41:45 +02:00
parent 641356f846
commit 7c3c74080a
2 changed files with 26 additions and 7 deletions

@ -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
#-------------------------------------------------------------------------------

@ -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