Exemplo n.º 1
0
class RootResource(object):
    __name__ = None
    __parent__ = None
    __acl__ = [
        (Allow, Everyone, 'everyone'),
        (Allow, Authenticated, 'authenticated'),
    ]

    def __init__(self, request):
        self.engine = Engine()
        self.engine.connect_to_host(host='localhost',
                                    port=8000,
                                    is_secure=False)

        self._repos = {
            "user": UserRepository(self.engine),
            "user_credential": UserCredentialRepository(self.engine),
            "access_token": AccessTokenRepository(self.engine),
            "refresh_token": RefreshTokenRepository(self.engine),
            "client_session": ClientSessionRepository(self.engine),
            "village": VillageRepository(self.engine),
            "resident": ResidentRepository(self.engine),
            "event": EventRepository(self.engine),
            "behavior": BehaviorRepository(self.engine),
        }
        self._message_handler = MessageHandler(self)

    @property
    def repos(self):
        return self._repos

    @property
    def message_handler(self):
        return self._message_handler
Exemplo n.º 2
0
class RootResource(object):
    __name__ = None
    __parent__ = None
    __acl__ = [
        (Allow, Everyone, 'everyone'),
        (Allow, Authenticated, 'authenticated'),
    ]

    def __init__(self, request):
        self.engine = Engine()
        self.engine.connect_to_host(host='localhost', port=8000, is_secure=False)

        self._repos = {
            "user": UserRepository(self.engine),
            "user_credential": UserCredentialRepository(self.engine),
            "access_token": AccessTokenRepository(self.engine),
            "refresh_token": RefreshTokenRepository(self.engine),
            "client_session": ClientSessionRepository(self.engine),
            "village": VillageRepository(self.engine),
            "resident": ResidentRepository(self.engine),
            "event": EventRepository(self.engine),
            "behavior": BehaviorRepository(self.engine),
        }
        self._message_handler = MessageHandler(self)

    @property
    def repos(self):
        return self._repos

    @property
    def message_handler(self):
        return self._message_handler
Exemplo n.º 3
0
def configure_flywheel_engine(graph):
    """
    Create the flywheel engine.

    """
    namespace = ()
    if graph.metadata.testing:
        namespace = 'test'

    engine = Engine(namespace=namespace)
    engine.connect_to_region(graph.config.dynamodb.region)

    return engine
Exemplo n.º 4
0
    def configure(cls, settings):
        kwargs = super(DynamoCache, cls).configure(settings)

        access_key = settings.get('db.aws_access_key_id')
        secret_key = settings.get('db.aws_secret_access_key')
        region = settings.get('db.region_name')
        host = settings.get('db.host')
        port = int(settings.get('db.port', 8000))
        secure = asbool(settings.get('db.secure', False))
        namespace = settings.get('db.namespace', ())
        graceful_reload = asbool(settings.get('db.graceful_reload', False))

        if host is not None:
            connection = DynamoDBConnection.connect(region,
                                                    host=host,
                                                    port=port,
                                                    is_secure=secure,
                                                    access_key=access_key,
                                                    secret_key=secret_key)
        elif region is not None:
            connection = DynamoDBConnection.connect(region,
                                                    access_key=access_key,
                                                    secret_key=secret_key)
        else:
            raise ValueError("Must specify either db.region_name or db.host!")
        kwargs['engine'] = engine = Engine(namespace=namespace,
                                           dynamo=connection)
        kwargs['graceful_reload'] = graceful_reload

        engine.register(DynamoPackage, PackageSummary)
        LOG.info("Checking if DynamoDB tables exist")
        engine.create_schema()
        return kwargs
Exemplo n.º 5
0
    def configure(cls, settings):
        kwargs = super(DynamoCache, cls).configure(settings)

        access_key = settings.get('db.access_key')
        secret_key = settings.get('db.secret_key')
        region = settings.get('db.region')
        host = settings.get('db.host')
        port = int(settings.get('db.port', 8000))
        secure = asbool(settings.get('db.secure', False))
        namespace = settings.get('db.namespace', ())

        if region is not None:
            connection = DynamoDBConnection.connect(region,
                                                    access_key=access_key,
                                                    secret_key=secret_key)
        elif host is not None:
            connection = DynamoDBConnection.connect('us-east-1',
                                                    host=host,
                                                    port=port,
                                                    is_secure=secure,
                                                    access_key=access_key,
                                                    secret_key=secret_key)
        else:
            raise ValueError("Must specify either db.region or db.host!")
        kwargs['engine'] = engine = Engine(namespace=namespace,
                                           dynamo=connection)

        engine.register(DynamoPackage, PackageSummary)
        engine.create_schema()
        return kwargs
Exemplo n.º 6
0
    def __init__(self, args):
        self.engine = Engine()
        self.engine.connect_to_region('eu-west-1')

        # Register our model with the engine so it can create the Dynamo table
        self.engine.register(Approval)

        # Create the dynamo table for our registered model
        self.engine.create_schema()
        self.args = args
        # Setup logging
        if args.verbose:
            loglevel = logging.DEBUG
        else:
            loglevel = logging.INFO
        logging.basicConfig(format="%(levelname)s: %(message)s",
                            level=loglevel)
