Ejemplo n.º 1
0
    def setup(self):
        update_session_options(db)  # get rid of the zope transaction extension

        self.app = app = IndicoFlask('indico_zodbimport')
        app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
        app.config['PLUGINENGINE_PLUGINS'] = self.plugins
        app.config['SQLALCHEMY_DATABASE_URI'] = self.sqlalchemy_uri
        plugin_engine.init_app(app)
        if not plugin_engine.load_plugins(app):
            print cformat(
                '%{red!}Could not load some plugins: {}%{reset}').format(
                    ', '.join(plugin_engine.get_failed_plugins(app)))
            sys.exit(1)
        db.init_app(app)
        import_all_models()
        alembic_migrate.init_app(
            app, db, os.path.join(app.root_path, '..', 'migrations'))

        self.connect_zodb()

        try:
            self.tz = pytz.timezone(
                getattr(self.zodb_root['MaKaCInfo']['main'], '_timezone',
                        'UTC'))
        except KeyError:
            self.tz = pytz.utc

        with app.app_context():
            if not self.pre_check():
                sys.exit(1)

            if self.destructive:
                print cformat('%{yellow!}*** DANGER')
                print cformat(
                    '%{yellow!}***%{reset} '
                    '%{red!}ALL DATA%{reset} in your database %{yellow!}{!r}%{reset} will be '
                    '%{red!}PERMANENTLY ERASED%{reset}!').format(db.engine.url)
                if raw_input(
                        cformat(
                            '%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: '
                        )) != 'YES':
                    print 'Aborting'
                    sys.exit(1)
                delete_all_tables(db)
                stamp()
                db.create_all()
            if self.has_data():
                # Usually there's no good reason to migrate with data in the DB. However, during development one might
                # comment out some migration tasks and run the migration anyway.
                print cformat('%{yellow!}*** WARNING')
                print cformat(
                    '%{yellow!}***%{reset} Your database is not empty, migration will most likely fail!'
                )
                if raw_input(
                        cformat(
                            '%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: '
                        )) != 'YES':
                    print 'Aborting'
                    sys.exit(1)
Ejemplo n.º 2
0
Archivo: cli.py Proyecto: florv/indico
    def setup(self):
        update_session_options(db)  # get rid of the zope transaction extension

        self.app = app = IndicoFlask("indico_zodbimport")
        app.config["PLUGINENGINE_NAMESPACE"] = "indico.plugins"
        app.config["PLUGINENGINE_PLUGINS"] = self.plugins
        app.config["SQLALCHEMY_DATABASE_URI"] = self.sqlalchemy_uri
        app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
        plugin_engine.init_app(app)
        if not plugin_engine.load_plugins(app):
            print(
                cformat("%{red!}Could not load some plugins: {}%{reset}").format(
                    ", ".join(plugin_engine.get_failed_plugins(app))
                )
            )
            sys.exit(1)
        db.init_app(app)
        import_all_models()
        alembic_migrate.init_app(app, db, os.path.join(app.root_path, "..", "migrations"))

        self.connect_zodb()

        try:
            self.tz = pytz.timezone(getattr(self.zodb_root["MaKaCInfo"]["main"], "_timezone", "UTC"))
        except KeyError:
            self.tz = pytz.utc

        with app.app_context():
            if not self.pre_check():
                sys.exit(1)

            if self.destructive:
                print(cformat("%{yellow!}*** DANGER"))
                print(
                    cformat(
                        "%{yellow!}***%{reset} "
                        "%{red!}ALL DATA%{reset} in your database %{yellow!}{!r}%{reset} will be "
                        "%{red!}PERMANENTLY ERASED%{reset}!"
                    ).format(db.engine.url)
                )
                if raw_input(cformat("%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: ")) != "YES":
                    print("Aborting")
                    sys.exit(1)
                delete_all_tables(db)
                stamp()
                db.create_all()
            if self.has_data():
                # Usually there's no good reason to migrate with data in the DB. However, during development one might
                # comment out some migration tasks and run the migration anyway.
                print(cformat("%{yellow!}*** WARNING"))
                print(
                    cformat(
                        "%{yellow!}***%{reset} Your database is not empty, migration may fail or add duplicate " "data!"
                    )
                )
                if raw_input(cformat("%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: ")) != "YES":
                    print("Aborting")
                    sys.exit(1)
