From 294285c164fdb212ea2fc6277c97fc877e889cb2 Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@bento.schottelius.org>
Date: Thu, 6 Feb 2014 00:08:40 +0100
Subject: [PATCH] import __ccollect_source type

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
---
 .../type/__ccollect_source/explorer/cksum     | 34 +++++++
 .../conf/type/__ccollect_source/explorer/stat | 47 ++++++++++
 .../conf/type/__ccollect_source/explorer/type | 33 +++++++
 .../type/__ccollect_source/gencode-remote     | 93 +++++++++++++++++++
 cdist/conf/type/__ccollect_source/man.text    | 64 +++++++++++++
 cdist/conf/type/__ccollect_source/manifest    | 53 +++++++++++
 .../type/__ccollect_source/parameter/boolean  |  1 +
 .../parameter/default/ccollectconf            |  1 +
 .../__ccollect_source/parameter/default/state |  1 +
 .../type/__ccollect_source/parameter/optional |  2 +
 .../parameter/optional_multiple               |  1 +
 .../type/__ccollect_source/parameter/required |  2 +
 12 files changed, 332 insertions(+)
 create mode 100755 cdist/conf/type/__ccollect_source/explorer/cksum
 create mode 100755 cdist/conf/type/__ccollect_source/explorer/stat
 create mode 100755 cdist/conf/type/__ccollect_source/explorer/type
 create mode 100755 cdist/conf/type/__ccollect_source/gencode-remote
 create mode 100644 cdist/conf/type/__ccollect_source/man.text
 create mode 100755 cdist/conf/type/__ccollect_source/manifest
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/boolean
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/default/ccollectconf
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/default/state
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/optional
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/optional_multiple
 create mode 100644 cdist/conf/type/__ccollect_source/parameter/required