Exemplo n.º 7
0
def generate_cf_dynamo_schema(target):
    dynamo_connection = DynamoDBConnection()

    class FakeClient(object):
        def create_table(self, *args, **kwargs):
            write_schema_to_yaml(target, **kwargs)
            return {}

    client = FakeClient()
    dynamo_connection.client = client

    class FakeDynamo(object):
        def list_tables(self):
            return []

        def create_table(self, *args):
            result = dynamo_connection.create_table(*args)

        def describe_table(self, *args):
            StatusStruct = namedtuple('Status', 'status')
            return StatusStruct(status='ACTIVE')

    dynamo = FakeDynamo()
    engine = Engine()
    engine.dynamo = dynamo

    sys.path = ['{}/app/models'.format(target)] + sys.path
    modelModules = glob.glob('{}/app/models'.format(target) + '/*.py')
    models = [basename(f)[:-3] for f in modelModules if isfile(f)]
    for modelName in models:
        if modelName != '__init__':
            engine.register(getattr(importlib.__import__(modelName),
                                    modelName))

    engine.create_schema()
Exemplo n.º 8
0
    def __init__(self, request):
        self.engine = Engine()
        self.engine.connect_to_host(host='localhost',
                                    port=8000,
                                    is_secure=False)

        self._repos = {
            "user": UserRepository(self.engine),
            "user_credential": UserCredentialRepository(self.engine),
            "access_token": AccessTokenRepository(self.engine),
            "refresh_token": RefreshTokenRepository(self.engine),
            "client_session": ClientSessionRepository(self.engine),
            "village": VillageRepository(self.engine),
            "resident": ResidentRepository(self.engine),
            "event": EventRepository(self.engine),
            "behavior": BehaviorRepository(self.engine),
        }
        self._message_handler = MessageHandler(self)
Exemplo n.º 9
0
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()
Exemplo n.º 10
0
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}')
Exemplo n.º 11
0
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
Exemplo n.º 12
0
    def __init__(self, command_name, json_data, command_argument):
        self.command_name = command_name
        self.command_argument = command_argument
        # Namespace the approval lock
        self.data = json.loads(json_data)
        self.wait_lock = 10
        self.pool = ''
        self.engine = Engine()

        # allow debug logging to console for tests
        if os.getenv('RESOURCE_DEBUG', False) or self.data.get('source', {}).get('debug', False):
            log.basicConfig(level=log.DEBUG)
        else:
            logfile = tempfile.NamedTemporaryFile(delete=False, prefix='log')
            log.basicConfig(level=log.DEBUG, filename=logfile.name)
        stderr = log.StreamHandler()
        stderr.setLevel(log.INFO)
        log.getLogger().addHandler(stderr)

        log.debug('command: %s', command_name)
        log.debug('input: %s', self.data)
        log.debug('args: %s', command_argument)
        log.debug('environment: %s', os.environ)
Exemplo n.º 13
0
def configure_flywheel_engine(graph):
    """
    Create the flywheel engine.

    """
    namespace = graph.config.dynamodb.namespace

    if graph.metadata.testing:
        from microcosm_dynamodb.mockengine import MockEngine
        engine = MockEngine(namespace="test-")
    else:
        engine = Engine(namespace=namespace)

    engine.connect_to_region(graph.config.dynamodb.region)
    return engine
Exemplo n.º 14
0
    def __init__(self, request):
        self.engine = Engine()
        self.engine.connect_to_host(host='localhost', port=8000, is_secure=False)

        self._repos = {
            "user": UserRepository(self.engine),
            "user_credential": UserCredentialRepository(self.engine),
            "access_token": AccessTokenRepository(self.engine),
            "refresh_token": RefreshTokenRepository(self.engine),
            "client_session": ClientSessionRepository(self.engine),
            "village": VillageRepository(self.engine),
            "resident": ResidentRepository(self.engine),
            "event": EventRepository(self.engine),
            "behavior": BehaviorRepository(self.engine),
        }
        self._message_handler = MessageHandler(self)
Exemplo n.º 15
0
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
Exemplo n.º 16
0
    def configure(cls, settings):
        kwargs = super(DynamoCache, cls).configure(settings)

        access_key = settings.get("db.aws_access_key_id")
        secret_key = settings.get("db.aws_secret_access_key")
        region = settings.get("db.region_name")
        host = settings.get("db.host")
        port = int(settings.get("db.port", 8000))
        secure = asbool(settings.get("db.secure", False))
        namespace = settings.get("db.namespace", ())
        graceful_reload = asbool(settings.get("db.graceful_reload", False))

        tablenames = aslist(settings.get("db.tablenames", []))
        if tablenames:
            if len(tablenames) != 2:
                raise ValueError("db.tablenames must be a 2-element list")
            DynamoPackage.meta_.name = tablenames[0]
            PackageSummary.meta_.name = tablenames[1]

        if host is not None:
            connection = DynamoDBConnection.connect(
                region,
                host=host,
                port=port,
                is_secure=secure,
                access_key=access_key,
                secret_key=secret_key,
            )
        elif region is not None:
            connection = DynamoDBConnection.connect(region,
                                                    access_key=access_key,
                                                    secret_key=secret_key)
        else:
            raise ValueError("Must specify either db.region_name or db.host!")
        kwargs["engine"] = engine = Engine(namespace=namespace,
                                           dynamo=connection)
        kwargs["graceful_reload"] = graceful_reload

        engine.register(DynamoPackage, PackageSummary)
        LOG.info("Checking if DynamoDB tables exist")
        engine.create_schema()
        return kwargs
Exemplo n.º 17
0
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
Exemplo n.º 18
0
# logging.basicConfig(level=logging.DEBUG)

