From 76f07511a9c13eb33565d4af11953a80be67987c Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Mon, 18 Jul 2011 13:34:39 +0200 Subject: [PATCH 1/7] add --state parameter to __file type which allows removal of files Signed-off-by: Steven Armstrong --- conf/type/__file/gencode-local | 31 ++++++++++--------- conf/type/__file/gencode-remote | 46 +++++++++++++++++------------ conf/type/__file/man.text | 18 +++++++---- conf/type/__file/manifest | 27 +++++++++++++++++ conf/type/__file/parameter/optional | 1 + 5 files changed, 85 insertions(+), 38 deletions(-) create mode 100755 conf/type/__file/manifest diff --git a/conf/type/__file/gencode-local b/conf/type/__file/gencode-local index 1168919e..ee198fb1 100755 --- a/conf/type/__file/gencode-local +++ b/conf/type/__file/gencode-local @@ -22,23 +22,26 @@ # destination="/$__object_id" +state_should="$(cat "$__object/parameter/state")" -if [ -f "$__object/parameter/source" ]; then - source="$(cat "$__object/parameter/source")" +if [ "$state_should" = "present" ]; then + if [ -f "$__object/parameter/source" ]; then + source="$(cat "$__object/parameter/source")" - if [ -f "$source" ]; then - local_cksum="$(cksum < "$source")" - remote_cksum="$(cat "$__object/explorer/cksum")" + if [ -f "$source" ]; then + local_cksum="$(cksum < "$source")" + remote_cksum="$(cat "$__object/explorer/cksum")" - if [ "$local_cksum" != "$remote_cksum" ]; then - # FIXME: The username is ugly and hardcoded, replace after 1.0! - # Probably a better aproach is to have the user configured - # ~/.ssh/config to contain the right username - # Probably describe it in cdist-quickstart... - echo scp "$source" "root@${__target_host}:${destination}" + if [ "$local_cksum" != "$remote_cksum" ]; then + # FIXME: The username is ugly and hardcoded, replace after 1.0! + # Probably a better aproach is to have the user configured + # ~/.ssh/config to contain the right username + # Probably describe it in cdist-quickstart... + echo scp "$source" "root@${__target_host}:${destination}" + fi + else + echo "Source \"$source\" does not exist." >&2 + exit 1 fi - else - echo "Source \"$source\" does not exist." >&2 - exit 1 fi fi diff --git a/conf/type/__file/gencode-remote b/conf/type/__file/gencode-remote index 7c5cf7ce..b6e3c095 100755 --- a/conf/type/__file/gencode-remote +++ b/conf/type/__file/gencode-remote @@ -22,25 +22,33 @@ # destination="/$__object_id" +state_should="$(cat "$__object/parameter/state")" +exists="$(cat "$__object/explorer/exists")" -# No source? Create empty file -if [ ! -f "$__object/parameter/source" ]; then - if [ no = "$(cat "$__object/explorer/exists")" ]; then - echo touch \"$destination\" +if [ "$state_should" = "present" ]; then + # No source? Create empty file + if [ ! -f "$__object/parameter/source" ]; then + if [ "$exists" = "no" ]; then + echo touch \"$destination\" + fi + fi + + # Mode settings + if [ -f "$__object/parameter/mode" ]; then + echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\" + fi + + # Group + if [ -f "$__object/parameter/group" ]; then + echo chgrp \"$(cat "$__object/parameter/group")\" \"$destination\" + fi + + # Owner + if [ -f "$__object/parameter/owner" ]; then + echo chown \"$(cat "$__object/parameter/owner")\" \"$destination\" + fi +elif [ "$state_should" = "absent" ]; then + if [ "$exists" = "yes" ]; then + echo rm \"$destination\" fi fi - -# Mode settings -if [ -f "$__object/parameter/mode" ]; then - echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\" -fi - -# Group -if [ -f "$__object/parameter/group" ]; then - echo chgrp \"$(cat "$__object/parameter/group")\" \"$destination\" -fi - -# Owner -if [ -f "$__object/parameter/owner" ]; then - echo chown \"$(cat "$__object/parameter/owner")\" \"$destination\" -fi diff --git a/conf/type/__file/man.text b/conf/type/__file/man.text index 67ab53d1..fa6b7644 100644 --- a/conf/type/__file/man.text +++ b/conf/type/__file/man.text @@ -5,21 +5,24 @@ Nico Schottelius NAME ---- -cdist-type__file - Create files +cdist-type__file - Manage files DESCRIPTION ----------- -This cdist type allows you to create files on the target. +This cdist type allows you to create files, remove files and set file +attributes on the target. REQUIRED PARAMETERS ------------------- None. - OPTIONAL PARAMETERS ------------------- +state:: + 'present' or 'absent', defaults to 'present' + group:: Group to chgrp to. @@ -40,13 +43,18 @@ EXAMPLES -------------------------------------------------------------------------------- # Create /etc/cdist-configured as an empty file __file /etc/cdist-configured +# The same thing +__file /etc/cdist-configured --ensure present +# Delete existing file +__file /etc/cdist-configured --ensure absent # Use __file from another type -__file /etc/issue --source "$__type/files/archlinux" +__file /etc/issue --source "$__type/files/archlinux" --ensure present # Supply some more settings __file /etc/shadow --source "$__type/files/shadow" \ - --owner root --group shadow --mode 0640 + --owner root --group shadow --mode 0640 \ + --ensure present -------------------------------------------------------------------------------- diff --git a/conf/type/__file/manifest b/conf/type/__file/manifest new file mode 100755 index 00000000..59f10e42 --- /dev/null +++ b/conf/type/__file/manifest @@ -0,0 +1,27 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +name="$__object_id" + +if [ -f "$__object/parameter/state" ]; then + state="$(cat "$__object/parameter/state")" +else + echo "present" > "$__object/parameter/state" +fi diff --git a/conf/type/__file/parameter/optional b/conf/type/__file/parameter/optional index 06120518..c696d592 100644 --- a/conf/type/__file/parameter/optional +++ b/conf/type/__file/parameter/optional @@ -1,3 +1,4 @@ +state group mode owner From 37612ff102f3b87dcce38af667bfb0c8d1a80a51 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 21 Jul 2011 23:06:13 +0200 Subject: [PATCH 2/7] add --state parameter to __directory type which allows removal of directories Signed-off-by: Steven Armstrong --- conf/type/__directory/gencode-remote | 76 ++++++++++++++---------- conf/type/__directory/man.text | 10 +++- conf/type/__directory/manifest | 25 ++++++++ conf/type/__directory/parameter/optional | 1 + 4 files changed, 80 insertions(+), 32 deletions(-) create mode 100755 conf/type/__directory/manifest diff --git a/conf/type/__directory/gencode-remote b/conf/type/__directory/gencode-remote index a5458b4b..7fd589be 100755 --- a/conf/type/__directory/gencode-remote +++ b/conf/type/__directory/gencode-remote @@ -25,39 +25,55 @@ # destination="/$__object_id" +state_should="$(cat "$__object/parameter/state")" -# Include parent directories? -if [ -f "$__object/parameter/parents" ]; then - parents="$(cat "$__object/parameter/parents")" - if [ yes = "$parents" ]; then - mkdiropt="-p" - else - mkdiropt="" - fi -fi +case "$state_should" in + present) + # Include parent directories? + if [ -f "$__object/parameter/parents" ]; then + parents="$(cat "$__object/parameter/parents")" + if [ yes = "$parents" ]; then + mkdiropt="-p" + else + mkdiropt="" + fi + fi -if [ -f "$__object/parameter/recursive" ]; then - if [ yes = "$(cat "$__object/parameter/recursive")" ]; then - recursive="-R" - fi -fi + if [ -f "$__object/parameter/recursive" ]; then + if [ yes = "$(cat "$__object/parameter/recursive")" ]; then + recursive="-R" + fi + fi -# Only create if not already existent -if [ no = "$(cat "$__object/explorer/exists")" ]; then - echo mkdir $mkdiropt \"$destination\" -fi + # Only create if not already existent + if [ no = "$(cat "$__object/explorer/exists")" ]; then + echo mkdir $mkdiropt \"$destination\" + fi -# Mode settings -if [ -f "$__object/parameter/mode" ]; then - echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\" -fi + # Mode settings + if [ -f "$__object/parameter/mode" ]; then + echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\" + fi -# Group -if [ -f "$__object/parameter/group" ]; then - echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\" -fi + # Group + if [ -f "$__object/parameter/group" ]; then + echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\" + fi -# Owner -if [ -f "$__object/parameter/owner" ]; then - echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\" -fi + # Owner + if [ -f "$__object/parameter/owner" ]; then + echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\" + fi + ;; + absent) + # Only delete if it exists + if [ yes = "$(cat "$__object/explorer/exists")" ]; then + echo rm -r \"$destination\" + fi + + ;; + *) + echo "Unknown state" >&2 + exit 1 + ;; +esac diff --git a/conf/type/__directory/man.text b/conf/type/__directory/man.text index e2201172..315d1fb9 100644 --- a/conf/type/__directory/man.text +++ b/conf/type/__directory/man.text @@ -5,12 +5,12 @@ Nico Schottelius NAME ---- -cdist-type__directory - Create a directory +cdist-type__directory - Manage a directory DESCRIPTION ----------- -This cdist type allows you to create directories on the target. +This cdist type allows you to create or remove directories on the target. REQUIRED PARAMETERS @@ -20,6 +20,9 @@ None. OPTIONAL PARAMETERS ------------------- +state:: + 'present' or 'absent', defaults to 'present' + group:: Group to chgrp to. @@ -45,6 +48,9 @@ EXAMPLES # A silly example __directory /tmp/foobar +# Remove a directory +__directory /tmp/foobar --ensure absent + # Ensure /etc exists correctly __directory /etc --owner root --group root --mode 0755 diff --git a/conf/type/__directory/manifest b/conf/type/__directory/manifest new file mode 100755 index 00000000..2fcc085d --- /dev/null +++ b/conf/type/__directory/manifest @@ -0,0 +1,25 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +if [ -f "$__object/parameter/state" ]; then + state="$(cat "$__object/parameter/state")" +else + echo "present" > "$__object/parameter/state" +fi diff --git a/conf/type/__directory/parameter/optional b/conf/type/__directory/parameter/optional index 247e2f64..27f9d76a 100644 --- a/conf/type/__directory/parameter/optional +++ b/conf/type/__directory/parameter/optional @@ -1,3 +1,4 @@ +state group mode owner From 65de5c18883d42844ccb54a4fd245157cb7b5f22 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 22 Jul 2011 00:28:41 +0200 Subject: [PATCH 3/7] add --state parameter to __link type which allows removal of links Signed-off-by: Steven Armstrong --- conf/type/__link/gencode-remote | 15 ++++++++++++++- conf/type/__link/man.text | 3 ++- conf/type/__link/manifest | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100755 conf/type/__link/manifest diff --git a/conf/type/__link/gencode-remote b/conf/type/__link/gencode-remote index 07da987c..3bfa90d8 100755 --- a/conf/type/__link/gencode-remote +++ b/conf/type/__link/gencode-remote @@ -39,4 +39,17 @@ case "$type" in ;; esac -echo ln ${lnopt} -f \"$source\" \"$destination\" +state_should="$(cat "$__object/parameter/state")" +case "$state_should" in + present) + echo ln ${lnopt} -f \"$source\" \"$destination\" + ;; + absent) + echo rm \"$destination\" + ;; + *) + echo "Unknown state" >&2 + exit 1 + ;; +esac + diff --git a/conf/type/__link/man.text b/conf/type/__link/man.text index fb914298..a7d33c59 100644 --- a/conf/type/__link/man.text +++ b/conf/type/__link/man.text @@ -25,7 +25,8 @@ type:: OPTIONAL PARAMETERS ------------------- -None. +state:: + 'present' or 'absent', defaults to 'present' EXAMPLES diff --git a/conf/type/__link/manifest b/conf/type/__link/manifest new file mode 100755 index 00000000..2fcc085d --- /dev/null +++ b/conf/type/__link/manifest @@ -0,0 +1,25 @@ +#!/bin/sh +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# 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 . +# + +if [ -f "$__object/parameter/state" ]; then + state="$(cat "$__object/parameter/state")" +else + echo "present" > "$__object/parameter/state" +fi From 71c33f4483c1dc89591b070e6b557a08236c3ca1 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 22 Jul 2011 00:33:06 +0200 Subject: [PATCH 4/7] add forgotten parameter Signed-off-by: Steven Armstrong --- conf/type/__link/parameter/optional | 1 + 1 file changed, 1 insertion(+) create mode 100644 conf/type/__link/parameter/optional diff --git a/conf/type/__link/parameter/optional b/conf/type/__link/parameter/optional new file mode 100644 index 00000000..ff72b5c7 --- /dev/null +++ b/conf/type/__link/parameter/optional @@ -0,0 +1 @@ +state From 4836504ad677553abb6eb0ebb804e56dc7e50776 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 22 Jul 2011 00:41:37 +0200 Subject: [PATCH 5/7] ignore nonexistent files when deleting Signed-off-by: Steven Armstrong --- conf/type/__file/gencode-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/type/__file/gencode-remote b/conf/type/__file/gencode-remote index b6e3c095..ef42dd54 100755 --- a/conf/type/__file/gencode-remote +++ b/conf/type/__file/gencode-remote @@ -49,6 +49,6 @@ if [ "$state_should" = "present" ]; then fi elif [ "$state_should" = "absent" ]; then if [ "$exists" = "yes" ]; then - echo rm \"$destination\" + echo rm -f \"$destination\" fi fi From 2b8d0731a147206fd3e43937e1058a86a5164f67 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 22 Jul 2011 00:42:12 +0200 Subject: [PATCH 6/7] ignore nonexistent files when deleting Signed-off-by: Steven Armstrong --- conf/type/__link/gencode-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/type/__link/gencode-remote b/conf/type/__link/gencode-remote index 3bfa90d8..bb43de2e 100755 --- a/conf/type/__link/gencode-remote +++ b/conf/type/__link/gencode-remote @@ -45,7 +45,7 @@ case "$state_should" in echo ln ${lnopt} -f \"$source\" \"$destination\" ;; absent) - echo rm \"$destination\" + echo rm -f \"$destination\" ;; *) echo "Unknown state" >&2 From 2e61f54c111d9218116d6142f530f799cd70587f Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Mon, 9 Jan 2012 10:51:53 +0100 Subject: [PATCH 7/7] import latest changes from private repo Signed-off-by: Steven Armstrong --- conf/type/__directory/gencode-remote | 6 ------ conf/type/__directory/manifest | 8 +++----- conf/type/__file/gencode-local | 6 +----- conf/type/__file/manifest | 8 +++----- conf/type/__link/manifest | 8 +++----- 5 files changed, 10 insertions(+), 26 deletions(-) diff --git a/conf/type/__directory/gencode-remote b/conf/type/__directory/gencode-remote index 7fd589be..e9feecf4 100755 --- a/conf/type/__directory/gencode-remote +++ b/conf/type/__directory/gencode-remote @@ -17,12 +17,6 @@ # You should have received a copy of the GNU General Public License # along with cdist. If not, see . # -# -# Handle directories -# -# -# __directory /etc [--mode --owner --group --parents [yes|no] ] -# destination="/$__object_id" state_should="$(cat "$__object/parameter/state")" diff --git a/conf/type/__directory/manifest b/conf/type/__directory/manifest index 2fcc085d..a8ee5a6f 100755 --- a/conf/type/__directory/manifest +++ b/conf/type/__directory/manifest @@ -18,8 +18,6 @@ # along with cdist. If not, see . # -if [ -f "$__object/parameter/state" ]; then - state="$(cat "$__object/parameter/state")" -else - echo "present" > "$__object/parameter/state" -fi +# set defaults +state="$(cat "$__object/parameter/state" 2>/dev/null \ + || echo "present" | tee "$__object/parameter/state")" diff --git a/conf/type/__file/gencode-local b/conf/type/__file/gencode-local index ee198fb1..d9839a19 100755 --- a/conf/type/__file/gencode-local +++ b/conf/type/__file/gencode-local @@ -33,11 +33,7 @@ if [ "$state_should" = "present" ]; then remote_cksum="$(cat "$__object/explorer/cksum")" if [ "$local_cksum" != "$remote_cksum" ]; then - # FIXME: The username is ugly and hardcoded, replace after 1.0! - # Probably a better aproach is to have the user configured - # ~/.ssh/config to contain the right username - # Probably describe it in cdist-quickstart... - echo scp "$source" "root@${__target_host}:${destination}" + echo "$__remote_copy" "$source" "${__target_host}:${destination}" fi else echo "Source \"$source\" does not exist." >&2 diff --git a/conf/type/__file/manifest b/conf/type/__file/manifest index 59f10e42..915a2ca9 100755 --- a/conf/type/__file/manifest +++ b/conf/type/__file/manifest @@ -20,8 +20,6 @@ name="$__object_id" -if [ -f "$__object/parameter/state" ]; then - state="$(cat "$__object/parameter/state")" -else - echo "present" > "$__object/parameter/state" -fi +# set defaults +state="$(cat "$__object/parameter/state" 2>/dev/null \ + || echo "present" | tee "$__object/parameter/state")" diff --git a/conf/type/__link/manifest b/conf/type/__link/manifest index 2fcc085d..a8ee5a6f 100755 --- a/conf/type/__link/manifest +++ b/conf/type/__link/manifest @@ -18,8 +18,6 @@ # along with cdist. If not, see . # -if [ -f "$__object/parameter/state" ]; then - state="$(cat "$__object/parameter/state")" -else - echo "present" > "$__object/parameter/state" -fi +# set defaults +state="$(cat "$__object/parameter/state" 2>/dev/null \ + || echo "present" | tee "$__object/parameter/state")"