예제 #1
0
    def __init__(self, forseti_config_file_path, forseti_db_connect_string,
                 endpoint):
        """Initialize.

        Args:
            forseti_config_file_path (str): Path to Forseti configuration file
            forseti_db_connect_string (str): Forseti database string
            endpoint (str): server endpoint
        """

        super(ServiceConfig, self).__init__()
        self.thread_pool = ThreadPool()

        # Enable pool_pre_ping to ensure that disconnected or errored
        # connections are dropped and recreated before use.
        self.engine = create_engine(forseti_db_connect_string,
                                    pool_recycle=3600,
                                    pool_pre_ping=True)
        self.model_manager = ModelManager(self.engine)
        self.sessionmaker = db.create_scoped_sessionmaker(self.engine)
        self.endpoint = endpoint

        self.forseti_config_file_path = forseti_config_file_path

        self.inventory_config = None
        self.scanner_config = None
        self.notifier_config = None
        self.global_config = None
        self.forseti_config = None

        self.update_lock = threading.RLock()
def create_test_engine_with_file(enforce_fks=True):
    """Create a test engine with a db file in /tmp/."""

    fd, tmpfile = tempfile.mkstemp('.db', 'forseti-test-')
    try:
        LOGGER.info('Creating database at %s', tmpfile)
        engine = create_engine('sqlite:///{}'.format(tmpfile),
                               sqlite_enforce_fks=enforce_fks,
                               connect_args={'check_same_thread': True})
        return engine, tmpfile
    finally:
        os.close(fd)
def create_sqlite_db(threads=1):
    """Create and initialize a sqlite db engine for use as the CAI temp store.

    Args:
        threads (int): The number of threads to support. Pool size is set to 5
            greater than the number of threads, so that each thread can get its
            own connection to the temp database, with a few spare.
    Returns:
        Tuple[sqlalchemy.engine.Engine, str]: A tuple containing an engine
            object initialized to a temporary sqlite db file, and the path to
            the temporary file.
    """
    dbfile, tmpfile = tempfile.mkstemp('.db', 'forseti-cai-store-')
    pool_size = threads + 5
    try:
        engine = create_engine('sqlite:///{}'.format(tmpfile),
                               sqlite_enforce_fks=False,
                               pool_size=pool_size,
                               connect_args={'check_same_thread': False},
                               poolclass=SingletonThreadPool)
        _initialize(engine)
        return engine, tmpfile
    finally:
        os.close(dbfile)
    Returns:
        list: Subclasses of the given parent class.
    """
    results = []
    for subclass in cls.__subclasses__():
        results.append(subclass)
    return results


if __name__ == '__main__':
    # If the DB connection string is passed in, use that, otherwise
    # fall back to the default DB connection string.
    print(sys.argv)
    DB_CONN_STR = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_DB_CONN_STR

    SQL_ENGINE = general_dao.create_engine(DB_CONN_STR, pool_recycle=3600)

    # Drop the CaiTemporaryStore table to ensure it is using the
    # latest schema.
    inventory_dao.initialize(SQL_ENGINE)
    INVENTORY_TABLES = inventory_dao.BASE.metadata.tables
    CAI_TABLE = INVENTORY_TABLES.get(
        inventory_dao.CaiTemporaryStore.__tablename__)
    CAI_TABLE.drop(SQL_ENGINE)

    # Create tables if not exists.
    inventory_dao.initialize(SQL_ENGINE)
    scanner_dao.initialize(SQL_ENGINE)

    # Find all the child classes inherited from declarative base class.
    SCANNER_DAO_CLASSES = _find_subclasses(scanner_dao.BASE)
예제 #5
0
 def __init__(self, db_connect_string):
     engine = create_engine(db_connect_string, echo=False)
     self.model_manager = ModelManager(engine)