# parsing arguments
PARSER = argparse.ArgumentParser(description='Client message processor')
PARSER.add_argument('API_token', help="the individual API token given to your team")
PARSER.add_argument('API_base', help="the base URL for the game API")

ARGS = PARSER.parse_args()

# defining global vars
API_BASE = ARGS.API_base
# 'https://csm45mnow5.execute-api.us-west-2.amazonaws.com/dev'

APP = Flask(__name__)

DDB_ENGINE = Engine()
DDB_ENGINE.connect_to_region('eu-central-1')
Message.meta_.name = 'gameday-production'

DDB_ENGINE.register(Message)

#ELASTICACHE_CLIENT = boto3.client('elasticache')


# creating flask route for type argument
@APP.route('/ping', methods=['GET', 'POST'])
def healthcheck():
    return "ok"

@APP.route('/', methods=['GET', 'POST'])
def main_handler():
Exemplo n.º 19
0
    artist = Field(hash_key=True)
    ts = Field(data_type=datetime, range_key=True)
    album = Field()
    title = Field()
    date = Field()
    scrobble = Field() #should have made this NUMBER, int

    def __init__(self, artist, ts, title, album, date, scrobble):
        self.artist = artist
        self.ts = ts
        self.album = album
        self.title = title
        self.date = date
        self.scrobble = scrobble

engine = Engine()
#engine.connect_to_host(host='localhost', port=8000)
engine.connect_to_region('us-east-1')

engine.register(scrobble)

# uncomment the following if you actually want to create the database for the first time
#engine.create_schema() 

# below is an example of how you would write to the the DynamoDB if you wanted to create a record
# in tft_sonos_instagram.py I am using boto but if it was using flywheel, it would look like the following
#z = scrobble("Patty Griffin", datetime.now(), "Making Pies", "Children Running through it", "Date: 1234", "14")
#engine.save(z)

days = input("How many days do you want to go back? ")
Exemplo n.º 20
0
    ts = Field(data_type=datetime, range_key=True)
    album = Field()
    title = Field()
    date = Field()
    scrobble = Field()  #should have made this NUMBER, int

    def __init__(self, artist, ts, title, album, date, scrobble):
        self.artist = artist
        self.ts = ts
        self.album = album
        self.title = title
        self.date = date
        self.scrobble = scrobble


engine = Engine()
#engine.connect_to_host(host='localhost', port=8000)
engine.connect_to_region('us-east-1')

engine.register(scrobble)

# uncomment the following if you actually want to create the database for the first time
#engine.create_schema()

# below is an example of how you would write to the the DynamoDB if you wanted to create a record
# in tft_sonos_instagram.py I am using boto but if it was using flywheel, it would look like the following
#z = scrobble("Patty Griffin", datetime.now(), "Making Pies", "Children Running through it", "Date: 1234", "14")
#engine.save(z)

days = input("How many days do you want to go back? ")
Exemplo n.º 21
0
# -*- coding: utf-8 -*-
from flywheel import Engine

from werewolf.domain.user import *
from werewolf.domain.game import *
from werewolf.domain.game.repository import *

engine = Engine()
engine.connect_to_host(host='localhost', port=8000, is_secure=False)

engine.register(User)
engine.register(UserCredential)
engine.register(ClientSession)
engine.register(AccessToken)
engine.register(RefreshToken)
engine.register(VillageModel)
engine.register(ResidentModel)
engine.register(BehaviorModel)
engine.register(EventModel)

engine.create_schema()

repo_village = VillageRepository(engine)

repo_village.add(u'PyConJP 村')
repo_village.add(u'ペンギン村')
Exemplo n.º 22
0
class Dynamo():
    engine = Engine()

    def init_engine(self, dy_region, use_local, local_port=8000, create_tables=False, register_models=True, client_id="CLIENT_ID", client_secret="secret", client_metadata=None):
        # Create an engine and connect to an AWS region
        if use_local:
            self.engine.connect(host='localhost', port=local_port, region='', is_secure=False)
        else:
            self.engine.connect(region=dy_region, is_secure=False)

        # Register models with the engine so it can create the Dynamo table
        if register_models:
            self.engine.register(OAuth2DynamoToken)
            self.engine.register(OAuth2DynamoClient)
            self.engine.register(DynamoPasswordResetToken)

        # Create the dynamo tables
        if create_tables:
            self.engine.delete_schema()
            self.engine.create_schema()
            self.create_client(client_id, client_secret, client_metadata)

    # token functions
    def save_token(self, token):
        self.engine.save(token)

    def get_token(self, _access_token):
        token = self.engine.get(OAuth2DynamoToken, access_token=_access_token)
        if not token:
            raise InvalidTokenError()
        else:
            return token
    
    def get_token_by_refresh(self, _refresh_token):
        token = self.engine.query(OAuth2DynamoToken).filter(OAuth2DynamoToken.refresh_token == _refresh_token).index('refresh-index').one()
        if not token:
            raise InvalidTokenError()
        else:
            # refresh gets fields not projected by index
            token.refresh()
            return token
    
    def get_tokens_by_userid(self, _user_id):
        tokens = self.engine.query(OAuth2DynamoToken).filter(OAuth2DynamoToken.user_id == _user_id).index('user-index')
        if not tokens:
            raise InvalidTokenError()
        else:
            # refresh gets fields not projected by index
            for token in tokens:
                token.refresh()
            return tokens
    
    def delete_tokens_by_userid(self, _user_id):
        self.engine.query(OAuth2DynamoToken).filter(OAuth2DynamoToken.user_id == _user_id).index('user-index').delete()
    
    def delete_token(self, _access_token):
        self.engine.delete_key(OAuth2DynamoToken, access_token=_access_token)

    # client functions
    def get_client(self, _client_id):
        return self.engine.get(OAuth2DynamoClient, client_id=_client_id)

    def save_client(self, client):
        self.engine.save(client)
 
    def create_client(self, client_id="CLIENT_ID", client_secret="secret", client_metadata=None):
        client_id_issued_at = int(time.time())
        client = OAuth2DynamoClient(
            client_id=client_id,
            client_id_issued_at=client_id_issued_at
        )

        if not client_metadata:
            client_metadata = {
                "client_name": "Client Name",
                "grant_types": ["password", "refresh_token"],
                "scope": "profile",
                "token_endpoint_auth_method": "client_secret_basic"
            }
        client.set_client_metadata(client_metadata)
        client.client_secret = client_secret

        self.save_client(client)

    # password token functions
    def save_pw_token(self, token):
        self.engine.save(token)

    def get_pw_token(self, user_id, reset_code):
        token = self.engine.get(DynamoPasswordResetToken, user_id=user_id, reset_code=reset_code)
        if not token:
            raise InvalidTokenError()
        else:
            return token

    def delete_pw_token(self, user_id, reset_code):
        self.engine.delete_key(DynamoPasswordResetToken, user_id=user_id, reset_code=reset_code)