Ejemplo n.º 3
0
def setup(logger, zodb_root, sqlalchemy_uri, dblog=False, restore=False):
    app = IndicoFlask('indico_migrate')
    app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
    app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    _monkeypatch_config()

    plugin_engine.init_app(app)
    if not plugin_engine.load_plugins(app):
        print(
            cformat('%[red!]Could not load some plugins: {}%[reset]').format(
                ', '.join(plugin_engine.get_failed_plugins(app))))
        sys.exit(1)
    db.init_app(app)
    if dblog:
        app.debug = True
        apply_db_loggers(app, force=True)
        db_logger = Logger.get('_db')
        db_logger.level = logging.DEBUG
        db_logger.propagate = False
        db_logger.addHandler(SocketHandler('127.0.0.1', 9020))

    # avoid "no handlers registered" warnings
    logging.root.addHandler(logging.NullHandler())

    import_all_models()
    configure_mappers()
    alembic_migrate.init_app(app, db, os.path.join(app.root_path,
                                                   'migrations'))

    try:
        tz = pytz.timezone(
            getattr(zodb_root['MaKaCInfo']['main'], '_timezone', 'UTC'))
    except KeyError:
        tz = pytz.utc

    with app.app_context():
        if not restore:
            all_tables = sum(get_all_tables(db).values(), [])
            if all_tables:
                if db_has_data():
                    logger.fatal_error(
                        'Your database is not empty!\n'
                        'If you want to reset it, please drop and recreate it first.'
                    )
            else:
                # the DB is empty, prepare DB tables
                # prevent alembic from messing with the logging config
                tmp = logging.config.fileConfig
                logging.config.fileConfig = lambda fn: None
                prepare_db(empty=True,
                           root_path=get_root_path('indico'),
                           verbose=False)
                logging.config.fileConfig = tmp
                _create_oauth_apps()
    return app, tz
Ejemplo n.º 4
0
def make_app(testing=False, config_override=None):
    # If you are reading this code and wonder how to access the app:
    # >>> from flask import current_app as app
    # This only works while inside an application context but you really shouldn't have any
    # reason to access it outside this method without being inside an application context.

    if _app_ctx_stack.top:
        Logger.get('flask').warning(
            'make_app called within app context, using existing app')
        return _app_ctx_stack.top.app
    app = IndicoFlask('indico',
                      static_folder='web/static',
                      static_url_path='/',
                      template_folder='web/templates')
    app.config['TESTING'] = testing
    app.config['INDICO'] = load_config(only_defaults=testing,
                                       override=config_override)
    configure_app(app)

    with app.app_context():
        if not testing:
            Logger.init(app)
            init_sentry(app)
        celery.init_app(app)
        cache.init_app(app)
        babel.init_app(app)
        if config.DEFAULT_LOCALE not in get_all_locales():
            Logger.get('i18n').error(
                f'Configured DEFAULT_LOCALE ({config.DEFAULT_LOCALE}) does not exist'
            )
        multipass.init_app(app)
        setup_oauth_provider(app)
        webpack.init_app(app)
        setup_jinja(app)
        configure_db(app)
        mm.init_app(app)  # must be called after `configure_db`!
        limiter.init_app(app)
        extend_url_map(app)
        add_handlers(app)
        setup_request_stats(app)
        add_blueprints(app)
        plugin_engine.init_app(app, Logger.get('plugins'))
        if not plugin_engine.load_plugins(app):
            raise Exception('Could not load some plugins: {}'.format(', '.join(
                plugin_engine.get_failed_plugins(app))))
        setup_jinja_customization(app)
        # Below this points plugins are available, i.e. sending signals makes sense
        add_plugin_blueprints(app)
        # themes can be provided by plugins
        signals.core.app_created.send(app)
        config.validate()
        check_db()

    return app
