]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Adds sudo check for privileged operations.
authorDave Wilde <david.wilde@rackspace.com>
Sun, 19 May 2013 10:05:39 +0000 (10:05 +0000)
committerDave Wilde <david.wilde@rackspace.com>
Mon, 20 May 2013 06:18:33 +0000 (06:18 +0000)
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

bin/heat-db-setup

index 2b3033bcddbab2d4bec4891520ec97eaabcc8b9f..c1d7bb2b6e2797c5b35e152e29690ccd41bfe963 100755 (executable)
@@ -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=""
                ;;
        *)