Edit on GitHub

backend.bootstrap

4CAT Backend init - used to start the backend!

 1"""
 24CAT Backend init - used to start the backend!
 3"""
 4import shutil
 5import os
 6
 7from pathlib import Path
 8
 9from common.lib.queue import JobQueue
10from common.lib.database import Database
11from common.lib.module_loader import ModuleCollector
12from backend.lib.manager import WorkerManager
13from common.lib.logger import Logger
14from common.config_manager import ConfigManager
15
16def run(as_daemon=True, log_level="INFO"):
17	# initialise configuration reader
18	config = ConfigManager()
19
20	pidfile = Path(config.get('PATH_ROOT'), config.get('PATH_LOCKFILE'), "4cat.pid")
21
22	if as_daemon:
23		with pidfile.open("w") as outfile:
24			outfile.write(str(os.getpid()))
25
26	else:
27		indent_spaces = round(shutil.get_terminal_size().columns / 2) - 33
28		indent = "".join([" " for i in range(0, indent_spaces)]) if indent_spaces > 0 else ""
29		print("\n\n")
30		print(indent + "+---------------------------------------------------------------+")
31		print(indent + "|                                                               |")
32		print(indent + "|                           welcome to                          |")
33		print(indent + "|                                                               |")
34		print(indent + "|                  j88D   .o88b.  .d8b.  d888888b               |")
35		print(indent + "|                 j8~88  d8P  Y8 d8' `8b `~~88~~'               |")
36		print(indent + "|                j8' 88  8P      88ooo88    88                  |")
37		print(indent + "|                V88888D 8b      88~~~88    88                  |")
38		print(indent + "|                    88  Y8b  d8 88   88    88                  |")
39		print(indent + "|                    VP   `Y88P' YP   YP    YP                  |")
40		print(indent + "|                                                               |")
41		print(indent + "|               4CAT: Capture and Analysis Toolkit              |")
42		print(indent + "|                                                               |")
43		print(indent + "|                                                               |")
44		print(indent + "+---------------------------------------------------------------+")
45		print(indent + "|                   use ctrl + c to shut down                   |")
46		print(indent + "|                                                               |")
47		print(indent + "| WARNING: Not running as a daemon.  Quitting this process will |")
48		print(indent + "|                 shut down the backend as well.                |")
49		print(indent + "+---------------------------------------------------------------+\n\n")
50
51	# load everything
52	log_folder = config.get('PATH_ROOT').joinpath(config.get('PATH_LOGS'))
53	if config.get("USING_DOCKER"):
54		as_daemon = True
55		# Rename log if Docker setup
56		log = Logger(output=True, log_path=log_folder.joinpath("backend_4cat.log"), log_level=log_level)
57	else:
58		log = Logger(output=not as_daemon, log_path=log_folder.joinpath("backend_4cat.log"), log_level=log_level)
59
60	log.info("4CAT Backend started, logger initialised")
61	db = Database(logger=log, appname="main",
62				  dbname=config.DB_NAME, user=config.DB_USER, password=config.DB_PASSWORD, host=config.DB_HOST, port=config.DB_PORT)
63	queue = JobQueue(logger=log, database=db)
64
65	# clean up after ourselves
66	db.commit()
67	queue.release_all()
68
69	# test memcache and clear upon backend restart
70	if config.get("MEMCACHE_SERVER"):
71		if config.memcache:
72			log.debug("Memcache connection initialized - clearing")
73			config.clear_cache()
74		else:
75			log.warning(f"Memcache server address configured, but connection could not be initialized at "
76						f"{config.get('MEMCACHE_SERVER')}. Back-end configuration cache inactive.")
77
78	# ensure database consistency for settings table
79	config.with_db(db)
80	config.ensure_database()
81
82	log.load_webhook(config)
83
84	# load 4CAT modules and cache the results
85	modules = ModuleCollector(config=config, write_cache=True)
86
87	# make it happen
88	# this is blocking until the back-end is shut down
89	WorkerManager(logger=log, database=db, queue=queue, modules=modules, as_daemon=as_daemon)
90
91	# clean up pidfile, if running as daemon
92	if as_daemon:
93		if pidfile.exists():
94			pidfile.unlink()
95
96	log.info("4CAT Backend shut down.")
def run(as_daemon=True, log_level='INFO'):
17def run(as_daemon=True, log_level="INFO"):
18	# initialise configuration reader
19	config = ConfigManager()
20
21	pidfile = Path(config.get('PATH_ROOT'), config.get('PATH_LOCKFILE'), "4cat.pid")
22
23	if as_daemon:
24		with pidfile.open("w") as outfile:
25			outfile.write(str(os.getpid()))
26
27	else:
28		indent_spaces = round(shutil.get_terminal_size().columns / 2) - 33
29		indent = "".join([" " for i in range(0, indent_spaces)]) if indent_spaces > 0 else ""
30		print("\n\n")
31		print(indent + "+---------------------------------------------------------------+")
32		print(indent + "|                                                               |")
33		print(indent + "|                           welcome to                          |")
34		print(indent + "|                                                               |")
35		print(indent + "|                  j88D   .o88b.  .d8b.  d888888b               |")
36		print(indent + "|                 j8~88  d8P  Y8 d8' `8b `~~88~~'               |")
37		print(indent + "|                j8' 88  8P      88ooo88    88                  |")
38		print(indent + "|                V88888D 8b      88~~~88    88                  |")
39		print(indent + "|                    88  Y8b  d8 88   88    88                  |")
40		print(indent + "|                    VP   `Y88P' YP   YP    YP                  |")
41		print(indent + "|                                                               |")
42		print(indent + "|               4CAT: Capture and Analysis Toolkit              |")
43		print(indent + "|                                                               |")
44		print(indent + "|                                                               |")
45		print(indent + "+---------------------------------------------------------------+")
46		print(indent + "|                   use ctrl + c to shut down                   |")
47		print(indent + "|                                                               |")
48		print(indent + "| WARNING: Not running as a daemon.  Quitting this process will |")
49		print(indent + "|                 shut down the backend as well.                |")
50		print(indent + "+---------------------------------------------------------------+\n\n")
51
52	# load everything
53	log_folder = config.get('PATH_ROOT').joinpath(config.get('PATH_LOGS'))
54	if config.get("USING_DOCKER"):
55		as_daemon = True
56		# Rename log if Docker setup
57		log = Logger(output=True, log_path=log_folder.joinpath("backend_4cat.log"), log_level=log_level)
58	else:
59		log = Logger(output=not as_daemon, log_path=log_folder.joinpath("backend_4cat.log"), log_level=log_level)
60
61	log.info("4CAT Backend started, logger initialised")
62	db = Database(logger=log, appname="main",
63				  dbname=config.DB_NAME, user=config.DB_USER, password=config.DB_PASSWORD, host=config.DB_HOST, port=config.DB_PORT)
64	queue = JobQueue(logger=log, database=db)
65
66	# clean up after ourselves
67	db.commit()
68	queue.release_all()
69
70	# test memcache and clear upon backend restart
71	if config.get("MEMCACHE_SERVER"):
72		if config.memcache:
73			log.debug("Memcache connection initialized - clearing")
74			config.clear_cache()
75		else:
76			log.warning(f"Memcache server address configured, but connection could not be initialized at "
77						f"{config.get('MEMCACHE_SERVER')}. Back-end configuration cache inactive.")
78
79	# ensure database consistency for settings table
80	config.with_db(db)
81	config.ensure_database()
82
83	log.load_webhook(config)
84
85	# load 4CAT modules and cache the results
86	modules = ModuleCollector(config=config, write_cache=True)
87
88	# make it happen
89	# this is blocking until the back-end is shut down
90	WorkerManager(logger=log, database=db, queue=queue, modules=modules, as_daemon=as_daemon)
91
92	# clean up pidfile, if running as daemon
93	if as_daemon:
94		if pidfile.exists():
95			pidfile.unlink()
96
97	log.info("4CAT Backend shut down.")