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,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"
|
||||
Reference in new issue
Block a user