-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.py
More file actions
303 lines (238 loc) · 8.2 KB
/
webserver.py
File metadata and controls
303 lines (238 loc) · 8.2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
Arcade App Alerter - Web Server
Serves a dashboard showing:
- Current versions for each monitored app (from *.ver files)
- Last check info (from lastcheck file)
- Recent log lines from the shared log file
All paths and settings are driven by config.ini.
"""
from __future__ import annotations
import os
import configparser
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional
from flask import Flask, request, render_template, abort, redirect, url_for
# -----------------------------------------------------------------------------
# Config loading
# -----------------------------------------------------------------------------
CONFIG_ENV_VAR = "ARCADE_APP_CONFIG"
DEFAULT_CONFIG_PATH = "/config/arcade_app/config.ini"
CONFIG_PATH = os.environ.get(CONFIG_ENV_VAR, DEFAULT_CONFIG_PATH)
parser = configparser.ConfigParser()
parser.read(CONFIG_PATH)
if not parser.has_section("web"):
raise RuntimeError(f"[web] section missing in config file: {CONFIG_PATH}")
WEB = parser["web"]
DATA_DIR: str = WEB.get("data_dir", "/data/arcade_app")
LASTCHECK_FILE: str = WEB.get("lastcheck_file", "lastcheck")
LOG_PATH: str = WEB.get("log_path", "/var/log/arcadecheck.log")
LOG_LINES: int = WEB.getint("log_lines", 40)
TITLE: str = WEB.get("title", "Arcade App Version Monitor")
FLASK_PORT: int = WEB.getint("port", 5000)
# allowed_hosts: comma or space separated
_raw_hosts = WEB.get("allowed_hosts", "").replace(",", " ")
ALLOWED_HOSTS = [h.strip() for h in _raw_hosts.split() if h.strip()]
# [apps] section lists app ids used in [app.<id>]
if not parser.has_section("apps"):
raise RuntimeError(f"[apps] section missing in config file: {CONFIG_PATH}")
APPS_SECTION = parser["apps"]
APP_IDS: List[str] = [a.strip() for a in APPS_SECTION.get("apps", "").split(",") if a.strip()]
# Normalize data dir
DATA_DIR = os.path.abspath(DATA_DIR)
# -----------------------------------------------------------------------------
# Helper functions
# -----------------------------------------------------------------------------
def elapsed_time(start_time_str: str, withsecs: bool, append: Optional[str] = None) -> str:
"""
Convert a timestamp string to a human-readable elapsed time.
start_time_str:
If withsecs=True: '%m-%d-%Y %H:%M:%S'
If withsecs=False: '%m-%d-%Y'
append:
Text to append at the end (e.g., "ago").
"""
if withsecs:
fmt = "%m-%d-%Y %H:%M:%S"
else:
fmt = "%m-%d-%Y"
try:
start = datetime.strptime(start_time_str, fmt)
except ValueError:
# If parsing fails, just return the original string
return start_time_str
now = datetime.now()
if not withsecs:
# "Today" / "Yesterday" shortcuts for date-only values
if start.date() == now.date():
return f"Today {append}" if append else "Today"
if start.date() == (now.date() - timedelta(days=1)):
return f"Yesterday {append}" if append else "Yesterday"
seconds = int((now - start).total_seconds())
intervals = (
("Years", 31536000),
("Months", 2592000),
("Weeks", 604800),
("Days", 86400),
("Hours", 3600),
("Minutes", 60),
("Seconds", 1),
)
parts: List[str] = []
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
name = name.rstrip("s")
parts.append(f"{value} {name}")
if len(parts) == 2:
break
if not parts:
parts = ["0 Seconds"]
s = ", ".join(parts)
if append:
s += f" {append}"
return s
def load_app_versions() -> List[Dict[str, Any]]:
"""
Load version info for each app defined in [apps] and [app.<id>].
Each entry in the returned list has:
{
"id": "<id>",
"label": "<label>",
"version": "<version or None>",
"date": "<date string or None>",
"elapsed": "<human-readable age or None>",
}
"""
apps: List[Dict[str, Any]] = []
for app_id in APP_IDS:
sec_name = f"app.{app_id}"
label = app_id
ver_file = f"{app_id}.ver"
if parser.has_section(sec_name):
sec = parser[sec_name]
label = sec.get("label", label)
ver_file = sec.get("file", ver_file)
path = os.path.join(DATA_DIR, ver_file)
version: Optional[str] = None
date_str: Optional[str] = None
elapsed_str: Optional[str] = None
if os.path.exists(path):
try:
with open(path, "r", encoding="utf-8") as f:
lines = f.read().splitlines()
if lines:
version = (lines[0] or "").strip() or None
if len(lines) > 1:
date_str = (lines[1] or "").strip() or None
if date_str:
elapsed_str = elapsed_time(date_str, withsecs=False, append="ago")
except OSError:
# Ignore read errors and leave fields as None
pass
apps.append(
{
"id": app_id,
"label": label,
"version": version,
"date": date_str,
"elapsed": elapsed_str,
}
)
return apps
def load_lastcheck() -> Optional[Dict[str, str]]:
"""
Load lastcheck info from LASTCHECK_FILE in DATA_DIR.
Expected format:
line 0: timestamp '%m-%d-%Y %H:%M:%S'
line 1: app label
"""
path = os.path.join(DATA_DIR, LASTCHECK_FILE)
if not os.path.exists(path):
return None
try:
with open(path, "r", encoding="utf-8") as f:
lines = f.read().splitlines()
except OSError:
return None
if len(lines) < 2:
return None
ts_str = (lines[0] or "").strip()
app_name = (lines[1] or "").strip()
if not ts_str or not app_name:
return None
elapsed_str = elapsed_time(ts_str, withsecs=True, append="ago")
return {
"timestamp": ts_str,
"app": app_name,
"elapsed": elapsed_str,
}
def load_log_text() -> str:
"""
Load the last LOG_LINES lines from LOG_PATH.
Implemented purely in Python (no external 'tail' dependency).
"""
if not os.path.exists(LOG_PATH):
return ""
try:
with open(LOG_PATH, "r", encoding="utf-8", errors="replace") as f:
lines = f.read().splitlines()
except OSError:
return ""
if not lines:
return ""
tail = lines[-LOG_LINES:]
return "\n".join(tail)
# -----------------------------------------------------------------------------
# Flask app
# -----------------------------------------------------------------------------
app = Flask(__name__)
@app.before_request
def limit_remote_addr():
"""
Optional source IP filtering based on [web].allowed_hosts.
If allowed_hosts is empty, no restriction is applied.
"""
if not ALLOWED_HOSTS:
return
remote = request.remote_addr
if remote not in ALLOWED_HOSTS:
abort(403, description="You're not allowed to access this resource")
@app.route("/health")
def health() -> tuple[str, int]:
"""
Simple health endpoint for Docker healthcheck.
"""
return "ok", 200
@app.route("/")
def index():
apps = load_app_versions()
lastcheck = load_lastcheck()
log_text = load_log_text()
return render_template(
"index.html",
title=TITLE,
apps=apps,
lastcheck=lastcheck,
log=log_text,
log_lines=LOG_LINES,
log_path=LOG_PATH,
)
@app.route("/clear-log", methods=["POST"])
def clear_log():
"""
Clear the contents of the log file, then redirect back to the dashboard.
"""
try:
# Truncate the file to zero length (create if missing)
os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
with open(LOG_PATH, "w", encoding="utf-8"):
pass
except OSError:
# If something goes wrong, just ignore; the next page load will show the old log
pass
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=FLASK_PORT, debug=True)