feat: add guarded generic startup apply

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-14 22:02:00 +03:00
1 parent 3743cdbd3e
commit de453ed2c8
11 files changed
+1255 -49

No files matched your search

+37 -15
View File
@@ -28,6 +28,13 @@ if [ -n "${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}"
generic_mode=1
echo 'INTRUSIVE ISOLATED qualification target: do not route working traffic here' >&2
fi
case "${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}" in
0|'') ;;
1) [ "$generic_mode" -eq 1 ] && [ "$qualification_mode" -eq 0 ] && [ -n "${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}" ] || {
echo 'error: resource apply requires generic config and excludes qualification' >&2; exit 64;
} ;;
*) echo 'error: invalid resource apply flag' >&2; exit 64 ;;
esac
case "$dns_mode" in doh|native) ;; *) exit 64 ;; esac
config_dir=/run/zapret-autotune-ss
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
@@ -104,7 +111,7 @@ cleanup_session() {
for session_signal in TERM KILL; do
signal_session "$monitor_sid" "$session_signal" || return 78
session_deadline=$(($(date +%s) + 3))
if [ "$generic_mode" -eq 1 ] && [ "$session_deadline" -gt "$generic_cleanup_deadline" ]; then
if [ "$generic_mode" -eq 1 ] && [ -n "$generic_cleanup_deadline" ] && [ "$session_deadline" -gt "$generic_cleanup_deadline" ]; then
session_deadline=$generic_cleanup_deadline
fi
while :; do
@@ -325,6 +332,23 @@ start_dns() {
pause 1
done
}
launch_controller() {
rm -f "$config_dir/monitor-ready"
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune "$@" 8<&- 9<&- &
monitor_pid=$!
monitor_ack_deadline=$(($(date +%s) + 5))
while [ ! -e "$config_dir/monitor-ready" ]; do
if ! kill -0 "$monitor_pid" 2>/dev/null || [ "$(date +%s)" -ge "$monitor_ack_deadline" ]; then
signal_tree "$monitor_pid" KILL
return 78
fi
done
capture_monitor_session || return 78
if proc_identity "$monitor_pid" && [ "$proc_sid" != "$monitor_sid" ]; then
monitor_sid=
return 78
fi
}
run_cycle() {
[ "$qualification_attempted" -eq 0 ] || return 78
start_dns || return $?
@@ -357,6 +381,17 @@ fi
timeout -k 5 45 /usr/local/bin/zapret-autotune managed reconcile || return 78
if [ "$qualification_mode" -eq 0 ]; then
zapret_started=1
if [ "${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}" = 1 ]; then
launch_controller managed resource-apply --resource-config "$ZAPRET_AUTOTUNE_RESOURCE_CONFIG" || return 78
while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do pause 1; done
{ dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75
apply_code=0
wait "$monitor_pid" || apply_code=$?
monitor_pid=
cleanup_session || return 78
[ "$apply_code" -eq 0 ] || return 78
fi
# Idempotent apply may have only verified the already committed config.
timeout -k 5 30 /opt/zapret2/init.d/sysv/zapret2 start || return 78
fi
@@ -377,20 +412,7 @@ if [ "$qualification_mode" -eq 1 ]; then
else
set -- managed monitor
fi
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune "$@" 8<&- 9<&- &
monitor_pid=$!
monitor_ack_deadline=$(($(date +%s) + 5))
while [ ! -e "$config_dir/monitor-ready" ]; do
if ! kill -0 "$monitor_pid" 2>/dev/null || [ "$(date +%s)" -ge "$monitor_ack_deadline" ]; then
signal_tree "$monitor_pid" KILL
return 78
fi
done
capture_monitor_session || return 78
if proc_identity "$monitor_pid" && [ "$proc_sid" != "$monitor_sid" ]; then
monitor_sid=
return 78
fi
launch_controller "$@" || return 78
while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do
pause 1
done