I wanted to bring together 3 solutions posted into 1 single topic on how I have included into a sitemap the ability to both check for an OH2 update, and then if an update exists to unhide and press a button (err. switch) to upgrade your OH2 installation.
This was done on a Ubuntu 16 Server system, so the process could be slightly different for Habian and others.
The main 3 steps that need to be done is to.
- Add a ‘Sudoers’ file to give your openhab user Sudo access without needing to prompt for any passwords
- Create 2 Shell scripts.
- Create the OH2 Rules, Items, and SiteMap Info.
As a point of reference, these are the 3 main topics that helped me in getting things done. A BIG thanks to these guys in sharing all their hard work.
edit soduers hxxps://community.openhab.org/t/exec-binding-with-sudo/6482/7
Create Scripts hxxps://community.openhab.org/t/stop-start-oh2-from-sitemap-with-rule-script/37104/9
Create Rules hxxps://community.openhab.org/t/checking-for-openhab-updates-simple-rule/36313
Step 1. Create your openhabcontrol Sudoer’s file and provide Sudo access to the scripts you will need. You MUST do this with viSudo to create the file to avoid messing up your system.
sudo visudo -f /etc/sudoers.d/openhabcontrol
And then add this line to the file
openhab ALL = (ALL) NOPASSWD: ALL
Note 1. IMPORTANT. The file will actually be saved as a ‘tmp’ file. Do NOT change this or you may get locked out of sudo. So the file will save as
’/etc/sudoers.d/openhabcontrol.tmp’
Note 2. Using PASSWD: ALL is a higher risk allowing the user openhab full sudo access without a password. But for some reason, I cannot get restricting access to just 2 scripts to work:
Should work, but doesn’t?
openhab ALL = (ALL) NOPASSWD: /etc/openhab2/scripts/oh-screen.sh, /etc/openhab2/scripts/oh-upgrade.sh
Step 2. Create your 2 scripts. The first one is used to launch the second script inside Screen which will then update OH2 via Stop, update, updgrade, and any other commands you think necessary. ie, I prefer to delete my logs prior to a restart to make it easier to review and find things.
2.1 Create the /etc/openhab2/scripts/oh-screen.sh script
sudo nano /etc/openhab2/scripts/oh-screen.sh
And add this code
#!/bin/bash
sudo -u openhab screen -dmS OHScreen /etc/openhab2/scripts/oh-upgrade.sh
2.2 And the actual upgrade script. /etc/openhab2/scripts/oh-upgrade.sh
Note, the use of Sudo in each line may not be necessary here, but I left it since it works as is. Can probably also combine the apt-get lines as well.
#!/bin/bash
sudo systemctl stop openhab2.service
sleep 30s
sudo apt-get -qq update
sleep 30s
sudo apt-get -qq -y install --only-upgrade openhab2
sleep 90s
sudo rm /var/log/openhab2/openhab.*
sudo rm /var/log/openhab2/events.*
sudo systemctl start openhab2.service
#sudo reboot -h now
Step 3. Create your rules. I decided to go with the ‘Rules’ approach vs the Http binding approach, primarily so that I could check for an OH2 update at any time.
I have 2 rules here. The first rule is based off the earlier rules mentioned above, where I only modified to post the actual Build Number as a Number Item vs a String Item to the sitemap. The first rule will also set a dummy switch to On to unhide (make visible) the Upgrade button in the sitemap. And the second rule runs the actual update script.
The hardest part for me was getting the right syntax combination working in the ExecuteCommand line. The below works passing the info to your openhab user to run the script with sudo permssions. But perhaps their is an easier way.
rule "Check for openHAB distro updates"
when
System started or
Time cron "0 0 0 * * ?" or
Item OH_CheckForUpdates received command ON
then
OH_Status.postUpdate("Checking...")
var String cloudbeesUrl =
"https://openhab.ci.cloudbees.com/job/openHAB-Distribution/api/json?tree=lastSuccessfulBuild[number]"
var String currentBuild =
executeCommandLine("/etc/openhab2/scripts/check-oh-build.sh", 2000)
// logInfo("update", "currentBuild: " + currentBuild)
var response = sendHttpGetRequest(cloudbeesUrl)
var lastBuildNumber = transform("JSONPATH", "$.lastSuccessfulBuild.number", response)
// logInfo("update", "lastBuild: " + lastBuildNumber)
OH_CurrentBuild.postUpdate(Integer.parseInt(currentBuild))
OH_LatestBuild.postUpdate(Integer.parseInt(lastBuildNumber))
if (Integer.parseInt(currentBuild) < Integer.parseInt(lastBuildNumber)) {
OH_Status.postUpdate("Update available: #" + lastBuildNumber)
//Make Upgrade Switch Visible on Sitemap
OH_Upgradeable.postUpdate(ON)
// Do something with that information
} else {
OH_Status.postUpdate("No updates")
OH_Upgradeable.postUpdate(OFF)
}
OH_CheckForUpdates.postUpdate(OFF)
end
rule "Upgrade Openhab"
when
Item OH_Upgrade received command ON
then
//Run Upgrade Script in Screen
executeCommandLine("sudo su -c '/etc/openhab2/scripts/oh-screen.sh' -s /bin/bash openhab", 5000)
//Reset to Off
OH_Upgrade.postUpdate(OFF)
end
and then for Items file, I have"
/* Check OH Build */
Switch OH_CheckForUpdates "OH: Check for updates"
String OH_Status "OH: Status [%s]"
Number OH_CurrentBuild "Current Build [%d]"
Number OH_LatestBuild "Latest Build [%d]"
Switch OH_Upgrade "Upgrade Now?"
Switch OH_Upgradeable
and the sitemap
Frame {
Text label="openHAB Update" {
Switch item=OH_CheckForUpdates mappings=[ON="Check now!"]
Text item=OH_Status
Text item=OH_CurrentBuild
Text item=OH_LatestBuild
Switch item=OH_Upgrade mappings=[ON="Run Upgrade"] visibility=[OH_Upgradeable == ON]
}
}