Exemplo n.º 23
0
    updated_at = Field(data_type=datetime.datetime, nullable=True)
    deleted_at = Field(data_type=datetime.datetime,
                       nullable=True,
                       index='create-index')
    referral = Field(data_type=str, default='')
    avatar = Field(data_type=dict, nullable=True)
    win = Field(data_type=dict, nullable=True)
    win_email = Field(data_type=list, nullable=True)
    is_dirty = Field(data_type=bool, default=False)
    is_admin = Field(data_type=int, default=0, index='admin-index')


dyn = DynamoDBConnection.connect(region=FLYWHEEL_REGION,
                                 access_key=AWS_ACCESS_KEY,
                                 secret_key=AWS_SECRET_ACCESS_KEY)
db = Engine(dynamo=dyn)

cont = {
    'easy': [],
    'medium': [],
    'hard': [],
    'impossible': [],
}

if __name__ == '__main__':
    context = db.scan(User).filter(User.mobile != None).gen()
    print("DONE SCAN")
    for u in context:
        try:
            if u.cpf is not None:
                for i in u.win.values():
Exemplo n.º 24
0
# app.py or app/__init__.py
from flask import Flask
# for dynamodb
from flywheel import Engine


# begin app construction
app = Flask(__name__)
app.config.from_object('config')

# dynadbo connection, create an engine and connect to an AWS region or localhost
engine = Engine()
engine.connect_to_host(host='localhost',port=8000)
#engine.connect_to_region('us-west-2')
# Now we can access the configuration variables via app.config["VAR_NAME"].
from gullycricket import models, views, api,utils
#register models to dynamodb
engine.register(models.Player)
login_manager = utils.get_login_manager()

#below code should go into a init script
# Create the dynamo table for our registered model
engine.create_schema()

for i in range(1, 10):
    player = engine.get(models.Player, playerid=str(i), fullname='Player:'+str(i))
    if player:
        print('record already present, updating: ', str(player))
    else:
        player = models.Player(i, 'Player:'+str(i), 'testplayer',
                        'player'+str(i)+'@somewhere.com', '+1-000-000-0001')
Exemplo n.º 25
0
# app.py or app/__init__.py
from flask import Flask
# for dynamodb
from flywheel import Engine

# begin app construction
app = Flask(__name__)
app.config.from_object('config')

# dynadbo connection, create an engine and connect to an AWS region or localhost
engine = Engine()
engine.connect_to_host(host='localhost', port=8000)
#engine.connect_to_region('us-west-2')
# Now we can access the configuration variables via app.config["VAR_NAME"].
from gullycricket import models, views, api, utils
#register models to dynamodb
engine.register(models.Player)
login_manager = utils.get_login_manager()

#below code should go into a init script
# Create the dynamo table for our registered model
engine.create_schema()

