def load_directory_with_handlers(module_prefix, bot_handler_file): if bot_handler_file.endswith("_bot_handler.py"): bot_handler_module_name = bot_handler_file.replace(".py", "") logger.info("loading bot handler %s", bot_handler_module_name) module = "{}.{}".format(module_prefix, bot_handler_module_name) if module not in sys.modules: __import__(module)
def send_single_message(self, content, settings): logger.info("seding single message to google chat") content = self.__normalize_content(content) first_message = content.pop(0) threads = self.__post_message(first_message, [], settings) self.__send_to_thread(deepcopy(threads), content, settings)
def init_thread(self, content, settings): logger.info("creating a new thread in google chat") content = self.__normalize_content(content) first_message = content.pop(0) threads = self.__post_message(first_message, [], settings) self.__send_to_thread(threads, content, settings) return threads
def append_notification_implementation(implementation): """ Append a new implementation """ if implementation.__name__ not in [ notification_method.__class__.__name__ for notification_method in NOTIFICATION_METHODS ]: logger.info("appending implementation %s", implementation.__name__) NOTIFICATION_METHODS.append(implementation())
def declare_implementation(repository, implementation): if repository in IMPLEMENTATIONS: logger.warning("overwriting implementation for respository %s", repository) logger.info( "loading implementation %s for repository %s", implementation.__name__, repository, ) IMPLEMENTATIONS[repository] = implementation()
def load_validations(): """ Load validations from application path """ sys.path.append(LIFEGUARD_DIRECTORY) for validation_file in os.listdir(os.path.join(LIFEGUARD_DIRECTORY, "validations")): if validation_file.endswith("_validation.py"): validation_module_name = validation_file.replace(".py", "") logger.info("loading validation %s", validation_module_name) module = "validations.%s" % (validation_module_name) if module not in sys.modules: __import__(module)
def load_custom_controllers(): """ Load custom controllers from application path """ sys.path.append(LIFEGUARD_DIRECTORY) if not os.path.exists(os.path.join(LIFEGUARD_DIRECTORY, "controllers")): return for controller_file in os.listdir( os.path.join(LIFEGUARD_DIRECTORY, "controllers")): if controller_file.endswith("_controller.py"): controller_module_name = controller_file.replace(".py", "") logger.info("loading custom controller %s", controller_module_name) module = "controllers.%s" % (controller_module_name) if module not in sys.modules: __import__(module)
def send_email(validation_response, settings): """ Send email action """ if validation_response.status in settings["email"].get( "remove_from_sent_list_when", [NORMAL] ) and (validation_response.validation_name in EMAIL_NOTIFICATIONS): EMAIL_NOTIFICATIONS.pop(validation_response.validation_name) if ( validation_response.status in settings["email"].get("send_in", [PROBLEM]) and validation_response.validation_name not in EMAIL_NOTIFICATIONS ): EMAIL_NOTIFICATIONS[validation_response.validation_name] = { "sent_at": datetime.now() } message = Message( settings["email"]["receivers"], settings["email"]["subject"], str(validation_response.details), LIFEGUARD_CONTEXT.alert_email_template, ) emails = [receiver["email"] for receiver in settings["email"]["receivers"]] logger.info("sending email %s to %s", message.render(), emails) try: session = smtplib.SMTP( LIFEGUARD_EMAIL_SMTP_SERVER, port=LIFEGUARD_EMAIL_SMTP_PORT ) session.starttls() session.login(LIFEGUARD_EMAIL_SMTP_USER, LIFEGUARD_EMAIL_SMTP_PASSWD) session.sendmail(LIFEGUARD_EMAIL_SMTP_USER, emails, message.render()) session.quit() except: logger.error( "error on send email %s to %s", message.render(), emails, extra={"traceback": traceback.format_exc()}, )
def recover_settings(): """ Get settings.py from root of project """ logger.info("lifeguard directory %s", LIFEGUARD_DIRECTORY) sys.path.append(LIFEGUARD_DIRECTORY) logger.info("path: %s", sys.path) lifeguard_settings = "lifeguard_settings" logger.info("loading %s", lifeguard_settings) if importlib.util.find_spec(lifeguard_settings) is None: logger.error("lifeguard_settings.py not found!!!") sys.exit(-1) return __import__(lifeguard_settings)
def wrapped(*args, **kwargs): try: if LIFEGUARD_RUN_ONLY_VALIDATIONS and ( decorated.__name__ not in LIFEGUARD_RUN_ONLY_VALIDATIONS ): logger.info( "validation %s not in LIFEGUARD_RUN_ONLY_VALIDATIONS", decorated.__name__, ) return None if LIFEGUARD_SKIP_VALIDATIONS and ( decorated.__name__ in LIFEGUARD_SKIP_VALIDATIONS ): logger.info( "validation %s in LIFEGUARD_SKIP_VALIDATIONS", decorated.__name__, ) return None result = decorated(*args, **kwargs) for action in actions or []: logger.info( "executing action %s with result %s...", str(action.__name__), str(result), ) action(result, settings) return result except Exception as exception: logger.warning( "validation error %s: %s", str(decorated.__name__), str(exception), extra={"traceback": traceback.format_exc()}, )
def init(lifeguard_context): newpid = os.fork() if newpid == 0: logger.info("starting telegram process") LifeguardTelegramPlugin(lifeguard_context)
def close_thread(self, threads, content, settings): logger.info("closing thread %s in google chat", threads) self.__send_to_thread(threads, content, settings)
def __log_response(response): if GOOGLE_LOG_RESPONSE: logger.info("google api response: %s", response)