Beispiel #1
0
def check_schema(db_url, base_type, echo=False, autoexit=False):
    engine = create_engine(db_url, echo=echo)
    session_type = sessionmaker()
    session_type.configure(bind=engine)

    # import pdb;pdb.set_trace()

    tmp_rdb_session = session_type()
    schema_errors = get_schema_errors(base_type, tmp_rdb_session)
    if not schema_errors:
        print '++  schema validated ok'
    else:
        for err in schema_errors:
            print '!! ', err
        print '!!  recreate the database and update the code, then try again'
        if autoexit:
            sys.exit(2)
    return schema_errors
Beispiel #2
0
def create_app(env_name='prod'):
    # rendering is handled by MessageMiddleware
    ui_routes = (PUBLIC_UI_ROUTES + JUROR_UI_ROUTES + ADMIN_UI_ROUTES +
                 META_UI_ROUTES)
    api_routes = (PUBLIC_API_ROUTES + JUROR_API_ROUTES + ADMIN_API_ROUTES +
                  META_API_ROUTES)
    print '==  creating WSGI app using env name: %s' % (env_name, )

    config_file_name = 'config.%s.yaml' % env_name
    config_file_path = os.path.join(PROJ_PATH, config_file_name)

    print '==  loading config file: %s' % (config_file_path, )

    config = yaml.load(open(config_file_path))

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)

    engine = create_engine(config.get('db_url', DEFAULT_DB_URL),
                           pool_recycle=60)
    session_type = sessionmaker()
    session_type.configure(bind=engine)
    tmp_rdb_session = session_type()

    schema_errors = get_schema_errors(Base, tmp_rdb_session)
    if not schema_errors:
        print '++  schema validated ok'
    else:
        for err in schema_errors:
            print '!! ', err
        print '!!  recreate the database and update the code, then try again'
        sys.exit(2)

    # create maintainer users if they don't exist yet
    musers = bootstrap_maintainers(tmp_rdb_session)
    if musers:
        print '++ created new users for maintainers: %r' % (musers, )

    new_series = ensure_series(tmp_rdb_session)
    if new_series:
        print '++ created new series: %r' % new_series

    tmp_rdb_session.commit()

    engine.echo = config.get('db_echo', False)

    if not config.get('db_disable_ping'):
        event.listen(engine, 'engine_connect', ping_connection)

    renderer = AshesRenderFactory(TEMPLATES_PATH)

    cookie_secret = config['cookie_secret']
    assert cookie_secret

    root_path = config.get('root_path', '/')

    scm_secure = env_name == 'prod'  # https only in prod
    scm_mw = SignedCookieMiddleware(secret_key=cookie_secret,
                                    path=root_path,
                                    http_only=True,
                                    secure=scm_secure)
    if not scm_secure:
        scm_mw.data_expiry = NEVER

    def get_engine():
        engine = create_engine(config.get('db_url', DEFAULT_DB_URL),
                               pool_recycle=60)
        engine.echo = config.get('db_echo', False)
        if not config.get('db_disable_ping'):
            event.listen(engine, 'engine_connect', ping_connection)
        return engine

    blank_session_type = sessionmaker()

    middlewares = [
        TimingMiddleware(), scm_mw,
        DBSessionMiddleware(blank_session_type, get_engine),
        UserMiddleware()
    ]
    api_log_path = config.get('api_log_path')
    if api_log_path:
        log_mw = LoggingMiddleware(api_log_path)
        middlewares.insert(0, log_mw)
        # hack
        config['api_exc_log_path'] = getattr(log_mw, 'exc_log_path', None)

    replay_log_path = config.get('replay_log_path')
    if replay_log_path:
        replay_log_mw = ReplayLogMiddleware(replay_log_path)
        middlewares.append(replay_log_mw)

    consumer_token = ConsumerToken(config['oauth_consumer_token'],
                                   config['oauth_secret_token'])

    resources = {
        'config': config,
        'consumer_token': consumer_token,
        'root_path': root_path,
        'ashes_renderer': renderer
    }

    api_app = Application(api_routes,
                          resources,
                          middlewares=[MessageMiddleware()] + middlewares,
                          render_factory=render_basic)
    ui_app = Application(ui_routes,
                         resources,
                         middlewares=[MessageMiddleware(use_ashes=True)] +
                         middlewares,
                         render_factory=renderer)

    static_app = StaticApplication(STATIC_PATH)

    root_app = Application([
        StaticFileRoute('/', STATIC_PATH + '/index.html'), ('/', static_app),
        ('/', ui_app), ('/v1/', api_app), ('/meta', MetaApplication())
    ])

    return root_app