Ejemplo n.º 5
0
    def setup(self):
        self.app = app = IndicoFlask('indico_zodbimport')
        app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
        app.config['PLUGINENGINE_PLUGINS'] = self.plugins
        app.config['SQLALCHEMY_DATABASE_URI'] = self.sqlalchemy_uri
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
        plugin_engine.init_app(app)
        if not plugin_engine.load_plugins(app):
            print(
                cformat(
                    '%{red!}Could not load some plugins: {}%{reset}').format(
                        ', '.join(plugin_engine.get_failed_plugins(app))))
            sys.exit(1)
        db.init_app(app)
        setup_request_stats(app)
        if self.dblog:
            app.debug = True
            apply_db_loggers(app)
        import_all_models()
        alembic_migrate.init_app(app, db,
                                 os.path.join(app.root_path, 'migrations'))

        self.connect_zodb()

        try:
            self.tz = pytz.timezone(
                getattr(self.zodb_root['MaKaCInfo']['main'], '_timezone',
                        'UTC'))
        except KeyError:
            self.tz = pytz.utc

        with app.app_context():
            request_stats_request_started()

            if not self.pre_check():
                sys.exit(1)

            if self.has_data():
                # Usually there's no good reason to migrate with data in the DB. However, during development one might
                # comment out some migration tasks and run the migration anyway.
                print(cformat('%{yellow!}*** WARNING'))
                print(
                    cformat(
                        '%{yellow!}***%{reset} Your database is not empty, migration may fail or add duplicate '
                        'data!'))
                if raw_input(
                        cformat(
                            '%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: '
                        )) != 'YES':
                    print('Aborting')
                    sys.exit(1)
Ejemplo n.º 6
0
def make_app(set_path=False, testing=False, config_override=None):
    # If you are reading this code and wonder how to access the app:
    # >>> from flask import current_app as app
    # This only works while inside an application context but you really shouldn't have any
    # reason to access it outside this method without being inside an application context.
    # When set_path is enabled, SERVER_NAME and APPLICATION_ROOT are set according to BASE_URL
    # so URLs can be generated without an app context, e.g. in the indico shell

    if _app_ctx_stack.top:
        Logger.get('flask').warn(
            'make_app({}) called within app context, using existing app:\n{}'.
            format(set_path, '\n'.join(traceback.format_stack())))
        return _app_ctx_stack.top.app
    app = IndicoFlask('indico',
                      static_folder=None,
                      template_folder='web/templates')
    app.config['TESTING'] = testing
    app.config['INDICO'] = load_config(only_defaults=testing,
                                       override=config_override)
    configure_app(app, set_path)

    with app.app_context():
        if not testing:
            Logger.init(app)
        celery.init_app(app)
        babel.init_app(app)
        multipass.init_app(app)
        oauth.init_app(app)
        setup_mako(app)
        setup_jinja(app)

        core_env.init_app(app)
        setup_assets()

        configure_db(app)
        mm.init_app(app)
        extend_url_map(app)
        add_handlers(app)
        setup_request_stats(app)
        add_blueprints(app)
        plugin_engine.init_app(app, Logger.get('plugins'))
        if not plugin_engine.load_plugins(app):
            raise Exception('Could not load some plugins: {}'.format(', '.join(
                plugin_engine.get_failed_plugins(app))))
        # Below this points plugins are available, i.e. sending signals makes sense
        add_plugin_blueprints(app)
        # themes can be provided by plugins
        register_theme_sass()
        signals.app_created.send(app)
    return app
