diff --git a/sulu/sulu-builder/files/etc/init.d/sulu b/sulu/sulu-builder/files/etc/init.d/sulu index e1fa47ff0..6c9829a3e 100644 --- a/sulu/sulu-builder/files/etc/init.d/sulu +++ b/sulu/sulu-builder/files/etc/init.d/sulu @@ -7,9 +7,17 @@ USE_PROCD=1 PROG=/etc/sulu/sulu_watcher.sh -start_service() -{ +. /etc/sulu/sulu-lib.sh + +start_service() { procd_open_instance "sulu" procd_set_param command ${PROG} procd_close_instance "sulu" } + +boot() { + # TODO: remove this HACK it changes network.lan into the sulu wan interface + if ! change_network_lan_to_sulu_wan; then + logger -t sulu "sulu preset update did not run (preset missing or invalid)" + fi +} diff --git a/sulu/sulu-builder/files/etc/sulu/sulu-lib.sh b/sulu/sulu-builder/files/etc/sulu/sulu-lib.sh new file mode 100644 index 000000000..716b9c269 --- /dev/null +++ b/sulu/sulu-builder/files/etc/sulu/sulu-lib.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +# format using "shfmt" +# shellcheck shell=dash + +SULU_PRESET="/sulu/presets/sulu-core-preset.json" + +run_jq() { + local cmd="$1" + local temp_file + + temp_file=$(mktemp) || return 1 + + if jq "${cmd}" "${SULU_PRESET}" >"${temp_file}"; then + mv "${temp_file}" "${SULU_PRESET}" || { + rm "${temp_file}" + return 1 + } + return 0 + else + rm -f "${temp_file}" + return 1 + fi +} + +change_network_lan_to_sulu_wan() { + local current_lan current_wan wan lan_proto + if [ ! -f "${SULU_PRESET}" ]; then + return 1 + fi + current_wan=$(jq -r '.["Wan-interface"].matchByInterfaceField.valueToMatch' "${SULU_PRESET}") + current_lan=$(jq -r '.["Lan-interface"].matchByInterfaceField.valueToMatch' "${SULU_PRESET}") + wan=$(uci -q get network.wan) + lan_proto=$(uci -q get network.lan.proto) + + # if current config is default and there is no wan interface and lan proto is dhcp, change sulu-wan to lan + if [ "${current_wan}" = "wan" ] && [ "${current_lan}" = "lan" ] && [ "${wan}" != "interface" ] && [ "${lan_proto}" = "dhcp" ]; then + # replace wan with lan and set lan to "" + run_jq '.["Wan-interface"].matchByInterfaceField.valueToMatch = "lan" | .["Lan-interface"].matchByInterfaceField.valueToMatch = ""' || return 1 + fi + # if current sulu wan is lan and there is a wan interface, switch back to default + if [ "${current_wan}" = "lan" ] && [ "${wan}" = "interface" ]; then + # reset wan to "wan" and lan to "lan" + run_jq '.["Wan-interface"].matchByInterfaceField.valueToMatch = "wan" | .["Lan-interface"].matchByInterfaceField.valueToMatch = "lan"' || return 1 + fi +}