I know it’s frowned upon to have some sort of automated updates, but my Linux knowledge is very limited, so I would just install any update regardless. So whether it’s me updating everything every day, or a script, the result is the same. I’ve also never had issues.
That is, except for the fact that if openHAB is upgraded, “CoAP Transport” is uninstalled (or not installed again in the new version - that’s maybe more accurate). My openHAB system needs “CoAP Transport” for the Shelly binding (since I use the jar file, because it’s more up-to-date than the one in the marketplace). Because I run my automated updates during the night, I discover the non-working Shelly binding in the morning, which means some of my lights don’t turn on as expected.
So I created a rule that automatically installs “CoAP Transport” if it’s not installed. It runs after startup has been completed (“start level 100”). This is the rule code:
var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/bin/bash", 'bin/bashscripts/openhabcoap.sh')
// Geen idee of 300 seconden genoeg is...
// Pogingen om "sudo" toe te laten:
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/bin/bash", 'sudo', 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/usr/bin/sudo", 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "sudo", 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/usr/bin/sudo", '/var/lib/openhab/bin/bashscripts/openhabcoap.sh')
// Uiteindelijk heb ik "openhab ALL=(ALL) NOPASSWD: /bin/systemctl restart openhab.service" toegevoegd aan "/etc/sudoers.d/1openhabsudo"
// Locatie: "/var/lib/openhab/bin/bashscripts/"
//console.log("coapinstalled = "+coapinstalled)
This is /var/lib/openhab/bin/bashscripts/openhabcoap.sh
:
#!/bin/bash
UPGRADE=$(openhab-cli console -p habopen "feature:list -i | grep openhab-transport-coap")
TEZOEKENTEKST="openhab-transport-coap"
if [[ "$UPGRADE" =~ "$TEZOEKENTEKST" ]]
then
echo "was al geïnstalleerd"
else
openhab-cli console -p habopen feature:install openhab-transport-coap
echo "net geïnstalleerd"
sudo systemctl restart openhab.service
echo "opnieuw opgestart"
fi
(The echo
parts are useless of course, I plan to replace them by mail
commands.)
I had to do some fiddling with the Linux part of it all, since user openhab
wasn’t allowed to do sudo
stuff. So, as the comment states, I created /etc/sudoers.d/1openhabsudo
and added the following to it:
openhab ALL=(ALL) NOPASSWD: /bin/systemctl restart openhab.service
It now works, but I assume there are tweaks that could be made to improve it… If anyone has any, feedback is welcome!