class Main(): """TODO: Doku.""" def __init__(self) -> None: """TODO: Doku.""" self.cfg = Config(CONFIG_PATH) self.cfg_log = Config(LOG_CONFIG_PATH) loglevel = self.cfg_log.get_value("LOG", "level") to_stdout = self.cfg_log.get_value("LOG", "to_stdout") to_files = self.cfg_log.get_value("LOG", "to_files") logpath = self.cfg_log.get_value("LOG", "filepath") logfile = self.cfg_log.get_value("LOG", "filename") self.log = Log(str(loglevel), bool(to_stdout), bool(to_files), str(logpath), str(logfile)) info("starting easywall") self.easywall = Easywall(self.cfg) self.event_handler = ModifiedHandler(self.apply) self.observer = Observer() self.stop_flag = False info("easywall has been started") def apply(self, filename: str) -> None: """TODO: Doku.""" info("starting apply process from easywall") delete_file_if_exists(filename) self.easywall.apply() def start_observer(self) -> None: """ Keep the main process running until it should be stopped. if someone is pressing ctrl + C the software will initiate the stop process """ self.observer.schedule(self.event_handler, ".") self.observer.start() try: while not self.stop_flag: sleep(2) except KeyboardInterrupt: info("KeyboardInterrupt received, starting shutdown") finally: self.shutdown() def shutdown(self) -> None: """Stop all threads and shut the software down gracefully.""" info("starting shutdown") self.observer.stop() delete_file_if_exists(".acceptance") self.observer.join() info("shutdown completed") self.log.close_logging()
def shutdown(observer: Observer, config: Config, log: Log): """ the function stops all threads and shuts the software down gracefully """ info("starting shutdown...") observer.stop() delete_file_if_exists(".running") delete_file_if_exists(config.get_value("ACCEPTANCE", "filename")) observer.join() info("shutdown successfully completed") log.close_logging() exit(0)
class TestLog(unittest.TestCase): """ this class contains all test functions for the log module """ def setUp(self): self.log = Log("INFO", True, True, ".", "test.log") def tearDown(self): delete_file_if_exists("test.log") self.log.close_logging() def test_correct_level_debug(self): self.assertEqual(self.log.correct_level("DEBUG"), DEBUG) def test_correct_level_info(self): self.assertEqual(self.log.correct_level("INFO"), INFO) def test_correct_level_warning(self): self.assertEqual(self.log.correct_level("WARNING"), WARNING) def test_correct_level_error(self): self.assertEqual(self.log.correct_level("ERROR"), ERROR) def test_correct_level_critical(self): self.assertEqual(self.log.correct_level("CRITICAL"), CRITICAL) def test_correct_level_unknown(self): self.assertEqual(self.log.correct_level("SOMETHING"), NOTSET)
def __init__(self, debug: bool = False) -> None: """TODO: Doku.""" self.cfg_log = Config(LOG_CONFIG_PATH) loglevel = self.cfg_log.get_value("LOG", "level") to_stdout = self.cfg_log.get_value("LOG", "to_stdout") to_files = self.cfg_log.get_value("LOG", "to_files") logpath = self.cfg_log.get_value("LOG", "filepath") logfile = self.cfg_log.get_value("LOG", "filename") self.log = Log(str(loglevel), bool(to_stdout), bool(to_files), str(logpath), str(logfile)) info("starting easywall-web") self.cfg = Config(CONFIG_PATH) if debug is True: info("loading Flask debug configuration") APP.config.from_object('easywall.web.__main__.DevelopmentConfig') else: info("loading Flask production configuration") APP.config.from_object('easywall.web.__main__.ProductionConfig') self.rules_handler = RulesHandler() self.login_attempts = self.cfg.get_value("WEB", "login_attempts") self.login_bantime = self.cfg.get_value("WEB", "login_bantime") self.ip_ban = IpBan(app=APP, ban_count=self.login_attempts, ban_seconds=self.login_bantime, ipc=True) self.ip_ban.url_pattern_add('^/static.*$', match_type='regex')
def run(): """this is the main function of the program""" # Startup Process config = Config("config/easywall.ini") loglevel = config.get_value("LOG", "level") to_stdout = config.get_value("LOG", "to_stdout") to_files = config.get_value("LOG", "to_files") logpath = config.get_value("LOG", "filepath") logfile = config.get_value("LOG", "filename") log = Log(loglevel, to_stdout, to_files, logpath, logfile) info("Starting up easywall...") ensure_rules_files(config) event_handler = ModifiedHandler() observer = Observer() observer.schedule(event_handler, config.get_value("RULES", "filepath")) observer.start() info("easywall is up and running.") # waiting for file modifications try: while True: sleep(2) except KeyboardInterrupt: info("KeyboardInterrupt received, starting shutdown") finally: shutdown(observer, config, log)
def __init__(self, debug: bool = False) -> None: self.cfg = Config(CONFIG_PATH) loglevel = self.cfg.get_value("LOG", "level") to_stdout = self.cfg.get_value("LOG", "to_stdout") to_files = self.cfg.get_value("LOG", "to_files") logpath = self.cfg.get_value("LOG", "filepath") logfile = self.cfg.get_value("LOG", "filename") self.log = Log(str(loglevel), bool(to_stdout), bool(to_files), str(logpath), str(logfile)) self.login_attempts = self.cfg.get_value("WEB", "login_attempts") self.login_bantime = self.cfg.get_value("WEB", "login_bantime") self.ip_ban = IpBan(app=APP, ban_count=self.login_attempts, ban_seconds=self.login_bantime, ipc=True) self.ip_ban.url_pattern_add('^/static.*$', match_type='regex') info("starting easywall-web") self.rules_handler = RulesHandler() self.rules_handler.ensure_files_exist() if debug is True: port = self.cfg.get_value("WEB", "bindport") host = self.cfg.get_value("WEB", "bindip") APP.config.from_object('easywall_web.__main__.DevelopmentConfig') APP.run(str(host), str(port)) else: APP.config.from_object('easywall_web.__main__.ProductionConfig')
def __init__(self, debug: bool = False) -> None: self.cfg = Config(CONFIG_PATH) loglevel = self.cfg.get_value("LOG", "level") to_stdout = self.cfg.get_value("LOG", "to_stdout") to_files = self.cfg.get_value("LOG", "to_files") logpath = self.cfg.get_value("LOG", "filepath") logfile = self.cfg.get_value("LOG", "filename") self.log = Log(str(loglevel), bool(to_stdout), bool(to_files), str(logpath), str(logfile)) info("starting easywall-web") self.is_first_run = not folder_exists("rules") self.rules_handler = RulesHandler() if self.is_first_run: self.rules_handler.ensure_files_exist() if debug is True: port = self.cfg.get_value("WEB", "bindport") host = self.cfg.get_value("WEB", "bindip") APP.config.from_object('easywall_web.__main__.DevelopmentConfig') APP.run(str(host), str(port)) else: APP.config.from_object('easywall_web.__main__.ProductionConfig')
def __init__(self) -> None: """TODO: Doku.""" self.cfg = Config(CONFIG_PATH) self.cfg_log = Config(LOG_CONFIG_PATH) loglevel = self.cfg_log.get_value("LOG", "level") to_stdout = self.cfg_log.get_value("LOG", "to_stdout") to_files = self.cfg_log.get_value("LOG", "to_files") logpath = self.cfg_log.get_value("LOG", "filepath") logfile = self.cfg_log.get_value("LOG", "filename") self.log = Log(str(loglevel), bool(to_stdout), bool(to_files), str(logpath), str(logfile)) info("starting easywall") self.easywall = Easywall(self.cfg) self.event_handler = ModifiedHandler(self.apply) self.observer = Observer() self.stop_flag = False info("easywall has been started")
def __init__(self): self.cfg = Config(CONFIG_PATH) loglevel = self.cfg.get_value("LOG", "level") to_stdout = self.cfg.get_value("LOG", "to_stdout") to_files = self.cfg.get_value("LOG", "to_files") logpath = self.cfg.get_value("LOG", "filepath") logfile = self.cfg.get_value("LOG", "filename") self.log = Log(loglevel, to_stdout, to_files, logpath, logfile) info("starting easywall") self.is_first_run = not folder_exists("rules") self.rules_handler = RulesHandler() if self.is_first_run: self.rules_handler.rules_firstrun() self.easywall = Easywall(self.cfg) self.event_handler = ModifiedHandler(self.apply) self.observer = Observer() self.stop_flag = False info("easywall has been started")
def run(): """ this is the first and main function of the program """ config = Config(CONFIG_PATH) loglevel = config.get_value("LOG", "level") to_stdout = config.get_value("LOG", "to_stdout") to_files = config.get_value("LOG", "to_files") logpath = config.get_value("LOG", "filepath") logfile = config.get_value("LOG", "filename") log = Log(loglevel, to_stdout, to_files, logpath, logfile) info("executing startup sequence...") create_rule_files(config) event_handler = ModifiedHandler() observer = Observer() observer.schedule(event_handler, config.get_value("RULES", "filepath")) observer.start() info("startup sequence successfully finished") start_observer(observer, config, log)
def run(): """this is the main function of the program""" # Startup Process masterlog = Log("config/easywall.ini") logging.info("Starting up easywall...") masterconfig = Config("config/easywall.ini") ensure_rules_files(masterconfig) event_handler = ModifiedHandler() observer = Observer() observer.schedule( event_handler, masterconfig.get_value("RULES", "filepath")) observer.start() logging.info("easywall is up and running.") # waiting for file modifications try: while True: time.sleep(2) except KeyboardInterrupt: shutdown(observer, masterconfig, masterlog) shutdown(observer, masterconfig, masterlog)
def __init__(self, debug=False): APP.secret_key = os.urandom(12) self.cfg = Config(CONFIG_PATH) loglevel = self.cfg.get_value("LOG", "level") to_stdout = self.cfg.get_value("LOG", "to_stdout") to_files = self.cfg.get_value("LOG", "to_files") logpath = self.cfg.get_value("LOG", "filepath") logfile = self.cfg.get_value("LOG", "filename") self.log = Log(loglevel, to_stdout, to_files, logpath, logfile) info("starting easywall-web") self.is_first_run = not folder_exists("rules") self.rules_handler = RulesHandler() if self.is_first_run: self.rules_handler.rules_firstrun() if debug is True: port = self.cfg.get_value("WEB", "bindport") host = self.cfg.get_value("WEB", "bindip") APP.run(host, port, debug)
def setUp(self): self.log = Log("INFO", True, True, ".", "test.log")
class Main(object): """ TODO: Doku """ def __init__(self): self.cfg = Config(CONFIG_PATH) loglevel = self.cfg.get_value("LOG", "level") to_stdout = self.cfg.get_value("LOG", "to_stdout") to_files = self.cfg.get_value("LOG", "to_files") logpath = self.cfg.get_value("LOG", "filepath") logfile = self.cfg.get_value("LOG", "filename") self.log = Log(loglevel, to_stdout, to_files, logpath, logfile) info("starting easywall") self.is_first_run = not folder_exists("rules") self.rules_handler = RulesHandler() if self.is_first_run: self.rules_handler.rules_firstrun() self.easywall = Easywall(self.cfg) self.event_handler = ModifiedHandler(self.apply) self.observer = Observer() self.stop_flag = False info("easywall has been started") def apply(self, filename: str) -> None: """ TODO: Doku """ info("starting apply process from easywall") delete_file_if_exists(filename) self.easywall.apply() def start_observer(self) -> None: """ this function is called to keep the main process running if someone is pressing ctrl + C the software will initiate the stop process """ self.observer.schedule(self.event_handler, ".") self.observer.start() try: while not self.stop_flag: sleep(2) except KeyboardInterrupt: info("KeyboardInterrupt received, starting shutdown") finally: self.shutdown() def shutdown(self) -> None: """ the function stops all threads and shuts the software down gracefully """ info("starting shutdown") self.observer.stop() delete_file_if_exists(".acceptance") self.observer.join() info("shutdown completed") self.log.close_logging()
def setUp(self) -> None: delete_file_if_exists("./test.log") self.log = Log("INFO", True, True, ".", "test.log")