serverwatcher 5.19__tar.gz → 5.21__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {serverwatcher-5.19/src/serverwatcher.egg-info → serverwatcher-5.21}/PKG-INFO +1 -1
  2. {serverwatcher-5.19 → serverwatcher-5.21}/pyproject.toml +1 -1
  3. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/configclasses/config.py +2 -4
  4. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/configclasses/watcher.py +8 -6
  5. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/defaultconfigs/config.yaml +2 -4
  6. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/defaultconfigs/watcher.yaml +5 -5
  7. serverwatcher-5.21/src/serverwatcher/validator.py +218 -0
  8. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/watcher.py +18 -28
  9. {serverwatcher-5.19 → serverwatcher-5.21/src/serverwatcher.egg-info}/PKG-INFO +1 -1
  10. serverwatcher-5.19/src/serverwatcher/validator.py +0 -139
  11. {serverwatcher-5.19 → serverwatcher-5.21}/LICENSE +0 -0
  12. {serverwatcher-5.19 → serverwatcher-5.21}/README.md +0 -0
  13. {serverwatcher-5.19 → serverwatcher-5.21}/setup.cfg +0 -0
  14. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/__init__.py +0 -0
  15. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/__main__.py +0 -0
  16. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/configclasses/__init__.py +0 -0
  17. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/configclasses/messages.py +0 -0
  18. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher/defaultconfigs/messages.yaml +0 -0
  19. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher.egg-info/SOURCES.txt +0 -0
  20. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher.egg-info/dependency_links.txt +0 -0
  21. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher.egg-info/requires.txt +0 -0
  22. {serverwatcher-5.19 → serverwatcher-5.21}/src/serverwatcher.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: serverwatcher
3
- Version: 5.19
3
+ Version: 5.21
4
4
  Summary: A HungerLib-powered Minecraft server automation engine.
5
5
  Author: iFamished
6
6
  License: GPL-3.0
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "serverwatcher"
10
- version = "5.19"
10
+ version = "5.21"
11
11
  description = "A HungerLib-powered Minecraft server automation engine."
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.14"
@@ -11,8 +11,6 @@ class GlobalConfig:
11
11
  panel_url: str = 'panel.url'
12
12
  panel_api_key: str = 'panel.api_key'
13
13
 
14
- origin_server_id: str = 'origin.server_id'
15
-
16
14
  server_name: str = 'server.name'
17
15
  server_id: str = 'server.server_id'
18
16
  server_domain: str = 'server.domain'
@@ -32,6 +30,7 @@ class GlobalConfig:
32
30
  error_prefix: str = 'logger.prefixes.error'
33
31
 
34
32
  clear_terminal: bool = 'terminal.enable_clearing'
33
+ handle_keyboard_interrupt: bool = 'terminal.handle_keyboard_interrupt'
35
34
 
36
35
 
37
36
  class fallbacks:
@@ -41,8 +40,6 @@ class fallbacks:
41
40
  panel_url = 'https://example.com'
42
41
  panel_api_key = 'CHANGE_ME'
43
42
 
44
- origin_server_id = 'CHANGE_ME'
45
-
46
43
  server_name = 'My SMP'
47
44
  server_id = 'CHANGE_ME'
48
45
  server_domain = 'mc.example.com'
@@ -62,3 +59,4 @@ class fallbacks:
62
59
  error_prefix = '<red>[ERROR]: '
63
60
 
64
61
  clear_terminal = True
62
+ handle_keyboard_interrupt = True
@@ -7,13 +7,12 @@ class WatcherConfig:
7
7
 
8
8
  watch_interval: int = 'watch_interval'
9
9
 
10
- schedule_control: bool = 'schedule_control.enabled'
11
- restart_soon_id: int = 'schedule_control.restart_soon_id'
12
-
13
10
  threshold_ram: int = 'thresholds.ram'
14
11
  threshold_cpu: int = 'thresholds.cpu'
15
12
  threshold_uptime: int = 'thresholds.uptime'
