about summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/svtree-compile3
-rwxr-xr-xbin/svtree-generate66
-rwxr-xr-xbin/svtree-run6
3 files changed, 73 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} }