Esempio n. 1
0
File: manage.py Progetto: oii/ogre
def create_user(username, password, email, role='user', confirmed=False, test=False):
    """
    Create a new user for OGRE

    test (bool)
        Only check if user has been created; don't actually do anything
    """
    try:
        # load a user
        setup_db_session(app)
        from ogreserver.models.user import User
        user = User.query.filter_by(username=username).first()

    except ProgrammingError as e:
        if "doesn't exist" in str(e):
            print 'You must run init_ogre command first!'
            sys.exit(1)
        else:
            raise e

    if test is True:
        # only report state in test mode
        if user is None:
            print "User doesn't exist"
            sys.exit(1)
        else:
            print 'User {} exists'.format(username)
            sys.exit(0)
    else:
        if user is None:
            try:
                # celery is required for flask_security as it imports tasks.py
                app.celery = make_celery(app)
                register_tasks(app)

                from ogreserver.extensions.flask_security import init_security

                app.security = init_security(app)
                user = app.security.datastore.create_user(
                    username=username, email=email, password=password
                )
                if confirmed:
                    from flask.ext.security.confirmable import confirm_user
                    confirm_user(user)

                app.security.datastore.commit()

                print "Created user {} with role '{}'".format(username, role)

            except IntegrityError:
                print 'A user with this email address already exists'
                sys.exit(1)
        else:
            print 'User {} already exists'.format(username)
            sys.exit(1)
Esempio n. 2
0
File: conftest.py Progetto: 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")
Esempio n. 3
0
File: manage.py Progetto: 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'