From d30103e9d2ea55013517ebd25029a5ab7292c6b5 Mon Sep 17 00:00:00 2001
From: Kamila Souckova <kamila@ksp.sk>
Date: Thu, 1 Jun 2017 17:43:43 +0200
Subject: [PATCH] __daemontools*: add/improve; + add man pages

---
 .../type/__daemontools/files/init.d-svscan    | 63 +++++++++++++++++++
 cdist/conf/type/__daemontools/man.rst         | 42 +++++++++++++
 cdist/conf/type/__daemontools/manifest        | 20 ++++++
 .../conf/type/__daemontools/parameter/boolean |  1 +
 .../parameter/default/from-package            |  1 +
 .../parameter/default/install-init-script     |  0
 .../type/__daemontools/parameter/optional     |  1 +
 cdist/conf/type/__daemontools/singleton       |  0
 .../type/__daemontools_service/explorer/svc   |  1 +
 cdist/conf/type/__daemontools_service/man.rst | 63 +++++++++++++++++++
 .../conf/type/__daemontools_service/manifest  |  7 +++
 11 files changed, 199 insertions(+)
 create mode 100644 cdist/conf/type/__daemontools/files/init.d-svscan
 create mode 100644 cdist/conf/type/__daemontools/man.rst
 create mode 100644 cdist/conf/type/__daemontools/manifest
 create mode 100644 cdist/conf/type/__daemontools/parameter/boolean
 create mode 100644 cdist/conf/type/__daemontools/parameter/default/from-package
 create mode 100644 cdist/conf/type/__daemontools/parameter/default/install-init-script
 create mode 100644 cdist/conf/type/__daemontools/parameter/optional
 create mode 100644 cdist/conf/type/__daemontools/singleton
 create mode 100644 cdist/conf/type/__daemontools_service/explorer/svc
 create mode 100644 cdist/conf/type/__daemontools_service/man.rst

