From 71ebc6ff65e6ae3982a7e8a7ecf9ff80fd18d6bb Mon Sep 17 00:00:00 2001 From: Vincent Untz Date: Tue, 12 Jun 2012 13:45:15 +0200 Subject: [PATCH] Add is_package_installed function to know if a package is installed This helps reduce the distro-dependent code in stack.sh, and also fixes the bug where "rpm -qa | grep sudo" will work if gnome-sudoku is installed. Rebased Change-Id: Ib1330b29b915b41d9724197edd791f0d4e0fe373 --- functions | 20 ++++++++++++++++++++ stack.sh | 13 ++----------- tests/functions.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/functions b/functions index 8ed99604c6..664cfa0cbe 100644 --- a/functions +++ b/functions @@ -548,6 +548,26 @@ function install_package() { } +# Distro-agnostic function to tell if a package is installed +# is_package_installed package [package ...] +function is_package_installed() { + if [[ -z "$@" ]]; then + return 1 + fi + + if [[ -z "$os_PACKAGE" ]]; then + GetOSVersion + fi + if [[ "$os_PACKAGE" = "deb" ]]; then + dpkg -l "$@" > /dev/null + return $? + else + rpm --quiet -q "$@" + return $? + fi +} + + # Test if the named environment variable is set and not zero length # is_set env-var function is_set() { diff --git a/stack.sh b/stack.sh index 687e5bf253..2c488b4a37 100755 --- a/stack.sh +++ b/stack.sh @@ -182,11 +182,7 @@ if [[ $EUID -eq 0 ]]; then sleep $ROOTSLEEP # Give the non-root user the ability to run as **root** via ``sudo`` - if [[ "$os_PACKAGE" = "deb" ]]; then - dpkg -l sudo || install_package sudo - else - rpm -qa | grep sudo || install_package sudo - fi + is_package_installed sudo || install_package sudo if ! getent group stack >/dev/null; then echo "Creating a group called stack" groupadd stack @@ -215,12 +211,7 @@ if [[ $EUID -eq 0 ]]; then exit 1 else # We're not **root**, make sure ``sudo`` is available - if [[ "$os_PACKAGE" = "deb" ]]; then - CHECK_SUDO_CMD="dpkg -l sudo" - else - CHECK_SUDO_CMD="rpm -q sudo" - fi - $CHECK_SUDO_CMD || die "Sudo is required. Re-run stack.sh as root ONE TIME ONLY to set up sudo." + is_package_installed sudo || die "Sudo is required. Re-run stack.sh as root ONE TIME ONLY to set up sudo." # UEC images ``/etc/sudoers`` does not have a ``#includedir``, add one sudo grep -q "^#includedir.*/etc/sudoers.d" /etc/sudoers || diff --git a/tests/functions.sh b/tests/functions.sh index f111a48dbb..3a0f31999d 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -239,3 +239,44 @@ test_disable_negated_services 'a,-a' '' test_disable_negated_services 'b,a,-a' 'b' test_disable_negated_services 'a,b,-a' 'b' test_disable_negated_services 'a,-a,b' 'b' + + +echo "Testing is_package_installed()" + +if [[ -z "$os_PACKAGE" ]]; then + GetOSVersion +fi + +if [[ "$os_PACKAGE" = "deb" ]]; then + is_package_installed dpkg + VAL=$? +else + is_package_installed rpm + VAL=$? +fi +if [[ "$VAL" -eq 0 ]]; then + echo "OK" +else + echo "is_package_installed() on existing package failed" +fi + +if [[ "$os_PACKAGE" = "deb" ]]; then + is_package_installed dpkg bash + VAL=$? +else + is_package_installed rpm bash + VAL=$? +fi +if [[ "$VAL" -eq 0 ]]; then + echo "OK" +else + echo "is_package_installed() on more than one existing package failed" +fi + +is_package_installed zzzZZZzzz +VAL=$? +if [[ "$VAL" -ne 0 ]]; then + echo "OK" +else + echo "is_package_installed() on non-existing package failed" +fi