diff --git a/automation/alerts/generate_monitor_workflow.py b/automation/alerts/generate_monitor_workflow.py index 3369503..4d7b9d9 100644 --- a/automation/alerts/generate_monitor_workflow.py +++ b/automation/alerts/generate_monitor_workflow.py @@ -10,6 +10,8 @@ REPOS = { GITHUB_REPO = f"https://{os.getenv('GITHUB_TOKEN')}@github.com/dvirh/lab-monitor.git" BASE_DIR = "./repos" WORKFLOW_FILE = "./repos/lab-monitor/.github/workflows/monitor.yml" +REPO_PATH = os.path.join(BASE_DIR, "lab-monitor") + def clone_repos(): os.makedirs(BASE_DIR, exist_ok=True) @@ -20,6 +22,7 @@ def clone_repos(): else: subprocess.run(["git", "clone", url, repo_path]) + def extract_urls(): urls = [] for repo in REPOS: @@ -57,6 +60,7 @@ def severity_to_priority(sev): "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: @@ -82,7 +86,7 @@ jobs: STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL") TIME=$(date "+%Y-%m-%d %H:%M:%S") - if [[ "$STATUS" == "$CODE" ]]; then + if [[ "$STATUS" != "$CODE" ]]; then curl -s \\ --form-string "token=${{ secrets.PUSHOVER_TOKEN }}" \\ --form-string "user=${{ secrets.PUSHOVER_USER }}" \\ @@ -100,22 +104,31 @@ jobs: for item in urls: f.write(f' check_url "{item["url"]}" "{item["name"]}" "{item["code"]}" "{item["message"]}" "{severity_to_priority(item["severity"])}"\n') + def push_workflow(): - repo_path = os.path.join(BASE_DIR, "lab-monitor") - if not os.path.exists(repo_path): - subprocess.run(["git", "clone", GITHUB_REPO, repo_path]) - subprocess.run(["git", "-C", repo_path, "pull", "origin", "main"]) + # clone if needed + if not os.path.exists(REPO_PATH): + print("📥 Cloning lab-monitor...") + result = subprocess.run(["git", "clone", GITHUB_REPO, REPO_PATH]) + if result.returncode != 0: + raise Exception("❌ Failed to clone lab-monitor repo") + + # checkout main if needed + subprocess.run(["git", "-C", REPO_PATH, "fetch"], check=True) + subprocess.run(["git", "-C", REPO_PATH, "checkout", "-B", "main", "origin/main"], check=False) # Set 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"]) + 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 - 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) + 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) # Push with upstream set to main - subprocess.run(["git", "-C", repo_path, "push", "--set-upstream", "origin", "main"]) + result = subprocess.run(["git", "-C", REPO_PATH, "push", "--set-upstream", "origin", "main"]) + if result.returncode != 0: + raise Exception("❌ Failed to push monitor.yml to origin/main") if __name__ == "__main__": @@ -123,4 +136,3 @@ if __name__ == "__main__": urls = extract_urls() generate_workflow(urls) push_workflow() -