bundles/sshmon: add check_forgejo_for_new_release
This commit is contained in:
parent
120dfea24f
commit
c5e45cbafc
1 changed files with 65 additions and 0 deletions
65
bundles/sshmon/files/check_forgejo_for_new_release
Normal file
65
bundles/sshmon/files/check_forgejo_for_new_release
Normal file
|
@ -0,0 +1,65 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
from sys import argv, exit
|
||||
|
||||
from requests import get
|
||||
|
||||
try:
|
||||
# Arch Linux (possibly future Ubuntu releases)
|
||||
from packaging.version import parse
|
||||
except ModuleNotFoundError:
|
||||
# Debian
|
||||
from setuptools._vendor.packaging.version import parse
|
||||
|
||||
|
||||
assert (
|
||||
len(argv) >= 4
|
||||
), "Usage: check_forgejo_for_new_release <host> <repo> <version> [tag prefix]"
|
||||
|
||||
host = argv[1]
|
||||
repo = argv[2]
|
||||
current_version = argv[3]
|
||||
if len(argv) >= 5:
|
||||
tag_prefix = argv[4]
|
||||
else:
|
||||
tag_prefix = ""
|
||||
|
||||
try:
|
||||
r = get(
|
||||
"https://{}/api/v1/repos/{}/releases".format(host, repo),
|
||||
headers={"Accept": "application/json"},
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
releases = r.json()
|
||||
|
||||
newest_release = None
|
||||
|
||||
for i in releases:
|
||||
if i["tag_name"].startswith(tag_prefix):
|
||||
if (
|
||||
newest_release is None
|
||||
or parse(i["tag_name"]) > parse(newest_release["tag_name"])
|
||||
) and not (i["prerelease"] or i["draft"]):
|
||||
newest_release = i
|
||||
|
||||
assert newest_release is not None, "Could not determine latest release"
|
||||
|
||||
if parse(newest_release["tag_name"]) > parse(current_version):
|
||||
print(
|
||||
"There is a newer version available: {} (currently installed: {})".format(
|
||||
newest_release["tag_name"], current_version
|
||||
)
|
||||
)
|
||||
exit(2)
|
||||
else:
|
||||
print(
|
||||
"Currently installed version {} matches newest release on github".format(
|
||||
current_version
|
||||
)
|
||||
)
|
||||
exit(0)
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
exit(3)
|
Loading…
Reference in a new issue