This commit is contained in:
dvirlabs 2025-06-30 03:05:17 +03:00
parent e906eb7925
commit 7cd45f1ac8

View File

@ -8,7 +8,6 @@ REPOS = {
"infra": "https://git.dvirlabs.com/dvirlabs/infra.git",
"observability-stack": "https://git.dvirlabs.com/dvirlabs/observability-stack.git",
"sandbox": "https://git.dvirlabs.com/dvirlabs/sandbox.git",
# ✅ Add GitHub repo to REPOS list (not hardcoded separately)
"lab-monitor": f"https://{os.getenv('GITHUB_TOKEN')}@github.com/dvirlabs/lab-monitor.git",
}
@ -31,7 +30,7 @@ def extract_urls():
urls = []
for repo in REPOS:
if repo == "lab-monitor":
continue # skip this, it's where we push
continue
manifests_path = os.path.join(BASE_DIR, repo, "manifests")
if not os.path.isdir(manifests_path):
continue
@ -47,26 +46,13 @@ def extract_urls():
if not ext or not ext.get("url"):
continue
app_name = cfg.get("app", app)
for code, sev in ext.get("expected_codes", {}).items():
urls.append({
"url": ext["url"],
"name": app_name,
"code": code,
"message": f"⚠️ {app_name} down ({code})",
"severity": sev
"url": ext["url"]
})
return urls
def severity_to_priority(sev):
return {
"critical": 2,
"high": 1,
"warning": 0,
"info": 0
}.get(sev.lower(), 0)
def generate_workflow(urls):
os.makedirs(os.path.dirname(WORKFLOW_FILE), exist_ok=True)
with open(WORKFLOW_FILE, "w") as f:
@ -81,24 +67,21 @@ jobs:
monitor:
runs-on: ubuntu-latest
steps:
- name: Check all URLs
- name: Check services
run: |
check_url() {
URL=$1
NAME=$2
CODE=$3
MESSAGE=$4
PRIORITY=$5
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
TIME=$(date "+%Y-%m-%d %H:%M:%S")
if [[ "$STATUS" == "$CODE" ]]; then
if [[ "$STATUS" == "502" || "$STATUS" == "404" ]]; then
curl -s \\
--form-string "token=${{ secrets.PUSHOVER_TOKEN }}" \\
--form-string "user=${{ secrets.PUSHOVER_USER }}" \\
--form-string "title=🔴 $NAME Alert" \\
--form-string "message=$MESSAGE at $TIME" \\
--form-string "priority=$PRIORITY" \\
--form-string "message=⚠️ $URL is down ($STATUS) at $TIME" \\
--form-string "priority=2" \\
--form-string "retry=60" \\
--form-string "expire=600" \\
https://api.pushover.net/1/messages.json
@ -106,15 +89,16 @@ jobs:
echo "✅ $NAME is up: $STATUS"
fi
}
""")
for item in urls:
f.write(f' check_url "{item["url"]}" "{item["name"]}" "{item["code"]}" "{item["message"]}" "{severity_to_priority(item["severity"])}"\n')
f.write(f' check_url "{item["url"]}" "{item["name"]}"\n')
def push_workflow():
subprocess.run(["ls", "-l", REPO_PATH])
# Detect default branch (main or master)
# Detect default branch
result = subprocess.run(
["git", "-C", REPO_PATH, "symbolic-ref", "refs/remotes/origin/HEAD"],
capture_output=True, text=True, check=True
@ -122,11 +106,8 @@ def push_workflow():
default_branch = result.stdout.strip().split("/")[-1]
subprocess.run(["git", "-C", REPO_PATH, "checkout", default_branch], check=True)
# Git identity
subprocess.run(["git", "-C", REPO_PATH, "config", "user.name", "lab-monitor-bot"])
subprocess.run(["git", "-C", REPO_PATH, "config", "user.email", "bot@dvirlabs.com"])
# Add + Commit + Push
subprocess.run(["git", "-C", REPO_PATH, "add", ".github/workflows/monitor.yml"])
subprocess.run(["git", "-C", REPO_PATH, "commit", "-m", "update monitor.yml from monitoring.yaml"], check=False)
result = subprocess.run(["git", "-C", REPO_PATH, "push", "--set-upstream", "origin", default_branch])