Compare commits

...

5 Commits

Author SHA1 Message Date
romain-dartigues d0ce59f167 use add-apt-repository instead of add-apt-repository
In Ubuntu Bionic 18.04 and Focal 20.04, the command remove-apt-repository
either does not exists or fail in a default installation with the error:

    ModuleNotFoundError: No module named 'softwareproperties'

But add-apt-repository allow removal through the `-r` flag.
2021-12-24 15:43:41 +01:00
romain-dartigues 02c770830c force add-apt-repository to act in non-interactive mode 2021-12-24 15:43:41 +01:00
Dennis Camera f50e90f740 [__package_apt] (fixup mark manual) rework version management
See: https://github.com/ungleich/cdist/pull/782
2021-12-24 15:43:41 +01:00
ander 22ece8b6a9 update README, fix #324 2021-12-24 15:43:41 +01:00
Dennis Camera 4884b794ff [__package_apt] Mark already installed packages as manual
...if they were automatically installed as a dependency of another package.
This prevents these pacakges from being uninstalled by `apt-get autoremove`.

See: https://github.com/ungleich/cdist/pull/781
2021-12-24 15:43:41 +01:00
5 changed files with 96 additions and 52 deletions

View File

@ -10,13 +10,11 @@ or at **``docs/src``** for manual in **reStructuredText** format.
## Contributing
Merge/Pull requests can be made in both
[upstream **GitLab**](https://code.ungleich.ch/ungleich-public/cdist/merge_requests)
(managed by [**ungleich**](https://ungleich.ch))
and [**GitHub** project](https://github.com/ungleich/cdist/pulls).
Pull requests can be made in both [upstream](https://code.ungleich.ch/ungleich-public/cdist)
(managed by [**ungleich**](https://ungleich.ch)) and [GitHub](https://github.com/ungleich/cdist).
Issues can be made and other project management activites happen
[**only in GitLab**](https://code.ungleich.ch/ungleich-public/cdist)
[**only in upstream**](https://code.ungleich.ch/ungleich-public/cdist)
(needs [**ungleich** account](https://account.ungleich.ch)).
For community-maintained types there is

View File

@ -29,9 +29,9 @@ fi
case "$state_should" in
present)
echo "add-apt-repository '$name'"
echo "add-apt-repository -y '$name'"
;;
absent)
echo "remove-apt-repository '$name'"
echo "add-apt-repository -r -y '$name'"
;;
esac

View File

@ -1,6 +1,7 @@
#!/bin/sh
#
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
# 2021 Dennis Camera (cdist at dtnr.ch)
#
# This file is part of cdist.
#
@ -21,19 +22,48 @@
# Retrieve the status of a package - parsed dpkg output
#
if [ -f "$__object/parameter/name" ]; then
name="$(cat "$__object/parameter/name")"
breify() {
# Convert arguments to a POSIX BRE-compatible form, i.e. escape special
# characters (incl. delimiter)
printf '%s' "$*" | sed -e 's/[].^$*\[]/\\&/g' -e 's:/:\\/:g'
}
if test -f "${__object}/parameter/name"
then
name=$(cat "${__object:?}/parameter/name")
else
name="$__object_id"
name=${__object_id:?}
fi
# Except dpkg failing, if package is not known / installed
packages="$(apt-cache showpkg "$name" | sed -e "1,/Reverse Provides:/d" | cut -d ' ' -f 1) $name"
for p in $packages; do
if dpkg -s "$p" 2>/dev/null | grep --quiet "^Status: install ok installed$" ; then
version=$(dpkg -s "$p" 2>/dev/null | grep "^Version:" | cut -d ' ' -f 2)
echo "present $p $version"
exit 0
fi
state_dir=$(apt-config dump | sed -n -e 's/^Dir::State *"\(.*\)";$/\/\1/p')
extended_states_file=${state_dir%/}/extended_states
# if $name is e.g. editor, check if any editor is installed instead
rprovides=$(apt-cache showpkg "${name}" | sed -e '1,/^Reverse Provides:/d' -e 's/ .*$//')
for pkg in ${name} ${rprovides}
do
if dpkg -s "${pkg}" 2>/dev/null | grep -qxF 'Status: install ok installed'
then
version=$(dpkg -s "${pkg}" 2>/dev/null | sed -n -e 's/^Version: *//p')
if test -f "${extended_states_file}"
then
# On Debian >= 5 check if the package is auto installed
# NOTE: instead of using apt-mark(8) parse the extended_states file
# directly. apt-mark is sloow and didn't have a stable
# interface in older Debian versions.
is_auto=$(sed -n -e '/^Package: '$(breify "${name}")'$/,/^$/!d' -e 's/^Auto-Installed: *//p' "${extended_states_file}")
fi
# NOTE: older versions don't have apt-mark -> all packages are manual
auto_word=$(test $((is_auto)) -ne 0 && echo auto || echo manual)
echo "present ${auto_word} ${pkg} ${version}"
exit 0
fi
done
echo absent

View File

@ -1,6 +1,7 @@
#!/bin/sh -e
#
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
# 2021 Dennis Camera (cdist at dtnr.ch)
#
# This file is part of cdist.
#
@ -27,14 +28,8 @@ else
name="$__object_id"
fi
state_should="$(cat "$__object/parameter/state")"
version_param="$__object/parameter/version"
version=""
if [ -f "$version_param" ]; then
version="$(cat "$version_param")"
fi
state_should=$(cat "${__object:?}/parameter/state")
version_should=$(cat "${__object:?}/parameter/version" 2>/dev/null) || true
if [ -f "$__object/parameter/target-release" ]; then
target_release="--target-release $(cat "$__object/parameter/target-release")"
@ -56,31 +51,51 @@ else
fi
# FIXME: use grep directly, state is a list, not a line!
state_is="$(cat "$__object/explorer/state")"
case "$state_is" in
present*)
name="$(echo "$state_is" | cut -d ' ' -f 2)"
version_is="$(echo "$state_is" | cut -d ' ' -f 3)"
state_is="present"
;;
*)
version_is=""
;;
esac
if [ "$state_is" = "$state_should" ]; then
if [ -z "$version" ] || [ "$version" = "$version_is" ]; then
exit 0;
fi
fi
read -r state_is auto_state name_is version_is <"${__object:?}/explorer/state"
# Hint if we need to avoid questions at some point:
# DEBIAN_PRIORITY=critical can reduce the number of questions
aptget="DEBIAN_FRONTEND=noninteractive apt-get --quiet --yes -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\""
case "$state_should" in
case ${state_should}
in
present)
if test -n "${version_should}" -a "${version_should}" != "${version_is}"
then
# other version should be installed
name="${name}=${version_should}"
aptget="${aptget:?} -o APT::Get::allow-downgrades=true"
elif test "${state_is}" = 'present'
then
if test "${auto_state}" = 'auto'
then
# package is installed, but marked auto -> mark it manual so
# that it doesn't get removed by apt-get autoremove.
# NOTE: There are two implementations of apt-mark(8) with a
# different API. Although the new implementation (C++) still
# supports the API of the old implementation (Python), it is
# marked as deprecated:
# https://salsa.debian.org/apt-team/apt/-/blob/0.8.15/cmdline/apt-mark.cc#L105
#
# Debian < 5 (without apt-mark) don't need to be supported,
# because the state explorer will report all packages as manual
# for those.
echo 'if ! head -n1 "$(command -v apt-mark)" | grep -qF python'
# new C++ implementation (Debian >= 7)
echo "then apt-mark manual '${name_is}' >/dev/null"
# old Python implementation (Debian 5-6)
echo "else apt-mark unmarkauto '${name_is}'"
echo 'fi'
fi
# package already installed -> exit
exit 0
fi
# There are special arguments to apt(8) to prevent aborts if apt woudn't been
# updated after the 19th April 2021 till the bullseye release. The additional
# arguments acknoledge the happend suite change (the apt(8) update does the
@ -101,15 +116,15 @@ if [ ! -f /var/cache/apt/pkgcache.bin ] || [ "\$( stat --format %Y /var/cache/ap
then echo apt-get $apt_opts update > /dev/null 2>&1 || true
fi
EOF
if [ -n "$version" ]; then
name="${name}=${version}"
fi
echo "$aptget $recommendsparam install $target_release '$name'"
echo "installed" >> "$__messages_out"
echo "${aptget} ${recommendsparam} install ${target_release} '${name}'"
echo 'installed' >>"${__messages_out:?}"
;;
absent)
echo "$aptget remove $purgeparam '$name'"
echo "removed" >> "$__messages_out"
test "${state_is}" != 'absent' || exit 0
echo "${aptget} remove ${purgeparam} '${name_is}'"
echo 'removed' >>"${__messages_out:?}"
;;
*)
echo "Unknown state: $state_should" >&2

View File

@ -12,6 +12,7 @@ next:
* Type __package_apt: Fix complaint about suite change (Matthias Stecher)
* Type __debconf_set_selections: Fix bug where --file was unsupported (Evilham)
* Types __letsencrypt_cert, __grafana_dashboard: Improve bullseye support (Evilham)
* Type __package_apt: Mark already installed packages as manual (Dennis Camera)
6.9.8: 2021-08-24
* Type __rsync: Rewrite (Ander Punnar)