From 02183cf8a39fe95d999bd5a3c982f2fb10ed0dba Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 Jun 2022 07:21:57 +0200 Subject: Automatically restoring previous state after reboot by using last-on/last-off config options, thanks to Sakirnth Nagarasa for the use-case and idea. Signed-off-by: Daniel Baumann --- libexec/container/auto | 55 ++++++++++++++++++++++++++++++++++++------------- libexec/container/build | 2 +- libexec/container/stop | 21 ++++++++++++++++--- 3 files changed, 60 insertions(+), 18 deletions(-) (limited to 'libexec/container') diff --git a/libexec/container/auto b/libexec/container/auto index 461e67d..83c5c50 100755 --- a/libexec/container/auto +++ b/libexec/container/auto @@ -109,25 +109,52 @@ case "${FORCE}" in ;; esac -case "${ACTION}" in - stop) - OPTIONS="${OPTIONS} -f" - ;; -esac - for FILE in "${CONFIG}"/*.conf do - if grep -Eqs "^ *cnt.auto=force-true" "${FILE}" + if ! grep -Eqs "^ *cnt.container-server=${HOST}" "${FILE}" then - OPTIONS="${OPTIONS} -f" + continue fi - if grep -Eqs "^ *cnt.auto=(force-true|true)" "${FILE}" && grep -Eqs "^ *cnt.container-server=${HOST}" "${FILE}" - then - CONTAINER="$(basename ${FILE} .conf)" - - cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true - fi + CONTAINER="$(basename ${FILE} .conf)" + CNT_AUTO="$(grep -Es "^ *cnt.auto=" ${FILE} | awk -F= '{ print $2 }')" + + case "${ACTION}" in + start) + case "${CNT_AUTO}" in + force-true) + OPTIONS="${OPTIONS} -f" + + cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true + ;; + + last-on) + if grep -qs start "/var/lib/${SOFTWARE}/state/${CONTAINER}.run" || \ + [ ! -e "/var/lib/${SOFTWARE}/state/${CONTAINER}.run" ] + then + cnt start -n ${CONTAINER} ${OPTIONS} -f || true + fi + ;; + + last-off) + if grep -qs start "/var/lib/${SOFTWARE}/state/${CONTAINER}.run" + then + cnt start -n ${CONTAINER} ${OPTIONS} -f || true + fi + ;; + + true) + cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true + ;; + esac + ;; + + stop) + OPTIONS="${OPTIONS} -f --stateless" + + cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true + ;; + esac done # Post hooks diff --git a/libexec/container/build b/libexec/container/build index 48f2d4b..2c29730 100755 --- a/libexec/container/build +++ b/libexec/container/build @@ -123,7 +123,7 @@ Parameters () Usage () { - echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--cnt.auto=true|false|force-true] [--cnt.container-server=true|false|FQDN] [--cnt.overlay=DIRECTORY_LOWER:DIRECTORY_UPPER:DIRECTORY_WORK:DIRECTORY_MERGED] [--cnt.overlay-options=OPTION[,OPTION]] [--cnt.start=OPTION[,OPTION]] [-b|--bind DIRECTORY:DIRECTORY[:OPTIONS]] [--bind-ro DIRECTORY:DIRECTORY[:OPTIONS]] [-c|--capability CAPABILITY[,CAPABILITY]] [-d|--drop-capability DROP_CAPABILITY[,DROP_CAPABILITY]] [-s|--script SCRIPT] [-v|--verbose] [-- SCRIPT_OPTIONS]" >&2 + echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--cnt.auto=true|false|force-true|last-on|last-off] [--cnt.container-server=true|false|FQDN] [--cnt.overlay=DIRECTORY_LOWER:DIRECTORY_UPPER:DIRECTORY_WORK:DIRECTORY_MERGED] [--cnt.overlay-options=OPTION[,OPTION]] [--cnt.start=OPTION[,OPTION]] [-b|--bind DIRECTORY:DIRECTORY[:OPTIONS]] [--bind-ro DIRECTORY:DIRECTORY[:OPTIONS]] [-c|--capability CAPABILITY[,CAPABILITY]] [-d|--drop-capability DROP_CAPABILITY[,DROP_CAPABILITY]] [-s|--script SCRIPT] [-v|--verbose] [-- SCRIPT_OPTIONS]" >&2 echo echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." diff --git a/libexec/container/stop b/libexec/container/stop index 793b323..8ca98ce 100755 --- a/libexec/container/stop +++ b/libexec/container/stop @@ -34,7 +34,7 @@ Parameters () { OPTIONS_ALL="" - GETOPT_LONGOPTIONS="name:,force,interactive,kill,clean,verbose," + GETOPT_LONGOPTIONS="name:,force,interactive,kill,clean,stateless,verbose," GETOPT_OPTIONS="n:,f,i,k,v," PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" @@ -84,6 +84,14 @@ Parameters () OPTONS_ALL="${OPTIONS_ALL} --clean" ;; + --stateless) + # internal option + STATELESS="true" + shift 1 + + OPTIONS_ALL="${OPTIONS_ALL} --stateless" + ;; + -v|--verbose) VERBOSE="true" shift 1 @@ -326,8 +334,15 @@ case "${KILL}" in ;; esac -mkdir -p "/var/lib/${SOFTWARE}/state" -echo "stop" > "/var/lib/${SOFTWARE}/state/${NAME}.run" +case "${STATELESS}" in + true) + ;; + + *) + mkdir -p "/var/lib/${SOFTWARE}/state" + echo "stop" > "/var/lib/${SOFTWARE}/state/${NAME}.run" + ;; +esac case "${VERBOSE}" in true) -- cgit v1.2.3