def rundb(target): """ Start running a local DynamoDB instance. :param target: :return: """ load_env(target) os.environ['AWS_REGION'] = 'us-west-2' shared_db = './dynamo_db/shared-local-instance.db' if os.path.exists(shared_db): os.remove(shared_db) dynamo_command = [ 'java', '-Djava.library.path={}/dynamo_db/DynamoDBLocal_lib'.format(CWD), '-jar', '{}/dynamo_db/DynamoDBLocal.jar'.format(CWD), '-sharedDb', '-dbPath', './dynamo_db' ] try: dynamo_process = subprocess.Popen(dynamo_command, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) except Exception as e: pass try: ''' Connect to DynamoDB and register and create tables for application models. ''' engine = Engine() engine.connect(os.environ['AWS_REGION'], host='localhost', port=8000, access_key='anything', secret_key='anything', is_secure=False) # load models sys.path = ['./app/models'] + sys.path modelModules = glob.glob('./app/models' + "/*.py") models = [basename(f)[:-3] for f in modelModules if isfile(f)] for modelName in models: if modelName != '__init__': engine.register(getattr(__import__(modelName), modelName)) engine.create_schema() tables = [table for table in engine.dynamo.list_tables()] print("This engine has the following tables " + str(tables)) for table in tables: engine.dynamo.describe_table(table) except Exception as e: # IF anything goes wrong, then we self-destruct. dynamo_process.kill() raise e # Wait for process to finish. dynamo_process.wait()
def write_data(namepsace: str, val3_base: str, port: int) -> None: db = Engine(namespace=namepsace) db.connect( 'local', access_key='AK', secret_key='SK', host='localhost', port=port, is_secure=False, ) db.register(Data) db.create_schema() started = datetime.utcnow() db.save(items=[ Data(id=str(i), val1=str(i), val2=i, val3=f'{val3_base}-{i}') for i in range(100) ]) elapsed = datetime.utcnow() - started print(f'{namepsace} elapsed time: {elapsed}')
def setup_dynamodb(models, region=DB_REGION, access_key=DB_KEY, secret_key=DB_SECRET, host=DB_HOST, port=DB_PORT, is_secure=DB_SECURE): """ Setups DynamoDB Local and registers flywheel models. Parameters: models : list List of flywheel models to register region : str, optional access_key : str, optional secret_key : str, optional host : str, optional port : int, optional is_secure : bool, optional """ # Create an engine and connect to DynamoDB Local engine = Engine() engine.connect(region, access_key=access_key, secret_key=secret_key, host=host, port=port, is_secure=is_secure) # Register models with the engine so it can create Dynamo tables engine.register(*models) # Drop any existing schema in the database, so we can sync it up with # current schema. This is only for development. engine.delete_schema() # Create the dynamo table for our registered models engine.create_schema() return engine
def connect(*args, **kwargs): engine = getattr(current_module, 'engine') session = getattr(current_module, 'session') or kwargs.pop('session', None) if engine: return engine else: engine = Engine() # Connect the engine to either a local DynamoDB or a particular region. if ('USE_LOCAL_DB' in os.environ and os.environ['USE_LOCAL_DB'] == 'True'): engine.connect(os.environ['AWS_REGION'], host='localhost', port=8000, access_key='anything', secret_key='anything', is_secure=False, session=session) elif ('CI' in os.environ and os.environ['CI'] == 'True'): engine.connect_to_region(os.environ['AWS_REGION'], session=session) else: engine.connect_to_region(os.environ['AWS_REGION']) setattr(current_module, 'engine', engine) return engine
def setup_dynamodb(models, region=DB_REGION, access_key=DB_KEY, secret_key=DB_SECRET, host=DB_HOST, port=DB_PORT, is_secure=DB_SECURE): """ Setups DynamoDB Local and registers flywheel models. Parameters: models : list List of flywheel models to register region : str, optional access_key : str, optional secret_key : str, optional host : str, optional port : int, optional is_secure : bool, optional """ # Create an engine and connect to DynamoDB Local engine = Engine() engine.connect( region, access_key=access_key, secret_key=secret_key, host=host, port=port, is_secure=is_secure ) # Register models with the engine so it can create Dynamo tables engine.register(*models) # Drop any existing schema in the database, so we can sync it up with # current schema. This is only for development. engine.delete_schema() # Create the dynamo table for our registered models engine.create_schema() return engine
import json import os from flask import Flask from flywheel import Engine from flywheel.query import EntityNotFoundException from service.models import Data db = Engine(namespace=os.environ['DB_NAMESPACE']) db.connect( 'local', access_key='AK', secret_key='SK', host='local-db', port= 8000, # The application communicates on the docker-compose network with local DynamoDB is_secure=False, ) app = Flask(__name__) @app.route('/health', methods=['GET']) def health(): return json.dumps({'status': 'success'}) @app.route('/test/<id_>', methods=['GET']) def test(id_: int): return json.dumps({