diff --git a/cdist/conf/type/__update_alternatives/explorer/alternatives b/cdist/conf/type/__update_alternatives/explorer/alternatives new file mode 100755 index 00000000..34aaca56 --- /dev/null +++ b/cdist/conf/type/__update_alternatives/explorer/alternatives @@ -0,0 +1,4 @@ +#!/bin/sh -e + +update-alternatives --display "$__object_id" 2>/dev/null \ + | awk -F ' - ' '/priority [0-9]+$/ { print $1 }' diff --git a/cdist/conf/type/__update_alternatives/explorer/link b/cdist/conf/type/__update_alternatives/explorer/link new file mode 100755 index 00000000..6519e7c2 --- /dev/null +++ b/cdist/conf/type/__update_alternatives/explorer/link @@ -0,0 +1,40 @@ +#!/bin/sh -e + +# fedora's (update-)alternatives --display output doesn't have +# "link is " 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" diff --git a/cdist/conf/type/__update_alternatives/explorer/path_is b/cdist/conf/type/__update_alternatives/explorer/path_is new file mode 100755 index 00000000..fc304d5d --- /dev/null +++ b/cdist/conf/type/__update_alternatives/explorer/path_is @@ -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" diff --git a/cdist/conf/type/__update_alternatives/explorer/path_should_state b/cdist/conf/type/__update_alternatives/explorer/path_should_state new file mode 100755 index 00000000..59e015c5 --- /dev/null +++ b/cdist/conf/type/__update_alternatives/explorer/path_should_state @@ -0,0 +1,8 @@ +#!/bin/sh -e + +if [ -f "$( cat "$__object/parameter/path" )" ] +then + echo 'present' +else + echo 'absent' +fi diff --git a/cdist/conf/type/__update_alternatives/explorer/state b/cdist/conf/type/__update_alternatives/explorer/state deleted file mode 100755 index 04a78aaa..00000000 --- a/cdist/conf/type/__update_alternatives/explorer/state +++ /dev/null @@ -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 diff --git a/cdist/conf/type/__update_alternatives/gencode-remote b/cdist/conf/type/__update_alternatives/gencode-remote index c0b49814..e393cdef 100755 --- a/cdist/conf/type/__update_alternatives/gencode-remote +++ b/cdist/conf/type/__update_alternatives/gencode-remote @@ -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 . -# -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'" diff --git a/cdist/conf/type/__update_alternatives/man.rst b/cdist/conf/type/__update_alternatives/man.rst index 73d82d11..0dc973f2 100644 --- a/cdist/conf/type/__update_alternatives/man.rst +++ b/cdist/conf/type/__update_alternatives/man.rst @@ -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 +Ander Punnar 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. diff --git a/cdist/conf/type/__update_alternatives/parameter/boolean b/cdist/conf/type/__update_alternatives/parameter/boolean new file mode 100644 index 00000000..7c32f559 --- /dev/null +++ b/cdist/conf/type/__update_alternatives/parameter/boolean @@ -0,0 +1 @@ +install