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
This commit is contained in:
Vincent Untz 2012-06-12 13:45:15 +02:00 committed by Dean Troyer
parent c132625d1d
commit 71ebc6ff65
3 changed files with 63 additions and 11 deletions

View File

@ -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() {

View File

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

View File

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