Initial LanUps implementation and native installers
This commit is contained in:
commit
67a043c89b
66 files changed
+2870
No files matched your search
@@ -0,0 +1,8 @@
|
||||
Package: lanups-router
|
||||
Version: 0.1.0-1
|
||||
Architecture: all
|
||||
Maintainer: LanUps Project
|
||||
Section: utils
|
||||
Priority: optional
|
||||
Depends: nut-server, nut-driver-blazer_usb, nut-upsc
|
||||
Description: LAN UPS configuration and optional low-battery policy for OpenWrt
|
||||
@@ -0,0 +1,121 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
mode=read-only
|
||||
case "${1:-}" in
|
||||
'') ;;
|
||||
--enable-fsd) mode=fsd ;;
|
||||
--check) mode=check ;;
|
||||
*) echo 'Usage: sh install.sh [--check|--enable-fsd]' >&2; exit 2 ;;
|
||||
esac
|
||||
[ "$(id -u)" = 0 ] && command -v uci >/dev/null || { echo 'Run as root on OpenWrt/LEDE' >&2; exit 1; }
|
||||
base=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
|
||||
lan=$(uci -q get network.lan.ipaddr | cut -d/ -f1)
|
||||
case "$lan" in ''|*[!0-9.]*) echo 'A static LAN IPv4 address is required' >&2; exit 1;; esac
|
||||
|
||||
usb=''
|
||||
for device in /sys/bus/usb/devices/*; do
|
||||
if [ -r "$device/idVendor" ] && [ "$(cat "$device/idVendor"):$(cat "$device/idProduct")" = 0665:5161 ]; then
|
||||
usb=$(sed -n 's/^DEVNAME=//p' "$device/uevent")
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$usb" ] || { echo 'Connect the H1000M USB device (0665:5161) before installing' >&2; exit 1; }
|
||||
if [ "$mode" = check ]; then
|
||||
echo "LAN $lan; UPS USB 0665:5161 ($usb)"
|
||||
if command -v upsc >/dev/null; then upsc h1000m@127.0.0.1 ups.status || echo 'Current UPS data is unavailable'; fi
|
||||
exit 0
|
||||
fi
|
||||
[ -z "$(uci -q changes nut_server || :)" ] || { echo 'Commit or discard pending nut_server edits before installing' >&2; exit 1; }
|
||||
|
||||
if [ "${LANUPS_SKIP_PACKAGES:-0}" = 1 ]; then
|
||||
command -v upsc >/dev/null && [ -x /etc/init.d/nut-server ] || { echo 'NUT package dependencies are not ready' >&2; exit 1; }
|
||||
elif command -v opkg >/dev/null; then
|
||||
missing=''
|
||||
packages='nut-server nut-driver-blazer_usb nut-upsc'
|
||||
if [ "$mode" = fsd ] && ! command -v timeout >/dev/null; then packages="$packages coreutils-timeout"; fi
|
||||
for package in $packages; do
|
||||
if ! opkg list-installed "$package" | grep -q "^$package - "; then missing="$missing $package"; fi
|
||||
done
|
||||
if [ -n "$missing" ]; then
|
||||
opkg update || echo 'Some feeds did not refresh; trying available indexes' >&2
|
||||
# Package names are constants defined above.
|
||||
opkg install $missing
|
||||
fi
|
||||
elif command -v apk >/dev/null; then
|
||||
apk add nut-server nut-driver-blazer_usb nut-upsc
|
||||
if [ "$mode" = fsd ] && ! command -v timeout >/dev/null; then apk add coreutils-timeout; fi
|
||||
else
|
||||
echo 'No supported OpenWrt package manager found' >&2; exit 1
|
||||
fi
|
||||
|
||||
umask 077
|
||||
mkdir -p /etc/lanups
|
||||
backup=$(mktemp /etc/lanups/nut-server.XXXXXX)
|
||||
cp /etc/config/nut_server "$backup"
|
||||
changed=1
|
||||
if [ "$(uci -q get nut_server.h1000m.driver || :)" = blazer_usb ] &&
|
||||
[ "$(uci -q get nut_server.h1000m.port || :)" = auto ] &&
|
||||
[ "$(uci -q get nut_server.h1000m.vendorid || :)" = 0665 ] &&
|
||||
[ "$(uci -q get nut_server.h1000m.productid || :)" = 5161 ] &&
|
||||
uci show nut_server | grep -Fq ".address='$lan'" &&
|
||||
uci show nut_server | grep -Fq ".address='127.0.0.1'"; then
|
||||
changed=0
|
||||
fi
|
||||
|
||||
if [ "$changed" = 1 ]; then
|
||||
uci set nut_server.h1000m=driver
|
||||
uci set nut_server.h1000m.driver=blazer_usb
|
||||
uci set nut_server.h1000m.port=auto
|
||||
uci set nut_server.h1000m.vendorid=0665
|
||||
uci set nut_server.h1000m.productid=5161
|
||||
for entry in "lanups_local:127.0.0.1" "lanups_lan:$lan"; do
|
||||
section=${entry%%:*}; ip=${entry#*:}
|
||||
if ! uci show nut_server | grep -Fq ".address='$ip'"; then
|
||||
uci set "nut_server.$section=listen_address"
|
||||
uci set "nut_server.$section.address=$ip"
|
||||
uci set "nut_server.$section.port=3493"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
if [ "$mode" = fsd ]; then
|
||||
command -v timeout >/dev/null && command -v nc >/dev/null || { echo 'FSD mode requires timeout and nc' >&2; uci revert nut_server; exit 1; }
|
||||
password=$(uci -q get nut_server.lanups_policy.password || :)
|
||||
if [ -z "$password" ]; then password=$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n'); fi
|
||||
case "$password" in ''|*[!A-Fa-f0-9]*) echo 'Invalid FSD service credential' >&2; uci revert nut_server; exit 1;; esac
|
||||
uci set nut_server.lanups_policy=user
|
||||
uci set nut_server.lanups_policy.username=lanups_policy
|
||||
uci set "nut_server.lanups_policy.password=$password"
|
||||
uci set nut_server.lanups_policy.actions=FSD
|
||||
changed=1
|
||||
fi
|
||||
|
||||
chown nut:nut "/dev/$usb"
|
||||
chmod 0660 "/dev/$usb"
|
||||
if [ "$changed" = 1 ]; then uci commit nut_server; fi
|
||||
if [ "$mode" = fsd ]; then chmod 0600 /etc/config/nut_server; fi
|
||||
if [ "$changed" = 1 ] || ! /etc/init.d/nut-server running || ! upsc h1000m@127.0.0.1 ups.status >/dev/null 2>&1; then
|
||||
/etc/init.d/nut-server restart
|
||||
fi
|
||||
state=''
|
||||
for attempt in 1 2 3 4 5 6 7 8 9 10; do
|
||||
state=$(upsc h1000m@127.0.0.1 ups.status 2>/dev/null) && break
|
||||
sleep 1
|
||||
done
|
||||
if [ -z "$state" ]; then
|
||||
if [ "$changed" = 1 ]; then cp "$backup" /etc/config/nut_server; /etc/init.d/nut-server restart || :; fi
|
||||
echo "NUT could not read H1000M; backup: $backup" >&2
|
||||
exit 1
|
||||
fi
|
||||
/etc/init.d/nut-server enable
|
||||
if [ "$mode" = fsd ]; then
|
||||
if [ "$base" != /usr/lib/lanups-router ]; then
|
||||
cp "$base/lanups-policy" /usr/sbin/lanups-policy
|
||||
cp "$base/lanups-fsd.init" /etc/init.d/lanups-fsd
|
||||
fi
|
||||
chmod 0755 /usr/sbin/lanups-policy /etc/init.d/lanups-fsd
|
||||
/etc/init.d/lanups-fsd enable
|
||||
/etc/init.d/lanups-fsd restart
|
||||
fi
|
||||
echo "NUT ready: h1000m@$lan:3493 ($state); installer mode: $mode; backup: $backup"
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh /etc/rc.common
|
||||
START=83
|
||||
STOP=27
|
||||
USE_PROCD=1
|
||||
|
||||
start_service() {
|
||||
procd_open_instance
|
||||
procd_set_param command /usr/sbin/lanups-policy
|
||||
procd_set_param respawn
|
||||
procd_close_instance
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
# Sourced by OpenWrt hotplug. A separate process owns the retry lock.
|
||||
case "${ACTION:-}:${PRODUCT:-}" in
|
||||
add:665/5161/*|add:0665/5161/*)
|
||||
if [ -f /etc/lanups/pending ] && [ -n "${DEVNAME:-}" ]; then
|
||||
(
|
||||
lock=/var/run/lanups-router-setup.lock
|
||||
mkdir "$lock" 2>/dev/null || exit 0
|
||||
trap 'rmdir "$lock"' EXIT
|
||||
sleep 2
|
||||
LANUPS_SKIP_PACKAGES=1 /usr/sbin/lanups-router 2>&1 | logger -t lanups-router
|
||||
) </dev/null >/dev/null 2>&1 &
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
online_since=0
|
||||
while :; do
|
||||
if state=$(upsc h1000m@127.0.0.1 ups.status 2>/dev/null); then
|
||||
case " $state " in
|
||||
*' FSD '*)
|
||||
case " $state " in
|
||||
*' OL '*)
|
||||
read -r uptime _ </proc/uptime
|
||||
now=${uptime%%.*}
|
||||
[ "$online_since" -ne 0 ] || online_since=$now
|
||||
if [ "$((now-online_since))" -ge 120 ]; then
|
||||
# upsd latches FSD. Restart it only after a stable return to mains.
|
||||
/etc/init.d/nut-server restart
|
||||
online_since=0
|
||||
fi
|
||||
;;
|
||||
*) online_since=0 ;;
|
||||
esac
|
||||
;;
|
||||
*)
|
||||
online_since=0
|
||||
case " $state " in
|
||||
*' OB '*) case " $state " in
|
||||
*' LB '*)
|
||||
password=$(uci -q get nut_server.lanups_policy.password)
|
||||
if { printf 'USERNAME lanups_policy\nPASSWORD %s\nFSD h1000m\n' "$password"; sleep 1; } |
|
||||
timeout 5 nc 127.0.0.1 3493 | grep -q '^OK FSD-SET'; then
|
||||
logger -t lanups-fsd 'Published FSD after on-battery and low-battery status'
|
||||
fi
|
||||
;;
|
||||
esac ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
else
|
||||
online_since=0
|
||||
fi
|
||||
sleep 3
|
||||
done
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
if [ "${1:-}" = --check ]; then exec /bin/sh /usr/lib/lanups-router/install.sh "$@"; fi
|
||||
/bin/sh /usr/lib/lanups-router/install.sh "$@" || exit $?
|
||||
rm -f /etc/lanups/pending
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
[ -z "${IPKG_INSTROOT:-}" ] || exit 0
|
||||
if ! LANUPS_SKIP_PACKAGES=1 /usr/sbin/lanups-router; then
|
||||
mkdir -p /etc/lanups
|
||||
touch /etc/lanups/pending
|
||||
echo 'LanUps package installed; UPS setup will retry automatically when the USB cable is reconnected.' >&2
|
||||
fi
|
||||
exit 0
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
[ -z "${IPKG_INSTROOT:-}" ] || exit 0
|
||||
[ "${1:-remove}" = remove ] || exit 0
|
||||
if [ -x /etc/init.d/lanups-fsd ]; then
|
||||
/etc/init.d/lanups-fsd stop 2>/dev/null || :
|
||||
/etc/init.d/lanups-fsd disable 2>/dev/null || :
|
||||
fi
|
||||
exit 0
|
||||
Reference in new issue
Block a user