diff --git a/cdist/conf/type/__daemontools/files/init.d-svscan b/cdist/conf/type/__daemontools/files/init.d-svscan
new file mode 100644
index 00000000..127dfdb3
--- /dev/null
+++ b/cdist/conf/type/__daemontools/files/init.d-svscan
@@ -0,0 +1,63 @@
+#!/bin/bash
+### BEGIN INIT INFO
+# Provides:          svscan
+# Required-Start:
+# Required-Stop:
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: svscan
+# Description:       djb svscan
+### END INIT INFO
+# from https://gist.githubusercontent.com/pacojp/5766990/raw/2ed009ab19515afc9e58291b636d673c5ca864b3/init.d.svscan
+# written by Adam McKenna <adam@debian.org>
+# edited by Kamila Součková <kamila@ksp.sk>
+
+export PATH=$PATH:/usr/local/bin
+
+l=/var/log/svscan
+
+if [ ! -d $l ]; then
+  mkdir $l
+  chown daemon $l
+fi
+
+case "$1" in
+    start)
+        echo -n "Starting daemontools: "
+        if [ ! `pidof svscan` ]; then
+                echo -n "svscan "
+                env - PATH="$PATH" svscan /service 2>&1 | setuidgid daemon multilog t /var/log/svscan &
+                echo "."
+        else
+                echo "already running."
+        fi
+        ;;
+    stop)
+        echo -n "Stopping daemontools: "
+        if [ `pidof svscan` ]; then
+                echo -n "svscan"
+                while [ `pidof svscan` ]; do
+                     kill `pidof svscan`
+                     echo -n "."
+                done
+        fi
+        echo -n " services"
+        for i in `ls -d /service/*`; do
+                svc -dx $i
+                echo -n "."
+        done
+        echo -n " logging "
+        for i in `ls -d /service/*/log`; do
+                svc -dx $i
+                echo -n "."
+        done
+        echo ""
+        ;;
+    restart|force-reload)
+        $0 stop
+        $0 start
+        ;;
+    *)
+        echo 'Usage: /etc/init.d/svscan {start|stop|restart|force-reload}'
+        exit 1
+esac
diff --git a/cdist/conf/type/__daemontools/man.rst b/cdist/conf/type/__daemontools/man.rst
new file mode 100644
index 00000000..62dc5681
--- /dev/null
+++ b/cdist/conf/type/__daemontools/man.rst
@@ -0,0 +1,42 @@
+cdist-type__daemontools(7)
+==========================
+
+NAME
+----
+cdist-type__daemontools - Install daemontools
+
+
+DESCRIPTION
+-----------
+Install djb daemontools and (optionally) an init script.
+
+
+REQUIRED PARAMETERS
+-------------------
+None.
+
+
+OPTIONAL PARAMETERS
+-------------------
+from-package
+   Package to install. Must be compatible with the original daemontools. Example: daemontools-encore. Default: daemontools.
+
+BOOLEAN PARAMETERS
+------------------
+install-init-script
+   Add an init script and set it to start on boot. Default yes.
+
+EXAMPLES
+--------
+
+.. code-block:: sh
+
+    __daemontools --from-package daemontools-encore  # if you prefer
+
+SEE ALSO
+--------
+cdist-type__daemontools_service
+
+AUTHORS
+-------
+Kamila Součková <kamila--@--ksp.sk>
diff --git a/cdist/conf/type/__daemontools/manifest b/cdist/conf/type/__daemontools/manifest
new file mode 100644
index 00000000..550994a7
--- /dev/null
+++ b/cdist/conf/type/__daemontools/manifest
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+pkg=$(cat "$__object/parameter/from-package")
+
+__package $pkg
+
+if [ -f "$__object/parameter/install-init-script" ]; then
+	init=$(cat "$__global/explorer/init")
+	case $init in
+		init)
+			__config_file /etc/init.d/svscan --mode 755 --source "$__type/files/init.d-svscan"
+			require="$require __config_file/etc/init.d/svscan" __start_on_boot svscan
+			require="$require __start_on_boot/svscan" __process svscan --start 'service svscan start'
+		;;
+		*)
+			echo "Your init system ($init) is not supported by this type. Submit a patch at github.com/ungleich/cdist!"
+			exit 1
+		;;
+	esac
+fi
diff --git a/cdist/conf/type/__daemontools/parameter/boolean b/cdist/conf/type/__daemontools/parameter/boolean
new file mode 100644
index 00000000..99a1cefd
--- /dev/null
+++ b/cdist/conf/type/__daemontools/parameter/boolean
@@ -0,0 +1 @@
+install-init-script
diff --git a/cdist/conf/type/__daemontools/parameter/default/from-package b/cdist/conf/type/__daemontools/parameter/default/from-package
new file mode 100644
index 00000000..598dd40a
--- /dev/null
+++ b/cdist/conf/type/__daemontools/parameter/default/from-package
@@ -0,0 +1 @@
+daemontools
diff --git a/cdist/conf/type/__daemontools/parameter/default/install-init-script b/cdist/conf/type/__daemontools/parameter/default/install-init-script
new file mode 100644
index 00000000..e69de29b
diff --git a/cdist/conf/type/__daemontools/parameter/optional b/cdist/conf/type/__daemontools/parameter/optional
new file mode 100644
index 00000000..8eca305b
--- /dev/null
+++ b/cdist/conf/type/__daemontools/parameter/optional
@@ -0,0 +1 @@
+from-package
diff --git a/cdist/conf/type/__daemontools/singleton b/cdist/conf/type/__daemontools/singleton
new file mode 100644
index 00000000..e69de29b
diff --git a/cdist/conf/type/__daemontools_service/explorer/svc b/cdist/conf/type/__daemontools_service/explorer/svc
new file mode 100644
index 00000000..4a867485
--- /dev/null
+++ b/cdist/conf/type/__daemontools_service/explorer/svc
@@ -0,0 +1 @@
+command -v svc
diff --git a/cdist/conf/type/__daemontools_service/man.rst b/cdist/conf/type/__daemontools_service/man.rst
new file mode 100644
index 00000000..aa15a826
--- /dev/null
+++ b/cdist/conf/type/__daemontools_service/man.rst
@@ -0,0 +1,63 @@
+cdist-type__daemontools_service(7)
+==================================
+
+NAME
+----
+cdist-type__daemontools_service - Create a daemontools-compatible service dir.
+
+
+DESCRIPTION
+-----------
+Create a directory structure compatible with daemontools-like service management.
+
+Note that svc must be present on the target system.
+
+The object ID will be used as the service name.
+
+REQUIRED PARAMETERS
+-------------------
+None.
+
+OPTIONAL PARAMETERS
+-------------------
+run
+   Command to run. exec-ing and stderr redirection will be added. One of run, run-file must be specified.
+
+   Example: `my-program`
+
+run-file
+   File to save as <servicedir>/run. One of run, run-file must be specified.
+
+   Example:
+.. code-block:: sh
+    #!/bin/sh
+    exec 2>&1
+    exec my_program
+
+
+log-run
+   Command to run for log consumption. Default: `multilog t ./main`
+
+servicedir
+   Directory to install into. Default: `/service`
+
+BOOLEAN PARAMETERS
+------------------
+None.
+
+EXAMPLES
+--------
+
+.. code-block:: sh
+
+    require="__daemontools" __daemontools_service prometheus --run "setuidgid prometheus $GOBIN/prometheus $FLAGS"
+
+
+SEE ALSO
+--------
+cdist-type__daemontools
+
+
+AUTHORS
+-------
+Kamila Součková <kamila--@--ksp.sk>
diff --git a/cdist/conf/type/__daemontools_service/manifest b/cdist/conf/type/__daemontools_service/manifest
index 34f45545..175066af 100644
--- a/cdist/conf/type/__daemontools_service/manifest
+++ b/cdist/conf/type/__daemontools_service/manifest
@@ -10,6 +10,13 @@ run=$(cat "$__object/parameter/run")
 runfile=$(cat "$__object/parameter/run-file")
 logrun=$(cat "$__object/parameter/log-run")
 
+svc=$(cat "$__type/explorer/svc")
+
+if [ -z "$svc" ]; then
+	echo "svc not found! Install daemontools first: see __daemontools"
+	exit 1
+fi
+
 badusage() {
 	echo "__daemontools_service/$__object_id: exactly one of --run, --run-file must be set" >&2
 	exit 1