firmware/src/packages/fff/fff-layer3-config/files/usr/sbin/configure-layer3

118 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
# IMPORTANT!!
# DO NOT RUN THIS IN CRONJOB!
execute_subshell() {
if [ $# -ne 1 ]; then
echo "Usage:" "$0" "<function>"
fi
for script in /etc/layer3.d/*; do
(
# unset function to prevent executing parents shell function
unset -f "$1"
. "$script"
if type "$1" | grep -q "function" ; then
"$1"
fi
)
if [ $? -ne 0 ]; then
echo
echo "Error when executing" "$1" "from" "$(basename "$script")"
exit 1
fi
done
}
configure() {
echo "This script might remove existing vlans, interfaces, addresses, etc."
read -r -p "Do you really want to continue? (y/n) " response
if ! ( [ "$response" == "y" ] || [ "$response" == "Y" ] ); then
exit 1
fi
echo
execute_subshell configure
exit 0
}
reload_services() {
execute_subshell reload
reload_config
}
apply_changes() {
execute_subshell apply
reload_services
exit 0
}
revert_changes() {
execute_subshell revert
exit 0
}
keep_changes() {
if [ -f "/tmp/configure-layer3-pid" ]; then
echo "Keep changes"
kill -9 $(cat /tmp/configure-layer3-pid)
rm /tmp/configure-layer3-pid
# We need exit because trap in test_changes() will not stop
# the script here and revert settings otherwise
exit
else
echo "configure-layer3 -t is not running"
fi
}
test_changes() {
echo $$ > /tmp/configure-layer3-pid
trap keep_changes SIGINT SIGTERM
reload_services
sleep 5
echo "Configuration reloaded. Changes will be reverted in 200s."
echo "Kill this script or use configure-layer3 -k to keep changes."
sleep 200
echo "Reverting changes.."
# on revert we must delete the pid-file
rm /tmp/configure-layer3-pid
revert_changes
reload_services
}
usage() {
echo "Usage: $0 [OPTION]"
echo
echo "Options:"
echo " -c: configure. No commit, no restart!"
echo " -t: test changes. Restarts services, waits up to 200s for SIGINT"
echo " -k: keep changes from test mode"
echo " -a: apply changes"
echo " -r: revert changes"
}
if [ $# != 1 ]; then
usage; exit 1
fi
case "$1" in
-c) configure ;;
-t) test_changes ;;
-k) keep_changes ;;
-a) apply_changes ;;
-r) revert_changes ;;
*) usage; exit 1 ;;
esac