Update tools.func
This commit is contained in:
parent
d24b15281f
commit
150522061f
@ -651,21 +651,42 @@ fetch_and_deploy_gh_release() {
|
|||||||
setup_local_ip_helper() {
|
setup_local_ip_helper() {
|
||||||
local BASE_DIR="/usr/local/community-scripts/ip-management"
|
local BASE_DIR="/usr/local/community-scripts/ip-management"
|
||||||
local SCRIPT_PATH="$BASE_DIR/update_local_ip.sh"
|
local SCRIPT_PATH="$BASE_DIR/update_local_ip.sh"
|
||||||
local SERVICE_PATH="/etc/systemd/system/update-local-ip.service"
|
local IP_FILE="/run/local-ip.env"
|
||||||
local TIMER_PATH="/etc/systemd/system/update-local-ip.timer"
|
local DISPATCHER_SCRIPT="/etc/networkd-dispatcher/routable.d/10-update-local-ip.sh"
|
||||||
|
|
||||||
mkdir -p "$BASE_DIR"
|
mkdir -p "$BASE_DIR"
|
||||||
|
|
||||||
# Create update script
|
# Install networkd-dispatcher if not present
|
||||||
|
if ! dpkg -s networkd-dispatcher >/dev/null 2>&1; then
|
||||||
|
apt-get update -qq
|
||||||
|
apt-get install -yq networkd-dispatcher
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Write update_local_ip.sh
|
||||||
cat <<'EOF' >"$SCRIPT_PATH"
|
cat <<'EOF' >"$SCRIPT_PATH"
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -euo pipefail
|
||||||
|
|
||||||
IP_FILE="/run/local-ip.env"
|
IP_FILE="/run/local-ip.env"
|
||||||
mkdir -p "$(dirname "$IP_FILE")"
|
mkdir -p "$(dirname "$IP_FILE")"
|
||||||
|
|
||||||
get_current_ip() {
|
get_current_ip() {
|
||||||
ip route get 1 | awk '{print $7; exit}' 2>/dev/null
|
local targets=("8.8.8.8" "1.1.1.1" "192.168.1.1" "10.0.0.1" "172.16.0.1" "default")
|
||||||
|
local ip
|
||||||
|
|
||||||
|
for target in "${targets[@]}"; do
|
||||||
|
if [[ "$target" == "default" ]]; then
|
||||||
|
ip=$(ip route get 1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')
|
||||||
|
else
|
||||||
|
ip=$(ip route get "$target" 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')
|
||||||
|
fi
|
||||||
|
if [[ -n "$ip" ]]; then
|
||||||
|
echo "$ip"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
current_ip="$(get_current_ip)"
|
current_ip="$(get_current_ip)"
|
||||||
@ -677,9 +698,7 @@ fi
|
|||||||
|
|
||||||
if [[ -f "$IP_FILE" ]]; then
|
if [[ -f "$IP_FILE" ]]; then
|
||||||
source "$IP_FILE"
|
source "$IP_FILE"
|
||||||
if [[ "$LOCAL_IP" == "$current_ip" ]]; then
|
[[ "$LOCAL_IP" == "$current_ip" ]] && exit 0
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "LOCAL_IP=$current_ip" > "$IP_FILE"
|
echo "LOCAL_IP=$current_ip" > "$IP_FILE"
|
||||||
@ -688,36 +707,17 @@ EOF
|
|||||||
|
|
||||||
chmod +x "$SCRIPT_PATH"
|
chmod +x "$SCRIPT_PATH"
|
||||||
|
|
||||||
# Create systemd service
|
# Install dispatcher hook
|
||||||
cat <<EOF >"$SERVICE_PATH"
|
mkdir -p "$(dirname "$DISPATCHER_SCRIPT")"
|
||||||
[Unit]
|
cat <<EOF >"$DISPATCHER_SCRIPT"
|
||||||
Description=Update LOCAL_IP file
|
#!/bin/bash
|
||||||
After=network-online.target
|
$SCRIPT_PATH
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=oneshot
|
|
||||||
ExecStart=$SCRIPT_PATH
|
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Create systemd timer
|
chmod +x "$DISPATCHER_SCRIPT"
|
||||||
cat <<EOF >"$TIMER_PATH"
|
systemctl enable --now networkd-dispatcher.service
|
||||||
[Unit]
|
|
||||||
Description=Periodic LOCAL_IP update
|
|
||||||
|
|
||||||
[Timer]
|
$STD msg_ok "LOCAL_IP helper installed using networkd-dispatcher"
|
||||||
OnBootSec=15
|
|
||||||
OnUnitActiveSec=60
|
|
||||||
Persistent=true
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=timers.target
|
|
||||||
EOF
|
|
||||||
|
|
||||||
systemctl daemon-reexec
|
|
||||||
systemctl daemon-reload
|
|
||||||
systemctl enable -q --now update-local-ip.timer
|
|
||||||
|
|
||||||
$STD msg_ok "Setup LOCAL_IP helper in $BASE_DIR with systemd integration"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
import_local_ip() {
|
import_local_ip() {
|
||||||
@ -728,7 +728,26 @@ import_local_ip() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z "${LOCAL_IP:-}" ]]; then
|
if [[ -z "${LOCAL_IP:-}" ]]; then
|
||||||
LOCAL_IP="$(ip route get 1 | awk '{print $7; exit}' 2>/dev/null)"
|
get_current_ip() {
|
||||||
|
local targets=("8.8.8.8" "1.1.1.1" "192.168.1.1" "10.0.0.1" "172.16.0.1" "default")
|
||||||
|
local ip
|
||||||
|
|
||||||
|
for target in "${targets[@]}"; do
|
||||||
|
if [[ "$target" == "default" ]]; then
|
||||||
|
ip=$(ip route get 1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')
|
||||||
|
else
|
||||||
|
ip=$(ip route get "$target" 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')
|
||||||
|
fi
|
||||||
|
if [[ -n "$ip" ]]; then
|
||||||
|
echo "$ip"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
LOCAL_IP="$(get_current_ip || true)"
|
||||||
if [[ -z "$LOCAL_IP" ]]; then
|
if [[ -z "$LOCAL_IP" ]]; then
|
||||||
msg_error "Could not determine LOCAL_IP"
|
msg_error "Could not determine LOCAL_IP"
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user