diff --git a/cdist/conf/type/__ccollect_source/explorer/cksum b/cdist/conf/type/__ccollect_source/explorer/cksum
new file mode 100755
index 00000000..335e4e7a
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/explorer/cksum
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+#
+# Retrieve the md5sum of a file to be created, if it is already existing.
+#
+
+destination="/$__object_id"
+
+if [ -e "$destination" ]; then
+    if [ -f  "$destination" ]; then
+        cksum < "$destination"
+    else
+        echo "NO REGULAR FILE"
+    fi
+else
+    echo "NO FILE FOUND, NO CHECKSUM CALCULATED."
+fi
diff --git a/cdist/conf/type/__ccollect_source/explorer/stat b/cdist/conf/type/__ccollect_source/explorer/stat
new file mode 100755
index 00000000..298221b7
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/explorer/stat
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# 2013 Steven Armstrong (steven-cdist 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 <http://www.gnu.org/licenses/>.
+#
+
+destination="/$__object_id"
+
+# nothing to work with, nothing we could do
+[ -e "$destination" ] || exit 0
+
+os=$("$__explorer/os")
+case "$os" in
+   "freebsd")
+      # FIXME: should be something like this based on man page, but can not test
+      stat -f "type: %ST
+owner: %Du %Su
+group: %Dg %Sg
+mode: %Op %Sp
+size: %Dz
+links: %Dl
+" "$destination"
+   ;;
+   *)
+      stat --printf="type: %F
+owner: %u %U
+group: %g %G
+mode: %a %A
+size: %s
+links: %h
+" "$destination"
+   ;;
+esac
diff --git a/cdist/conf/type/__ccollect_source/explorer/type b/cdist/conf/type/__ccollect_source/explorer/type
new file mode 100755
index 00000000..e723047c
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/explorer/type
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# 2013 Steven Armstrong (steven-cdist 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 <http://www.gnu.org/licenses/>.
+#
+
+destination="/$__object_id"
+
+if [ ! -e "$destination" ]; then
+   echo none
+elif [ -h "$destination" ]; then
+   echo symlink
+elif [ -f "$destination" ]; then
+   echo file
+elif [ -d "$destination" ]; then
+   echo directory
+else
+   echo unknown
+fi
diff --git a/cdist/conf/type/__ccollect_source/gencode-remote b/cdist/conf/type/__ccollect_source/gencode-remote
new file mode 100755
index 00000000..c41b5179
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/gencode-remote
@@ -0,0 +1,93 @@
+#!/bin/sh
+#
+# 2014 Nico Schottelius (nico-cdist at schottelius.org)
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+
+destination="/$__object_id"
+state_should="$(cat "$__object/parameter/state")"
+type="$(cat "$__object/explorer/type")"
+stat_file="$__object/explorer/stat"
+
+
+get_current_value() {
+    if [ -s "$stat_file" ]; then
+        _name="$1"
+        _value="$2"
+        case "$_value" in
+            [0-9]*)
+                _index=2
+            ;;
+            *)
+                _index=3
+            ;;
+        esac
+        awk '/'"$_name"':/ { print $'$_index' }' "$stat_file"
+        unset _name _value _index
+    fi
+}
+
+set_group() {
+    echo chgrp \"$1\" \"$destination\"
+    echo chgrp $1 >> "$__messages_out"
+}
+
+set_owner() {
+    echo chown \"$1\" \"$destination\"
+    echo chown $1 >> "$__messages_out"
+}
+
+set_mode() {
+   echo chmod \"$1\" \"$destination\"
+   echo chmod $1 >> "$__messages_out"
+}
+
+set_attributes=
+case "$state_should" in
+    present|exists)
+    # Note: Mode - needs to happen last as a chown/chgrp can alter mode by
+    #  clearing S_ISUID and S_ISGID bits (see chown(2))
+    for attribute in group owner mode; do
+        if [ -f "$__object/parameter/$attribute" ]; then
+            value_should="$(cat "$__object/parameter/$attribute")"
+
+            # change 0xxx format to xxx format => same as stat returns
+            if [ "$attribute" = mode ]; then
+                value_should="$(echo $value_should | sed 's/^0\(...\)/\1/')"
+            fi
+            
+            value_is="$(get_current_value "$attribute" "$value_should")"
+            if [ -f "$__object/files/set-attributes" -o "$value_should" != "$value_is" ]; then
+                "set_$attribute" "$value_should"
+            fi
+        fi
+    done
+
+    ;;
+
+    absent)
+        if [ "$type" = "file" ]; then
+            echo rm -f \"$destination\"
+            echo remove >> "$__messages_out"
+        fi
+    ;;
+
+    *)
+        echo "Unknown state: $state_should" >&2
+        exit 1
+    ;;
+esac
diff --git a/cdist/conf/type/__ccollect_source/man.text b/cdist/conf/type/__ccollect_source/man.text
new file mode 100644
index 00000000..32a7467e
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/man.text
@@ -0,0 +1,64 @@
+cdist-type__ccollect_source(7)
+==============================
+Nico Schottelius <nico-cdist--@--schottelius.org>
+
+
+NAME
+----
+cdist-type__ccollect_source - Manage ccollect sources
+
+
+DESCRIPTION
+-----------
+This cdist type allows you to create or delete ccollect sources.
+
+REQUIRED PARAMETERS
+-------------------
+source::
+    The source from which to backup
+destination::
+    The destination directory
+
+
+OPTIONAL PARAMETERS
+-------------------
+state::
+    'present' or 'absent', defaults to 'present'
+ccollectconf::
+    The CCOLLECT_CONF directory. Defaults to /etc/ccollect.
+
+
+OPTIONAL MULTIPLE PARAMETERS
+----------------------------
+exclude::
+    Paths to exclude of backup
+
+BOOLEAN PARAMETERS
+------------------
+verbose::
+    Whether to report backup verbosely
+
+EXAMPLES
+--------
+
+--------------------------------------------------------------------------------
+__ccollect_source doc.ungleich.ch \
+    --source doc.ungleich.ch:/ \
+    --destination /backup/doc.ungleich.ch \
+    --exclude '/proc/*' --exclude '/sys/*' \
+    --verbose
+
+--------------------------------------------------------------------------------
+
+
+SEE ALSO
+--------
+- cdist-type(7)
+- ccollect(1)
+- http://www.nico.schottelius.org/software/ccollect/
+
+
+COPYING
+-------
+Copyright \(C) 2014 Nico Schottelius. Free use of this software is
+granted under the terms of the GNU General Public License version 3 (GPLv3).
diff --git a/cdist/conf/type/__ccollect_source/manifest b/cdist/conf/type/__ccollect_source/manifest
new file mode 100755
index 00000000..89c2ef2b
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/manifest
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# 2014 Nico Schottelius (nico-cdist at schottelius.org)
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+
+name="$__object_id"
+state="$(cat "$__object/parameter/state")"
+source="$(cat "$__object/parameter/source")"
+destination="$(cat "$__object/parameter/destination")"
+ccollectconf="$(cat "$__object/parameter/ccollectconf" | sed 's,/$,,')"
+
+sourcedir="$ccollectconf/sources"
+basedir="$sourcedir/$name"
+
+destination_file="$basedir/destination"
+source_file="$basedir/source"
+exclude_file="$basedir/exclude"
+verbose_file="$basedir/verbose"
+
+__directory "$basedir" --state "$state"
+
+export require="__directory$basedir"
+echo "$destination" | __file "$destination_file" --source - --state "$state"
+echo "$source" | __file "$source_file" --source - --state "$state"
+
+################################################################################
+# Booleans
+if [ -f "$__object/parameter/verbose" ]; then
+    verbosestate="present"
+else
+    verbosestate="absent"
+fi
+__file "$verbose_file" --state "$verbosestate"
+
+if [ -f "$__object/parameter/exclude" ]; then
+    __file "$exclude_file" --source - --state "$state" \
+        < "$__object/parameter/exclude"
+fi
diff --git a/cdist/conf/type/__ccollect_source/parameter/boolean b/cdist/conf/type/__ccollect_source/parameter/boolean
new file mode 100644
index 00000000..c00ee94a
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/boolean
@@ -0,0 +1 @@
+verbose
diff --git a/cdist/conf/type/__ccollect_source/parameter/default/ccollectconf b/cdist/conf/type/__ccollect_source/parameter/default/ccollectconf
new file mode 100644
index 00000000..a9fda009
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/default/ccollectconf
@@ -0,0 +1 @@
+/etc/ccollect
diff --git a/cdist/conf/type/__ccollect_source/parameter/default/state b/cdist/conf/type/__ccollect_source/parameter/default/state
new file mode 100644
index 00000000..e7f6134f
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/default/state
@@ -0,0 +1 @@
+present
diff --git a/cdist/conf/type/__ccollect_source/parameter/optional b/cdist/conf/type/__ccollect_source/parameter/optional
new file mode 100644
index 00000000..0249d11e
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/optional
@@ -0,0 +1,2 @@
+ccollectconf
+state
diff --git a/cdist/conf/type/__ccollect_source/parameter/optional_multiple b/cdist/conf/type/__ccollect_source/parameter/optional_multiple
new file mode 100644
index 00000000..9ba870ea
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/optional_multiple
@@ -0,0 +1 @@
+exclude
diff --git a/cdist/conf/type/__ccollect_source/parameter/required b/cdist/conf/type/__ccollect_source/parameter/required
new file mode 100644
index 00000000..9239646e
--- /dev/null
+++ b/cdist/conf/type/__ccollect_source/parameter/required
@@ -0,0 +1,2 @@
+source
+destination