diff options
author | pennae <pennae.git@eno.space> | 2023-09-22 20:55:05 +0200 |
---|---|---|
committer | pennae <pennae.git@eno.space> | 2023-09-22 21:06:55 +0200 |
commit | 66c6d2c1dfd4b3ef222bb64d3ccef9be915e0895 (patch) | |
tree | 0dde64acbdf9aa61134cdf066723bd731101f767 /openwrt/config_generation.sh | |
download | dewclaw-66c6d2c1dfd4b3ef222bb64d3ccef9be915e0895.tar.gz dewclaw-66c6d2c1dfd4b3ef222bb64d3ccef9be915e0895.tar.xz dewclaw-66c6d2c1dfd4b3ef222bb64d3ccef9be915e0895.zip |
initial commit
without warranty of any kind, express or impliend
Diffstat (limited to 'openwrt/config_generation.sh')
-rw-r--r-- | openwrt/config_generation.sh | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/openwrt/config_generation.sh b/openwrt/config_generation.sh new file mode 100644 index 0000000..c8e9bfa --- /dev/null +++ b/openwrt/config_generation.sh @@ -0,0 +1,95 @@ +#!/bin/sh /etc/rc.common + +EXTRA_COMMANDS="apply commit" +START=99 + +_unregister_script() { + /etc/init.d/config_generation disable + rm /etc/init.d/config_generation +} + +_rollback() { + rm -rf /overlay/upper.dead + mv /overlay/upper /overlay/upper.dead + # this should never fail, unless something *else* is also mucking + # with overlayfs state. + if mv -T /overlay/upper.prev /overlay/upper; then + rm -rf /overlay/upper.dead + else + echo "rollback failed, check /overlay/upper.dead and recover!" >&2 + exit 1 + fi +} + +apply() { + if ! rm -rf /overlay/upper.prev/ \ + || ! cp -al /overlay/upper/ /overlay/upper.prev/ \ + || ! rm -rf /overlay/upper.prev/etc/ \ + || ! cp -a /overlay/upper/etc/ /overlay/upper.prev/ + then + echo "failed to snapshot old config" + rm -rf /overlay/upper.prev + exit 1 + fi + + if ! /etc/init.d/config_generation enable + then + echo "failed to schedule rollback" + rm -rf /overlay/upper.prev + exit 1 + fi + + # everything after this point may fail. if it does we'll simply roll back + # immediately and reboot. + + trap 'reboot &' EXIT + + log() { + printf "$LOG_FMT\n" "$*" + } + + if ! ( + set -e + + @deploy_steps@ + ) + then + _rollback + fi +} + +commit() { + if ! [ -e /overlay/upper.prev ]; then + exit 1 + fi + touch /tmp/.abort-rollback +} + +start() { + [ -d /overlay/upper.prev ] || { + _unregister_script + exit 0 + } + + local needs_rollback=true + local timeout=@rollback_timeout@ + + while [ $timeout -gt 0 ]; do + timeout=$(( timeout - 1 )) + [ -e /tmp/.abort-rollback ] && { + needs_rollback=false + rm /tmp/.abort-rollback + break + } + sleep 1 + done + + if $needs_rollback; then + _rollback + _unregister_script + reboot + else + rm -rf /overlay/upper.prev + _unregister_script + fi +} |