From 041569b41c00a8053824906382c93a9875180580 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 24 Feb 2015 12:46:40 +0100
Subject: [PATCH] new type __config_file to manage configuration files

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 cdist/conf/type/__config_file/gencode-remote  | 34 +++++++++++
 cdist/conf/type/__config_file/man.text        | 60 +++++++++++++++++++
 cdist/conf/type/__config_file/manifest        | 41 +++++++++++++
 .../__config_file/parameter/default/state     |  1 +
 .../type/__config_file/parameter/optional     |  6 ++
 docs/changelog                                |  2 +
 6 files changed, 144 insertions(+)
 create mode 100755 cdist/conf/type/__config_file/gencode-remote
 create mode 100644 cdist/conf/type/__config_file/man.text
 create mode 100755 cdist/conf/type/__config_file/manifest
 create mode 100644 cdist/conf/type/__config_file/parameter/default/state
 create mode 100644 cdist/conf/type/__config_file/parameter/optional

diff --git a/cdist/conf/type/__config_file/gencode-remote b/cdist/conf/type/__config_file/gencode-remote
new file mode 100755
index 00000000..e9b38c35
--- /dev/null
+++ b/cdist/conf/type/__config_file/gencode-remote
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# 2015 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 <http://www.gnu.org/licenses/>.
+#
+
+destination="$__object_id"
+state="$(cat "$__object/parameter/state")"
+
+if [ "$state" = "absent" ]; then
+   # nothing to do
+   exit 0
+fi
+
+if [ -f "$__object/parameter/onchange" ]; then
+   if grep -q "^__file/${destination}" "$__messages_in"; then
+      cat "$__object/parameter/onchange"
+   fi
+fi
+
diff --git a/cdist/conf/type/__config_file/man.text b/cdist/conf/type/__config_file/man.text
new file mode 100644
index 00000000..a4ec7363
--- /dev/null
+++ b/cdist/conf/type/__config_file/man.text
@@ -0,0 +1,60 @@
+cdist-type__config_file(7)
+==========================
+Steven Armstrong <steven-cdist--@--armstrong.cc>
+
+
+NAME
+----
+cdist-type__config_file - manages config files
+
+
+DESCRIPTION
+-----------
+Deploy config files using the file type.
+Run the given code if the files changes.
+
+
+REQUIRED PARAMETERS
+-------------------
+None.
+
+
+OPTIONAL PARAMETERS
+-------------------
+group::
+   see cdist-type__file
+mode::
+   see cdist-type__file
+onchange::
+   the code to run if the file changes
+owner::
+   see cdist-type__file
+source::
+   Path to the config file.
+   If source is '-' (dash), take what was written to stdin as the config file content.
+state::
+   see cdist-type__file
+
+
+EXAMPLES
+--------
+
+--------------------------------------------------------------------------------
+__config_file /etc/consul/conf.d/watch_foo.json \
+   --owner root --group consul --mode 640 \
+   --source "$__type/files/watch_foo.json" \
+   --state present \
+   --onchange 'service consul status >/dev/null && service consul reload || true'
+--------------------------------------------------------------------------------
+
+
+SEE ALSO
+--------
+- cdist-type(7)
+- cdist-type__file(7)
+
+
+COPYING
+-------
+Copyright \(C) 2015 Steven Armstrong. 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/__config_file/manifest b/cdist/conf/type/__config_file/manifest
new file mode 100755
index 00000000..29add8b7
--- /dev/null
+++ b/cdist/conf/type/__config_file/manifest
@@ -0,0 +1,41 @@
+#!/bin/sh
+#
+# 2015 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 <http://www.gnu.org/licenses/>.
+#
+
+set -- "/${__object_id}"
+for param in $(ls "$__object/parameter/"); do
+   case "$param" in
+      source)
+         source="$(cat "$__object/parameter/source")"
+         if [ "$source" = "-" ]; then
+            source="$__object/stdin"
+         fi
+         set -- "$@" --source "$source"
+      ;;
+      owner|group|mode|state)
+         set -- "$@" "--${param}" "$(cat "$__object/parameter/$param")"
+      ;;
+      *)
+         # ignore unknown parameters
+         :
+      ;;
+   esac
+done
+
+__file "$@"
diff --git a/cdist/conf/type/__config_file/parameter/default/state b/cdist/conf/type/__config_file/parameter/default/state
new file mode 100644
index 00000000..e7f6134f
--- /dev/null
+++ b/cdist/conf/type/__config_file/parameter/default/state
@@ -0,0 +1 @@
+present
diff --git a/cdist/conf/type/__config_file/parameter/optional b/cdist/conf/type/__config_file/parameter/optional
new file mode 100644
index 00000000..085c7725
--- /dev/null
+++ b/cdist/conf/type/__config_file/parameter/optional
@@ -0,0 +1,6 @@
+group
+mode
+onchange
+owner
+source
+state
diff --git a/docs/changelog b/docs/changelog
index 761eb3ce..62095c27 100644
--- a/docs/changelog
+++ b/docs/changelog
@@ -4,6 +4,8 @@ Changelog
 	* Changes are always commented with their author in (braces)
 	* Exception: No braces means author == Nico Schottelius
 
+next:
+	* New type __config_file: Manage configuration files and run code on change
 
 3.1.11:
 	* Type __start_on_boot: Support Ubuntu upstart