Beispiel #3
0
def create_app(env_name='prod', config=None):
    # rendering is handled by MessageMiddleware
    ui_routes = (PUBLIC_UI_ROUTES + JUROR_UI_ROUTES + ADMIN_UI_ROUTES +
                 META_UI_ROUTES)
    api_routes = (PUBLIC_API_ROUTES + JUROR_API_ROUTES + ADMIN_API_ROUTES +
                  META_API_ROUTES)
    print '==  creating WSGI app using env name: %s' % (env_name, )

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)

    if config is None:
        config = load_env_config(env_name=env_name)
    print '==  loaded config file: %s' % (config['__file__'], )

    engine = create_engine(config.get('db_url', DEFAULT_DB_URL),
                           pool_recycle=60)
    session_type = sessionmaker()
    session_type.configure(bind=engine)
    tmp_rdb_session = session_type()

    schema_errors = get_schema_errors(Base, tmp_rdb_session)
    if not schema_errors:
        print '++  schema validated ok'
    else:
        for err in schema_errors:
            print '!! ', err
        print '!!  recreate the database and update the code, then try again'
        sys.exit(2)

    # create maintainer users if they don't exist yet
    musers = bootstrap_maintainers(tmp_rdb_session)
    if musers:
        print '++ created new users for maintainers: %r' % (musers, )

    new_series = ensure_series(tmp_rdb_session)
    if new_series:
        print '++ created new series: %r' % new_series

    tmp_rdb_session.commit()

    engine.echo = config.get('db_echo', False)

    if not config.get('db_disable_ping'):
        event.listen(engine, 'engine_connect', ping_connection)

    renderer = AshesRenderFactory(TEMPLATES_PATH)

    cookie_secret = config['cookie_secret']
    assert cookie_secret

    root_path = config.get('root_path', '/')

    scm_secure = env_name == 'prod'  # https only in prod
    scm_mw = SignedCookieMiddleware(secret_key=cookie_secret,
                                    path=root_path,
                                    http_only=True,
                                    secure=scm_secure)
    if not scm_secure:
        scm_mw.data_expiry = NEVER

    def get_engine():
        engine = create_engine(config.get('db_url', DEFAULT_DB_URL),
                               pool_recycle=60)
        engine.echo = config.get('db_echo', False)
        if not config.get('db_disable_ping'):
            event.listen(engine, 'engine_connect', ping_connection)
        return engine

    blank_session_type = sessionmaker()

    middlewares = [
        TimingMiddleware(),
        UserIPMiddleware(), scm_mw,
        DBSessionMiddleware(blank_session_type, get_engine),
        UserMiddleware()
    ]
    api_log_path = config.get('api_log_path', 'montage_api.log')
    if api_log_path:
        log_mw = LoggingMiddleware(api_log_path)
        middlewares.insert(0, log_mw)
        # hack
        config['api_exc_log_path'] = getattr(log_mw, 'exc_log_path', None)

    replay_log_path = config.get('replay_log_path')
    if replay_log_path:
        replay_log_mw = ReplayLogMiddleware(replay_log_path)
        middlewares.append(replay_log_mw)

    consumer_token = ConsumerToken(config['oauth_consumer_token'],
                                   config['oauth_secret_token'])

    resources = {
        'config': config,
        'consumer_token': consumer_token,
        'root_path': root_path,
        'ashes_renderer': renderer
    }

    debug_errors = bool(os.getenv('MONTAGE_PDB',
                                  False)) or config['__env__'] == 'devtest'
    api_app = Application(
        api_routes,
        resources,
        middlewares=[MessageMiddleware(debug_errors=debug_errors)] +
        middlewares,
        render_factory=render_basic)
    ui_app = Application(
        ui_routes,
        resources,
        middlewares=[
            MessageMiddleware(debug_errors=debug_errors, use_ashes=True)
        ] + middlewares,
        render_factory=renderer)

    static_app = StaticApplication(STATIC_PATH)

    root_mws = [HTTPCacheMiddleware(use_etags=True)]
    if not debug_errors:
        # don't need sentry if you've got pdb, etc.
        sentry_sdk.init(
            environment=config['__env__'],
            request_bodies='medium',
            dsn="https://[email protected]/3532775")
        root_mws.append(SentryMiddleware())

    root_app = Application([
        StaticFileRoute('/', STATIC_PATH + '/index.html'),
        StaticFileRoute('/a/', STATIC_PATH + '/a/index.html'),
        ('/', static_app), ('/', ui_app), ('/v1/', api_app),
        ('/meta', MetaApplication())
    ],
                           resources={'config': config},
                           middlewares=root_mws)

    return root_app