From d27375db70dde0190e753d54e9f49feb8bb562cc Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Thu, 23 Nov 2017 14:49:52 +0100 Subject: [PATCH] puppet: make check for installed packages more reliable "dpkg-query -s" doesn't necessarily provide the desired answer, it includes packages known to dpkg for different reasons, but does not exactly reply whether they are installed or not. Starting with dpkg version 1.17.11 the ${db:Status-Status} + ${db:Status-Eflag} flags can be used for checking the current state. While I don't think the puppet script is relevant for Debian/wheezy systems (or even older) let's still be backwards compatible. Thanks: Manuel Montecelo for reporting Thanks: Guillem Jover for feedback and code review Development sponsored by Sipwise GmbH, recorded as TT#25551 in customers' ticket system. --- puppet/apply.sh | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/puppet/apply.sh b/puppet/apply.sh index 72d2565..74ffe0f 100644 --- a/puppet/apply.sh +++ b/puppet/apply.sh @@ -97,8 +97,28 @@ $IP $(hostname).example.org $(hostname) exit 1 fi +if dpkg --compare-versions "$(dpkg-query -f "\${Version}\n" -W dpkg 2>/dev/null)" ge '1.17.11' 2>/dev/null ; then + package_check() { + [ "$(dpkg-query -f "\${db:Status-Status} \${db:Status-Eflag}" -W "$package" 2>/dev/null)" = 'installed ok' ] + } +else # dpkg versions older than 1.17.11 (e.g. Debian/wheezy) don't support db:Status* flags, so fall back then + package_check() { + dpkg --list "$package" 2>/dev/null | grep -q '^.i' + } +fi + +package_installed() { + local packages="$*" + + for package in $packages ; do + if ! package_check "$package" ; then + return 1 + fi + done +} + if puppet apply jenkins_debian_glue.pp ; then - if ! dpkg-query -s jenkins jenkins-debian-glue >/dev/null 2>&1 ; then + if ! package_installed jenkins jenkins-debian-glue; then echo "While puppet reported a successful run, jenkins and/or jenkins-debian-glue aren't successfully installed. :(" >&2 echo "Please re-execute this script and if the problem persists please report this at" >&2 echo "https://github.com/mika/jenkins-debian-glue/issues" >&2