16
13
  threshold_tps: float = 'thresholds.tps'
14
+ threshold_min_uptime: int = 'thresholds.min_uptime'
15
+ threshold_low_gap: int = 'thresholds.low_gap'
17
16
 
18
17
  weight_restart_soon: int = 'weights.restart_soon'
19
18
  weight_ram: int = 'weights.ram'
@@ -27,6 +26,8 @@ class WatcherConfig:
27
26
  low_gap_minutes: int = 'gaps.low_gap_minutes'
28
27
  high_gap_minutes: int = 'gaps.high_gap_minutes'
29
28
 
29
+ snap_minutes: list = 'snap_minutes'
30
+
30
31
  restart_wait_seconds: int = 'restart.wait_seconds'
31
32
  restart_timeout: int = 'restart.online_timeout'
32
33
  restart_online_interval: int = 'restart.online_interval'
@@ -35,13 +36,12 @@ class WatcherConfig:
35
36
  class fallbacks:
36
37
  watch_interval = 300
37
38
 
38
- schedule_control = False
39
- restart_soon_id = 0
40
-
41
39
  threshold_ram = 6
42
40
  threshold_cpu = 150
43
41
  threshold_uptime = 12
44
42
  threshold_tps = 19.5
43
+ threshold_min_uptime = 30
44
+ threshold_low_gap = 2
45
45
 
46
46
  weight_restart_soon = 3
47
47
  weight_ram = 1
@@ -55,6 +55,8 @@ class fallbacks:
55
55
  low_gap_minutes = 120
56
56
  high_gap_minutes = 60
57
57
 
58
+ snap_minutes = [0, 30]
59
+
58
60
  restart_wait_seconds = 30
59
61
  restart_timeout = 120
60
62
  restart_online_interval = 2
@@ -5,9 +5,6 @@ panel:
5
5
  url: 'https://example.com'
6
6
  api_key: 'CHANGE_ME'
7
7
 
8
- origin:
9
- server_id: 'CHANGE_ME'
10
-
11
8
  server:
12
9
  name: 'My SMP'
13
10
  server_id: 'CHANGE_ME'
@@ -30,4 +27,5 @@ logger:
30
27
  error: "<red>[%hh%:%mm%:%ss%] [ERROR]: "
31
28
 
32
29
  terminal:
33
- enable_clearing: True
30
+ enable_clearing: True
31
+ handle_keyboard_interrupt: True
@@ -1,14 +1,12 @@
1
- watch_interval: 300
2
-
3
- schedule_control:
4
- enabled: False
5
- restart_soon_id: 0 # replace this with a real schedule id
1
+ watch_interval: 300 # in seconds
6
2
 
7
3
  thresholds:
8
4
  ram: 6
9
5
  cpu: 150
10
6
  uptime: 12
11
7
  tps: 19.5
8
+ min_uptime: 30 # will not restart the server if the total uptime is less than 30 minutes
9
+ low_gap: 2
12
10
 
13
11
  weights:
14
12
  restart_soon: 3
@@ -23,6 +21,8 @@ gaps:
23
21
  low_gap_minutes: 120
24
22
  high_gap_minutes: 60
25
23
 
24
+ snap_minutes: [0, 30] # will snap the scheduled time to the nearest hour or half hour
25
+
26
26
  restart:
27
27
  wait_seconds: 30
28
28
  online_timeout: 120
