about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xbin/svtree-compile3
-rwxr-xr-xbin/svtree-generate66
-rwxr-xr-xbin/svtree-run6
-rw-r--r--templates/service/+service-log/consumer-for1
-rw-r--r--templates/service/+service-log/notification-fd1
-rw-r--r--templates/service/+service-log/pipeline-name1
-rw-r--r--templates/service/+service-log/run10
-rw-r--r--templates/service/+service-log/type1
-rw-r--r--templates/service/+service-srv/producer-for1
-rwxr-xr-xtemplates/service/+service-srv/run6
-rw-r--r--templates/service/+service-srv/type1
11 files changed, 95 insertions, 2 deletions
diff --git a/bin/svtree-compile b/bin/svtree-compile
index 3e8e63c..aba4f1d 100755
--- a/bin/svtree-compile
+++ b/bin/svtree-compile
@@ -4,6 +4,7 @@ set -eu
 LIVE_DIR=${LIVE_DIR:-/run/s6-rc}
 STATE_DIR=${STATE_DIR:-/etc/s6-rc}
 SOURCE_DIR=${SOURCE_DIR:-/etc/s6-rc/src}
+GENERATED_DIR=${SOURCE_DIR:-/run/svtree-generated}
 
 new_dir="$STATE_DIR"/"_$(date +%s)"
 
