コード例 #1
0
ファイル: __main__.py プロジェクト: paubrasil/jet-bridge
def main():
    args = sys.argv[1:]

    if 'ARGS' in os.environ:
        args = os.environ['ARGS'].split(' ')

    logger.info(datetime.now().strftime('%B %d, %Y - %H:%M:%S %Z'))
    logger.info('Jet Bridge version {}'.format(VERSION))

    if (len(args) >= 1 and args[0] == 'config'
        ) or missing_options == required_options_without_default:
        from jet_bridge.utils.create_config import create_config
        create_config(missing_options == required_options_without_default)
        return
    elif len(missing_options) and len(missing_options) < len(
            required_options_without_default):
        logger.info('Required options are not specified: {}'.format(
            ', '.join(missing_options)))
        return

    address = 'localhost' if settings.ADDRESS == '0.0.0.0' else settings.ADDRESS
    url = 'http://{}:{}/'.format(address, settings.PORT)
    api_url = '{}api/'.format(url)

    if len(args) >= 1:
        if args[0] == 'check_token':
            check_token_command(api_url)
            return

    database_connect()

    from jet_bridge.app import make_app

    app = make_app()
    server = HTTPServer(app)
    server.bind(settings.PORT, settings.ADDRESS)
    server.start(settings.WORKERS if not settings.DEBUG else 1)

    if settings.WORKERS > 1 and settings.DEBUG:
        logger.warning('Multiple workers are not supported in DEBUG mode')

    logger.info('Starting server at {}'.format(url))

    if settings.DEBUG:
        logger.warning('Server is running in DEBUG mode')

    logger.info('Quit the server with CONTROL-C')

    check_token_command(api_url)

    IOLoop.current().start()
コード例 #2
0
def main():
    args = sys.argv[1:]

    if 'ARGS' in os.environ:
        args = os.environ['ARGS'].split(' ')

    logger.info(datetime.now().strftime('%B %d, %Y - %H:%M:%S %Z'))
    logger.info('Jet Bridge version {}'.format(VERSION))

    if (len(args) >= 1 and args[0] == 'config') or missing_options == required_options_without_default:
        create_config(missing_options == required_options_without_default)
        return
    elif len(missing_options) and len(missing_options) < len(required_options_without_default):
        logger.info('Required options are not specified: {}'.format(', '.join(missing_options)))
        return

    if not engine_url:
        raise Exception('Database configuration is not set')

    address = 'localhost' if settings.ADDRESS == '0.0.0.0' else settings.ADDRESS
    url = 'http://{}:{}/'.format(address, settings.PORT)
    api_url = '{}api/'.format(url)

    if len(args) >= 1:
        if args[0] == 'check_token':
            check_token_command(api_url)
            return

    from jet_bridge.app import make_app

    app = make_app()
    app.listen(settings.PORT, settings.ADDRESS)

    logger.info('Starting server at {}'.format(url))

    if settings.DEBUG:
        logger.warning('Server is running in DEBUG mode')

    logger.info('Quit the server with CONTROL-C')

    check_token_command(api_url)

    tornado.ioloop.IOLoop.current().start()
コード例 #3
0
ファイル: check_token.py プロジェクト: preetgur/main
def check_token_command(api_url):
    try:
        if not is_token_activated():
            logger.warning('[!] Your server token is not activated')
            logger.warning('[!] Token: {}'.format(settings.TOKEN))

            if settings.AUTO_OPEN_REGISTER and api_url.startswith('http'):
                register_url = '{}register/?token={}'.format(
                    api_url, settings.TOKEN)

                if webbrowser.open(register_url):
                    logger.warning(
                        '[!] Activation page was opened in your browser - {}'.
                        format(register_url))
            else:
                register_url = '{}register/'.format(api_url)
                logger.warning(
                    '[!] Go to {} to activate it'.format(register_url))
    except RequestException:
        logger.error('[!] Can\'t connect to Jet Admin API')
        logger.error('[!] Token verification failed')
コード例 #4
0
    # 'project',
    # 'token',
    'database_engine',
    'database_name',
]

# Parse

options.parse_command_line(final=False)

if options.config:
    try:
        parse_config_file(options, options.config, 'JET', final=False)
    except IOError as e:
        if options.config != DEFAULT_CONFIG_PATH:
            logger.warning(e)

parse_environment(options, final=True)

missing_options = list(
    filter(lambda x: x not in options or options[x] is None, required_options))

# Settings

