Skip to content

Commit

Permalink
🔨 Tweaks and improvements (#27)
Browse files Browse the repository at this point in the history
* 🔨 Tweaks and improvements

* ✏️ Replace some Hass.io occurances

* ✏️ Typo
  • Loading branch information
frenck committed Mar 1, 2020
1 parent 23f30f2 commit 5715820
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 53 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[![Buy me a coffee][buymeacoffee-shield]][buymeacoffee]

A Network UPS Tools daemon to allow you to easily manage battery backup (UPS)
devices connected to your Hass.io machine.
devices connected to your Home Assistant machine.

## About

Expand Down Expand Up @@ -92,7 +92,7 @@ devices:
port: auto
config: []
mode: netserver
shutdown_hassio: 'false'
shutdown_host: 'false'
```

**Note**: _This is just an example, don't copy and paste it! Create your own!_
Expand Down Expand Up @@ -216,9 +216,9 @@ Recognized values are `netserver` and `netclient`.
- `netclient`: Only runs `upsmon` to connect to a remote system running as
`netserver`.

#### Option: `shutdown_hassio`
#### Option: `shutdown_host`

When this option is set to `true` on a UPS shutdown command, the Hassio system
When this option is set to `true` on a UPS shutdown command, the host system
will be shutdown. When set to `false` only the add-on will be stopped. This is to
allow testing without impact to the system.

Expand Down
2 changes: 1 addition & 1 deletion nut/.README.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Buy me a coffee][buymeacoffee-shield]][buymeacoffee]

A Network UPS Tools daemon to allow you to easily manage battery backup (UPS)
devices connected to your Hass.io machine.
devices connected to your Home Assistant machine.

## About

Expand Down
2 changes: 1 addition & 1 deletion nut/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ LABEL \
org.label-schema.usage="https://github.com/hassio-addons/addon-nut/tree/master/README.md" \
org.label-schema.vcs-ref=${BUILD_REF} \
org.label-schema.vcs-url="https://github.com/hassio-addons/addon-nut" \
org.label-schema.vendor="Community Hass.io Addons"
org.label-schema.vendor="Home Assistant Community Addons"
4 changes: 2 additions & 2 deletions nut/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
],
"mode": "netserver",
"shutdown_hassio": "false"
"shutdown_host": "false"
},
"schema": {
"log_level": "list(trace|debug|info|notice|warning|error|fatal)?",
Expand All @@ -58,7 +58,7 @@
}
],
"mode": "list(netserver|netclient)",
"shutdown_hassio": "bool",
"shutdown_host": "bool",
"list_usb_devices": "bool?",
"remote_ups_name": "str?",
"remote_ups_host": "str?",
Expand Down
46 changes: 19 additions & 27 deletions nut/rootfs/etc/cont-init.d/nut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
# ==============================================================================
readonly USERS_CONF=/etc/nut/upsd.users
readonly UPSD_CONF=/etc/nut/upsd.conf
declare upsmonpwd
declare nutmode
declare password
declare shutdowncmd
declare upsmonpwd
declare username
declare password

# Fix permissions
chmod -R 660 /etc/nut/*
chown -R root:nut /etc/nut/*

nutmode=$(bashio::config 'mode')
bashio::log.info "Setting mode to ${nutmode}..."
Expand All @@ -25,20 +21,18 @@ if bashio::config.true 'list_usb_devices' ;then
fi

if bashio::config.equals 'mode' 'netserver' ;then

bashio::log.info "Generating ${USERS_CONF}..."

# Create Monitor User
upsmonpwd=$(shuf -ze -n20 {A..Z} {a..z} {0..9}|tr -d '\0')

{
echo
echo "[upsmonmaster]"
echo " password = ${upsmonpwd}"
echo " upsmon master"
} >> "${USERS_CONF}"
{
echo
echo "[upsmonmaster]"
echo " password = ${upsmonpwd}"
echo " upsmon master"
} >> "${USERS_CONF}"

for user in $(bashio::config "users|keys"); do

bashio::config.require.username "users[${user}].username"
username=$(bashio::config "users[${user}].username")

Expand Down Expand Up @@ -76,17 +70,17 @@ if bashio::config.equals 'mode' 'netserver' ;then
fi

for device in $(bashio::config "devices|keys"); do

upsname=$(bashio::config "devices[${device}].name")
upsdriver=$(bashio::config "devices[${device}].driver")
upsport=$(bashio::config "devices[${device}].port")

bashio::log.info "Configuring Device named ${upsname}..."
{
echo
echo "[${upsname}]"
echo " driver = ${upsdriver}"
echo " port = ${upsport}"
} >> /etc/nut/ups.conf
{
echo
echo "[${upsname}]"
echo " driver = ${upsdriver}"
echo " port = ${upsport}"
} >> /etc/nut/ups.conf

OIFS=$IFS
IFS=$'\n'
Expand All @@ -97,7 +91,6 @@ if bashio::config.equals 'mode' 'netserver' ;then

echo "MONITOR ${upsname}@localhost 1 upsmonmaster ${upsmonpwd} master" \
>> /etc/nut/upsmon.conf

done

if bashio::config.true "fake_usb_devices"; then
Expand All @@ -122,13 +115,12 @@ if bashio::config.equals 'mode' 'netserver' ;then
else
upsdrvctl start
fi

fi

shutdowncmd="halt"
if bashio::config.true 'shutdown_hassio'; then
bashio::log.warning "UPS Shutdown will shutdown Hassio"
shutdowncmd="/usr/bin/shutdownhassio"
if bashio::config.true 'shutdown_host'; then
bashio::log.warning "UPS Shutdown will shutdown the host"
shutdowncmd="/usr/bin/shutdownhost"
fi

echo "SHUTDOWNCMD ${shutdowncmd}" >> /etc/nut/upsmon.conf
9 changes: 3 additions & 6 deletions nut/rootfs/etc/cont-init.d/nutclient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@
declare -a CONF_ENTRIES=("name" "host" "password" "user")

if bashio::config.equals 'mode' 'netclient' ;then

for entry in "${CONF_ENTRIES[@]}"; do
if ! bashio::config.exists "remote_ups_${entry}";then
bashio::log.fatal \
"Netclient mode specified but no ${entry} is configured"
bashio::exit.nok
fi
bashio::exit.nok \
"Netclient mode specified but no ${entry} is configured"
fi
done

rname=$(bashio::config "remote_ups_name")
rhost=$(bashio::config "remote_ups_host")
ruser=$(bashio::config "remote_ups_user")
rpwd=$(bashio::config "remote_ups_password")

echo "MONITOR ${rname}@${rhost} 1 ${ruser} ${rpwd} slave" \
>> /etc/nut/upsmon.conf
fi
6 changes: 3 additions & 3 deletions nut/rootfs/etc/fix-attrs.d/nut
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dev/bus/usb/*/* false root 0777 0777
/etc/nut/* true root:nut 0660 0660
/usr/bin/notify false root 0755 0755
/usr/bin/shutdownhost false root 0755 0755
/usr/bin/wall false root 0755 0755
/usr/bin/shutdownhassio false root 0755 0755
/var/run/nut false root:nut 0770 0770
/etc/nut/* false root:nut 0660 0660
/dev/bus/usb/*/* false root 0777 0777
8 changes: 5 additions & 3 deletions nut/rootfs/etc/services.d/upsd/run
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Home Assistant Community Add-on: Network UPS Tools
# Run upsd
# ==============================================================================
if bashio::config.equals 'mode' 'netserver' ;then
bashio::log.info "Starting the UPS information server..."
exec upsd -D
if ! bashio::config.equals 'mode' 'netserver' ;then
exec sleep 864000
fi

bashio::log.info "Starting the UPS information server..."
exec upsd -D
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/bin/bash
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Home Assistant Community Add-on: Network UPS Tools
# Script to shutdown Hassio from UPS Shutdowm Command
# Script to shutdown the host from UPS Shutdown Command
# ==============================================================================

curl -sS -X "POST" \
-H "X-Supervisor-Token: ${SUPERVISOR_TOKEN}" \
"http://hassio/host/shutdown"
bashio::host.shutdown

0 comments on commit 5715820

Please sign in to comment.