From 44d92a826a8789060fc08a8b60ccff77f42941f5 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Mon, 22 Sep 2025 20:12:34 +0200 Subject: support generating services at runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- bin/svtree-compile | 3 ++- bin/svtree-generate | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/svtree-run | 6 ++++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100755 bin/svtree-generate (limited to 'bin') 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/