@@ -12,7 +13,7 @@ main() {
         old="$(find "$STATE_DIR" -maxdepth 1 -name '_[0-9]*' -print)"
 
         # compile new db
-        s6-rc-compile "$new_dir" "$SOURCE_DIR"
+        s6-rc-compile "$new_dir" "$SOURCE_DIR" "$GENERATED_DIR"
 
         # switch to new db (if there's an active tree)
         test -e "$LIVE_DIR" && s6-rc-update -v2 -l "$LIVE_DIR" -b "$new_dir"
diff --git a/bin/svtree-generate b/bin/svtree-generate
new file mode 100755
index 0000000..f3f323f
--- /dev/null
+++ b/bin/svtree-generate
@@ -0,0 +1,66 @@
+#!/bin/sh
+set -eux
+
+STATE_DIR=${STATE_DIR:-/etc/s6-rc}
+TEMPLATE_DIR=${TEMPLATE_DIR:-${STATE_DIR}/templates}
+GENERATE_DIR=${GENERATE_DIR:-${STATE_DIR}/generate}
+
+dst_dir=${1:-/run/svtree-generated}
+
+doinstall() {
+	local sv_name=${1:?}
+	local dir=${2:?}
+	local dst
+
+	# replace placeholder in destination name
+	dst="$(readlink -e "$dst_dir")"/"$(basename "$dir" | sed "s/+service/${sv_name}/")"
+
+	# create destination
+	install -d $dst
+
+	# copy everything over
+	cp -r ${dir}/* ${dst}/
+
+	# replace placeholders with service name
+	find $dst -type f -exec \
+		sed \
+		-e "s,+statedir,${STATE_DIR},g" \
+		-e "s,+service,${sv_name},g" \
+		-i {} \+
+}
+
+dogenerate() {
+	local sv_name=${1:?}
+	local sv_template=${2:?}
+
+	printf "=> generating $sv_template : $sv_name\n" >&2
+
+	# iterate over files in $TEMPLATE_DIR/<template> and pass them to
+	# doinstall
+	for dir in "${TEMPLATE_DIR}/${sv_template}"/*; do
+		dir=$(readlink -e "$dir")
+		doinstall "$sv_name" "$dir"
+	done
+}
+
+main() {
+	test -e "$GENERATE_DIR" || exit 1
+	test -e "$TEMPLATE_DIR" || exit 1
+
+	# clear previous output (if any)
+	rm -rf "${dst_dir:?}"/* || true
+
+	# create destination directory
+	mkdir -p "$dst_dir"
+
+	# files in $GENERATE_DIR indicate which services we need to generate
+	# and what template to use: <template>.<service>
+	for gen in "$(readlink -e "$GENERATE_DIR")"/*; do
+		gen=$(basename "$gen")
+		sv_template=$(printf %s "$gen" | cut -d. -f1)
+		sv_name=$(printf %s "$gen" | cut -d. -f2)
+		dogenerate "$sv_name" "$sv_template"
+	done
+}
+
+main
diff --git a/bin/svtree-run b/bin/svtree-run
index e8d1422..1f3711b 100755
--- a/bin/svtree-run
+++ b/bin/svtree-run
@@ -8,9 +8,13 @@ define LIVE_DIR "${XDG_RUNTIME_DIR}/s6-rc"
 define SCAN_DIR "${XDG_RUNTIME_DIR}/service"
 define SOURCE_DIR "${STATEDIR}/src"
 define COMPILED_DIR "${STATEDIR}/compiled"
+define GENERATED_DIR "${XDG_RUNTIME_DIR}/svtree-generated"
+
+# generate additional services
+if { /opt/svtree/bin/svtree-generate }
 
 # compile s6-rc db
-foreground { /opt/svtree/bin/svtree-compile }
+if { /opt/svtree/bin/svtree-compile }
 
 # check that we have the db
 if { eltest -e ${COMPILED_DIR} }
diff --git a/templates/service/+service-log/consumer-for b/templates/service/+service-log/consumer-for
new file mode 100644
index 0000000..83c1762
--- /dev/null
+++ b/templates/service/+service-log/consumer-for
@@ -0,0 +1 @@
++service-srv
diff --git a/templates/service/+service-log/notification-fd b/templates/service/+service-log/notification-fd
new file mode 100644
index 0000000..00750ed
--- /dev/null
+++ b/templates/service/+service-log/notification-fd
@@ -0,0 +1 @@
+3
diff --git a/templates/service/+service-log/pipeline-name b/templates/service/+service-log/pipeline-name
new file mode 100644
index 0000000..0db7d83
--- /dev/null
+++ b/templates/service/+service-log/pipeline-name
@@ -0,0 +1 @@
++service
diff --git a/templates/service/+service-log/run b/templates/service/+service-log/run
new file mode 100644
index 0000000..d558f3c
--- /dev/null
+++ b/templates/service/+service-log/run
@@ -0,0 +1,10 @@
+#!/bin/execlineb -P
+
+envfile -I /etc/s6-rc/system.conf
+importas -sCuD "/run/log" s6_log_dir s6_log_dir
+
+envfile -I /etc/s6-rc/+service.conf
+importas -sCuD "n3 s2000000 T" s6_log_directives s6_log_directives
+
+foreground { install -d ${s6_log_dir}/+service }
+exec -c s6-log -d3 -b -- ${s6_log_directives} ${s6_log_dir}/+service
diff --git a/templates/service/+service-log/type b/templates/service/+service-log/type
new file mode 100644
index 0000000..5883cff
--- /dev/null
+++ b/templates/service/+service-log/type
@@ -0,0 +1 @@
+longrun
diff --git a/templates/service/+service-srv/producer-for b/templates/service/+service-srv/producer-for
new file mode 100644
index 0000000..cba6eea
--- /dev/null
+++ b/templates/service/+service-srv/producer-for
@@ -0,0 +1 @@
++service-log
diff --git a/templates/service/+service-srv/run b/templates/service/+service-srv/run
new file mode 100755
index 0000000..b0fe4f2
--- /dev/null
+++ b/templates/service/+service-srv/run
@@ -0,0 +1,6 @@
+#!/bin/execlineb -P
+
+# route all logs to stdout
+fdmove -c 2 1
+
+exec +statedir/services/+service.run
diff --git a/templates/service/+service-srv/type b/templates/service/+service-srv/type
new file mode 100644
index 0000000..5883cff
--- /dev/null
+++ b/templates/service/+service-srv/type
@@ -0,0 +1 @@
+longrun