ADDRESS = options.address
PORT = options.port
WORKERS = options.workers
DEBUG = options.debug
READ_ONLY = options.read_only
CONNECTIONS = options.connections
AUTO_OPEN_REGISTER = options.auto_open_register
コード例 #5
0
def connect_database(conf):
    global connections

    engine_url = build_engine_url(conf)

    if not engine_url:
        raise Exception('Database configuration is not set')

    connection_id = get_connection_id(conf)
    connection_params_id = get_connection_params_id(conf)

    if connection_id in connections:
        if connections[connection_id]['params_id'] == connection_params_id:
            return connections[connection_id]
        else:
            disconnect_database(conf)

    if conf.get('engine') == 'sqlite':
        engine = create_engine(engine_url)
    else:
        engine = create_engine(engine_url, pool_size=conf.get('connections'), max_overflow=10, pool_recycle=300)

    Session = scoped_session(sessionmaker(bind=engine))

    logger.info('Connected to database engine "{}"'.format(engine_url))

    Base.metadata.create_all(engine)

    def only(table, meta):
        if conf.get('only') is not None and table not in conf.get('only'):
            return False
        if conf.get('except') is not None and table in conf.get('except'):
            return False
        return True

    metadata = MetaData(schema=conf.get('schema') if conf.get('schema') and conf.get('schema') != '' else None)
    metadata.reflect(engine, only=only)
    MappedBase = automap_base(metadata=metadata)

    def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
        rnd = get_random_string(4)
        return referred_cls.__name__.lower() + '_jet_relation' + rnd

    def name_for_collection_relationship(base, local_cls, referred_cls, constraint):
        rnd = get_random_string(4)
        return referred_cls.__name__.lower() + '_jet_collection' + rnd

    def custom_generate_relationship(base, direction, return_fn, attrname, local_cls, referred_cls, **kw):
        rnd = get_random_string(4)
        attrname = attrname + '_jet_ref' + rnd
        return generate_relationship(base, direction, return_fn, attrname, local_cls, referred_cls, **kw)

    MappedBase.prepare(
        name_for_scalar_relationship=name_for_scalar_relationship,
        name_for_collection_relationship=name_for_collection_relationship,
        generate_relationship=custom_generate_relationship
    )

    for table_name, table in MappedBase.metadata.tables.items():
        if len(table.primary_key.columns) == 0 and table_name not in MappedBase.classes:
            logger.warning('Table "{}" does not have primary key and will be ignored'.format(table_name))

    connections[connection_id] = {
        'engine': engine,
        'Session': Session,
        'MappedBase': MappedBase,
        'params_id': connection_params_id
    }
    return connections[connection_id]
コード例 #6
0
ファイル: db.py プロジェクト: jaipradeesh/jet-bridge
def database_connect():
    global engine_url, engine, Session, MappedBase

    engine_url = build_engine_url_from_settings()

    if not engine_url:
        raise Exception('Database configuration is not set')

    if settings.DATABASE_ENGINE == 'sqlite':
        engine = create_engine(engine_url)
    else:
        engine = create_engine(engine_url,
                               pool_size=settings.DATABASE_CONNECTIONS,
                               max_overflow=10,
                               pool_recycle=1)

    Session = scoped_session(sessionmaker(bind=engine))

    logger.info('Connected to database engine "{}" with name "{}"'.format(
        settings.DATABASE_ENGINE, settings.DATABASE_NAME))

    Base.metadata.create_all(engine)

    def only(table, meta):
        if settings.DATABASE_ONLY is not None and table not in settings.DATABASE_ONLY:
            return False
        if settings.DATABASE_EXCEPT is not None and table in settings.DATABASE_EXCEPT:
            return False
        return True

    metadata = MetaData(schema=settings.DATABASE_SCHEMA)
    metadata.reflect(engine, only=only)
    MappedBase = automap_base(metadata=metadata)

    def name_for_scalar_relationship(base, local_cls, referred_cls,
                                     constraint):
        rnd = get_random_string(4)
        return referred_cls.__name__.lower() + '_jet_relation' + rnd

    def name_for_collection_relationship(base, local_cls, referred_cls,
                                         constraint):
        rnd = get_random_string(4)
        return referred_cls.__name__.lower() + '_jet_collection' + rnd

    def custom_generate_relationship(base, direction, return_fn, attrname,
                                     local_cls, referred_cls, **kw):
        rnd = get_random_string(4)
        attrname = attrname + '_jet_ref' + rnd
        return generate_relationship(base, direction, return_fn, attrname,
                                     local_cls, referred_cls, **kw)

    MappedBase.prepare(
        name_for_scalar_relationship=name_for_scalar_relationship,
        name_for_collection_relationship=name_for_collection_relationship,
        generate_relationship=custom_generate_relationship)

    for table_name, table in MappedBase.metadata.tables.items():
        if len(table.primary_key.columns
               ) == 0 and table_name not in MappedBase.classes:
            logger.warning(
                'Table "{}" does not have primary key and will be ignored'.
                format(table_name))
コード例 #7
0
def map_data_type(value):
    for rule in reversed(map_data_types):
        if isinstance(value, rule['query']):
            return rule['date_type']
    logger.warning('Unknown database type: {}'.format(str(value)))
    return default_data_type
コード例 #8
0
ファイル: configuration.py プロジェクト: omran78/jet-bridge
 def on_model_pre_create(self, model, instance):
     try:
         model_cls, django_instance = self.get_django_instance(model, instance)
         pre_save.send(model_cls, raw=True, using=self, instance=django_instance, update_fields=[])
     except Exception as e:
         logger.warning('[!] on_model_pre_create signal failed: {}'.format(str(e)))
コード例 #9
0
ファイル: configuration.py プロジェクト: omran78/jet-bridge
 def on_model_post_delete(self, model, instance):
     try:
         model_cls = self.model_classes.get(model)
         post_delete.send(model_cls, using=self, instance=self.pre_delete_django_instance)
     except Exception as e:
         logger.warning('[!] on_model_post_delete signal failed: {}'.format(str(e)))