Esempio n. 1
0
def init_db():
    # Init DB
    # Check if MongoDB is enabled as the database, otherwise, fallback to TinyDB
    if hxtool_global.hxtool_config.get_child_item('db', 'type') == "mongodb":
        from hxtool_mongodb import hxtool_mongodb

        return hxtool_mongodb(
            hxtool_global.hxtool_config.get_child_item('db', 'host', False),
            hxtool_global.hxtool_config.get_child_item('db', 'port', 27017),
            hxtool_global.hxtool_config.get_child_item('db', 'user', False),
            hxtool_global.hxtool_config.get_child_item('db', 'password',
                                                       False),
            hxtool_global.hxtool_config.get_child_item('db', 'auth_source',
                                                       "admin"),
            hxtool_global.hxtool_config.get_child_item('db', 'auth_mechanism',
                                                       "SCRAM-SHA-256"),
            hxtool_global.hxtool_config.get_child_item('db', 'db_name',
                                                       "hxtool"))
    else:
        from hxtool_tinydb import hxtool_tinydb

        # Disable the write cache altogether - too many issues reported with it enabled.
        return hxtool_tinydb(
            combine_app_path(hxtool_vars.data_path, 'hxtool.db'),
            apicache=hxtool_global.hxtool_config.get_child_item(
                'apicache', 'enabled', False),
            apicache_refresh_interval=hxtool_global.hxtool_config.
            get_child_item('apicache', 'refresh_interval'),
            write_cache_size=0)
Esempio n. 2
0
def app_init(debug=False):
    hxtool_global.initialize()

    # Log early init/failures to stdout
    console_log = logging.StreamHandler(sys.stdout)
    console_log.setFormatter(
        logging.Formatter(
            '[%(asctime)s] {%(module)s} {%(threadName)s} %(levelname)s - %(message)s'
        ))
    logger.addHandler(console_log)
    #app.logger.addHandler(console_log)

    # If we're debugging use a static key
    if debug:
        app.secret_key = 'B%PT>65`)x<3_CRC3S~D6CynM7^F~:j0'.encode(
            default_encoding)
        logger.setLevel(logging.DEBUG)
        logger.debug("Running in debug mode.")
    else:
        app.secret_key = crypt_generate_random(32)
        logger.setLevel(logging.INFO)

    hxtool_global.hxtool_config = hxtool_config(
        combine_app_path(hxtool_vars.data_path, 'conf.json'))

    # Initialize configured log handlers
    for log_handler in hxtool_global.hxtool_config.log_handlers():
        logger.addHandler(log_handler)

    # Init DB
    # Check if MongoDB is enabled as the database, otherwise, fallback to TinyDB
    if hxtool_global.hxtool_config.get_child_item('db', 'type') == "mongodb":
        from hxtool_mongodb import hxtool_mongodb

        hxtool_global.hxtool_db = hxtool_mongodb(
            hxtool_global.hxtool_config.get_child_item('db', 'host', False),
            hxtool_global.hxtool_config.get_child_item('db', 'port', 27017),
            hxtool_global.hxtool_config.get_child_item('db', 'user', False),
            hxtool_global.hxtool_config.get_child_item('db', 'password',
                                                       False),
            hxtool_global.hxtool_config.get_child_item('db', 'auth_source',
                                                       "admin"),
            hxtool_global.hxtool_config.get_child_item('db', 'auth_mechanism',
                                                       "SCRAM-SHA-256"))
    else:
        # Disable the write cache altogether - too many issues reported with it enabled.
        hxtool_global.hxtool_db = hxtool_db(
            combine_app_path(hxtool_vars.data_path, 'hxtool.db'),
            apicache=hxtool_global.hxtool_config.get_child_item(
                'apicache', 'enabled', False),
            apicache_refresh_interval=hxtool_global.hxtool_config.
            get_child_item('apicache', 'refresh_interval'),
            write_cache_size=0)

    # TODO: Disabled for now
    # Enable X15 integration if config options are present
    #if hxtool_global.hxtool_config['x15']:
    #	from hxtool_x15_db import hxtool_x15
    #	hxtool_global.hxtool_x15_object = hxtool_x15()

    # Initialize the scheduler
    hxtool_global.hxtool_scheduler = hxtool_scheduler(
        hxtool_global.hxtool_config['scheduler']['thread_count'])
    hxtool_global.hxtool_scheduler.start()

    # Initialize background API sessions
    hxtool_global.hxtool_scheduler.initialize_task_api_sessions()

    # Load tasks from the database after the task API sessions have been initialized
    hxtool_global.hxtool_scheduler.load_from_database()

    app.config['SESSION_COOKIE_NAME'] = "hxtool_session"
    app.permanent_session_lifetime = datetime.timedelta(days=7)
    app.session_interface = hxtool_session_interface(
        app,
        expiration_delta=hxtool_global.hxtool_config['network']
        ['session_timeout'])

    # Disable API cache for now
    # TODO: Restricted to MongoDB only?
    #if hxtool_global.hxtool_config.get_child_item('apicache', 'enabled', False):
    #	for profile in hxtool_global.hxtool_db.profileList():
    #		if profile['profile_id'] in hxtool_global.hxtool_scheduler.task_hx_api_sessions:
    #			hxtool_global.apicache[profile['profile_id']] = hxtool_api_cache(hxtool_global.hxtool_scheduler.task_hx_api_sessions[profile['profile_id']], profile['profile_id'], hxtool_global.hxtool_config['apicache']['intervals'], hxtool_global.hxtool_config['apicache']['types'])
    #		else:
    #			logger.info("No background credential for {}, not starting apicache".format(profile['profile_id']))

    set_svg_mimetype()