2019-04-23 18:09:06 +02:00
|
|
|
#!/bin/sh
|
2021-02-10 01:53:21 +01:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2019-04-23 18:09:06 +02:00
|
|
|
|
|
|
|
# IMPORTANT!!
|
|
|
|
# DO NOT RUN THIS IN CRONJOB!
|
|
|
|
|
|
|
|
execute_subshell() {
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
echo "Usage:" "$0" "<function>"
|
|
|
|
fi
|
|
|
|
|
2020-12-14 14:42:13 +01:00
|
|
|
for script in /etc/layer3.d/*; do
|
2019-04-23 18:09:06 +02:00
|
|
|
(
|
|
|
|
# unset function to prevent executing parents shell function
|
|
|
|
unset -f "$1"
|
|
|
|
. "$script"
|
|
|
|
|
2019-05-25 14:11:10 +02:00
|
|
|
if type "$1" | grep -q "function" ; then
|
2019-04-23 18:09:06 +02:00
|
|
|
"$1"
|
|
|
|
fi
|
|
|
|
)
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
2019-08-14 01:00:44 +02:00
|
|
|
echo
|
2019-04-23 18:09:06 +02:00
|
|
|
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
|
|
|
|
|
2019-08-14 01:00:44 +02:00
|
|
|
echo
|
2019-04-23 18:09:06 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 09:42:11 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:09:06 +02:00
|
|
|
test_changes() {
|
2020-12-13 09:42:11 +01:00
|
|
|
echo $$ > /tmp/configure-layer3-pid
|
|
|
|
trap keep_changes SIGINT SIGTERM
|
|
|
|
|
2019-04-23 18:09:06 +02:00
|
|
|
reload_services
|
|
|
|
|
|
|
|
sleep 5
|
2020-12-13 09:42:11 +01:00
|
|
|
|
2019-04-23 18:09:06 +02:00
|
|
|
echo "Configuration reloaded. Changes will be reverted in 200s."
|
2020-12-13 09:42:11 +01:00
|
|
|
echo "Kill this script or use configure-layer3 -k to keep changes."
|
|
|
|
|
2019-04-23 18:09:06 +02:00
|
|
|
sleep 200
|
|
|
|
|
2020-12-13 09:42:11 +01:00
|
|
|
echo "Reverting changes.."
|
|
|
|
# on revert we must delete the pid-file
|
|
|
|
rm /tmp/configure-layer3-pid
|
2019-04-23 18:09:06 +02:00
|
|
|
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"
|
2020-12-13 09:42:11 +01:00
|
|
|
echo " -k: keep changes from test mode"
|
2019-04-23 18:09:06 +02:00
|
|
|
echo " -a: apply changes"
|
|
|
|
echo " -r: revert changes"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if [ $# != 1 ]; then
|
|
|
|
usage; exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
-c) configure ;;
|
|
|
|
-t) test_changes ;;
|
2020-12-13 09:42:11 +01:00
|
|
|
-k) keep_changes ;;
|
2019-04-23 18:09:06 +02:00
|
|
|
-a) apply_changes ;;
|
|
|
|
-r) revert_changes ;;
|
|
|
|
*) usage; exit 1 ;;
|
|
|
|
esac
|