From: Dave Wilde Date: Sun, 19 May 2013 10:05:39 +0000 (+0000) Subject: Adds sudo check for privileged operations. X-Git-Tag: 2014.1~581^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=1c8f3bf2c667bf4a79877fca630c3bbc20993b97;p=openstack-build%2Fheat-build.git Adds sudo check for privileged operations. Adds root user check to determine whether or not sudo is required for MySQL status and installation. We now run sudo -l to verify that the user is in the sudoers file, the credentials are then cached for the rest of the operations. bug 1181701 Change-Id: I294b269046ee2049b0fdee3c871096387cefbf24 --- diff --git a/bin/heat-db-setup b/bin/heat-db-setup index 2b3033bc..c1d7bb2b 100755 --- a/bin/heat-db-setup +++ b/bin/heat-db-setup @@ -58,31 +58,42 @@ install_mysql_server() { } start_mysql_server() { - sudo $SERVICE_START + $SERVICE_START } MYSQL_HEAT_PW_DEFAULT="heat" MYSQL_HEAT_PW=${MYSQL_HEAT_PW_DEFAULT} HEAT_CONFIG="/etc/heat/heat-engine.conf" ASSUME_YES="" +ELEVATE="" + +# Check for root privileges +if [[ $EUID -ne 0 ]] ; then + echo "This operation requires superuser privileges, using sudo:" + if sudo -l > /dev/null ; then + ELEVATE="sudo" + else + exit 1 + fi +fi case "$1" in rpm) echo "Installing on an RPM system." - PACKAGE_INSTALL="yum install" + PACKAGE_INSTALL="$ELEVATE yum install" PACKAGE_STATUS="rpm -q" SERVICE_MYSQLD="mysqld" - SERVICE_START="service $SERVICE_MYSQLD start" + SERVICE_START="$ELEVATE service $SERVICE_MYSQLD start" SERVICE_STATUS="service $SERVICE_MYSQLD status" - SERVICE_ENABLE="chkconfig" + SERVICE_ENABLE="$ELEVATE chkconfig" ;; deb) echo "Installing on a Debian system." - PACKAGE_INSTALL="apt-get install" + PACKAGE_INSTALL="$ELEVATE apt-get install" PACKAGE_STATUS="dpkg-query -s" SERVICE_MYSQLD="mysql" - SERVICE_START="service $SERVICE_MYSQLD start" - SERVICE_STATUS="service $SERVICE_MYSQLD status" + SERVICE_START="$ELEVATE service $SERVICE_MYSQLD start" + SERVICE_STATUS="$ELEVATE service $SERVICE_MYSQLD status" SERVICE_ENABLE="" ;; *)