def s3bucket(app_config): s3 = connect_s3(app_config) try: bucket = s3.create_bucket(app_config["EBOOK_S3_BUCKET"]) except boto.exception.S3CreateError: bucket = s3.get_bucket(app_config["EBOOK_S3_BUCKET"]) yield bucket for k in bucket.list(): k.delete() s3.delete_bucket(bucket)
def show_s3(test=False): if test: # import the configuration for pytest runs from conftest import app_config config = app_config() else: config = app.config # connect to S3 s3 = connect_s3(config) bucket = s3.get_bucket(config['EBOOK_S3_BUCKET']) for item in bucket.list(): print item
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'