Ejemplo n.º 1
0
Archivo: conftest.py Proyecto: oii/ogre
def mysqldb(request, _flask_app):
    if request.config.getoption("--only-client"):
        yield None
        return

    import sqlalchemy
    from ogreserver.extensions.database import setup_db_session, create_tables

    # use raw sqlalchemy for create/drop DB
    engine = sqlalchemy.create_engine("mysql://*****:*****@localhost/mysql")

    def run_query(sql):
        # get the internal connection obj, close the open transaction, then run
        conn = engine.connect()
        conn.execute("commit")
        conn.execute(sql)
        conn.close()

    # create a test database using sqlalchemy
    run_query("drop database if exists test")
    run_query("create database test")

    with _flask_app.test_request_context():
        # init app tables into test database
        create_tables(_flask_app)
        db_session = setup_db_session(_flask_app)
        yield db_session

    # cleanup the test mysql db
    run_query("drop database if exists test")
Ejemplo n.º 2
0
Archivo: manage.py Proyecto: oii/ogre
def init_ogre(test=False):
    """
    Initialize the AWS S3 bucket & RethinkDB storage for OGRE.

    test (bool)
        Only check if OGRE has been setup; don't actually do anything
    """

    # init S3
    s3 = connect_s3(app.config)

    # check bucket already exists
    aws_setup1 = aws_setup2 = False
    for b in s3.get_all_buckets():
        if b.name == app.config['EBOOK_S3_BUCKET']:
            aws_setup1 = True
        elif b.name == app.config['STATIC_S3_BUCKET']:
            aws_setup2 = True
    aws_setup = aws_setup1 & aws_setup2

    # check mysql DB created
    try:
        setup_db_session(app)
        from ogreserver.models.user import User
        User.query.first()
        db_setup = True
    except ProgrammingError:
        db_setup = False

    # check rethinkdb initialized
    conn = r.connect('localhost', 28015)
    try:
        r.db('ogreserver').table('ebooks').run(conn)
        rdb_setup = True
    except RqlRuntimeError:
        rdb_setup = False

    if test is True:
        # only report state in test mode
        if aws_setup is True and db_setup is True and rdb_setup is True:
            print 'Already setup'
            sys.exit(0)
        else:
            print 'Not setup'
            sys.exit(1)
    else:
        if aws_setup is True and db_setup is True and rdb_setup is True:
            print 'You have already initialized OGRE :D'
            sys.exit(1)

        # create the local mysql database from our models
        if db_setup is False:
            create_tables(app)
            # celery is required for setup_roles as it imports tasks.py via flask_security
            app.celery = make_celery(app)
            register_tasks(app)
            setup_roles(app)

        for bucket in (app.config['EBOOK_S3_BUCKET'], app.config['STATIC_S3_BUCKET']):
            try:
                if not app.config['DEBUG']:
                    s3.create_bucket(bucket, location=app.config['AWS_REGION'])
                    print 'Created S3 bucket in {}'.format(app.config['AWS_REGION'])
                else:
                    s3.create_bucket(bucket)
                    print 'Created S3 bucket'

            except boto.exception.S3ResponseError as e:
                sys.stderr.write('Failed verifying or creating S3 bucket.. ({})\n'.format(e.error_message))
                sys.exit(1)
            except boto.exception.S3CreateError as e:
                if e.error_code == 'BucketAlreadyExists':
                    sys.stderr.write('Bucket name already in use! ({})\n'.format(e.error_message))
                    sys.exit(1)
                elif e.error_code == 'BucketAlreadyOwnedByYou':
                    pass
                else:
                    raise e

        if rdb_setup is False:
            # create a database and a couple of tables
            r.db_create('ogreserver').run(conn)
            r.db('ogreserver').table_create('ebooks', primary_key='ebook_id').run(conn)
            r.db('ogreserver').table_create('versions', primary_key='version_id').run(conn)
            r.db('ogreserver').table_create('formats', primary_key='file_hash').run(conn)
            r.db('ogreserver').table_create('authors', primary_key='author_id').run(conn)
            r.db('ogreserver').table_create('sync_events').run(conn)
            set_indexes()

    print 'Succesfully initialized OGRE'