def is_server_running(port): pid = daemonize.is_server_running(port) if pid is not None: logger.warn("Faraday Server is already running. PID: {}".format(pid)) return True else: return False
def is_server_running(port): pid = daemonize.is_server_running(port) if pid is not None: logger.warn("Faraday Server is already running. PID: {}".format(pid)) return True else: return False
def check_postgresql(): with app.app_context(): try: if not db.session.query(Workspace).count(): logger.warn('No workspaces found. Remember to execute CouchDB importer') except sqlalchemy.exc.ArgumentError: logger.error( '\n\b{RED}Please check your PostgreSQL connection string in the file ~/.faraday/config/server.ini on your home directory.{WHITE} \n'.format(RED=Fore.RED, WHITE=Fore.WHITE) ) sys.exit(1) except sqlalchemy.exc.OperationalError: logger.error( '\n\n{RED}Could not connect to PostgreSQL.\n{WHITE}Please check: \n{YELLOW} * if database is running \n * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} python manage.py initdb\n\n'.format(GREEN=Fore.GREEN, YELLOW=Fore.YELLOW, WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1)
def check_postgresql(): with app.app_context(): try: if not db.session.query(Workspace).count(): logger.warn('No workspaces found. Remeber to execute couchdb importer') except sqlalchemy.exc.ArgumentError: logger.error( '\n\b{RED}Please check you postgresql connection string in server.ini at .faraday on your ohme directory.{WHITE} \n'.format(RED=Fore.RED, WHITE=Fore.WHITE) ) sys.exit(1) except sqlalchemy.exc.OperationalError: logger.error( '\n\n{RED}Could not connect to postgresql.\n{WHITE}Please check: \n{YELLOW} * if database is running \n * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} python manage.py initdb\n\n'.format(GREEN=Fore.GREEN, YELLOW=Fore.YELLOW, WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1)
def run(self): def signal_handler(*args): logger.info('Received SIGTERM, shutting down.') logger.info("Stopping threads, please wait...") # teardown() self.raw_report_processor.stop() site = twisted.web.server.Site(self.__root_resource) if self.__ssl_enabled: ssl_context = self.__load_ssl_certs() self.__listen_func = functools.partial(reactor.listenSSL, contextFactory=ssl_context) else: self.__listen_func = reactor.listenTCP try: self.install_signal() # start threads and processes self.raw_report_processor = RawReportProcessor() self.raw_report_processor.start() # web and static content self.__listen_func(self.__listen_port, site, interface=self.__bind_address) # websockets try: listenWS(self.__build_websockets_resource(), interface=self.__bind_address) except: logger.warn( 'Could not start websockets, address already open. This is ok is you wan to run multiple instances.' ) logger.info('Faraday Server is ready') reactor.addSystemEventTrigger('before', 'shutdown', signal_handler) reactor.run() except error.CannotListenError as e: logger.error(str(e)) sys.exit(1) except Exception as e: logger.error( 'Something went wrong when trying to setup the Web UI') logger.exception(e) sys.exit(1)
def run(self): def signal_handler(*args): logger.info("Stopping threads, please wait...") # teardown() self.raw_report_processor.stop() reactor.stop() site = twisted.web.server.Site(self.__root_resource) if self.__ssl_enabled: ssl_context = self.__load_ssl_certs() self.__listen_func = functools.partial( reactor.listenSSL, contextFactory=ssl_context) else: self.__listen_func = reactor.listenTCP try: self.install_signal() # start threads and processes self.raw_report_processor = RawReportProcessor() self.raw_report_processor.start() # web and static content self.__listen_func( self.__listen_port, site, interface=self.__bind_address) # websockets try: listenWS(self.__build_websockets_resource(), interface=self.__bind_address) except : logger.warn('Could not start websockets, address already open. This is ok is you wan to run multiple instances.') logger.info('Faraday Server is ready') reactor.addSystemEventTrigger('before', 'shutdown', signal_handler) reactor.run() except error.CannotListenError as e: logger.error(str(e)) sys.exit(1) except Exception as e: logger.error('Something went wrong when trying to setup the Web UI') logger.exception(e) sys.exit(1)
def _get_order_field(self, **kwargs): try: order_field = flask.request.args[self.sort_field_paremeter_name] except KeyError: # Sort field not specified, return the default return self.order_field # Check that the field is in the schema to prevent unwanted fields # value leaking schema = self._get_schema_instance(kwargs) # Add metadata nested field try: metadata_field = schema.fields.pop('metadata') except KeyError: pass else: for (key, value) in metadata_field.target_schema.fields.items(): schema.fields['metadata.' + key] = value schema.fields[key] = value try: field_instance = schema.fields[order_field] except KeyError: if self.sort_pass_silently: logger.warn("Unknown field: %s" % order_field) return self.order_field raise InvalidUsage("Unknown field: %s" % order_field) # Translate from the field name in the schema to the database field # name order_field = field_instance.attribute or order_field # TODO migration: improve this checking or use a whitelist. # Handle PrimaryKeyRelatedField model_class = self.sort_model_class or self.model_class if order_field not in inspect(model_class).attrs: if self.sort_pass_silently: logger.warn("Field not in the DB: %s" % order_field) return self.order_field # It could be something like fields.Method raise InvalidUsage("Field not in the DB: %s" % order_field) if hasattr(model_class, order_field + '_id'): # Ugly hack to allow sorting by a parent field = getattr(model_class, order_field + '_id') else: field = getattr(model_class, order_field) sort_dir = flask.request.args.get(self.sort_direction_paremeter_name, self.default_sort_direction) if sort_dir not in ('asc', 'desc'): if self.sort_pass_silently: logger.warn("Invalid value for sorting direction: %s" % sort_dir) return self.order_field raise InvalidUsage("Invalid value for sorting direction: %s" % sort_dir) try: return getattr(field, sort_dir)() except NotImplementedError: if self.sort_pass_silently: logger.warn("field {} doesn't support sorting".format( order_field )) return self.order_field # There are some fields that can't be used for sorting raise InvalidUsage("field {} doesn't support sorting".format( order_field ))
def import_item(self, config_key=None, class_name=None, is_dictionary=False, is_multiple=False, item_value=None, ignore_errors=False, is_module=False): if item_value is None: conf_value = getattr(self.config, config_key) else: conf_value = item_value if is_module: modules = [] if conf_value: for module_name in conf_value: try: module = __import__(module_name) if '.' in module_name: module = reduce(getattr, module_name.split('.')[1:], module) for name in dir(module): obj = getattr(module, name) if inspect.isclass(obj) and type(obj) == type: modules.append(obj) except ImportError as e: if ignore_errors: logger.warn('Module %s could not be imported: %s', module_name, e) else: raise setattr(self, config_key.lower(), tuple(modules)) elif is_dictionary: modules = {} if conf_value: for key, module_name in conf_value.items(): try: module = self.import_class(module_name) modules[key] = (module) except ImportError as e: if ignore_errors: logger.warn('Module %s could not be imported: %s', module_name, e) else: raise setattr(self, config_key.lower(), dict(modules)) elif is_multiple: modules = [] if conf_value: for module_name in conf_value: try: if class_name is not None: module = self.import_class( '%s.%s' % (module_name, class_name)) else: module = self.import_class(module_name, get_module=True) modules.append(module) except ImportError as e: if ignore_errors: logger.warn('Module %s could not be imported: %s', module_name, e) else: raise setattr(self, config_key.lower(), tuple(modules)) else: if class_name is not None: module = self.import_class('%s.%s' % (conf_value, class_name)) else: module = self.import_class(conf_value, get_module=True) setattr(self, config_key.lower(), module)
def _get_order_field(self, **kwargs): try: order_field = flask.request.args[self.sort_field_paremeter_name] except KeyError: # Sort field not specified, return the default return self.order_field # Check that the field is in the schema to prevent unwanted fields # value leaking schema = self._get_schema_instance(kwargs) # Add metadata nested field try: metadata_field = schema.fields.pop('metadata') except KeyError: pass else: for (key, value) in metadata_field.target_schema.fields.items(): schema.fields['metadata.' + key] = value schema.fields[key] = value try: field_instance = schema.fields[order_field] except KeyError: if self.sort_pass_silently: logger.warn("Unknown field: %s" % order_field) return self.order_field raise InvalidUsage("Unknown field: %s" % order_field) # Translate from the field name in the schema to the database field # name order_field = field_instance.attribute or order_field # TODO migration: improve this checking or use a whitelist. # Handle PrimaryKeyRelatedField model_class = self.sort_model_class or self.model_class if order_field not in inspect(model_class).attrs: if self.sort_pass_silently: logger.warn("Field not in the DB: %s" % order_field) return self.order_field # It could be something like fields.Method raise InvalidUsage("Field not in the DB: %s" % order_field) if hasattr(model_class, order_field + '_id'): # Ugly hack to allow sorting by a parent field = getattr(model_class, order_field + '_id') else: field = getattr(model_class, order_field) sort_dir = flask.request.args.get(self.sort_direction_paremeter_name, self.default_sort_direction) if sort_dir not in ('asc', 'desc'): if self.sort_pass_silently: logger.warn("Invalid value for sorting direction: %s" % sort_dir) return self.order_field raise InvalidUsage("Invalid value for sorting direction: %s" % sort_dir) try: return getattr(field, sort_dir)() except NotImplementedError: if self.sort_pass_silently: logger.warn("field {} doesn't support sorting".format( order_field )) return self.order_field # There are some fields that can't be used for sorting raise InvalidUsage("field {} doesn't support sorting".format( order_field ))