From 644768cc02a4bf0a58a9ae83d3aaa3a318f5f9c8 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 2 Oct 2019 15:37:51 +0200 Subject: [PATCH 01/16] Add __select_editor type --- .../type/__select_editor/explorer/editor_path | 97 +++++++++++++++++++ .../conf/type/__select_editor/explorer/group | 26 +++++ .../type/__select_editor/explorer/user_home | 26 +++++ cdist/conf/type/__select_editor/man.rst | 51 ++++++++++ cdist/conf/type/__select_editor/manifest | 59 +++++++++++ .../__select_editor/parameter/default/state | 1 + .../type/__select_editor/parameter/optional | 1 + .../type/__select_editor/parameter/required | 1 + 8 files changed, 262 insertions(+) create mode 100644 cdist/conf/type/__select_editor/explorer/editor_path create mode 100644 cdist/conf/type/__select_editor/explorer/group create mode 100644 cdist/conf/type/__select_editor/explorer/user_home create mode 100644 cdist/conf/type/__select_editor/man.rst create mode 100644 cdist/conf/type/__select_editor/manifest create mode 100644 cdist/conf/type/__select_editor/parameter/default/state create mode 100644 cdist/conf/type/__select_editor/parameter/optional create mode 100644 cdist/conf/type/__select_editor/parameter/required diff --git a/cdist/conf/type/__select_editor/explorer/editor_path b/cdist/conf/type/__select_editor/explorer/editor_path new file mode 100644 index 00000000..88952e23 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/editor_path @@ -0,0 +1,97 @@ +#!/bin/sh +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: 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. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# +# Check if the given editor is present on the target system and determine its +# absolute path. +# + +case $("${__explorer}/os") +in + debian|devuan|ubuntu) + : # supported + ;; + *) + exit 0 # will produce an error message in the manifest + ;; +esac + + +editor=$(cat "${__object}/parameter/editor") +editors=$(update-alternatives --list editor) + +if test $(echo "${editors}" | wc -l) -lt 1 +then + echo 'No editors have been found on this system.' >&2 + exit 1 +fi + +case $editor +in + /*) + is_path=true + ;; + */*) + echo 'Relative editor paths are not supported' >&2 + exit 1 + ;; + *) + is_path=false + ;; +esac + + +IFS=' +' +if $is_path +then + if ! test -f "${editor}" + then + echo "Editor ${editor} is missing on the target system." >&2 + exit 1 + fi + + for e in $editors + do + if test "${editor}" = "${e}" + then + # Editor is present and part of the alternatives list -> use it! + echo "${editor}" + exit 0 + fi + done + + echo "Editor ${editor} is not in the alternatives list of the target system." >&2 + exit 1 +else + for e in $editors + do + if test "$(basename "${e}")" = "${editor}" + then + # Editor could be found by basename in the alternatives list -> use it! + echo "${e}" + exit 0 + fi + done + + echo "Editor ${editor} is missing on the target system." >&2 + exit 1 +fi + +exit 1 diff --git a/cdist/conf/type/__select_editor/explorer/group b/cdist/conf/type/__select_editor/explorer/group new file mode 100644 index 00000000..5d288189 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/group @@ -0,0 +1,26 @@ +#!/bin/sh -e +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: 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. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# +# Determines the primary group of the user. +# + +user=$__object_id + +id -gn "${user}" 2>/dev/null diff --git a/cdist/conf/type/__select_editor/explorer/user_home b/cdist/conf/type/__select_editor/explorer/user_home new file mode 100644 index 00000000..dc1725a0 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/user_home @@ -0,0 +1,26 @@ +#!/bin/sh +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: 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. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# +# Determines the home folder of the target user. +# + +user=$__object_id + +getent passwd "${user}" | cut -d':' -f6 diff --git a/cdist/conf/type/__select_editor/man.rst b/cdist/conf/type/__select_editor/man.rst new file mode 100644 index 00000000..98e5ef81 --- /dev/null +++ b/cdist/conf/type/__select_editor/man.rst @@ -0,0 +1,51 @@ +cdist-type__select_editor(7) +============================ + +NAME +---- +cdist-type__select_editor - Select the sensible-editor + + +DESCRIPTION +----------- +This cdist type allows you to select the sensible-editor on Debian-based systems +for a given user. + + +REQUIRED PARAMETERS +------------------- +editor + Name or path of the editor to be selected. + + +OPTIONAL PARAMETERS +------------------- +state + either "present" or "absent". Defaults to "present". + + +EXAMPLES +-------- + +.. code-block:: sh + + __select_editor root --editor /bin/ed # ed(1) is the standard + __select_editor noob --editor nano + + +SEE ALSO +-------- +none + + +AUTHOR +------- +Dennis Camera + + +COPYING +------- +Copyright \(C) 2019 Dennis Camera. +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/__select_editor/manifest b/cdist/conf/type/__select_editor/manifest new file mode 100644 index 00000000..5ed97533 --- /dev/null +++ b/cdist/conf/type/__select_editor/manifest @@ -0,0 +1,59 @@ +#!/bin/sh -e +# -*- mode: sh; indent-tabs-mode: t -*- +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: 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. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +os=$(cat "${__global}/explorer/os") + +state=$(cat "${__object}/parameter/state") +user=$__object_id + +editor_path=$(cat "${__object}/explorer/editor_path") +user_home=$(cat "${__object}/explorer/user_home") +group=$(cat "${__object}/explorer/group") + +case $os +in + debian|devuan|ubuntu) + test "${state}" = 'present' && __package_apt sensible-utils --state present + ;; + *) + echo "OS ${os} does not support select-editor." >&2 + exit 1 + ;; +esac + +if test -z "${user_home}" +then + echo "Could not find ${user}'s home directory." >&2 + exit 1 +fi + +if test -z "${editor_path}" +then + echo "Editor \"$(cat "${__object}/parameter/editor")\" is missing on the target system." >&2 + exit 1 +fi + +__file "${user_home}/.selected_editor" --state "${state}" \ + --owner "${user}" --group "${group}" --mode 0644 \ + --source - < Date: Fri, 4 Oct 2019 14:04:17 +0200 Subject: [PATCH 02/16] Rename __select_editor to __sensible_editor --- .../explorer/editor_path | 0 .../explorer/group | 0 .../explorer/user_home | 0 .../{__select_editor => __sensible_editor}/man.rst | 10 +++++----- .../{__select_editor => __sensible_editor}/manifest | 0 .../parameter/default/state | 0 .../parameter/optional | 0 .../parameter/required | 0 8 files changed, 5 insertions(+), 5 deletions(-) rename cdist/conf/type/{__select_editor => __sensible_editor}/explorer/editor_path (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/explorer/group (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/explorer/user_home (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/man.rst (73%) rename cdist/conf/type/{__select_editor => __sensible_editor}/manifest (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/parameter/default/state (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/parameter/optional (100%) rename cdist/conf/type/{__select_editor => __sensible_editor}/parameter/required (100%) diff --git a/cdist/conf/type/__select_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path similarity index 100% rename from cdist/conf/type/__select_editor/explorer/editor_path rename to cdist/conf/type/__sensible_editor/explorer/editor_path diff --git a/cdist/conf/type/__select_editor/explorer/group b/cdist/conf/type/__sensible_editor/explorer/group similarity index 100% rename from cdist/conf/type/__select_editor/explorer/group rename to cdist/conf/type/__sensible_editor/explorer/group diff --git a/cdist/conf/type/__select_editor/explorer/user_home b/cdist/conf/type/__sensible_editor/explorer/user_home similarity index 100% rename from cdist/conf/type/__select_editor/explorer/user_home rename to cdist/conf/type/__sensible_editor/explorer/user_home diff --git a/cdist/conf/type/__select_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst similarity index 73% rename from cdist/conf/type/__select_editor/man.rst rename to cdist/conf/type/__sensible_editor/man.rst index 98e5ef81..4dd20a3e 100644 --- a/cdist/conf/type/__select_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -1,9 +1,9 @@ -cdist-type__select_editor(7) +cdist-type__sensible_editor(7) ============================ NAME ---- -cdist-type__select_editor - Select the sensible-editor +cdist-type__sensible_editor - Select the sensible-editor DESCRIPTION @@ -21,7 +21,7 @@ editor OPTIONAL PARAMETERS ------------------- state - either "present" or "absent". Defaults to "present". + Either "present" or "absent". Defaults to "present". EXAMPLES @@ -29,8 +29,8 @@ EXAMPLES .. code-block:: sh - __select_editor root --editor /bin/ed # ed(1) is the standard - __select_editor noob --editor nano + __sensible_editor root --editor /bin/ed # ed(1) is the standard + __sensible_editor noob --editor nano SEE ALSO diff --git a/cdist/conf/type/__select_editor/manifest b/cdist/conf/type/__sensible_editor/manifest similarity index 100% rename from cdist/conf/type/__select_editor/manifest rename to cdist/conf/type/__sensible_editor/manifest diff --git a/cdist/conf/type/__select_editor/parameter/default/state b/cdist/conf/type/__sensible_editor/parameter/default/state similarity index 100% rename from cdist/conf/type/__select_editor/parameter/default/state rename to cdist/conf/type/__sensible_editor/parameter/default/state diff --git a/cdist/conf/type/__select_editor/parameter/optional b/cdist/conf/type/__sensible_editor/parameter/optional similarity index 100% rename from cdist/conf/type/__select_editor/parameter/optional rename to cdist/conf/type/__sensible_editor/parameter/optional diff --git a/cdist/conf/type/__select_editor/parameter/required b/cdist/conf/type/__sensible_editor/parameter/required similarity index 100% rename from cdist/conf/type/__select_editor/parameter/required rename to cdist/conf/type/__sensible_editor/parameter/required From 9a6ca1a343d983cc9e838ee23776af31cb21cadd Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 6 Oct 2019 12:07:54 +0200 Subject: [PATCH 03/16] [__sensible_editor] Remove mixed error reporting from manifest --- .../__sensible_editor/explorer/editor_path | 2 +- .../type/__sensible_editor/explorer/user_home | 11 +++++++++-- cdist/conf/type/__sensible_editor/man.rst | 2 +- cdist/conf/type/__sensible_editor/manifest | 18 +++--------------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index 88952e23..a1673a52 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # # 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # diff --git a/cdist/conf/type/__sensible_editor/explorer/user_home b/cdist/conf/type/__sensible_editor/explorer/user_home index dc1725a0..b88243f7 100644 --- a/cdist/conf/type/__sensible_editor/explorer/user_home +++ b/cdist/conf/type/__sensible_editor/explorer/user_home @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # # 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # @@ -22,5 +22,12 @@ # user=$__object_id +home=$(getent passwd "${user}" | cut -d':' -f6) -getent passwd "${user}" | cut -d':' -f6 +if ! test -d "${home}" +then + echo "Cannot find home directory of user ${user}" >&2 + exit 1 +fi + +echo "${home}" diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index 4dd20a3e..2d7682a6 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -1,5 +1,5 @@ cdist-type__sensible_editor(7) -============================ +============================== NAME ---- diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 5ed97533..b02625e2 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -24,10 +24,6 @@ os=$(cat "${__global}/explorer/os") state=$(cat "${__object}/parameter/state") user=$__object_id -editor_path=$(cat "${__object}/explorer/editor_path") -user_home=$(cat "${__object}/explorer/user_home") -group=$(cat "${__object}/explorer/group") - case $os in debian|devuan|ubuntu) @@ -39,17 +35,9 @@ in ;; esac -if test -z "${user_home}" -then - echo "Could not find ${user}'s home directory." >&2 - exit 1 -fi - -if test -z "${editor_path}" -then - echo "Editor \"$(cat "${__object}/parameter/editor")\" is missing on the target system." >&2 - exit 1 -fi +editor_path=$(cat "${__object}/explorer/editor_path") +user_home=$(cat "${__object}/explorer/user_home") +group=$(cat "${__object}/explorer/group") __file "${user_home}/.selected_editor" --state "${state}" \ --owner "${user}" --group "${group}" --mode 0644 \ From b6898b097f01941d68860c02118f5f5f2ddb8576 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Tue, 8 Oct 2019 23:05:23 +0200 Subject: [PATCH 04/16] [__sensible_editor] Add support for RedHat derivatives They have added sensible-utils lately. select-editor(1) does not work because their alternatives system is broken, but sensible-editor(1) works just fine, so we can support it. --- .../__sensible_editor/explorer/editor_path | 84 +++++++++++-------- cdist/conf/type/__sensible_editor/man.rst | 15 +++- cdist/conf/type/__sensible_editor/manifest | 18 +++- 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index a1673a52..8b10b17b 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -22,76 +22,92 @@ # absolute path. # +die() { + echo "$@" >&2 + exit 1 +} + +editor_missing() { die "Editor '$1' is missing on the target system."; } +editor_no_alternative() { die "Editor '$1' is not in the alternatives list of the target system."; } + + case $("${__explorer}/os") in debian|devuan|ubuntu) - : # supported + has_alternatives=true + editors=$(update-alternatives --list editor) ;; *) - exit 0 # will produce an error message in the manifest + # NOTE: RedHat has an alternatives system but it doesn't usually track + # editors and it is a pain to extract the list. + has_alternatives=false ;; esac - editor=$(cat "${__object}/parameter/editor") -editors=$(update-alternatives --list editor) - -if test $(echo "${editors}" | wc -l) -lt 1 -then - echo 'No editors have been found on this system.' >&2 - exit 1 -fi case $editor in /*) - is_path=true + is_abspath=true ;; */*) - echo 'Relative editor paths are not supported' >&2 - exit 1 + die 'Relative editor paths are not supported' ;; *) - is_path=false + is_abspath=false ;; esac -IFS=' -' -if $is_path +if $has_alternatives && test "$(echo "${editors}" | wc -l)" -gt 0 then - if ! test -f "${editor}" + IFS=' +' + if ! $is_abspath then - echo "Editor ${editor} is missing on the target system." >&2 - exit 1 + # First, try to resolve the absolute path using $editors. + for e in $editors + do + if test "$(basename "${e}")" = "${editor}" + then + editor="${e}" + break + fi + done fi + # Check if path is present + test -f "${editor}" || editor_missing "${editor}" + for e in $editors do if test "${editor}" = "${e}" then - # Editor is present and part of the alternatives list -> use it! + # Editor is part of the alternatives list -> use it! echo "${editor}" exit 0 fi done - echo "Editor ${editor} is not in the alternatives list of the target system." >&2 - exit 1 + editor_no_alternative "${editor}" else - for e in $editors - do - if test "$(basename "${e}")" = "${editor}" - then - # Editor could be found by basename in the alternatives list -> use it! - echo "${e}" - exit 0 - fi - done + # NOTE: This branch is mostly for RedHat-based systems which do + # not track editor alternatives. To make this type useful + # on RedHat at all we allow an absoloute path to be provided + # in any case. - echo "Editor ${editor} is missing on the target system." >&2 - exit 1 + if $is_abspath + then + test -x "${editor}" || editor_missing "${editor}" + + echo "${editor}" + exit 0 + else + die "The target doesn't list any editor alternatives. " \ + "Please specify an absolute path or populate the alternatives list." + fi fi +# The script should never reach this statement! exit 1 diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index 2d7682a6..9e2245ef 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -8,14 +8,15 @@ cdist-type__sensible_editor - Select the sensible-editor DESCRIPTION ----------- -This cdist type allows you to select the sensible-editor on Debian-based systems -for a given user. +This cdist type allows you to select the :strong:`sensible-editor` for +a given user. REQUIRED PARAMETERS ------------------- editor Name or path of the editor to be selected. + On systems other than Debian derivatives an absolute path is required. OPTIONAL PARAMETERS @@ -33,9 +34,17 @@ EXAMPLES __sensible_editor noob --editor nano +LIMITATIONS +----------- +This type only works on operating systems on which the sensible-utils package +is available. + +Hint: On RedHat-based systems setting up the EPEL repo might be necessary. + + SEE ALSO -------- -none +:strong:`select-editor`\ (1), :strong:`sensible-editor`\ (1). AUTHOR diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index b02625e2..56a6b02f 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -27,14 +27,28 @@ user=$__object_id case $os in debian|devuan|ubuntu) - test "${state}" = 'present' && __package_apt sensible-utils --state present + test "${state}" != 'absent' \ + && __package sensible-utils --state present --type apt + ;; + centos|fedora|redhat|scientific) + test "${state}" != 'absent' \ + && __package sensible-utils --state present --type yum ;; *) - echo "OS ${os} does not support select-editor." >&2 + echo "OS ${os} does not support sensible-editor." >&2 + echo "If it does, please provide a patch." >&2 exit 1 ;; esac +if test "${state}" != 'present' && test "${state}" != 'absent' +then + echo 'Only "present" and "absent" are allowed for --state' >&2 + exit 1 +fi + +test "${state}" = 'absent' || export __require='__package/sensible-utils' + editor_path=$(cat "${__object}/explorer/editor_path") user_home=$(cat "${__object}/explorer/user_home") group=$(cat "${__object}/explorer/group") From 522100b9fb2f80a1fe9cf7b3d946a432957ec739 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 12 Oct 2019 11:07:39 +0200 Subject: [PATCH 05/16] [__sensible_editor] Refactor --- .../__sensible_editor/explorer/editor_path | 5 +- cdist/conf/type/__sensible_editor/manifest | 48 ++++++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index 8b10b17b..b0de2f0a 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -35,7 +35,10 @@ case $("${__explorer}/os") in debian|devuan|ubuntu) has_alternatives=true - editors=$(update-alternatives --list editor) + + # NOTE: Old versions do not support `--list`, in this case ignore the errors. + # This will require an absolute path to be provided, though. + editors=$(update-alternatives --list editor 2>/dev/null) ;; *) # NOTE: RedHat has an alternatives system but it doesn't usually track diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 56a6b02f..2ad8c54e 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -19,35 +19,47 @@ # along with cdist. If not, see . # +not_supported() { + echo "OS ${os} does not support __sensible_editor." >&2 + echo 'If it does, please provide a patch.' >&2 + exit 1 +} + os=$(cat "${__global}/explorer/os") state=$(cat "${__object}/parameter/state") user=$__object_id -case $os -in - debian|devuan|ubuntu) - test "${state}" != 'absent' \ - && __package sensible-utils --state present --type apt - ;; - centos|fedora|redhat|scientific) - test "${state}" != 'absent' \ - && __package sensible-utils --state present --type yum - ;; - *) - echo "OS ${os} does not support sensible-editor." >&2 - echo "If it does, please provide a patch." >&2 - exit 1 - ;; -esac - if test "${state}" != 'present' && test "${state}" != 'absent' then echo 'Only "present" and "absent" are allowed for --state' >&2 exit 1 fi -test "${state}" = 'absent' || export __require='__package/sensible-utils' +case $os +in + debian) + pkg_type='apt' + ;; + devuan) + pkg_type='apt' + ;; + ubuntu) + pkg_type='apt' + ;; + centos|fedora|redhat|scientific) + pkg_type='yum' + ;; + *) + not_supported + ;; +esac + +if test "${state}" != 'absent' +then + __package sensible-utils --state present --type "${pkg_type}" + export __require='__package/sensible-utils' +fi editor_path=$(cat "${__object}/explorer/editor_path") user_home=$(cat "${__object}/explorer/user_home") From e64d1710b531cdb2db9a8a0bcb1c5dd7d287f3fb Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 12 Oct 2019 13:01:39 +0200 Subject: [PATCH 06/16] [__sensible_editor] Add support for old Ubuntu versions --- cdist/conf/type/__sensible_editor/manifest | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 2ad8c54e..988f00e5 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -19,6 +19,21 @@ # along with cdist. If not, see . # +version_ge() { + awk -F '[^0-9.]' -v target="${1:?}" ' + function max(x, y) { return x > y ? x : y; } + BEGIN { + getline; + nx = split($1, x, "."); + ny = split(target, y, "."); + for (i = 1; i <= max(nx, ny); ++i) { + if (x[i] < y[i]) exit 1; + else if (x[i] > y[i]) exit 0; + else continue; + } + }' +} + not_supported() { echo "OS ${os} does not support __sensible_editor." >&2 echo 'If it does, please provide a patch.' >&2 @@ -26,6 +41,7 @@ not_supported() { } os=$(cat "${__global}/explorer/os") +os_version=$(cat "${__global}/explorer/os_version") state=$(cat "${__object}/parameter/state") user=$__object_id @@ -36,6 +52,8 @@ then exit 1 fi +package_name='sensible-utils' + case $os in debian) @@ -45,6 +63,7 @@ in pkg_type='apt' ;; ubuntu) + "${os_version}" | version_ge 10.04 || package_name='debianutils' pkg_type='apt' ;; centos|fedora|redhat|scientific) @@ -57,8 +76,9 @@ esac if test "${state}" != 'absent' then - __package sensible-utils --state present --type "${pkg_type}" - export __require='__package/sensible-utils' + __package "${package_name}" --state present \ + --type "${pkg_type}" + export __require="__package/${package_name}" fi editor_path=$(cat "${__object}/explorer/editor_path") From d11b1915471c5c0c0b530ae9cd022b293866e292 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 12 Oct 2019 13:02:26 +0200 Subject: [PATCH 07/16] [__sensible_editor] Document supported operating systems in man page --- cdist/conf/type/__sensible_editor/man.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index 9e2245ef..d0f5356e 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -36,11 +36,19 @@ EXAMPLES LIMITATIONS ----------- -This type only works on operating systems on which the sensible-utils package -is available. -Hint: On RedHat-based systems setting up the EPEL repo might be necessary. +This type supports the following operating systems: +* Debian 8 (jessie) or later +* Devuan +* Ubuntu 8.10 (intrepid) or later +* RHEL/CentOS 7 or later (EPEL repo required) +* Fedora 21 or later +In general, this type could work on operating systems on which the +sensible-utils package is available. + +On old versions of Ubuntu the sensible-* utils are part of the +debianutils package. SEE ALSO -------- From f782a8547035decee53a745da56edb47490ba5a8 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 12 Oct 2019 13:27:42 +0200 Subject: [PATCH 08/16] [__sensible_editor] Reword limitations paragraph of man page --- cdist/conf/type/__sensible_editor/man.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index d0f5356e..4260a369 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -37,17 +37,17 @@ EXAMPLES LIMITATIONS ----------- -This type supports the following operating systems: -* Debian 8 (jessie) or later -* Devuan -* Ubuntu 8.10 (intrepid) or later -* RHEL/CentOS 7 or later (EPEL repo required) -* Fedora 21 or later +This type depends upon the :strong:`sensible-editor`\ (1) script which +is part of the sensible-utils package. -In general, this type could work on operating systems on which the -sensible-utils package is available. +Therefore, the following operating systems are supported: + * Debian 8 (jessie) or later + * Devuan + * Ubuntu 8.10 (intrepid) or later + * RHEL/CentOS 7 or later (EPEL repo required) + * Fedora 21 or later -On old versions of Ubuntu the sensible-* utils are part of the +Note: on old versions of Ubuntu the sensible-* utils are part of the debianutils package. SEE ALSO From 00dd16d7c34f576e76893636dbffc3ea3091e62a Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 10:13:20 +0200 Subject: [PATCH 09/16] [__sensible_editor] List available editors when an incorrect one is provided --- cdist/conf/type/__sensible_editor/explorer/editor_path | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index b0de2f0a..b6230545 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -28,7 +28,10 @@ die() { } editor_missing() { die "Editor '$1' is missing on the target system."; } -editor_no_alternative() { die "Editor '$1' is not in the alternatives list of the target system."; } +editor_no_alternative() { + die "Editor '$1' is not in the alternatives list of the target system." \ + "$(test "${editors}" && echo "\nPlease choose one of:\n\n${editors}")" +} case $("${__explorer}/os") @@ -63,7 +66,7 @@ in esac -if $has_alternatives && test "$(echo "${editors}" | wc -l)" -gt 0 +if $has_alternatives && test -n "${editors}" then IFS=' ' From 3bbb7b02d2c41fa4ff141d7e2a68d820136c3cd7 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 10:14:05 +0200 Subject: [PATCH 10/16] [__sensible_editor] Allow to omit the --editor parameter if --state is absent --- cdist/conf/type/__sensible_editor/explorer/editor_path | 7 ++++++- cdist/conf/type/__sensible_editor/man.rst | 2 ++ cdist/conf/type/__sensible_editor/parameter/optional | 1 + cdist/conf/type/__sensible_editor/parameter/required | 1 - 4 files changed, 9 insertions(+), 2 deletions(-) delete mode 100644 cdist/conf/type/__sensible_editor/parameter/required diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index b6230545..d19a5b06 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -33,6 +33,9 @@ editor_no_alternative() { "$(test "${editors}" && echo "\nPlease choose one of:\n\n${editors}")" } +# No need to check for the path if the file is supposed to be removed. +test "$(cat "${__object}/parameter/state")" != 'absent' || exit 0 + case $("${__explorer}/os") in @@ -50,7 +53,9 @@ in ;; esac -editor=$(cat "${__object}/parameter/editor") +# Read --editor parameter and check its value since it is "optional" +editor=$(cat "${__object}/parameter/editor" 2>/dev/null) || true +test -n "${editor}" || die 'Please provide an --editor to configure.' case $editor in diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index 4260a369..b11aff46 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -18,6 +18,8 @@ editor Name or path of the editor to be selected. On systems other than Debian derivatives an absolute path is required. + It is permissible to omit this parameter if --state is absent. + OPTIONAL PARAMETERS ------------------- diff --git a/cdist/conf/type/__sensible_editor/parameter/optional b/cdist/conf/type/__sensible_editor/parameter/optional index ff72b5c7..ae55e240 100644 --- a/cdist/conf/type/__sensible_editor/parameter/optional +++ b/cdist/conf/type/__sensible_editor/parameter/optional @@ -1 +1,2 @@ +editor state diff --git a/cdist/conf/type/__sensible_editor/parameter/required b/cdist/conf/type/__sensible_editor/parameter/required deleted file mode 100644 index 85042acd..00000000 --- a/cdist/conf/type/__sensible_editor/parameter/required +++ /dev/null @@ -1 +0,0 @@ -editor From 8b0734f719de690742361a8756d25511f2c16029 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 10:14:27 +0200 Subject: [PATCH 11/16] [__sensible_editor] Improve error message when a basename of an editor not in the alternatives is provided --- .../__sensible_editor/explorer/editor_path | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index d19a5b06..8a5199c9 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -78,17 +78,24 @@ then if ! $is_abspath then # First, try to resolve the absolute path using $editors. - for e in $editors + while true do - if test "$(basename "${e}")" = "${editor}" - then - editor="${e}" - break - fi + for e in $editors + do + if test "$(basename "${e}")" = "${editor}" + then + editor="${e}" + break 2 # break out of both loops + fi + done + + # Iterating through alternatives did not yield a result + editor_no_alternative "${editor}" + break done fi - # Check if path is present + # Check if editor is present test -f "${editor}" || editor_missing "${editor}" for e in $editors From 9edf5e8fe03edbf01f2b02a2ab4a5643d31b21d2 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 10:39:50 +0200 Subject: [PATCH 12/16] [__sensible_utils] Fix Ubuntu version detection --- cdist/conf/type/__sensible_editor/manifest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 988f00e5..43344dac 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -63,7 +63,7 @@ in pkg_type='apt' ;; ubuntu) - "${os_version}" | version_ge 10.04 || package_name='debianutils' + (echo "${os_version}" | version_ge 10.04) || package_name='debianutils' pkg_type='apt' ;; centos|fedora|redhat|scientific) From 365974c4471e0df503d771e9b0d604a8f1883af8 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 13:42:15 +0200 Subject: [PATCH 13/16] [__sensible_editor] Use int comparisons in version_ge --- cdist/conf/type/__sensible_editor/manifest | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 43344dac..3a1926b6 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -27,8 +27,9 @@ version_ge() { nx = split($1, x, "."); ny = split(target, y, "."); for (i = 1; i <= max(nx, ny); ++i) { - if (x[i] < y[i]) exit 1; - else if (x[i] > y[i]) exit 0; + diff = int(x[i]) - int(y[i]); + if (diff < 0) exit 1; + else if (diff > 0) exit 0; else continue; } }' From 58eb474b017813f1781cf31836018f40fdf2f0ae Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 13 Oct 2019 13:49:51 +0200 Subject: [PATCH 14/16] [__sensible_editor] Add exists as a possible --state value --- cdist/conf/type/__sensible_editor/man.rst | 10 +++++++++- cdist/conf/type/__sensible_editor/manifest | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index b11aff46..9b805e06 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -24,7 +24,15 @@ editor OPTIONAL PARAMETERS ------------------- state - Either "present" or "absent". Defaults to "present". + 'present', 'absent', or 'exists'. Defaults to 'present', where: + + present + the sensible-editor is exactly what is specified in --editor. + absent + no sensible-editor configuration is present. + exists + the sensible-editor will be set to what is specified in --editor, + unless there already is a configuration on the target system. EXAMPLES diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index 3a1926b6..d4883403 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -47,9 +47,9 @@ os_version=$(cat "${__global}/explorer/os_version") state=$(cat "${__object}/parameter/state") user=$__object_id -if test "${state}" != 'present' && test "${state}" != 'absent' +if test "${state}" != 'present' && test "${state}" != 'exists' && test "${state}" != 'absent' then - echo 'Only "present" and "absent" are allowed for --state' >&2 + echo 'Only "present", "exists", and "absent" are allowed for --state' >&2 exit 1 fi From 9567826dc19526037219558ddd81912e1cdcf129 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Mon, 14 Oct 2019 09:21:23 +0200 Subject: [PATCH 15/16] [__sensible_editor] Fix incorrect require variable --- cdist/conf/type/__sensible_editor/manifest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index d4883403..1cdb0c2c 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -79,7 +79,7 @@ if test "${state}" != 'absent' then __package "${package_name}" --state present \ --type "${pkg_type}" - export __require="__package/${package_name}" + export require="__package/${package_name}" fi editor_path=$(cat "${__object}/explorer/editor_path") From e462821e469f0f81d56f11f3e8a1637d3b0e2291 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Mon, 14 Oct 2019 11:29:49 +0200 Subject: [PATCH 16/16] [__sensible_editor] Fix SC2028 --- cdist/conf/type/__sensible_editor/explorer/editor_path | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index 8a5199c9..dcf63c9b 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -30,7 +30,7 @@ die() { editor_missing() { die "Editor '$1' is missing on the target system."; } editor_no_alternative() { die "Editor '$1' is not in the alternatives list of the target system." \ - "$(test "${editors}" && echo "\nPlease choose one of:\n\n${editors}")" + "$(test -n "${editors}" && printf '\nPlease choose one of:\n\n%s\n' "${editors}")" } # No need to check for the path if the file is supposed to be removed.