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 backend.lib.manager import WorkerManager
12from common.lib.logger import Logger
13
14from common.config_manager import config
15
16def run(as_daemon=True, log_level="INFO"):
17	pidfile = Path(config.get('PATH_ROOT'), config.get('PATH_LOCKFILE'), "4cat.pid")
18
19	if as_daemon:
20		with pidfile.open("w") as outfile:
21			outfile.write(str(os.getpid()))
22
23	else:
24		indent_spaces = round(shutil.get_terminal_size().columns / 2) - 33
25		indent = "".join([" " for i in range(0, indent_spaces)]) if indent_spaces > 0 else ""
26		print("\n\n")
27		print(indent + "+---------------------------------------------------------------+")
28		print(indent + "|                                                               |")
29		print(indent + "|                           welcome to                          |")
30		print(indent + "|                                                               |")
31		print(indent + "|                  j88D   .o88b.  .d8b.  d888888b               |")
32		print(indent + "|                 j8~88  d8P  Y8 d8' `8b `~~88~~'               |")
33		print(indent + "|                j8' 88  8P      88ooo88    88                  |")
34		print(indent + "|                V88888D 8b      88~~~88    88                  |")
35		print(indent + "|                    88  Y8b  d8 88   88    88                  |")
36		print(indent + "|                    VP   `Y88P' YP   YP    YP                  |")
37		print(indent + "|                                                               |")
38		print(indent + "|               4CAT: Capture and Analysis Toolkit              |")
39		print(indent + "|                                                               |")
40		print(indent + "|                                                               |")
41		print(indent + "+---------------------------------------------------------------+")
42		print(indent + "|                   use ctrl + c to shut down                   |")
43		print(indent + "|                                                               |")
44		print(indent + "| WARNING: Not running as a daemon.  Quitting this process will |")
45		print(indent + "|                 shut down the backend as well.                |")
46		print(indent + "+---------------------------------------------------------------+\n\n")
47
48	# load everything
49	if config.get("USING_DOCKER"):
50		as_daemon = True
51		# Rename log if Docker setup
52		log = Logger(output=True, filename='backend_4cat.log', log_level=log_level)
53	else:
54		log = Logger(output=not as_daemon, filename='4cat.log', log_level=log_level)
55
56	log.info("4CAT Backend started, logger initialised")
57	db = Database(logger=log, appname="main",
58				  dbname=config.DB_NAME, user=config.DB_USER, password=config.DB_PASSWORD, host=config.DB_HOST, port=config.DB_PORT)
59	queue = JobQueue(logger=log, database=db)
60
61	# clean up after ourselves
62	db.commit()
63	queue.release_all()
64
65	# ensure database consistency for settings table
66	config.with_db(db)
67	config.ensure_database()
68
69	# make it happen
70	# this is blocking until the back-end is shut down
71	WorkerManager(logger=log, database=db, queue=queue, as_daemon=as_daemon)
72
73	# clean up pidfile, if running as daemon
74	if as_daemon:
75		if pidfile.exists():
76			pidfile.unlink()
77
78	log.info("4CAT Backend shut down.")
def run(as_daemon=True, log_level='INFO'):
17def run(as_daemon=True, log_level="INFO"):
18	pidfile = Path(config.get('PATH_ROOT'), config.get('PATH_LOCKFILE'), "4cat.pid")
19
20	if as_daemon:
21		with pidfile.open("w") as outfile:
22			outfile.write(str(os.getpid()))
23
24	else:
25		indent_spaces = round(shutil.get_terminal_size().columns / 2) - 33
26		indent = "".join([" " for i in range(0, indent_spaces)]) if indent_spaces > 0 else ""
27		print("\n\n")
28		print(indent + "+---------------------------------------------------------------+")
29		print(indent + "|                                                               |")
30		print(indent + "|                           welcome to                          |")
31		print(indent + "|                                                               |")
32		print(indent + "|                  j88D   .o88b.  .d8b.  d888888b               |")
33		print(indent + "|                 j8~88  d8P  Y8 d8' `8b `~~88~~'               |")
34		print(indent + "|                j8' 88  8P      88ooo88    88                  |")
35		print(indent + "|                V88888D 8b      88~~~88    88                  |")
36		print(indent + "|                    88  Y8b  d8 88   88    88                  |")
37		print(indent + "|                    VP   `Y88P' YP   YP    YP                  |")
38		print(indent + "|                                                               |")
39		print(indent + "|               4CAT: Capture and Analysis Toolkit              |")
40		print(indent + "|                                                               |")
41		print(indent + "|                                                               |")
42		print(indent + "+---------------------------------------------------------------+")
43		print(indent + "|                   use ctrl + c to shut down                   |")
44		print(indent + "|                                                               |")
45		print(indent + "| WARNING: Not running as a daemon.  Quitting this process will |")
46		print(indent + "|                 shut down the backend as well.                |")
47		print(indent + "+---------------------------------------------------------------+\n\n")
48
49	# load everything
50	if config.get("USING_DOCKER"):
51		as_daemon = True
52		# Rename log if Docker setup
53		log = Logger(output=True, filename='backend_4cat.log', log_level=log_level)
54	else:
55		log = Logger(output=not as_daemon, filename='4cat.log', log_level=log_level)
56
57	log.info("4CAT Backend started, logger initialised")
58	db = Database(logger=log, appname="main",
59				  dbname=config.DB_NAME, user=config.DB_USER, password=config.DB_PASSWORD, host=config.DB_HOST, port=config.DB_PORT)
60	queue = JobQueue(logger=log, database=db)
61
62	# clean up after ourselves
63	db.commit()
64	queue.release_all()
65
66	# ensure database consistency for settings table
67	config.with_db(db)
68	config.ensure_database()
69
70	# make it happen
71	# this is blocking until the back-end is shut down
72	WorkerManager(logger=log, database=db, queue=queue, as_daemon=as_daemon)
73
74	# clean up pidfile, if running as daemon
75	if as_daemon:
76		if pidfile.exists():
77			pidfile.unlink()
78
79	log.info("4CAT Backend shut down.")