Ejemplo n.º 7
0
    def setup(self):
        update_session_options(db)  # get rid of the zope transaction extension

        self.app = app = IndicoFlask('indico_zodbimport')
        app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
        app.config['PLUGINENGINE_PLUGINS'] = self.plugins
        app.config['SQLALCHEMY_DATABASE_URI'] = self.sqlalchemy_uri
        plugin_engine.init_app(app)
        if not plugin_engine.load_plugins(app):
            print(cformat('%{red!}Could not load some plugins: {}%{reset}').format(
                ', '.join(plugin_engine.get_failed_plugins(app))))
            sys.exit(1)
        db.init_app(app)
        import_all_models()
        alembic_migrate.init_app(app, db, os.path.join(app.root_path, '..', 'migrations'))

        self.connect_zodb()

        try:
            self.tz = pytz.timezone(getattr(self.zodb_root['MaKaCInfo']['main'], '_timezone', 'UTC'))
        except KeyError:
            self.tz = pytz.utc

        with app.app_context():
            if not self.pre_check():
                sys.exit(1)

            if self.destructive:
                print(cformat('%{yellow!}*** DANGER'))
                print(cformat('%{yellow!}***%{reset} '
                              '%{red!}ALL DATA%{reset} in your database %{yellow!}{!r}%{reset} will be '
                              '%{red!}PERMANENTLY ERASED%{reset}!').format(db.engine.url))
                if raw_input(cformat('%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: ')) != 'YES':
                    print('Aborting')
                    sys.exit(1)
                delete_all_tables(db)
                stamp()
                db.create_all()
            if self.has_data():
                # Usually there's no good reason to migrate with data in the DB. However, during development one might
                # comment out some migration tasks and run the migration anyway.
                print(cformat('%{yellow!}*** WARNING'))
                print(cformat('%{yellow!}***%{reset} Your database is not empty, migration may fail or add duplicate '
                              'data!'))
                if raw_input(cformat('%{yellow!}***%{reset} To confirm this, enter %{yellow!}YES%{reset}: ')) != 'YES':
                    print('Aborting')
                    sys.exit(1)
Ejemplo n.º 8
0
def make_app(set_path=False, db_setup=True, testing=False):
    # If you are reading this code and wonder how to access the app:
    # >>> from flask import current_app as app
    # This only works while inside an application context but you really shouldn't have any
    # reason to access it outside this method without being inside an application context.
    # When set_path is enabled, SERVER_NAME and APPLICATION_ROOT are set according to BaseURL
    # so URLs can be generated without an app context, e.g. in the indico shell

    if _app_ctx_stack.top:
        Logger.get('flask').warn(
            'make_app({}) called within app context, using existing app:\n{}'.
            format(set_path, '\n'.join(traceback.format_stack())))
        return _app_ctx_stack.top.app
    app = IndicoFlask('indico',
                      static_folder=None,
                      template_folder='web/templates')
    app.config['TESTING'] = testing
    fix_root_path(app)
    configure_app(app, set_path)

    babel.init_app(app)
    multipass.init_app(app)
    setup_jinja(app)

    with app.app_context():
        setup_assets()

    if db_setup:
        configure_db(app)

    extend_url_map(app)
    add_handlers(app)
    add_blueprints(app)

    if app.config['INDICO_COMPAT_ROUTES']:
        add_compat_blueprints(app)
    Logger.init_app(app)
    plugin_engine.init_app(app, Logger.get('plugins'))
    if not plugin_engine.load_plugins(app):
        raise Exception('Could not load some plugins: {}'.format(', '.join(
            plugin_engine.get_failed_plugins(app))))
    # Below this points plugins are available, i.e. sending signals makes sense
    add_plugin_blueprints(app)
    signals.app_created.send(app)
    return app