@@ -0,0 +1,218 @@
1
+ import sys
2
+ from dataclasses import fields
3
+
4
+ from hungerlib import utils, loadConfig
5
+
6
+ from serverwatcher.configclasses.config import GlobalConfig
7
+ from serverwatcher.configclasses.messages import MessagesConfig
8
+ from serverwatcher.configclasses.watcher import WatcherConfig
9
+
10
+
11
+ utils.clearTerminal()
12
+ errors = []
13
+ warnings = []
14
+ defaults = []
15
+
16
+
17
+ def validate_key_types(config_obj, schema):
18
+ for f in fields(schema):
19
+ if f.name.startswith("__"):
20
+ continue
21
+
22
+ expected_type = f.type
23
+ value = getattr(config_obj, f.name, None)
24
+
25
+ # allow None (missing + no fallback) to be handled by other validators
26
+ if value is None:
27
+ continue
28
+
29
+ if not isinstance(value, expected_type):
30
+ errors.append(
31
+ f'{schema.__name__}.{f.name}: expected {expected_type.__name__}, '
32
+ f'got "{type(value).__name__}" ({value!r})'
33
+ )
34
+
35
+
36
+ def check_field(config_obj, name, allow_fallback=True):
37
+ """
38
+ Unified check for:
39
+ - missing YAML key (config.raw.<name> is None)
40
+ - fallback usage (config.<name> == config.fallbacks.<name>)
41
+ - whether fallback is allowed or not
42
+ """
43
+ raw = config_obj.raw
44
+ fb = config_obj.fallbacks
45
+
46
+ val = getattr(config_obj, name)
47
+ raw_val = getattr(raw, name)
48
+ fb_val = getattr(fb, name)
49
+
50
+ # 1) YAML key missing
51
+ if raw_val is None:
52
+ if allow_fallback:
53
+ warnings.append(f'{name}: key does not exist, using fallback "{fb_val}"')
54
+ else:
55
+ errors.append(f'{name}: key does not exist and fallback is not allowed')
56
+ return
57
+
58
+ # 2) YAML key exists but value equals fallback
59
+ if val == fb_val and not allow_fallback:
60
+ defaults.append(f'{name}: must not be left default or empty (got "{val}")')
61
+
62
+
63
+ def validate_global_config(config):
64
+ c = config
65
+
66
+ # Fallback policy:
67
+ # - NOT allowed (must be set by user): panel_url, panel_api_key,
68
+ # server_id, server_domain, bridge_token
69
+ # - Allowed: everything else
70
+
71
+ # timezone
72
+ check_field(c, "timezone")
73
+ if c.timezone == "":
74
+ errors.append('timezone: must not be empty')
75
+
76
+ # panel
77
+ check_field(c, "panel_name")
78
+ check_field(c, "panel_url", allow_fallback=False)
79
+ check_field(c, "panel_api_key", allow_fallback=False)
80
+
81
+ if c.panel_url and not (c.panel_url.startswith("http://") or c.panel_url.startswith("https://")):
82
+ errors.append(f'panel_url: must start with "http://" or "https://" (got "{c.panel_url}")')
83
+
84
+ if c.panel_api_key and not c.panel_api_key.startswith("ptlc_"):
85
+ errors.append(f'panel_api_key: must be a valid Pterodactyl client API key (got "{c.panel_api_key}")')
86
+ if c.panel_api_key and c.panel_api_key.startswith("plta_"):
87
+ errors.append(f'panel_api_key: should not be an application key (got "{c.panel_api_key}")')
88
+
89
+ # server
90
+ check_field(c, "server_name")
91
+ check_field(c, "server_id", allow_fallback=False)
92
+ check_field(c, "server_domain", allow_fallback=False)
93
+ check_field(c, "server_port")
94
+ if c.server_domain and (c.server_domain.startswith("http://") or c.server_domain.startswith("https://")):
95
+ errors.append(f'server_domain: must not contain protocol (got "{c.server_domain}")')
96
+ if c.server_port is not None and not (1 <= c.server_port <= 65535):
97
+ errors.append(f'server_port: must be 1–65535 (got "{c.server_port}")')
98
+
99
+ # tps_command
100
+ check_field(c, "tps_command")
101
+
102
+ # hungerbridge
103
+ check_field(c, "bridge_token", allow_fallback=False)
104
+ check_field(c, "bridge_port")
105
+ if c.bridge_port is not None and not (1 <= c.bridge_port <= 65535):
106
+ errors.append(f'bridge_port: must be 1–65535 (got "{c.bridge_port}")')
107
+
108
+ # logger
109
+ check_field(c, "enable_logging")
110
+ check_field(c, "logger_name")
111
+ check_field(c, "log_path")
112
+ check_field(c, "info_prefix")
113
+ check_field(c, "warn_prefix")
114
+ check_field(c, "error_prefix")
115
+
116
+ # terminal
117
+ check_field(c, "clear_terminal")
118
+ check_field(c, "handle_keyboard_interrupt")
119
+
120
+
121
+ def validate_watcher_config(watcherconfig):
122
+ c = watcherconfig
123
+ raw = c.raw
124
+ fb = c.fallbacks
125
+
126
+ # basic numeric sanity
127
+ if c.restart_wait_seconds <= 0:
128
+ errors.append(f'restart_wait_seconds: must be > 0 (got {c.restart_wait_seconds})')
129
+
130
+ if c.threshold_cpu <= 0:
131
+ errors.append(f'threshold_cpu: must be > 0 (got {c.threshold_cpu})')
132
+
133
+ if c.threshold_ram <= 0:
134
+ errors.append(f'threshold_ram: must be > 0 (got {c.threshold_ram})')
135
+
136
+ if c.threshold_tps <= 0 or c.threshold_tps > 20:
137
+ errors.append(f'threshold_tps: must be 1–20 (got {c.threshold_tps})')
138
+
139
+ # snap_minutes: must be a non-empty list of ints 0–59
140
+ if raw.snap_minutes is None:
141
+ warnings.append(f'snap_minutes: key does not exist, using fallback "{fb.snap_minutes}"')
142
+
143
+ if not isinstance(c.snap_minutes, list) or not c.snap_minutes:
144
+ errors.append(f'snap_minutes: must be a non-empty list of minute marks (got {c.snap_minutes!r})')
145
+ else:
146
+ bad = [m for m in c.snap_minutes if not isinstance(m, int) or not (0 <= m <= 59)]
147
+ if bad:
148
+ errors.append(f'snap_minutes: all values must be integers 0–59 (bad values: {bad})')
149
+
150
+
151
+ def validate_messages_config(messages):
152
+ pass
153
+ # m = messages
154
+
155
+ # for f in fields(MessagesConfig):
156
+ # if f.name.startswith("__"):
157
+ # continue
158
+ # value = getattr(m, f.name)
159
+ # if not isinstance(value, str) or value.strip() == "":
160
+ # errors.append(f'MessagesConfig.{f.name}: must be a non-empty string')
161
+
162
+
163
+ def validate_all():
164
+ config = loadConfig(GlobalConfig)
165
+ messages = loadConfig(MessagesConfig)
166
+ watcherconfig = loadConfig(WatcherConfig)
167
+
168
+ # type checks
169
+ validate_key_types(config, GlobalConfig)
170
+ validate_key_types(messages, MessagesConfig)
171
+ validate_key_types(watcherconfig, WatcherConfig)
172
+
173
+ # semantic checks
174
+ validate_global_config(config)
175
+ validate_watcher_config(watcherconfig)
176
+ validate_messages_config(messages)
177
+
178
+ # if too many critical defaults, assume "not configured at all"
179
+ critical_default_keys = [
180
+ "panel_url",
181
+ "panel_api_key",
182
+ "server_id",
183
+ "server_domain",
184
+ "bridge_token",
185
+ ]
186
+ critical_defaults_used = [
187
+ d for d in defaults
188
+ if any(d.startswith(k) for k in critical_default_keys)
189
+ ]
190
+
191
+ if len(critical_defaults_used) >= 3:
192
+ print('❌ CONFIG VALIDATION FAILED:\nIt looks like you haven\'t configured this yet! Please change these defaults:')
193
+ for d in critical_defaults_used:
194
+ print(' -', d)
195
+ sys.exit(1)
196
+
197
+ if errors or defaults:
198
+ print('❌ CONFIG VALIDATION FAILED:')
199
+ for e in errors:
200
+ print(' -', e)
201
+ for d in defaults:
202
+ print(' -', d)
203
+ if warnings:
204
+ print('\nWarnings:')
205
+ for w in warnings:
206
+ print(' -', w)
207
+ sys.exit(1)
208
+
209
+ if warnings:
210
+ print('⚠️ CONFIG VALIDATION WARNINGS:')
211
+ for w in warnings:
212
+ print(' -', w)
213
+
214
+ print('✅ All configs are valid.')
215
+
216
+
217
+ if __name__ == '__main__':
218
+ validate_all()
@@ -30,12 +30,6 @@ class ServerWatcher:
30
30
  api_key=self.config.panel_api_key,
