def init_env(_database_name = "test_of", _context=None, _data_files=[], _json_schema_folders=[], _uri_handlers={}): """ Initiates the test_broker database :param _context: If set, logs in and adds db_access, auth, session_id and peer_process_id properties :return: """ _data_files += [os.path.join(script_dir, "data_struct.json")] _json_schema_folders = [of_schema_folder()] + _json_schema_folders _uri_handlers.update({"ref": None}) _db_access = init_database(_database_name, _data_files=_data_files, _json_schema_folders=_json_schema_folders, _uri_handlers=_uri_handlers) if _context: _context.db_access = _db_access _context.auth = init_authentication(MongoDBAuthBackend(_context.db_access)) _context.session_id, _context.user = _context.auth.login( {"usernamePassword": {"username": "******", "password": "******"}}) _context.peer_process_id = str(ObjectId())
def before_feature(context, feature): """ Initialisation for all features. :param context: :param feature: :return: """ context.auth = init_authentication(MockupAuthenticationBackend())
def start_broker(): """ Starts the broker; Loads settings, connects to database, registers process and starts the web server. """ global process_id, database_access, address, web_socket_plugin, repository_parent_folder, \ web_config, schema_tools, namespaces, log_to_database_severity, plugins, settings process_id = str(ObjectId()) of.common.logging.callback = log_locally write_srvc_dbg("=====Starting broker=============================") try: _cfg_filename = resolve_config_path() settings = JSONXPath(_cfg_filename) except Exception as e: if os.name == "nt": write_to_log(_data="Error loading settings from " + _cfg_filename, _category=EC_SERVICE, _severity=SEV_FATAL) raise Exception("Error loading settings:" + str(e)) if os.name != "nt": x_logger = logging.FileHandler("/var/log/of.log") of.common.logging.severity = of.common.logging.severity_identifiers.index( settings.get("broker/logging/severityLevel", _default="warning")) log_to_database_severity = of.common.logging.severity_identifiers.index( settings.get("broker/logging/databaseLevel", _default="warning")) write_srvc_dbg("Loaded settings from " + _cfg_filename) # An address is completely neccessary. address = settings.get("broker/address", _default=None) if not address or address == "": write_to_log(_data="Broker cannot start, missing [broker] address setting in configuration file.", _category=EC_SERVICE, _severity=SEV_FATAL) raise Exception("Broker cannot start, missing address.") # TODO: Reorganize. It is likely that almost everything but external database credentials should be stored in the db PROD-105 # Initialize schema tools (of_uri_handler is later replaced by the general one) schema_tools = SchemaTools(_json_schema_folders=[os.path.join(script_dir, "../schemas/namespaces/")], _uri_handlers={"ref": of_uri_handler}) namespaces = CumulativeDict(_default={"schemas": []}) write_srvc_dbg("Load plugin data") # Find the plugin directory _plugins_folder = settings.get_path("broker/pluginsFolder", _default="plugins") # Load all plugin data plugins = CherryPyPlugins(_plugins_folder=_plugins_folder, _schema_tools=schema_tools, _namespaces=namespaces, _process_id=process_id, _no_package_name_override=settings.get("broker/packageNameOverride")) # Plugins may want to load settings or add globals plugins.call_hook("init_broker_scope", _broker_scope=globals(), _settings=settings) write_srvc_dbg("===Register signal handlers===") register_signals(stop_broker) plugins.call_hook("before_db_connect", _broker_scope=globals()) # Connect to the database _host = settings.get("broker/database/host", _default="127.0.0.1") _user = settings.get("broker/database/username", _default=None) _password = settings.get("broker/database/password", _default=None) if _user: write_srvc_dbg("===Connecting to remote MongoDB backend " + _host + "===") # http://api.mongodb.org/python/current/examples/authentication.html _client = MongoClient("mongodb://" + _user + ":" + _password + "@" + _host) else: write_srvc_dbg("===Connecting to local MongoDB backend===") _client = MongoClient() _database_name = settings.get("broker/database/databaseName", _default="optimalframework") write_srvc_dbg("Using database name :" + _database_name) _database = _client[_database_name] database_access = DatabaseAccess(_database=_database, _schema_tools=schema_tools) of.common.logging.callback = log_to_database database_access.save(store_process_system_document(_process_id=process_id, _name="Broker instance(" + address + ")"), _user=None, _allow_save_id=True) plugins.call_hook("after_db_connect", _broker_scope=globals()) # TODO: It is possible that one would like to initialize, or at least read the plugins *before* trying to connect to the database # Must have a valid CherryPy version if hasattr(cherrypy.engine, "subscribe"): # CherryPy >= 3.1 pass else: write_to_log(_data="This application requires CherryPy >= 3.1 or higher.", _category=EC_SERVICE, _severity=SEV_FATAL) raise Exception("Broker init: This application requires CherryPy >= 3.1 or higher.") # cherrypy.engine.on_stop_engine_list.append(_save_data) def ssl_path(): # Figure out the path to the ssl-certificates # TODO: Load from database instead. Or not? (PROD-19) return os.path.dirname(_cfg_filename) # Initialize CherryPy:s global configuration; note that this could be moved into a configuration file cherrypy.config.update({ "tools.encode.on": True, "tools.encode.encoding": "utf-8", "tools.decode.on": True, "tools.trailing_slash.on": True, "tools.staticdir.root": os.path.abspath(os.path.dirname(__file__)), "server.ssl_module": "builtin", # TODO: Remove this when this bug is fixed: # https://bitbucket.org/cherrypy/cherrypy/issue/1341/autoreloader-also-fails-if-six-is-present "engine.autoreload.on": False, 'server.socket_host': '0.0.0.0', "server.ssl_certificate": os.path.join(ssl_path(), "optimalframework_test_cert.pem"), "server.ssl_private_key": os.path.join(ssl_path(), "optimalframework_test_privkey.pem"), "error_page.default": error_message_default }) write_srvc_dbg("Starting CherryPy, ssl at " + os.path.join(ssl_path(), "optimalframework_test_privkey.pem")) web_config = { # The UI root "/": { "tools.staticdir.on": True, "tools.staticdir.dir": "ui", "tools.trailing_slash.on": True, "tools.staticdir.index": "index.html", }, # Note that the web socket handling is put under /socket. "/socket": { "tools.websocket.on": True, "tools.websocket.handler_cls": BrokerWebSocket } } global web_root cherrypy._global_conf_alias.update(web_config) web_socket_plugin = WebSocketPlugin(cherrypy.engine) web_socket_plugin.subscribe() cherrypy.tools.websocket = WebSocketTool() cherrypy.engine.signals.bus.signal_handler.handlers = {'SIGUSR1': cherrypy.engine.signals.bus.graceful} # Initialize the decorator-based authentication framework init_authentication(MongoDBAuthBackend(database_access)) # Initialize root UI web_root = CherryPyBroker(_process_id=process_id, _address=address, _database_access=database_access) # Initialize messaging of.common.messaging.websocket.monitor = Monitor(_handler=BrokerWebSocketHandler(process_id, _peers=web_root.peers, _database_access=database_access, _schema_tools=database_access.schema_tools, _address=address)) web_root.plugins = plugins # Generate the static content, initialisation plugins.call_hook("init_web", _broker_scope = globals()) _web_config_debug = "Broker configured. Starting web server. Web config:\n" for _curr_key, _curr_config in web_config.items(): if "tools.staticdir.dir" in _curr_config: _web_config_debug += "Path: " + _curr_key + " directory: " + _curr_config["tools.staticdir.dir"] else: _web_config_debug += "Path: " + _curr_key + " - no static dir" write_to_log(_web_config_debug, _category=EC_SERVICE, _severity=SEV_INFO) plugins.call_hook("pre_webserver_start", web_config=web_config, globals=globals()) cherrypy.quickstart(web_root, "/", web_config)
def __init__(self): self._reset_database() init_authentication(MongoDBAuthBackend(self._database_access)) self.node = CherryPyNode(self._database_access)