From 88ec06d06fdd2cf0907e8f36614d20f970279ace Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Fri, 22 Jul 2016 09:55:44 +0300 Subject: [PATCH 1/6] new type: __hosts This type adds or removes entries from /etc/hosts, ensuring that same hostname can never resolve to several different ip addresses. Signed-off-by: Dmitry Bogatov --- cdist/conf/type/__hosts/man.rst | 55 +++++++++++++++++++ cdist/conf/type/__hosts/manifest | 49 +++++++++++++++++ .../conf/type/__hosts/parameter/default/state | 1 + cdist/conf/type/__hosts/parameter/optional | 2 + 4 files changed, 107 insertions(+) create mode 100644 cdist/conf/type/__hosts/man.rst create mode 100755 cdist/conf/type/__hosts/manifest create mode 100644 cdist/conf/type/__hosts/parameter/default/state create mode 100644 cdist/conf/type/__hosts/parameter/optional diff --git a/cdist/conf/type/__hosts/man.rst b/cdist/conf/type/__hosts/man.rst new file mode 100644 index 00000000..f89a619c --- /dev/null +++ b/cdist/conf/type/__hosts/man.rst @@ -0,0 +1,55 @@ +cdist-type__hosts(7) +==================== + +NAME +---- + +cdist-type__hosts - manage entries in /etc/hosts + +DESCRIPTION +----------- + +Add or remove entries from */etc/hosts* file. + +OPTIONAL PARAMETERS +------------------- + +state + + If state is ``present``, make *object_id* resolve to *ip*. If + state is ``absent``, *object_id* will no longer resolve to + anything via */etc/hosts*. + +ip + + IP address, to which hostname (=\ *object_id*) must resolve. If + state is ``present``, this parameter is mandatory, if state is + ``absent``, this parameter is silently ignored. + +EXAMPLES +-------- + +.. code-block:: sh + + # Now `funny' resolves to 192.168.1.76, + __hosts funny --ip 192.168.1.76 + # and `happy' does not resolve (at least via /etc/hosts) + __hosts happy --state absent + +SEE ALSO +-------- + +:strong:`hosts`\ (5) + +AUTHORS +------- + +Dmitry Bogatov + + +COPYING +------- + +Copyright (C) 2015,2016 Dmitry Bogatov. Free use of this software is granted +under the terms of the GNU General Public License version 3 or later +(GPLv3+). diff --git a/cdist/conf/type/__hosts/manifest b/cdist/conf/type/__hosts/manifest new file mode 100755 index 00000000..99a06c09 --- /dev/null +++ b/cdist/conf/type/__hosts/manifest @@ -0,0 +1,49 @@ +#!/bin/sh +# Copyright (C) 2015 Bogatov Dmitry +# +# This program 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. +# +# This program 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 this program. If not, see . + +hostname="$__object_id" +state="$(cat "$__object/parameter/state")" + +# First remove all lines for given hostname and then insert again. +# Otherwise, we risk having multiple entries for same hostname. +# +# There is a corner case, which we do not handle. Namely, /etc/hosts +# format allows several host names on single line, like following: +# +# 192.168.15.16 foo bar +# +# If this type manages hostname `foo', then hostname bar will get erased. + +__line "__hosts/delete/${hostname}" \ + --state absent \ + --regex " ${hostname}[ ]*$" \ + --file /etc/hosts +export require="__line/__hosts/delete/${hostname}" + +case "$state" in + absent) + : # Nothing to do + ;; + present) + ip="$(cat "$__object/parameter/ip")" + __line "__hosts/insert/${hostname}" \ + --line "${ip} ${hostname}" \ + --file /etc/hosts + ;; + *) + echo "ERROR: type (${__type##*/}) does not support state \`$state'" + exit 1 +esac diff --git a/cdist/conf/type/__hosts/parameter/default/state b/cdist/conf/type/__hosts/parameter/default/state new file mode 100644 index 00000000..e7f6134f --- /dev/null +++ b/cdist/conf/type/__hosts/parameter/default/state @@ -0,0 +1 @@ +present diff --git a/cdist/conf/type/__hosts/parameter/optional b/cdist/conf/type/__hosts/parameter/optional new file mode 100644 index 00000000..411fc5d2 --- /dev/null +++ b/cdist/conf/type/__hosts/parameter/optional @@ -0,0 +1,2 @@ +state +ip From fdd1062b857a56e55e2f54464561888ba4058522 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sat, 23 Jul 2016 16:20:46 +0200 Subject: [PATCH 2/6] Update changelog --- docs/changelog | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/changelog b/docs/changelog index d85af785..db1cd080 100644 --- a/docs/changelog +++ b/docs/changelog @@ -1,6 +1,9 @@ Changelog --------- +next: + * New type: __hosts: manage entries in /etc/hosts (Dmitry Bogatov) + 4.2.1: 2016-07-18 * Build: Fix signed release (Darko Poljak) * Build: Fix building docs (Darko Poljak) From 574688c6e1f27fdcd3cc69c18b7e36506d528b95 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 2 Feb 2017 19:47:02 +0100 Subject: [PATCH 3/6] Rm due to new patch from Dmitry. --- cdist/conf/type/__hosts/man.rst | 55 ------------------- cdist/conf/type/__hosts/manifest | 49 ----------------- .../conf/type/__hosts/parameter/default/state | 1 - cdist/conf/type/__hosts/parameter/optional | 2 - 4 files changed, 107 deletions(-) delete mode 100644 cdist/conf/type/__hosts/man.rst delete mode 100755 cdist/conf/type/__hosts/manifest delete mode 100644 cdist/conf/type/__hosts/parameter/default/state delete mode 100644 cdist/conf/type/__hosts/parameter/optional diff --git a/cdist/conf/type/__hosts/man.rst b/cdist/conf/type/__hosts/man.rst deleted file mode 100644 index f89a619c..00000000 --- a/cdist/conf/type/__hosts/man.rst +++ /dev/null @@ -1,55 +0,0 @@ -cdist-type__hosts(7) -==================== - -NAME ----- - -cdist-type__hosts - manage entries in /etc/hosts - -DESCRIPTION ------------ - -Add or remove entries from */etc/hosts* file. - -OPTIONAL PARAMETERS -------------------- - -state - - If state is ``present``, make *object_id* resolve to *ip*. If - state is ``absent``, *object_id* will no longer resolve to - anything via */etc/hosts*. - -ip - - IP address, to which hostname (=\ *object_id*) must resolve. If - state is ``present``, this parameter is mandatory, if state is - ``absent``, this parameter is silently ignored. - -EXAMPLES --------- - -.. code-block:: sh - - # Now `funny' resolves to 192.168.1.76, - __hosts funny --ip 192.168.1.76 - # and `happy' does not resolve (at least via /etc/hosts) - __hosts happy --state absent - -SEE ALSO --------- - -:strong:`hosts`\ (5) - -AUTHORS -------- - -Dmitry Bogatov - - -COPYING -------- - -Copyright (C) 2015,2016 Dmitry Bogatov. Free use of this software is granted -under the terms of the GNU General Public License version 3 or later -(GPLv3+). diff --git a/cdist/conf/type/__hosts/manifest b/cdist/conf/type/__hosts/manifest deleted file mode 100755 index 99a06c09..00000000 --- a/cdist/conf/type/__hosts/manifest +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh -# Copyright (C) 2015 Bogatov Dmitry -# -# This program 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. -# -# This program 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 this program. If not, see . - -hostname="$__object_id" -state="$(cat "$__object/parameter/state")" - -# First remove all lines for given hostname and then insert again. -# Otherwise, we risk having multiple entries for same hostname. -# -# There is a corner case, which we do not handle. Namely, /etc/hosts -# format allows several host names on single line, like following: -# -# 192.168.15.16 foo bar -# -# If this type manages hostname `foo', then hostname bar will get erased. - -__line "__hosts/delete/${hostname}" \ - --state absent \ - --regex " ${hostname}[ ]*$" \ - --file /etc/hosts -export require="__line/__hosts/delete/${hostname}" - -case "$state" in - absent) - : # Nothing to do - ;; - present) - ip="$(cat "$__object/parameter/ip")" - __line "__hosts/insert/${hostname}" \ - --line "${ip} ${hostname}" \ - --file /etc/hosts - ;; - *) - echo "ERROR: type (${__type##*/}) does not support state \`$state'" - exit 1 -esac diff --git a/cdist/conf/type/__hosts/parameter/default/state b/cdist/conf/type/__hosts/parameter/default/state deleted file mode 100644 index e7f6134f..00000000 --- a/cdist/conf/type/__hosts/parameter/default/state +++ /dev/null @@ -1 +0,0 @@ -present diff --git a/cdist/conf/type/__hosts/parameter/optional b/cdist/conf/type/__hosts/parameter/optional deleted file mode 100644 index 411fc5d2..00000000 --- a/cdist/conf/type/__hosts/parameter/optional +++ /dev/null @@ -1,2 +0,0 @@ -state -ip From c4c2d45e5956cab0d044cff93edc47ffd59f43fe Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Thu, 2 Feb 2017 10:51:21 +0300 Subject: [PATCH 4/6] New type: __hosts --- cdist/conf/type/__hosts/man.rst | 57 +++++++++++++++++++ cdist/conf/type/__hosts/manifest | 29 ++++++++++ .../conf/type/__hosts/parameter/default/state | 1 + cdist/conf/type/__hosts/parameter/optional | 2 + docs/changelog | 1 + 5 files changed, 90 insertions(+) create mode 100644 cdist/conf/type/__hosts/man.rst create mode 100755 cdist/conf/type/__hosts/manifest create mode 100644 cdist/conf/type/__hosts/parameter/default/state create mode 100644 cdist/conf/type/__hosts/parameter/optional diff --git a/cdist/conf/type/__hosts/man.rst b/cdist/conf/type/__hosts/man.rst new file mode 100644 index 00000000..20f463e7 --- /dev/null +++ b/cdist/conf/type/__hosts/man.rst @@ -0,0 +1,57 @@ +cdist-type__hosts(7) +==================== + +NAME +---- + +cdist-type__hosts - manage entries in /etc/hosts + +DESCRIPTION +----------- + +Add or remove entries from */etc/hosts* file. + +OPTIONAL PARAMETERS +------------------- + +state + + If state is ``present``, make *object_id* resolve to *ip*. If + state is ``absent``, *object_id* will no longer resolve via + */etc/hosts*, if it was previously configured with this type. + Manually inserted entries are unaffected. + +ip + + IP address, to which hostname (=\ *object_id*) must resolve. If + state is ``present``, this parameter is mandatory, if state is + ``absent``, this parameter is silently ignored. + +EXAMPLES +-------- + +.. code-block:: sh + + # Now `funny' resolves to 192.168.1.76, + __hosts funny --ip 192.168.1.76 + # and `happy' no longer resolve via /etc/hosts if it was + # previously configured via __hosts. + __hosts happy --state absent + +SEE ALSO +-------- + +:strong:`hosts`\ (5) + +AUTHORS +------- + +Dmitry Bogatov + + +COPYING +------- + +Copyright (C) 2015,2016 Dmitry Bogatov. Free use of this software is granted +under the terms of the GNU General Public License version 3 or later +(GPLv3+). diff --git a/cdist/conf/type/__hosts/manifest b/cdist/conf/type/__hosts/manifest new file mode 100755 index 00000000..6fa21608 --- /dev/null +++ b/cdist/conf/type/__hosts/manifest @@ -0,0 +1,29 @@ +#!/bin/sh +# Copyright (C) 2015 Bogatov Dmitry +# +# This program 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. +# +# This program 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 this program. If not, see . +set -ue + +hostname="$__object_id" +state="$(cat "$__object/parameter/state")" +marker="# __hosts/$hostname" + +set -- "__hosts/$hostname" --file /etc/hosts --state "$state" + +if [ "$state" = absent ] ; then + __line "$@" --regex "$marker" +else + ip="$(cat "$__object/parameter/ip")" + __line "$@" --line "$ip $hostname $marker" +fi diff --git a/cdist/conf/type/__hosts/parameter/default/state b/cdist/conf/type/__hosts/parameter/default/state new file mode 100644 index 00000000..e7f6134f --- /dev/null +++ b/cdist/conf/type/__hosts/parameter/default/state @@ -0,0 +1 @@ +present diff --git a/cdist/conf/type/__hosts/parameter/optional b/cdist/conf/type/__hosts/parameter/optional new file mode 100644 index 00000000..411fc5d2 --- /dev/null +++ b/cdist/conf/type/__hosts/parameter/optional @@ -0,0 +1,2 @@ +state +ip diff --git a/docs/changelog b/docs/changelog index 6b869256..44631046 100644 --- a/docs/changelog +++ b/docs/changelog @@ -8,6 +8,7 @@ next: * Type __cron: Fix filter for new cron on sles12 sp2 (Daniel Heule) * Type __docker: Support absent state (Dominique Roux) * Type __docker_compose: Support absent state (Dominique Roux) + * New type: __hosts (Dmitry Bogatov) 4.4.1: 2016-12-17 * Documentation: Update docs for types that used man.rst as symbolic links (Darko Poljak) From 0eda926b8cfd8b66fd9bb40964254f5a2c81eb46 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 2 Feb 2017 19:49:39 +0100 Subject: [PATCH 5/6] Fix changelog. --- docs/changelog | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/changelog b/docs/changelog index 44631046..e50ed243 100644 --- a/docs/changelog +++ b/docs/changelog @@ -2,7 +2,6 @@ Changelog --------- next: - * New type: __hosts: manage entries in /etc/hosts (Dmitry Bogatov) * Core: Fix suppression of manifests' outputs (Darko Poljak) * Type __user_groups: Support FreeBSD (Andres Erbsen) * Type __cron: Fix filter for new cron on sles12 sp2 (Daniel Heule) From 72d73e307d020260d8c22ffde8626d5391235d57 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 2 Feb 2017 19:50:51 +0100 Subject: [PATCH 6/6] Fix changelog. --- docs/changelog | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/changelog b/docs/changelog index e50ed243..93e6e24f 100644 --- a/docs/changelog +++ b/docs/changelog @@ -43,7 +43,6 @@ next: * Type __filesystem: Spelling fixes (Dmitry Bogatov) * Core: Add target_host file to cache since cache dir name can be hash (Darko Poljak) * Core: Improve hostfile: support comments, skip empty lines (Darko Poljak) ->>>>>>> ungleich/master 4.3.0: 2016-08-19 * Documentation: Add Parallelization chapter (Darko Poljak)