Compare commits
3 commits
master
...
truly-dete
Author | SHA1 | Date | |
---|---|---|---|
caf3169829 | |||
ebe17beb8b | |||
8ba2f944dd |
5 changed files with 66 additions and 29 deletions
1
.envrc
Normal file
1
.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use nix
|
|
@ -1,11 +1,9 @@
|
|||
import json
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
|
||||
import click
|
||||
import click_log
|
||||
import uptime_kuma_api
|
||||
from uptime_kuma_api import UptimeKumaApi
|
||||
|
||||
from .hydratation import hydrate_http_probes
|
||||
|
@ -16,20 +14,6 @@ logger = logging.getLogger(__name__)
|
|||
click_log.basic_config()
|
||||
|
||||
|
||||
def get_api_with_retries(host, retries=5):
|
||||
delay = 5
|
||||
while True:
|
||||
try:
|
||||
return UptimeKumaApi(host)
|
||||
except uptime_kuma_api.exceptions.UptimeKumaException:
|
||||
logger.warn(f"Error while connectiong, retrying in {delay}s.")
|
||||
if retries == 0:
|
||||
raise
|
||||
retries -= 1
|
||||
time.sleep(delay)
|
||||
delay *= 4
|
||||
|
||||
|
||||
@click.group()
|
||||
def cli():
|
||||
pass
|
||||
|
@ -92,8 +76,7 @@ def apply_json(
|
|||
logger.debug(
|
||||
f"Flags value:\n - scrape_http_keywords: {scrape_http_keywords}\n - no_autocreate_tags: {no_autocreate_tags}\n - keywords_fallback: {keywords_fallback}"
|
||||
)
|
||||
|
||||
with get_api_with_retries(host) as api:
|
||||
with UptimeKumaApi(host) as api:
|
||||
logging.debug("Reading json")
|
||||
data = json.load(file)
|
||||
logging.debug("Parsing json")
|
||||
|
|
|
@ -5,6 +5,8 @@ Classes to make the needed operations to reach the specified state.
|
|||
import logging
|
||||
import sys
|
||||
|
||||
from requests.api import delete
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -13,6 +15,7 @@ class Manager:
|
|||
self.api = api
|
||||
self.prune_unused = prune_unused
|
||||
self.target_tree = target_tree
|
||||
self.to_prune = []
|
||||
|
||||
def process(self):
|
||||
self.sync_tags()
|
||||
|
@ -20,6 +23,7 @@ class Manager:
|
|||
self.sync_monitors()
|
||||
self.sync_status_pages()
|
||||
self.sync_settings()
|
||||
self.prune()
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
|
@ -27,25 +31,34 @@ class Manager:
|
|||
for i in v:
|
||||
i.save() # this method should be safe to be called in whatever order
|
||||
|
||||
def prune(self):
|
||||
for e in self.to_prune:
|
||||
e.prune()
|
||||
|
||||
|
||||
def sync_monitors(self):
|
||||
logger.debug(f"Syncing monitors...")
|
||||
old = self.api.get_monitors()
|
||||
new = self.target_tree.get("monitors", [])
|
||||
self.sync(new, old, "name")
|
||||
self.sync(new, old, "name", Monitor)
|
||||
|
||||
def sync_notifications(self):
|
||||
logger.debug(f"Syncing notifications...")
|
||||
old = self.api.get_notifications()
|
||||
new = self.target_tree.get("notifications", [])
|
||||
self.sync(new, old, "name")
|
||||
self.sync(new, old, "name", Notification)
|
||||
|
||||
def sync_tags(self):
|
||||
logger.debug(f"Syncing tags...")
|
||||
old = self.api.get_tags()
|
||||
new = self.target_tree.get("tags", [])
|
||||
self.sync(new, old, "name")
|
||||
self.sync(new, old, "name", Tag)
|
||||
|
||||
def sync_status_pages(self):
|
||||
logger.debug(f"Syncing status pages...")
|
||||
old = self.api.get_status_pages()
|
||||
new = self.target_tree.get("status_pages", [])
|
||||
self.sync(new, old, "slug")
|
||||
self.sync(new, old, "slug", StatusPage)
|
||||
|
||||
def sync_settings(self):
|
||||
if "settings" not in self.target_tree:
|
||||
|
@ -58,8 +71,9 @@ class Manager:
|
|||
if k not in new.kwargs:
|
||||
new.kwargs[k] = v
|
||||
|
||||
def sync(self, new, old, pk):
|
||||
def sync(self, new, old, pk, item_t):
|
||||
indexed_old = {elem[pk]: elem for elem in old}
|
||||
indexed_new = {elem.name: elem for elem in new}
|
||||
for k in new:
|
||||
if k.name in indexed_old:
|
||||
k.id = indexed_old[k.name]["id"]
|
||||
|
@ -72,6 +86,14 @@ class Manager:
|
|||
else:
|
||||
k.id = None # Useless
|
||||
logger.debug(f"Creating key {k}")
|
||||
for k in indexed_old:
|
||||
if k not in indexed_new:
|
||||
if item_t == StatusPage:
|
||||
self.to_prune.append(item_t(api=None, name=indexed_old[k]["slug"], id=indexed_old[k]["id"]))
|
||||
else:
|
||||
self.to_prune.append(item_t(api=self.api, name=indexed_old[k]["name"], id=indexed_old[k]["id"]))
|
||||
|
||||
|
||||
|
||||
|
||||
class Item:
|
||||
|
@ -85,6 +107,10 @@ class Item:
|
|||
def save(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
def prune(self):
|
||||
print(f"Would prune: {self}")
|
||||
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name != "saved":
|
||||
self.saved = False
|
||||
|
@ -157,6 +183,12 @@ class Monitor(Item):
|
|||
self.api.add_monitor_tag(tag_id=t.id, monitor_id=self.id, value=v)
|
||||
self.saved = True
|
||||
|
||||
|
||||
def prune(self):
|
||||
logger.debug(f"Deleting monitor {self.name}")
|
||||
self.api.delete_monitor(self.id)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return f"Monitor({str(self)})"
|
||||
|
||||
|
@ -185,6 +217,9 @@ class Tag(Item):
|
|||
)
|
||||
self.saved = True
|
||||
|
||||
def prune(self):
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return f"Tag({str(self)})"
|
||||
|
||||
|
|
17
test.json
Normal file
17
test.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"monitors": {
|
||||
"mdr": {
|
||||
"accepted_statuscodes": [
|
||||
"200-299"
|
||||
],
|
||||
"headers": "{\n \"Host\": \"julienmalka.me\"\n}\n",
|
||||
"keyword": "Ulm",
|
||||
"type": "keyword",
|
||||
"url": "https://82.67.34.230"
|
||||
}
|
||||
},
|
||||
"notifications": {},
|
||||
"settings": {},
|
||||
"status_pages": {},
|
||||
"tags": {}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
lib,
|
||||
buildPythonPackage,
|
||||
fetchFromGitHub,
|
||||
fetchFromGitea,
|
||||
setuptools,
|
||||
wheel,
|
||||
packaging,
|
||||
|
@ -11,14 +11,15 @@
|
|||
|
||||
buildPythonPackage rec {
|
||||
pname = "uptime-kuma-api";
|
||||
version = "1.2.1";
|
||||
version = "2.0.0";
|
||||
pyproject = true;
|
||||
|
||||
src = fetchFromGitHub {
|
||||
owner = "lucasheld";
|
||||
src = fetchFromGitea {
|
||||
domain = "git.dgnum.eu";
|
||||
owner = "Luj";
|
||||
repo = "uptime-kuma-api";
|
||||
rev = version;
|
||||
hash = "sha256-Mgp4bSQPiEeulK9dAl+Di4Nj1HG3oVFGKr1bIdRZI44=";
|
||||
rev = "782037348df18dd32c3904c812c289f3d2d79170";
|
||||
hash = "sha256-YK4KMtKPlxqWwzB6lLyFqLvq1RCiUWrIILcVmuiirFw=";
|
||||
};
|
||||
|
||||
nativeBuildInputs = [
|
||||
|
|
Loading…
Reference in a new issue