update 2024-12-06 16:27:41

This commit is contained in:
kenzok8 2024-12-06 16:27:41 +08:00
parent 0f6db5c297
commit f02e763e9f
7 changed files with 266 additions and 6 deletions

46
floatip/Makefile Normal file
View File

@ -0,0 +1,46 @@
#
# Copyright (C) 2024 jjm2473 <jjm2473@gmail.com>
#
# This is free software, licensed under the MIT License.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=floatip
PKG_VERSION:=1.0.0
PKG_RELEASE:=1
PKG_MAINTAINER:=jjm2473 <jjm2473@gmail.com>
include $(INCLUDE_DIR)/package.mk
define Package/$(PKG_NAME)
SECTION:=net
CATEGORY:=Network
SUBMENU:=IP Addresses and Names
TITLE:=Float IP
PKGARCH:=all
endef
define Package/$(PKG_NAME)/description
Auto setup an IP if some host down
endef
define Package/$(PKG_NAME)/conffiles
/etc/config/floatip
endef
define Build/Configure
endef
define Build/Compile
endef
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/usr/libexec $(1)/etc/init.d $(1)/etc/config $(1)/etc/uci-defaults
$(INSTALL_BIN) ./files/floatip.sh $(1)/usr/libexec/floatip.sh
$(INSTALL_BIN) ./files/floatip.init $(1)/etc/init.d/floatip
$(INSTALL_CONF) ./files/floatip.config $(1)/etc/config/floatip
$(INSTALL_BIN) ./files/floatip.uci-default $(1)/etc/uci-defaults/floatip
endef
$(eval $(call BuildPackage,$(PKG_NAME)))

View File

@ -0,0 +1,12 @@
config floatip 'main'
# 启动时enabled != 1 ,或者原 lan 口配置网段不包括 set_ip清除自身的 set_ip然后退出进程。
option enabled '0'
# fallback 表示后备
option role 'fallback'
# option role 'main'
# 对于 fallback 节点,检查到 check_ip 都不在线超过一定时间例如30秒就设置自身的 set_ip然后检查 check_ip 中任一 IP 在线就清除自身的 set_ip重复上述流程。
# 对于 main 节点,启动后不断检查 set_ip ,直到 set_ip 不在线,就设置自身的 set_ip然后退出进程。
option set_ip '192.168.100.2/24'
# check_ip 仅 fallback 有效,并且检查时只检查跟 set_ip 同一网段的
list check_ip '192.168.100.3'
# list check_ip '192.168.100.4'

54
floatip/files/floatip.init Executable file
View File

@ -0,0 +1,54 @@
#!/bin/sh /etc/rc.common
START=98
USE_PROCD=1
start_service() {
config_load floatip
config_get_bool enabled "main" enabled 0
ifdown floatip
[[ "$enabled" = 1 ]] || return 0
[[ "`uci -q get network.lan.proto`" = "static" ]] || return 0
local set_ip set_prefix
config_get set_ip "main" set_ip
[[ -n "$set_ip" ]] || return 0
eval "$(ipcalc.sh "$set_ip" )";set_prefix=$PREFIX;set_ip=$IP
[[ "$set_ip" = 0.0.0.0 ]] && set_ip=192.168.100.2
[[ "$set_prefix" = 0 ]] && set_prefix=24
local lan_ip="`uci -q get network.lan.ipaddr`"
[[ -n "$lan_ip" ]] || return 0
local lan_net lan_prefix set_net ip
local in_range=0
if echo "$lan_ip" | grep -Fq '/'; then
for ip in $lan_ip; do
eval "$(ipcalc.sh ${ip/\// } )";lan_net=$NETWORK;lan_prefix=$PREFIX
[[ "$set_prefix" -ge "$lan_prefix" ]] || continue
eval "$(ipcalc.sh $set_ip $lan_prefix } )";set_net=$NETWORK
[[ "$set_net" = "$lan_net" ]] && {
in_range=1
break
}
done
else
local netmask="`uci -q get network.lan.netmask`"
eval "$(ipcalc.sh "$lan_ip" "$netmask" )";lan_net=$NETWORK;lan_prefix=$PREFIX
if [[ "$set_prefix" -ge "$lan_prefix" ]]; then
eval "$(ipcalc.sh $set_ip $lan_prefix } )";set_net=$NETWORK
[[ "$set_net" = "$lan_net" ]] && in_range=1
fi
fi
[[ $in_range = 1 ]] || return 0
procd_open_instance
procd_set_param command /usr/libexec/floatip.sh
procd_set_param stderr 1
procd_set_param file /etc/config/floatip
procd_close_instance
}
stop_service() {
ifdown floatip
}
service_triggers() {
procd_add_reload_trigger "network" "floatip"
}

134
floatip/files/floatip.sh Executable file
View File

