-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemote_status_server.py
More file actions
134 lines (100 loc) · 3.65 KB
/
Remote_status_server.py
File metadata and controls
134 lines (100 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import subprocess
import sys
from datetime import datetime
# -------------------------------------------------
# Configuration
# -------------------------------------------------
# Remote health log (produced by PowerShell script)
remote_health_log = r"C:\Models\remote-health.log"
# Repo where the status file will be committed
repo_path = r"C:\Models\Reporting"
# Status file inside the repo
status_file_name = "remote_server_status.txt"
# Staleness threshold
health_stale_minutes = 90
# -------------------------------------------------
# Remote health check (LOG PARSING)
# -------------------------------------------------
def check_remote_health():
if not os.path.exists(remote_health_log):
return False, "Health log missing or server unreachable"
try:
last_write = datetime.fromtimestamp(os.path.getmtime(remote_health_log))
except Exception as e:
return False, f"Cannot stat health log ({e})"
age_minutes = (datetime.now() - last_write).total_seconds() / 60
if age_minutes > health_stale_minutes:
return False, f"Health log stale ({round(age_minutes,1)} minutes)"
try:
with open(remote_health_log, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
except Exception as e:
return False, f"Cannot read health log ({e})"
for line in reversed(lines):
if line.startswith("STATUS:"):
if line.strip() == "STATUS: OK":
return True, "Remote server accessible"
else:
return False, line.strip().replace("STATUS:", "").strip()
return False, "No STATUS line found in health log"
# -------------------------------------------------
# Write plain-text status file
# -------------------------------------------------
def write_status_file(ok, message):
status_path = os.path.join(repo_path, status_file_name)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status_text = "OK" if ok else "FAILURE"
content = [
f"Timestamp : {timestamp}",
f"Status : {status_text}",
f"Message : {message}",
]
with open(status_path, "w", encoding="utf-8") as f:
f.write("\n".join(content) + "\n")
return status_path
# -------------------------------------------------
# Git commit & push
# -------------------------------------------------
def commit_and_push(file_path):
try:
if not os.path.isdir(os.path.join(repo_path, ".git")):
print("ERROR: Target directory is not a git repository")
return
subprocess.run(
["git", "add", os.path.basename(file_path)],
cwd=repo_path,
check=True
)
status = subprocess.run(
["git", "status", "--porcelain"],
cwd=repo_path,
capture_output=True,
text=True
)
if not status.stdout.strip():
print("No changes to commit.")
return
subprocess.run(
["git", "commit", "-m", "Update remote server accessibility status"],
cwd=repo_path,
check=True
)
subprocess.run(
["git", "push"],
cwd=repo_path,
check=True
)
print("Status file committed and pushed.")
except subprocess.CalledProcessError as e:
print(f"Git error: {e}")
def main():
ok, message = check_remote_health()
# Always write status file (even on failure)
status_path = write_status_file(ok, message)
# Commit result to repo
commit_and_push(status_path)
# Exit code for schedulers / CI
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()