[__update_alternatives] rewrite and support --install

This commit is contained in:
ander 2020-09-21 11:18:39 +03:00
parent b6922508b9
commit 716cd37281
8 changed files with 107 additions and 16 deletions

View file

@ -0,0 +1,4 @@
#!/bin/sh -e
update-alternatives --display "$__object_id" 2>/dev/null \
| awk -F ' - ' '/priority [0-9]+$/ { print $1 }'

View file

@ -0,0 +1,40 @@
#!/bin/sh -e
# fedora's (update-)alternatives --display output doesn't have
# "link <name> is <path>" line, but debian does. so, let's find
# out how they store this information.
#
# debian and friends:
# https://salsa.debian.org/dpkg-team/dpkg/-/blob/master/utils/update-alternatives.c
# see calls to altdb_print_line function
#
# fedora and friends:
# https://github.com/fedora-sysv/chkconfig/blob/master/alternatives.c
# see calls to parseLine function
#
# conclusion: it is safe to assume that (master) link is on second line
for altdir in \
/var/lib/dpkg/alternatives \
/var/lib/alternatives
do
if [ ! -f "$altdir/$__object_id" ]
then
continue
fi
link="$( awk 'NR==2' "$altdir/$__object_id" )"
if [ -n "$link" ]
then
break
fi
done
if [ -z "$link" ]
then
echo "unable to get link for $__object_id" >&2
exit 1
fi
echo "$link"

View file

@ -0,0 +1,12 @@
#!/bin/sh -e
path_is="$( update-alternatives --display "$__object_id" 2>/dev/null \
| awk '/link currently points to/ {print $5}' )"
if [ -z "$path_is" ]
then
echo "unable to get current path for $__object_id" >&2
exit 1
fi
echo "$path_is"

View file

@ -0,0 +1,8 @@
#!/bin/sh -e
if [ -f "$( cat "$__object/parameter/path" )" ]
then
echo 'present'
else
echo 'absent'
fi

View file

@ -1,8 +0,0 @@
#!/bin/sh -e
path="$(cat "$__object/parameter/path")"
name="$__object_id"
link="$(readlink "/etc/alternatives/$name")"
if [ "$path" = "$link" ]
then echo present
else echo absent
fi

View file

@ -1,6 +1,7 @@
#!/bin/sh -e
#
# 2013 Nico Schottelius (nico-cdist at schottelius.org)
# 2020 Ander Punnar (ander@kvlt.ee)
#
# This file is part of cdist.
#
@ -16,12 +17,38 @@
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
if [ "$(cat "$__object/explorer/state")" = 'present' ]
then exit 0
path_is="$( cat "$__object/explorer/path_is" )"
path_should="$( cat "$__object/parameter/path" )"
if [ "$path_is" = "$path_should" ]
then
exit 0
fi
if [ "$( cat "$__object/explorer/path_should_state" )" = 'absent' ] && [ -z "$__cdist_dry_run" ]
then
echo "$path_should does not exist in target" >&2
exit 1
fi
path="$(cat "$__object/parameter/path")"
name="$__object_id"
echo "update-alternatives --quiet --set '$name' '$path'"
alternatives="$( cat "$__object/explorer/alternatives" )"
if ! echo "$alternatives" | grep -Fxq "$path_should"
then
if [ ! -f "$__object/parameter/install" ]
then
echo "$path_should is not in $name alternatives." >&2
echo 'Please install missing packages or use --install to add path to alternatives.' >&2
exit 1
fi
link="$( cat "$__object/explorer/link" )"
echo "update-alternatives --install '$link' '$name' '$path_should' 1000"
fi
echo "update-alternatives --set '$name' '$path_should'"

View file

@ -19,6 +19,12 @@ path
Use this path for the given alternative
BOOLEAN PARAMETERS
------------------
install
Add (``update-alternatives --install``) missing path to alternatives.
EXAMPLES
--------
@ -36,11 +42,12 @@ SEE ALSO
AUTHORS
-------
Nico Schottelius <nico-cdist--@--schottelius.org>
Ander Punnar <ander@kvlt.ee>
COPYING
-------
Copyright \(C) 2013 Nico Schottelius. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
Copyright \(C) 2013 Nico Schottelius and 2020 Ander Punnar. You can
redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View file

@ -0,0 +1 @@
install