@ -0,0 +1,134 @@
#!/bin/sh
# random number 0-255
random() {
local num=$(dd if=/dev/urandom bs=1 count=1 2>/dev/null | hexdump -ve '1/1 "%u"')
if [[ -z "$num" ]]; then
num=$(($(grep -om1 '[0-9][0-9]$' /proc/uptime) * 255 / 100))
fi
echo ${num:-1}
}
host_alive() {
ping -4 -c 2 -A -t 1 -W 1 -q "$1" >/dev/null
}
set_up() {
local ipaddr="$1"
echo "set my floatip to $ipaddr" >&2
if ! uci -q get network.floatip.ipaddr | grep -Fwq $ipaddr; then
if [[ "x$(uci -q get network.floatip)" = xinterface ]]; then
uci -q batch <<-EOF >/dev/null
delete network.floatip.ipaddr
add_list network.floatip.ipaddr=$ipaddr
EOF
else
uci -q batch <<-EOF >/dev/null
set network.floatip=interface
set network.floatip.proto=static
add_list network.floatip.ipaddr=$ipaddr
set network.floatip.device=br-lan
set network.floatip.auto=0
EOF
fi
uci commit network
fi
ifup floatip
}
. /lib/functions.sh
fallback_loop() {
local set_ip check_ip set_net set_prefix
config_get set_ip "main" set_ip
config_get check_ip "main" check_ip
eval "$(ipcalc.sh "$set_ip" )";set_net=$NETWORK;set_prefix=$PREFIX;set_ip=$IP
[[ "$set_net" = 0.0.0.0 ]] && set_net=192.168.100.0
[[ "$set_prefix" = 0 ]] && set_prefix=24
[[ "$set_ip" = 0.0.0.0 ]] && set_ip=192.168.100.2
local ipaddr="$set_ip/$set_prefix"
local valid_check_ip cip
for cip in $check_ip; do
eval "$(ipcalc.sh $cip $set_prefix )"
[[ "$NETWORK" = "$set_net" ]] && valid_check_ip="$valid_check_ip $cip"
done
valid_check_ip="$valid_check_ip "
local order_check_ip="$valid_check_ip"
local found_alive consume_time
local dead_counter=0 floatip_up=0
while :; do
found_alive=0
consume_time=0
echo "checking host(s) $order_check_ip alive"
for cip in $order_check_ip; do
if host_alive $cip; then
echo "host $cip alive"
found_alive=1
# reorder to reduce check time
order_check_ip=" ${cip}${valid_check_ip// $cip / }"
break
fi
consume_time=$(($consume_time + 2))
done
if [[ $found_alive = 1 ]]; then
if [[ $floatip_up = 1 ]]; then
echo "set down floatip" >&2
ifdown floatip
floatip_up=0
else
dead_counter=0
fi
[[ $consume_time -lt 10 ]] && sleep $((10 - $consume_time))
continue
fi
if [[ $floatip_up = 1 ]]; then
[[ $consume_time -lt 5 ]] && sleep $((5 - $consume_time))
continue
fi
dead_counter=$(($dead_counter + 1))
if [[ $dead_counter -lt 3 ]]; then
[[ $consume_time -lt 10 ]] && sleep $((10 - $consume_time))
continue
fi
echo "no host alive, set up floatip $ipaddr"
set_up "$ipaddr"
floatip_up=1
sleep 5
done
}
main_loop() {
local set_ip set_net set_prefix
config_get set_ip "main" set_ip
eval "$(ipcalc.sh "$set_ip" )";set_net=$NETWORK;set_prefix=$PREFIX;set_ip=$IP
[[ "$set_net" = 0.0.0.0 ]] && set_net=192.168.100.0
[[ "$set_prefix" = 0 ]] && set_prefix=24
[[ "$set_ip" = 0.0.0.0 ]] && set_ip=192.168.100.2
local ipaddr="$set_ip/$set_prefix"
while :; do
# sleep 2-6s
sleep $(( random / 60 + 2))
echo "checking host $set_ip alive"
if host_alive $set_ip; then
echo "host $set_ip alive"
continue
fi
echo "no host alive, set up floatip $ipaddr"
set_up "$ipaddr"
break
done
}
main() {
local role
config_load floatip
config_get role "main" role
if [[ "$role" = "main" ]]; then
main_loop
elif [[ "$role" = "fallback" ]]; then
fallback_loop
fi
}
main

View File

@ -0,0 +1,14 @@
uci -q batch <<-EOF >/dev/null
delete ucitrack.@floatip[-1]
add ucitrack floatip
set ucitrack.@floatip[-1].init=floatip
commit ucitrack
EOF
[[ "`uci -q get network.lan.proto`" = "static" && -n "`uci -q get network.lan.gateway`" ]] || exit 0
uci -q batch <<-EOF >/dev/null
set floatip.main.role=main
commit floatip
EOF

View File

@ -11,13 +11,13 @@ LUCI_DEPENDS:=+curl +opkg +luci-base +tar +libuci-lua +mount-utils +luci-lib-tas
LUCI_EXTRA_DEPENDS:=luci-lib-taskd (>=1.0.19)
LUCI_PKGARCH:=all
PKG_VERSION:=0.1.27-1
PKG_VERSION:=0.1.26-4
# PKG_RELEASE MUST be empty for luci.mk
PKG_RELEASE:=
ISTORE_UI_VERSION:=0.1.27
ISTORE_UI_RELEASE:=1
PKG_HASH:=e1cb70d586d880c18ef2d59848fcf6c60d3f827bfa662ea62de1fa0cbd385d39
ISTORE_UI_VERSION:=0.1.26
ISTORE_UI_RELEASE:=2
PKG_HASH:=01eb10649514c4ea0a86e848db1129b642f0ad5b5d371021821c2488454463fc
PKG_SOURCE_URL_FILE:=v$(ISTORE_UI_VERSION)-$(ISTORE_UI_RELEASE).tar.gz
PKG_SOURCE:=istore-ui-$(PKG_SOURCE_URL_FILE)

View File

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=taskd
PKG_VERSION:=1.0.3
PKG_RELEASE:=1
PKG_RELEASE:=2
PKG_MAINTAINER:=jjm2473 <jjm2473@gmail.com>
include $(INCLUDE_DIR)/package.mk
@ -18,7 +18,7 @@ define Package/$(PKG_NAME)
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Simple Task Manager
DEPENDS:=+procd +script-utils +coreutils-stty
DEPENDS:=+procd +script-utils +coreutils +coreutils-stty
PKGARCH:=all
endef