bundles/homeassistant: rework check_homeassistant_update

This commit is contained in:
Franzi 2024-09-27 08:42:58 +02:00
parent abdc7f751e
commit 4a28bc55c0
Signed by: kunsi
GPG key ID: 12E3D2136B818350
3 changed files with 35 additions and 41 deletions

View file

@ -2,48 +2,42 @@
from sys import exit
import requests
from packaging import version
from requests import get
bearer = "${bearer}"
domain = "${domain}"
OK = 0
WARN = 1
CRITICAL = 2
UNKNOWN = 3
status = 3
message = "Unknown Update Status"
domain = "hass.home.kunbox.net"
s = requests.Session()
s.headers.update({"Content-Type": "application/json"})
API_TOKEN = "${token}"
DOMAIN = "${domain}"
try:
stable_version = version.parse(
s.get("https://version.home-assistant.io/stable.json").json()["homeassistant"][
"generic-x86-64"
]
)
s.headers.update(
{"Authorization": f"Bearer {bearer}", "Content-Type": "application/json"}
)
running_version = version.parse(
s.get(f"https://{domain}/api/config").json()["version"]
)
if running_version == stable_version:
status = 0
message = f"OK - running version {running_version} equals stable version {stable_version}"
elif running_version > stable_version:
status = 1
message = f"WARNING - stable version {stable_version} is lower than running version {running_version}, check if downgrade is necessary."
else:
status = 2
message = f"CRITICAL - update necessary, running version {running_version} is lower than stable version {stable_version}"
r = get("https://version.home-assistant.io/stable.json")
r.raise_for_status()
stable_version = r.json()["homeassistant"]["generic-x86-64"]
except Exception as e:
message = f"{message}: {repr(e)}"
print(f"Could not get stable version information from home-assistant.io: {e!r}")
exit(3)
print(message)
exit(status)
try:
r = get(
f"https://{DOMAIN}/api/config",
headers={"Authorization": f"Bearer {API_TOKEN}", "Content-Type": "application/json"},
)
r.raise_for_status()
running_version = r.json()["version"]
except Exception as e:
print(f"Could not get running version information from homeassistant: {e!r}")
exit(3)
try:
if stable_version > running_version:
print(
f"There is a newer version available: {stable_version} (currently installed: {running_version})"
)
exit(2)
else:
print(
f"Currently running version {running_version} matches newest release on home-assistant.io"
)
exit(0)
except Exception as e:
print(repr(e))
exit(3)

View file

@ -30,7 +30,7 @@ files = {
'/usr/local/share/icinga/plugins/check_homeassistant_update': {
'content_type': 'mako',
'context': {
'bearer': repo.vault.decrypt(node.metadata.get('homeassistant/api_secret')),
'token': node.metadata.get('homeassistant/api_secret'),
'domain': node.metadata.get('homeassistant/domain'),
},
'mode': '0755',