for i in range(1, 10):
    player = engine.get(models.Player,
                        playerid=str(i),
                        fullname='Player:' + str(i))
    if player:
        print('record already present, updating: ', str(player))
    else:
        player = models.Player(i, 'Player:' + str(i), 'testplayer',
Exemplo n.º 26
0
def main(pargs):
    global redis
    if pargs.production:
        serverConf = configobj.ConfigObj('production.ini')
    else:
        serverConf = configobj.ConfigObj('develop.ini')

    routes = [
        (r"/auth/login", FacebookGraphLoginHandler),
        (r"/", HomeHandler),
        (r"/referral/([^/]+)", ReferralHandler),
        (r"/r/([^/]+)", ReferralHandler),
        (r'/(favicon.ico)', StaticFileHandler, {
            "path": "./static/"
        }),
    ]

    for api in [game, tips, user, referral, prizes]:
        routes.extend(get_routes(api))

    if pargs.admin:
        routes.extend(get_routes(admin))
        routes.extend(get_routes(box))
        from encontact import UserDataHandler
        routes.extend([
            (r"/admin/encontact?/", UserDataHandler),
        ])

    pprint(routes, indent=4)

    mailer = Mail(aws_access_key_id=serverConf['ses']['acceskeyid'],
                  aws_secret_access_key=serverConf['ses']['secretacceskeyid'],
                  region=serverConf['ses']['region'],
                  sender=serverConf['ses']['sender'],
                  template=serverConf['ses']['templates'])

    if pargs.production:
        dyn = DynamoDBConnection.connect(
            region=serverConf['dynamo']['region'],
            access_key=serverConf['dynamo']['acceskeyid'],
            secret_key=serverConf['dynamo']['secretacceskeyid'])
        session_settings = dict(
            driver="redis",
            force_persistence=True,
            cache_driver=True,
            driver_settings=dict(
                host=serverConf['redis']['host'],
                port=int(serverConf['redis']['port']),
                db=int(serverConf['redis']['db']),
                max_connections=1024,
            ),
        )
        pool = ConnectionPool(max_connections=2,
                              host=serverConf['redis']['host'],
                              port=int(serverConf['redis']['port']),
                              db=int(serverConf['redis']['db']) + 1)

        pool2 = ConnectionPool(max_connections=2,
                               host=serverConf['redis']['host'],
                               port=int(serverConf['redis']['port']),
                               db=int(serverConf['redis']['db']))

    else:
        dyn = DynamoDBConnection.connect(region='sp-east',
                                         host='127.0.0.1',
                                         port=8000,
                                         is_secure=False,
                                         access_key='asdas',
                                         secret_key='123ads')
        session_settings = dict(
            driver="redis",
            force_persistence=True,
            cache_driver=True,
            driver_settings=dict(
                host='localhost',
                port=6379,
                db=0,
                max_connections=1024,
            ),
        )
        pool = ConnectionPool(max_connections=2,
                              host='localhost',
                              port=6379,
                              db=1)

        pool2 = ConnectionPool(max_connections=2,
                               host='localhost',
                               port=6379,
                               db=0)

    redis = Redis(connection_pool=pool)
    redis2 = Redis(connection_pool=pool2)
    engine = Engine(dynamo=dyn)
    log = logging.getLogger(__name__)
    a = logging.basicConfig(
        level=logging.INFO,
        format=
        '[ %(asctime)s ][ %(levelname)s ][ %(filename)20s:%(lineno)4s - %(funcName)20s() ] %(message)s',
        datefmt='%m-%d %H:%M',
        filename='log/nextgame.log',
        filemode='a')

    log.addHandler(a)
    settings = {
        "debug": False,
        'xsrf_cookies': False,
        'serverConfig': serverConf,
        'instance': serverConf.get('instance'),
        'engine': engine,
        'facebook_api_key': serverConf['facebook']['key'],
        'facebook_secret': serverConf['facebook']['secret'],
        'session': session_settings,
        'template_path': serverConf['ses']['templates'],
        "login_url": "/auth/login/",
        "cookie_secret": 'sopadeletrinhas123',
        "mailer": mailer,
        "production": False
    }

    if (pargs.debug):
        # log.setLevel(logging.DEBUG)
        log.debug('DEBUGGING LOG')
        settings['debug'] = True

    app = Application(routes=routes,
                      settings=settings,
                      db_conn={
                          'ping': redis,
                          'session': redis2
                      })

    if pargs.provision:
        game.models.create_schemas(engine)
        tips.models.create_schemas(engine)
        referral.models.create_schemas(engine)

    if (pargs.production):
        print('Server Production Starting')
        server = tornado.httpserver.HTTPServer(app)
        server.bind(serverConf['tornado']['port'])
        server.start(int(serverConf['tornado']['instances']))
        tornado.ioloop.IOLoop.configure(TornadoUvloop)

    else:
        print('Server Develop Starting')
        app.listen(serverConf['tornado']['port'])
        tornado.ioloop.IOLoop.configure(TornadoUvloop)

    tornado.ioloop.IOLoop.current().start()
Exemplo n.º 27
0
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({
Exemplo n.º 28
0
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = "1"

api_url = os.environ.get('api_url')
client_id = os.environ.get('client_id')
client_secret = os.environ.get('client_secret')  # TODO: put in SSM?

scope = 'openid offline'

secret_base64_encoded = base64.b64encode(f"{client_id}:{client_secret}".encode())
authorization_header = {"Authorization": f"Basic {secret_base64_encoded}"}

region = os.environ.get('AWS_REGION', 'eu-west-1')
sessions_tablename = os.environ.get('SESSIONS_TABLENAME', 'apigw-bucko-dev')

engine = Engine()
engine.connect_to_region(region)


# Set up our data model
class SessionObject(Model):

    __metadata__ = {
        '_name': sessions_tablename,
    }

    key = Field(hash_key=True)
    state = Field()
    token = Field()

    def __init__(self, key, state):
Exemplo n.º 29
0
class ApprovalResource:
    """
        Approval resource implementation.
        This python script is the target of symbolic links and is used for check, in and out.
        These three commands are defined as methods on this class and common parameters live in the constructor.
        To enable the debug output, the resource source configuration must have the debug parameter.

    """

    def __init__(self, command_name, json_data, command_argument):
        self.command_name = command_name
        self.command_argument = command_argument
        # Namespace the approval lock
        self.data = json.loads(json_data)
        self.wait_lock = 10
        self.pool = ''
        self.engine = Engine()

        # allow debug logging to console for tests
        if os.getenv('RESOURCE_DEBUG', False) or self.data.get('source', {}).get('debug', False):
            log.basicConfig(level=log.DEBUG)
        else:
            logfile = tempfile.NamedTemporaryFile(delete=False, prefix='log')
            log.basicConfig(level=log.DEBUG, filename=logfile.name)
        stderr = log.StreamHandler()
        stderr.setLevel(log.INFO)
        log.getLogger().addHandler(stderr)

        log.debug('command: %s', command_name)
        log.debug('input: %s', self.data)
        log.debug('args: %s', command_argument)
        log.debug('environment: %s', os.environ)

    def check_cmd(self, source, version):
        """
        Check for new version(s)
        This function will look on Dynamodb if there's a lock within the pool
        and will return the last timestamps associated.
        :param source: is an arbitrary JSON object which specifies the location of the resource,
        including any credentials. This is passed verbatim from the pipeline configuration.
        :param version: is a JSON object with string fields, used to uniquely identify an instance of the resource.
        :return: a dict with the version fetched
        """

        log.debug('version: %s', version)

        if not version:
            version = {"timestamp": '0'}
        log.debug('source: %s', source)
        log.debug('version: %s', version)
        approval_locks = self.engine.query(Approval)\
            .filter(
                Approval.timestamp >= datetime.fromtimestamp(Decimal(version.get('timestamp'))),
                pool=self.pool) \
            .all()
        versions_list = []
        for lock in approval_locks:
            versions_list.append({"timestamp": "{timestamp}".format(timestamp=Decimal(lock.timestamp.timestamp()))})
        if not approval_locks:
            versions_list.append(version)
        log.debug(versions_list)
        return versions_list

    def in_cmd(self, target_dir, source, version, params):
        """
        This function will fetch a lock in dynamodb an write it in the target directory.
        If parameters lock_name and need_approval are passed, then the function will wait for a change
        on the dynamodb lock item.
        :param target_dir: a temporary directory which will be exposed as an output
        :param source: is the same value as passed to check
        :param version: is the same type of value passed to check, and specifies the version to fetch.
        :param params: is an arbitrary JSON object passed along verbatim from params on a get.
        :return: a dict with the version fetched and the metadata of the lock
        """
        log.debug('source: %s', source)
        log.debug('version: %s', version)

        if not version:
            version = {"timestamp": 0}

        # Does the get should wait for an approval or not ?
        if 'lock_name' in params and 'need_approval' in params:
            log.debug('Looking for the lock %s in the pool %s' % (params.get('lock_name'), self.pool))
            approval_lock = self.query_lock(params.get('lock_name'))
            if approval_lock:
                # We want to wait until the approve is done
                while approval_lock.approved is None and approval_lock.need_approval:
                    # Query the lock item in the loop
                    refresh_approval = self.query_lock(lock_name=params['lock_name'])

                    # If the lock has timed out, then we override the refresh_approval to simulate a reject
                    if 'timeout' in params:
                        if approval_lock.timestamp + timedelta(minutes=params['timeout']) <= datetime.now():
                            refresh_approval.approved = False
                        countdown = (approval_lock.timestamp.replace(microsecond=0) +
                                     timedelta(minutes=params['timeout'])) - datetime.now().replace(microsecond=0)
                        timedelta(minutes=params['timeout']).total_seconds() / self.wait_lock
                        if countdown.days >= 0:
                            log.info("The lock %s is waiting for an approval. There is %s left" %
                                     (params['lock_name'], str(countdown)))
                    else:
                        log.info("The lock %s is waiting for an approval" % params['lock_name'])
                    # If hasn't been approved or rejected, waiting a bit more
                    if refresh_approval.approved is None:
                        time.sleep(self.wait_lock)
                        continue

                    # Is it approved ?
                    if refresh_approval.approved:
                        approval_lock.approved = True
                    else:
                        approval_lock.approved = False
                # If the lock has been rejected we should fail the job and release the lock
                if not approval_lock.approved and approval_lock.need_approval:
                    log.info("The lock hasn't been approved, exiting")
                    approval_lock.claimed = False
                    approval_lock.approved = None
                    self.engine.save(approval_lock, overwrite=True)
                    exit(1)
        elif 'lock_name' in params:
            approval_lock = self.query_lock(params.get('lock_name'))
        else:
            # There is no approval, we have just a normal lock. Let's fetch the lock
            approval_lock = self.engine.query(Approval)\
                .filter(
                    Approval.timestamp >= datetime.fromtimestamp(Decimal(version.get('timestamp'))),
                    pool=self.pool)\
                .all()
            if approval_lock:
                approval_lock = approval_lock[0]

        metadata = []

        if not approval_lock:
            log.info("No lock have been found")
            exit(0)
        for key in approval_lock.keys_():
            value = getattr(approval_lock, key)
            if type(value) is datetime:
                value = str(Decimal(value.timestamp()))
            elif type(value) is bool:
                value = str(value)
            metadata.append(
                {
                    'name': key,
                    'value': value
                }
            )

        name_path = os.path.join(target_dir, 'name')
        with open(name_path, 'w') as name:
            name.write(getattr(approval_lock, 'lockname'))

        metadata_path = os.path.join(target_dir, 'metadata')
        with open(metadata_path, 'w') as metadata_file:
            json.dump(metadata, metadata_file)

        return {
            'version': {"timestamp": "{timestamp}".format(
                timestamp=Decimal(getattr(approval_lock, 'timestamp').timestamp()))},
            'metadata': metadata,
        }

    def _do_claim(self, params):
        """
        This method handle the claiming of a lock. If the lock is already claimed, it wait until the lock is
        available. Else, it create the lock.
        :param params: the params passed as parameters of the resource
        :return: the approval_lock item in dynamodb
        """
        approval_lock = self.query_lock(lock_name=params['lock_name'])
        need_approval = params.get('need_approval', False)
        override_approval = params.get('override_approval', False)

        if approval_lock:
            # To override the previous approval, we need to reject the previous one
            # Then the get will see it was rejected, will release the lock and fail the job
            if override_approval:
                approval_lock.approved = False
                approval_lock.timestamp = datetime.now()
                self.engine.save(approval_lock, overwrite=True)
                log.info("Rejecting the previous approval")
                # Let the get fail before acquiring the new lock
                time.sleep(self.wait_lock + 5)

            # We want to wait until the lock is not claimed
            log_only_once = False
            while approval_lock.claimed:
                if not log_only_once:
                    log_only_once = True
                    log.info("The lock %s is already claimed" % params['lock_name'])
                refresh_approval = self.query_lock(lock_name=params['lock_name'])
                if not refresh_approval:
                    log.info("The lock does not exist")
                    exit(1)
                if not refresh_approval.claimed:
                    approval_lock.claimed = False
                else:
                    time.sleep(self.wait_lock)

        else:
            approval_lock = Approval(
                id=uuid.uuid4().urn[9:],
                lockname=params['lock_name'],
                pool=self.pool,
                claimed=True,
                team=os.getenv('BUILD_TEAM_NAME', "team"),
                pipeline=os.getenv('BUILD_PIPELINE_NAME', "pipeline"),
                description=params.get('description', None)
            )

        if need_approval:
            approval_lock.need_approval = True
        approval_lock.claimed = True
        approval_lock.approved = None
        approval_lock.timestamp = datetime.now()
        self.engine.save(approval_lock, overwrite=True)
        log.info("Claiming the lock %s" % params['lock_name'])

        return approval_lock

    def _do_release(self, params):
        """
        This method handle the release of a claimed lock

        :param params: the params passed as parameters of the resource
        :return: the approval_lock item in dynamodb
        """
        approval_lock = self.query_lock(lock_name=params['lock_name'])

        if not approval_lock:
            log.info("The lock does not exist")
            exit(1)

        approval_lock.claimed = False
        approval_lock.approved = None
        approval_lock.timestamp = datetime.now()
        self.engine.save(approval_lock, overwrite=True)
        log.info("Releasing the lock %s" % params['lock_name'])

        return approval_lock

    def query_lock(self, lock_name):
        """
        This method is used to query the lock in the approval loop to check if there is a change on it
        :param lock_name: The name of the lock to fetch
        :return: the dynamodb item
        """
        approval_lock = self.engine.query(Approval) \
            .filter(
                lockname=lock_name,
                pool=self.pool) \
            .all()
        if approval_lock:
            approval_lock = approval_lock[0]
        return approval_lock

    def out_cmd(self, target_dir, source, params):
        """
        This method is responsible to acquire or release a lock. If the lock doesn't exist yet, then the method
        create it automatically.
        If a lock is already acquired, the method will wait indefinitely until being able to acquire it.
        :param target_dir:
        :param source: is the same value as passed to check.
        :param params: is an arbitrary JSON object passed along verbatim from params on a put.
        :return: a dict with the version fetched and the metadata of the lock
        """
        metadata = []

        if 'lock_name' not in params:
            log.error('You must set a lock_name on params')
        if 'action' not in params:
            log.error('You must set an action on params')

        if 'claim' in params['action']:
            approval_lock = self._do_claim(params=params)
        elif 'release' in params['action']:
            approval_lock = self._do_release(params=params)
        else:
            log.error('Please use an available action')
            exit(1)

        metadata = []
        for key in approval_lock.keys_():
            value = getattr(approval_lock, key)
            if type(value) is datetime:
                value = str(Decimal(value.timestamp()))
            elif type(value) is bool:
                value = str(value)

            metadata.append(
                {
                    'name': key,
                    'value': value
                }
            )

        name_path = os.path.join(target_dir, 'name')
        with open(name_path, 'w') as name:
            name.write(approval_lock.lockname)

        metadata_path = os.path.join(target_dir, 'metadata')
        with open(metadata_path, 'w') as metadata_file:
            json.dump(metadata, metadata_file)

        return {
            'version': {"timestamp": "{timestamp}".format(
                timestamp=Decimal(approval_lock.timestamp.timestamp()))},
            'metadata': metadata,
        }

    def run(self):
        """Parse input/arguments, perform requested command return output."""
        # Extract informations from the json
        source = self.data.get('source', {})
        params = self.data.get('params', {})
        version = self.data.get('version', {})

        # To use AWS with efficiency, we are pushing the AWS credentials into environment
        os.environ['AWS_ACCESS_KEY_ID'] = source.get('AWS_ACCESS_KEY_ID', '')
        os.environ['AWS_SECRET_ACCESS_KEY'] = source.get('AWS_SECRET_ACCESS_KEY', '')
        os.environ['AWS_DEFAULT_REGION'] = source.get('AWS_DEFAULT_REGION', "eu-west-1")
        self.wait_lock = source.get('wait_lock', 10)

        # Ensure we are receiving the required parameters on the configuration
        if 'pool' not in source:
            log.error("pool must exist in the source configuration")
            exit(1)
        else:
            self.pool = source.get('pool')

        # Configure the connection to Dynamodb
        self.engine.connect_to_region(os.environ.get('AWS_DEFAULT_REGION', 'eu-west-1'))
        # Register our model with the engine so it can create the Dynamo table
        self.engine.register(Approval)
        # Create the dynamo table for our registered model
        self.engine.create_schema()

        # Define which operation to perform
        if self.command_name == 'check':
            response = self.check_cmd(source, version)
        elif self.command_name == 'in':
            response = self.in_cmd(self.command_argument[0], source, version, params)
        else:
            response = self.out_cmd(self.command_argument[0], source, params)

        return json.dumps(response)
Exemplo n.º 30
0
class CLI():
    def __init__(self, args):
        self.engine = Engine()
        self.engine.connect_to_region('eu-west-1')

        # Register our model with the engine so it can create the Dynamo table
        self.engine.register(Approval)

        # Create the dynamo table for our registered model
        self.engine.create_schema()
        self.args = args
        # Setup logging
        if args.verbose:
            loglevel = logging.DEBUG
        else:
            loglevel = logging.INFO
        logging.basicConfig(format="%(levelname)s: %(message)s",
                            level=loglevel)

    def main(self):
        if self.args.action == 'list':
            self.list()
        elif self.args.action == 'approve':
            self.approve()
        elif self.args.action == 'reject':
            self.reject()
        else:
            logging.error('Please use a correct argument')
            exit(1)

    def approve(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(
            id=self.args.id).all()
        if not approval_lock:
            logging.info('No lock with the id %s has been found' %
                         self.args.id)
            exit(1)
        approval_lock = approval_lock[0]
        approval_lock.approved = True
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been approved' % self.args.id)

    def reject(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(
            id=self.args.id).all()
        if not approval_lock:
            logging.info('No lock with the id %s has been found' %
                         self.args.id)
            exit(1)
        approval_lock = approval_lock[0]
        approval_lock.approved = False
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been rejected' % self.args.id)

    def list(self):
        approval_locks = self.engine.scan(Approval).filter(claimed=True).all()
        table = []

        if approval_locks:
            headers = sorted(approval_locks[0].keys_())
        else:
            headers = None
        if approval_locks:
            for item in approval_locks:
                row = []
                for key in sorted(item.keys_()):
                    row.append(getattr(item, key))
                table.append(row)

            print(tabulate(table, headers))
        else:
            print('There is no waiting approval')
Exemplo n.º 31
0
from flywheel import Engine
from pyramid.paster import bootstrap

from werewolf.app.resource import RootResource
from werewolf.domain.base import Identity
from werewolf.domain.user import *
from werewolf.domain.user.repository import *
from werewolf.domain.game import *
from werewolf.domain.game.repository import *
from werewolf.domain.game.exception import GameException, GameNotFinished

env = bootstrap('development.ini')

context = RootResource(None)
engine = Engine()
engine.connect_to_host(host='localhost', port=8000, is_secure=False)

# repo_user = UserRepository(engine)
# a = repo_user.get_by_name('COM1')

game = GameService(context, '4db80ce9c0b445728a39d1d47f2cb742')

try:
    game.start()
except GameException as e:
    print e.message

targets = game.execute_night()

pprint(targets)
from dynamo3 import DynamoDBConnection
from flywheel import Engine
from auth.auth_main import *

connection = DynamoDBConnection.connect(region=DynamoAuth.region,
                                        access_key=DynamoAuth.access_key,
                                        secret_key=DynamoAuth.secret_key,
                                        host="localhost",
                                        port=8000,
                                        is_secure=False)

engine = Engine(connection)
Exemplo n.º 33
0
# -*- coding: utf-8 -*-
from flywheel import Engine

from werewolf.domain.user import *
from werewolf.domain.game import *
from werewolf.domain.game.repository import *


engine = Engine()
engine.connect_to_host(host='localhost', port=8000, is_secure=False)

engine.register(User)
engine.register(UserCredential)
engine.register(ClientSession)
engine.register(AccessToken)
engine.register(RefreshToken)
engine.register(VillageModel)
engine.register(ResidentModel)
engine.register(BehaviorModel)
engine.register(EventModel)

engine.create_schema()

repo_village = VillageRepository(engine)

repo_village.add(u'PyConJP 村')
repo_village.add(u'ペンギン村')
Exemplo n.º 34
0
# Import flask and template operators
from flask import Flask
from flask_cors import CORS
from flywheel import Engine

SUPER_ADMIN = '*****@*****.**'
ALL_PARENTS = '*****@*****.**'

#dynamodb move
engine = Engine()
#engine.connect(region='dummy', host='localhost', port=8000,  access_key='dummy', secret_key='dummy', is_secure=False, session = None)
engine.connect_to_region('eu-west-1')


def create_app():
    # Define the WSGI application object
    app = Flask(__name__, instance_relative_config=True)
    CORS(app)

    # Configurations
    app.config.from_object('config')

    # Import a module / component using its blueprint handler variable (mod_group)
    from base.mod_draft.controllers import mod_draft as draft_module
    from base.mod_group.controllers import mod_group as group_module
    from base.mod_member.controllers import mod_member as member_module
    from base.mod_workday.controllers import mod_workday as workday_module
    from base.mod_rule.controllers import mod_rule as rule_module
    from base.mod_term.controllers import mod_term as term_module
    from base.mod_switchday.controllers import mod_switchday as switchday_module
    from base.mod_communicate.controllers import mod_communicate as communication_module