forked from I-am-PUID-0/DMB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
285 lines (248 loc) · 11.7 KB
/
main.py
File metadata and controls
285 lines (248 loc) · 11.7 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
from utils.config_loader import CONFIG_MANAGER as config
from utils.global_logger import logger, websocket_manager
from utils import duplicate_cleanup, user_management
from api.api_service import start_fastapi_process
from utils.processes import ProcessHandler
from utils.auto_update import Update
from utils.dependencies import initialize_dependencies
import subprocess, threading, time, tomllib
from time import sleep
from utils.setup import phalanx_setup
def main():
with open("pyproject.toml", "rb") as file:
pyproject = tomllib.load(file)
version = pyproject["tool"]["poetry"]["version"]
ascii_art = f"""
DDDDDDDDDDDDD MMMMMMMM MMMMMMMMBBBBBBBBBBBBBBBBB
D::::::::::::DDD M:::::::M M:::::::MB::::::::::::::::B
D:::::::::::::::DD M::::::::M M::::::::MB::::::BBBBBB:::::B
DDD:::::DDDDD:::::D M:::::::::M M:::::::::MBB:::::B B:::::B
D:::::D D:::::D M::::::::::M M::::::::::M B::::B B:::::B
D:::::D D:::::DM:::::::::::M M:::::::::::M B::::B B:::::B
D:::::D D:::::DM:::::::M::::M M::::M:::::::M B::::BBBBBB:::::B
D:::::D D:::::DM::::::M M::::M M::::M M::::::M B:::::::::::::BB
D:::::D D:::::DM::::::M M::::M::::M M::::::M B::::BBBBBB:::::B
D:::::D D:::::DM::::::M M:::::::M M::::::M B::::B B:::::B
D:::::D D:::::DM::::::M M:::::M M::::::M B::::B B:::::B
D:::::D D:::::D M::::::M MMMMM M::::::M B::::B B:::::B
DDD:::::DDDDD:::::D M::::::M M::::::MBB:::::BBBBBB::::::B
D:::::::::::::::DD M::::::M M::::::MB:::::::::::::::::B
D::::::::::::DDD M::::::M M::::::MB::::::::::::::::B
DDDDDDDDDDDDD MMMMMMMM MMMMMMMMBBBBBBBBBBBBBBBBB
Version: {version}
"""
logger.info(ascii_art.format(version=version) + "\n" + "\n")
process_handler = ProcessHandler(logger)
updater = Update(process_handler)
initialize_dependencies(
process_handler=process_handler,
updater=updater,
websocket_manager=websocket_manager,
logger=logger,
)
if config.get("dmb", {}).get("api_service", {}).get("enabled"):
start_fastapi_process()
try:
user_management.create_system_user()
except Exception as e:
logger.error(f"An error occurred while creating system user: {e}")
process_handler.shutdown(exit_code=1)
try:
dmb_config = config.get("dmb", {})
frontend_config = dmb_config.get("frontend", {})
process_name = frontend_config.get("process_name")
api_config = dmb_config.get("api_service", {})
if frontend_config.get("enabled") and api_config.get("enabled"):
if frontend_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
else:
logger.info(f"{process_name} is disabled. Skipping process start.")
except Exception as e:
logger.error(f"An error occurred in the DMB Frontend setup: {e}")
process_handler.shutdown(exit_code=1)
try:
key = "zurg"
zurg_instances = config.get(key, {}).get("instances", {})
enabled_zurg_instances = [
name for name, instance in zurg_instances.items() if instance.get("enabled")
]
if not enabled_zurg_instances:
logger.info("No Zurg instances are enabled. Skipping Zurg setup.")
else:
for instance_name in enabled_zurg_instances:
instance = zurg_instances[instance_name]
process_name = instance.get("process_name")
if instance.get("auto_update"):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
try:
key = "rclone"
duplicate_cleanup_enabled = config.get("dmb", {}).get("duplicate_cleanup")
rclone_instances = config.get(key, {}).get("instances", {})
enabled_rclone_instances = [
name
for name, instance in rclone_instances.items()
if instance.get("enabled")
]
if not enabled_rclone_instances:
logger.info("No rclone instances are enabled. Skipping rclone setup.")
else:
for instance_name in enabled_rclone_instances:
instance_config = rclone_instances[instance_name]
if mount_name := instance_config.get("mount_name"):
logger.info(
f"Configuring rclone for instance: {instance_name} with mount name: {mount_name}"
)
try:
# if duplicate_cleanup_enabled:
# logger.info(
# f"Duplicate cleanup is enabled for instance: {instance_name}"
# )
# duplicate_cleanup.setup()
process_name = instance_config.get("process_name")
updater.auto_update(process_name, False)
except Exception as e:
logger.error(
f"Error during rclone setup for instance {instance_name}: {e}"
)
raise
else:
raise ValueError(
f"No mount name found for rclone instance: {instance_name}"
)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
try:
plex_debrid_config = config.get("plex_debrid") or {}
cli_debrid_config = config.get("cli_debrid") or {}
cli_battery_config = config.get("cli_battery") or {}
decypharr_config = config.get("decypharr") or {}
phalanx_db_config = config.get("phalanx_db") or {}
postgres_config = config.get("postgres", {})
pgadmin_config = config.get("pgadmin", {})
riven_backend_config = config.get("riven_backend", {})
riven_frontend_config = config.get("riven_frontend", {})
zilean_config = config.get("zilean", {})
if postgres_config.get("enabled"):
try:
process_name = postgres_config.get("process_name")
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if pgadmin_config.get("enabled"):
try:
process_name = pgadmin_config.get("process_name")
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if zilean_config.get("enabled"):
try:
process_name = zilean_config.get("process_name")
if zilean_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if plex_debrid_config.get("enabled"):
try:
process_name = plex_debrid_config.get("process_name")
if plex_debrid_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if phalanx_db_config.get("enabled"):
try:
process_name = phalanx_db_config.get("process_name")
if phalanx_db_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if cli_battery_config.get("enabled"):
try:
process_name = cli_battery_config.get("process_name")
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if cli_debrid_config.get("enabled"):
try:
process_name = cli_debrid_config.get("process_name")
if cli_debrid_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
sleep(10)
if riven_backend_config.get("enabled"):
try:
process_name = riven_backend_config.get("process_name")
if riven_backend_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if riven_frontend_config.get("enabled"):
try:
process_name = riven_frontend_config.get("process_name")
if riven_frontend_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
if decypharr_config.get("enabled"):
try:
process_name = decypharr_config.get("process_name")
if decypharr_config.get("auto_update", False):
updater.auto_update(process_name, True)
else:
updater.auto_update(process_name, False)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
except Exception as e:
logger.error(e)
process_handler.shutdown(exit_code=1)
def healthcheck():
time.sleep(60)
while True:
time.sleep(10)
try:
result = subprocess.run(
["python", "healthcheck.py"], capture_output=True, text=True
)
if result.stderr:
logger.error(result.stderr.strip())
except Exception as e:
logger.error("Error running healthcheck.py: %s", e)
time.sleep(50)
thread = threading.Thread(target=healthcheck, daemon=True)
thread.start()
def perpetual_wait():
stop_event = threading.Event()
stop_event.wait()
perpetual_wait()
if __name__ == "__main__":
main()