Ejemplo n.º 9
0
def make_app(set_path=False, testing=False, config_override=None):
    # If you are reading this code and wonder how to access the app:
    # >>> from flask import current_app as app
    # This only works while inside an application context but you really shouldn't have any
    # reason to access it outside this method without being inside an application context.
    # When set_path is enabled, SERVER_NAME and APPLICATION_ROOT are set according to BASE_URL
    # so URLs can be generated without an app context, e.g. in the indico shell

    if _app_ctx_stack.top:
        Logger.get('flask').warn('make_app called within app context, using existing app')
        return _app_ctx_stack.top.app
    app = IndicoFlask('indico', static_folder=None, template_folder='web/templates')
    app.config['TESTING'] = testing
    app.config['INDICO'] = load_config(only_defaults=testing, override=config_override)
    configure_app(app, set_path)

    with app.app_context():
        if not testing:
            Logger.init(app)
        celery.init_app(app)
        babel.init_app(app)
        multipass.init_app(app)
        oauth.init_app(app)
        setup_mako(app)
        setup_jinja(app)

        core_env.init_app(app)
        setup_assets()

        configure_db(app)
        mm.init_app(app)
        extend_url_map(app)
        add_handlers(app)
        setup_request_stats(app)
        add_blueprints(app)
        plugin_engine.init_app(app, Logger.get('plugins'))
        if not plugin_engine.load_plugins(app):
            raise Exception('Could not load some plugins: {}'.format(', '.join(plugin_engine.get_failed_plugins(app))))
        # Below this points plugins are available, i.e. sending signals makes sense
        add_plugin_blueprints(app)
        # themes can be provided by plugins
        register_theme_sass()
        signals.app_created.send(app)
    return app
Ejemplo n.º 10
0
def setup(logger, zodb_root, sqlalchemy_uri, dblog=False, restore=False):
    app = IndicoFlask('indico_migrate')
    app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
    app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    _monkeypatch_config()

    plugin_engine.init_app(app)
    if not plugin_engine.load_plugins(app):
        print(
            cformat('%[red!]Could not load some plugins: {}%[reset]').format(
                ', '.join(plugin_engine.get_failed_plugins(app))))
        sys.exit(1)
    db.init_app(app)
    if dblog:
        app.debug = True
        apply_db_loggers(app)

    import_all_models()
    configure_mappers()
    alembic_migrate.init_app(app, db, os.path.join(app.root_path,
                                                   'migrations'))

    try:
        tz = pytz.timezone(
            getattr(zodb_root['MaKaCInfo']['main'], '_timezone', 'UTC'))
    except KeyError:
        tz = pytz.utc

    with app.app_context():
        if not restore:
            all_tables = sum(get_all_tables(db).values(), [])
            if all_tables:
                if db_has_data():
                    logger.fatal_error(
                        'Your database is not empty!\n'
                        'If you want to reset it, please drop and recreate it first.'
                    )
            else:
                # the DB is empty, prepare DB tables
                prepare_db(empty=True,
                           root_path=get_root_path('indico'),
                           verbose=False)
    return app, tz
Ejemplo n.º 11
0
Archivo: app.py Proyecto: k3njiy/indico
def make_app(set_path=False, db_setup=True, testing=False):
    # If you are reading this code and wonder how to access the app:
    # >>> from flask import current_app as app
    # This only works while inside an application context but you really shouldn't have any
    # reason to access it outside this method without being inside an application context.
    # When set_path is enabled, SERVER_NAME and APPLICATION_ROOT are set according to BaseURL
    # so URLs can be generated without an app context, e.g. in the indico shell

    if _app_ctx_stack.top:
        Logger.get("flask").warn(
            "make_app({}) called within app context, using existing app:\n{}".format(
                set_path, "\n".join(traceback.format_stack())
            )
        )
        return _app_ctx_stack.top.app
    app = IndicoFlask("indico", static_folder=None, template_folder="web/templates")
    app.config["TESTING"] = testing
    fix_root_path(app)
    configure_app(app, set_path)
    celery.init_app(app)

    babel.init_app(app)
    multipass.init_app(app)
    oauth.init_app(app)
    setup_jinja(app)

    with app.app_context():
        setup_assets()

    if db_setup:
        configure_db(app)

    extend_url_map(app)
    add_handlers(app)
    add_blueprints(app)
    Logger.init_app(app)
    plugin_engine.init_app(app, Logger.get("plugins"))
    if not plugin_engine.load_plugins(app):
        raise Exception("Could not load some plugins: {}".format(", ".join(plugin_engine.get_failed_plugins(app))))
    # Below this points plugins are available, i.e. sending signals makes sense
    add_plugin_blueprints(app)
    signals.app_created.send(app)
    return app