From 1f81e9ac8a4ca4a7047d96067f8644e4f55fff29 Mon Sep 17 00:00:00 2001 From: TheSpad Date: Fri, 10 Jun 2022 16:16:25 +0100 Subject: [PATCH] Add custom service handling --- root/docker-mods | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/root/docker-mods b/root/docker-mods index 732f167..486be1d 100755 --- a/root/docker-mods +++ b/root/docker-mods @@ -131,3 +131,65 @@ done # Set executable bit on cont-init and services that may have been unpacked by mods set_legacy_executable_bits + +# Process Custom Services +SERVICES_DIR_OLD="/config/custom-services.d" +SERVICES_DIR="/custom-services.d" + +if [ -e "${SERVICES_DIR_OLD}" ]; then + chown -R 0:0 "${SERVICES_DIR_OLD}" +fi + +# Remove all existing custom services before continuing to ensure +# we aren't running anything the user may have removed +if [ -n "$(/bin/ls -A /etc/s6-overlay/s6-rc.d/custom-svc-* 2>/dev/null)" ]; then + echo "[custom-init] removing existing custom services..." + rm -rf /etc/s6-overlay/s6-rc.d/custom-svc-* +fi + +if { [ -z "$(/bin/ls -A ${SERVICES_DIR} 2>/dev/null)" ]; } && \ + { [ -z "$(/bin/ls -A ${SERVICES_DIR_OLD} 2>/dev/null)" ]; }; then + echo "[custom-init] no custom files found, exiting..." + exit 0 +fi + +# Make sure custom init directory exists and has files in it +if { [ -e "${SERVICES_DIR}" ] && [ -n "$(/bin/ls -A ${SERVICES_DIR} 2>/dev/null)" ]; }; then + if [ -n "$(/bin/ls -A ${SERVICES_DIR} 2>/dev/null)" ]; then + echo "[custom-init] service files found in ${SERVICES_DIR}" + for SERVICE in ${SERVICES_DIR}/*; do + NAME="$(basename "${SERVICE}")" + if [ -f "${SERVICE}" ]; then + echo "[custom-init] ${NAME}: service detected, copying..." + mkdir -p /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/ + cp "${SERVICE}" /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run + chmod +x /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run + echo "oneshot" > /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/type + echo "/etc/s6-overlay/s6-rc.d/custom-svc-${NAME}/run" > /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/up + echo "[custom-init] ${NAME}: copied" + elif [ ! -f "${SERVICE}" ]; then + echo "[custom-init] ${NAME}: is not a file" + fi + done + fi +fi + +if { [ -e "${SERVICES_DIR_OLD}" ] && [ -n "$(/bin/ls -A ${SERVICES_DIR_OLD} 2>/dev/null)" ]; }; then + if [ -n "$(/bin/ls -A ${SERVICES_DIR_OLD} 2>/dev/null)" ]; then + echo "[custom-init] service files found in ${SERVICES_DIR_OLD}" + for SERVICE in ${SERVICES_DIR_OLD}/*; do + NAME="$(basename "${SERVICE}")" + if [ -f "${SERVICE}" ]; then + echo "[custom-init] ${NAME}: service detected, copying..." + mkdir -p /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/ + cp "${SERVICE}" /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run + chmod +x /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run + echo "oneshot" > /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/type + echo "/etc/s6-overlay/s6-rc.d/custom-svc-${NAME}/run" > /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/up + echo "[custom-init] ${NAME}: copied" + elif [ ! -f "${SERVICE}" ]; then + echo "[custom-init] ${NAME}: is not a file" + fi + done + fi +fi \ No newline at end of file