31
31
  )
32
32
 
33
- self.origin = servers.Generic(
34
- name="Origin",
35
- panel=self.panel,
36
- server_id=self.config.origin_server_id
37
- )
38
-
39
33
  self.server = servers.Minecraft(
40
34
  name=self.config.server_name,
41
35
  panel=self.panel,
@@ -72,9 +66,6 @@ class ServerWatcher:
72
66
  raise SystemExit
73
67
 
74
68
  def restart_and_wait(self):
75
- if self.watcherconfig.schedule_control:
76
- self.origin.disableSchedule(self.watcherconfig.restart_soon_id)
77
-
78
69
  self.server.restart()
79
70
  self.router.info(self.messages.restart_action_sent)
80
71
  time.sleep(self.watcherconfig.restart_wait_seconds)
@@ -93,7 +84,7 @@ class ServerWatcher:
93
84
  self.router.error(self.messages.server_failed_restart)
94
85
 
95
86
  def schedule_restart(self, minutes):
96
- info = utils.snapSchedule(minimumMinutes=minutes)
87
+ info = utils.snapSchedule(minimumMinutes=minutes, snapMinutes=tuple(sorted(self.watcherconfig.snap_minutes)))
97
88
  scheduled = info["scheduled"]
98
89
 
99
90
  local_time = scheduled.astimezone(self.tz)
@@ -104,7 +95,8 @@ class ServerWatcher:
104
95
  minute_callbacks = {
105
96
  int(k.split("_")[1]): (
106
97
  lambda raw=self.messages.as_map()[k]:
107
- self.router.broadcast(mapit(raw))
98
+ self.router.broadcast(mapit(raw)),
99
+ self.router.origin(raw)
108
100
  )
109
101
  for k in self.messages.as_map()
110
102
  if k.startswith("minute_")
@@ -113,17 +105,14 @@ class ServerWatcher:
113
105
  second_callbacks = {
114
106
  int(k.split("_")[1]): (
115
107
  lambda raw=self.messages.as_map()[k]:
116
- self.router.broadcast(mapit(raw))
108
+ self.router.broadcast(mapit(raw)),
109
+ self.router.origin(raw)
117
110
  )
118
111
  for k in self.messages.as_map()
119
112
  if k.startswith("second_")
120
113
  }
121
114
 
122
- utils.runCountdownEvents(
123
- target_time=scheduled,
124
- minute_callbacks=minute_callbacks,
125
- second_callbacks=second_callbacks,
126
- )
115
+ utils.runCountdownEvents(target_time=scheduled, minute_callbacks=minute_callbacks, second_callbacks=second_callbacks)
127
116
 
128
117
  def evaluate(self):
129
118
  # Lets Pterodactyl know the server is online, then clears terminal and uses user-defined startup key
@@ -145,10 +134,6 @@ class ServerWatcher:
145
134
  restart_reasons = []
146
135
  no_restart_reasons = []
147
136
 
148
- if self.watcherconfig.schedule_control and self.server.getSchedule(self.watcherconfig.restart_soon_id)["is_active"]:
149
- restart_reasons.append(self.messages.reason_restart_soon)
150
- pro += self.watcherconfig.weight_restart_soon
151
-
152
137
  if snap.ram >= self.watcherconfig.threshold_ram:
153
138
  restart_reasons.append(mapit(self.messages.reason_ram, ram=snap.ram, threshold=self.watcherconfig.threshold_ram))
154
139
  pro += int(round(snap.ram, 0) - (self.watcherconfig.threshold_ram - 1))
@@ -167,7 +152,7 @@ class ServerWatcher:
167
152
  restart_reasons.append(mapit(self.messages.reason_tps, tps=snap.tps, threshold=self.watcherconfig.threshold_tps))
168
153
  pro += self.watcherconfig.weight_tps
169
154
 
170
- if snap.uptime // 60 < 30:
155
+ if snap.uptime // 60 < self.watcherconfig.threshold_min_uptime:
171
156
  no_restart_reasons.append(mapit(self.messages.reason_low_uptime, uptime=snap.uptime_formatted))
172
157
  anti += self.watcherconfig.weight_low_uptime
173
158
 
@@ -203,7 +188,7 @@ class ServerWatcher:
203
188
 
204
189
  self.router.info(self.messages.scheduled)
205
190
 
206
- if gap <= 2:
191
+ if gap <= self.watcherconfig.threshold_low_gap:
207
192
  self.router.warn(self.messages.gap_low, gap=gap)
208
193
  self.schedule_restart(self.watcherconfig.low_gap_minutes)
209
194
  else:
@@ -213,13 +198,18 @@ class ServerWatcher:
213
198
  self.restart_and_wait()
214
199
 
215
200
  def run(self):
216
- try:
217
- if self.config.clear_terminal:
218
- utils.clearTerminal()
201
+ if self.config.handle_keyboard_interrupt:
202
+ try:
203
+ while True:
204
+ if self.config.clear_terminal:
205
+ utils.clearTerminal()
206
+ self.evaluate()
207
+ time.sleep(self.watcherconfig.watch_interval)
208
+ except KeyboardInterrupt:
209
+ self.shutdown()
210
+ else:
219
211
  while True:
220
212
  if self.config.clear_terminal:
221
213
  utils.clearTerminal()
222
214
  self.evaluate()
223
215
  time.sleep(self.watcherconfig.watch_interval)
224
- except KeyboardInterrupt:
225
- self.shutdown()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: serverwatcher
3
- Version: 5.19
3
+ Version: 5.21
4
4
  Summary: A HungerLib-powered Minecraft server automation engine.
5
5
  Author: iFamished
6
6
  License: GPL-3.0
@@ -1,139 +0,0 @@
1
- import sys
2
- from dataclasses import fields
3
- from hungerlib import utils, loadConfig
4
-
5
- from serverwatcher.configclasses.config import GlobalConfig
6
- from serverwatcher.configclasses.messages import MessagesConfig
7
- from serverwatcher.configclasses.watcher import WatcherConfig
8
-
9
- utils.clearTerminal()
10
-
11
- def deep_get_attr(obj, dotted):
12
- parts = dotted.split('.')
13
- cur = obj
14
- for p in parts:
15
- if not hasattr(cur, p):
16
- return None
17
- cur = getattr(cur, p)
18
- return cur
19
-
20
- def validate_type(name, value, expected_type, errors):
21
- if not isinstance(value, expected_type):
22
- errors.append(f'{name}: expected {expected_type.__name__}, got {type(value).__name__}')
23
-
24
- def validate_positive(name, value, errors):
25
- if isinstance(value, (int, float)) and value < 0:
26
- errors.append(f'{name}: must be >= 0 (got {value})')
27
-
28
- def validate_nonempty(name, value, errors):
29
- if isinstance(value, str) and value.strip() == '':
30
- errors.append(f'{name}: cannot be empty')
31
-
32
- def validate_dataclass(config_obj, schema, errors):
33
- for f in fields(schema):
34
- name = f.name
35
- expected_type = f.type
36
- value = deep_get_attr(config_obj, name)
37
-
38
- validate_type(name, value, expected_type, errors)
39
-
40
- if expected_type is str:
41
- validate_nonempty(name, value, errors)
42
-
43
- if expected_type in (int, float):
44
- validate_positive(name, value, errors)
45
-
46
- def validate_global_config(config, errors):
47
- if config.server_port <= 0 or config.server_port > 65535:
48
- errors.append(f'server_port: must be 1–65535 (got {config.server_port})')
49
-
50
- if config.bridge_port <= 0 or config.bridge_port > 65535:
51
- errors.append(f'bridge_port: must be 1–65535 (got {config.bridge_port})')
52
-
53
- def validate_watcher_config(watcherconfig, errors):
54
- if watcherconfig.restart_wait_seconds < 1:
55
- errors.append(f'restart_wait_seconds: must be >= 1 (got {watcherconfig.restart_wait_seconds})')
56
-
57
- if watcherconfig.threshold_cpu <= 0:
58
- errors.append(f'threshold_cpu: must not be less than 1 (got {watcherconfig.threshold_cpu})')
59
-
60
- if watcherconfig.threshold_ram <= 0:
61
- errors.append(f'threshold_ram: must be > 0 (got {watcherconfig.threshold_ram})')
62
-
63
- if watcherconfig.threshold_tps <= 0 or watcherconfig.threshold_tps > 20:
64
- errors.append(f'threshold_tps: must be 1–20 (got {watcherconfig.threshold_tps})')
65
-
66
- def validate_messages_config(messages, errors):
67
- for f in fields(MessagesConfig):
68
- value = getattr(messages, f.name)
69
- if isinstance(value, str) and '{prefix}' not in value:
70
- pass
71
-
72
-
73
- # fallback detection
74
- def ensure_no_global_defaults(config, defaults):
75
- fb = config.fallbacks
76
-
77
- if config.panel_url == fb.panel_url:
78
- defaults.append('panel_url')
79
-
80
- if config.panel_api_key == fb.panel_api_key:
81
- defaults.append('panel_api_key')
82
-
83
- if config.origin_server_id == fb.origin_server_id:
84
- defaults.append('origin_server_id')
85
-
86
- if config.server_id == fb.server_id:
87
- defaults.append('server_id')
88
-
89
- if config.server_domain == fb.server_domain:
90
- defaults.append('server_domain')
91
-
92
- if config.bridge_token == fb.bridge_token:
93
- defaults.append('bridge_token')
94
-
95
- def ensure_no_watcher_defaults(watcherconfig, defaults):
96
- fb = watcherconfig.fallbacks
97
-
98
- if watcherconfig.schedule_control and watcherconfig.restart_soon_id == fb.restart_soon_id:
99
- defaults.append('restart_soon_id')
100
-
101
- # main validation entrypoint
102
- def validate_all():
103
- errors = []
104
- defaults = []
105
-
106
- config = loadConfig(GlobalConfig)
107
- messages = loadConfig(MessagesConfig)
108
- watcher = loadConfig(WatcherConfig)
109
-
110
- validate_dataclass(config, GlobalConfig, errors)
111
- validate_dataclass(messages, MessagesConfig, errors)
112
- validate_dataclass(watcher, WatcherConfig, errors)
113
-
114
- validate_global_config(config, errors)
115
- validate_messages_config(messages, errors)
116
- validate_watcher_config(watcher, errors)
117
-
118
- ensure_no_global_defaults(config, defaults)
119
- ensure_no_watcher_defaults(watcher, defaults)
120
-
121
- if len(defaults) >= 5:
122
- print('❌ CONFIG VALIDATION FAILED:\nIt looks like you haven\'t configured this yet! Please change these defaults:')
123
- for d in defaults:
124
- print(' -', d)
125
- sys.exit(1)
126
-
127
- if errors or defaults:
128
- print('❌ CONFIG VALIDATION FAILED:')
129
- for e in errors:
130
- print(' -', e)
131
- for d in defaults:
132
- print(' -', d, ': must not be left default')
133
- sys.exit(1)
134
-
135
- print('✅ All configs are valid.')
136
-
137
-
138
- if __name__ == '__main__':
139
- validate_all()
File without changes
File without changes
File without changes