diff --git a/.gitignore b/.gitignore index d303b97..d87586b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ config/mail-status.json config/mail-status.json telemetry/__pycache__/ + +__pycache__/ +*.pyc diff --git a/collectors/git.py b/collectors/git.py new file mode 100644 index 0000000..a974292 --- /dev/null +++ b/collectors/git.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import subprocess + +REPOSITORIES = { + "infrastructure": "/opt/git/infrastructure", + "voyager": "/opt/git/voyager", +} + + +def collect(): + repos = [] + dirty = False + + for name, path in REPOSITORIES.items(): + result = subprocess.run( + ["git", "-C", path, "status", "--short", "--branch"], + capture_output=True, + text=True, + ) + + if result.returncode != 0: + repos.append({ + "name": name, + "state": "offline", + }) + dirty = True + continue + + lines = result.stdout.splitlines() + branch = lines[0].replace("## ", "") + modified = sum(1 for l in lines[1:] if not l.startswith("??")) + untracked = sum(1 for l in lines[1:] if l.startswith("??")) + + state = "clean" if modified == 0 and untracked == 0 else "dirty" + + if state != "clean": + dirty = True + + repos.append({ + "name": name, + "state": state, + "branch": branch, + "modified": modified, + "untracked": untracked, + }) + + return { + "state": "warning" if dirty else "online", + "summary": f"{len(repos)} repositories", + "detail": "All clean" if not dirty else "Repositories require attention", + "repositories": repos, + } diff --git a/telemetry/collector.py b/telemetry/collector.py index 7e113bc..a510014 100755 --- a/telemetry/collector.py +++ b/telemetry/collector.py @@ -3,7 +3,8 @@ from pathlib import Path from .common import write_json -from collectors.backup import collect +from collectors.backup import collect as backup_collect +from collectors.git import collect as git_collect OUTPUT = Path("/opt/docker/caddy/site/voyager/data/status.json") @@ -11,7 +12,8 @@ OUTPUT = Path("/opt/docker/caddy/site/voyager/data/status.json") def main(): payload = { "systems": { - "backup": collect() + "backup": backup_collect(), + "git": git_collect(), } }