Exemple #1
0
def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    try:
        server_config = get_server_config()
        logger = get_pbench_logger(__name__, server_config)
    except Exception as e:
        print(e)
        sys.exit(1)

    url = Database.get_engine_uri(server_config, logger)
    if url is None:
        sys.exit(1)

    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

    with context.begin_transaction():
        context.run_migrations()
Exemple #2
0
def app():
    try:
        server_config = get_server_config()
    except (ConfigFileNotSpecified, BadConfig) as e:
        print(e)
        sys.exit(1)
    return create_app(server_config)
Exemple #3
0
def server_config(pytestconfig, monkeypatch):
    """
    Mock a pbench-server.cfg configuration as defined above.

    Args:
        pytestconfig: pytest environmental configuration fixture
        monkeypatch: testing environment patch fixture

    Returns:
        a PbenchServerConfig object the test case can use
    """
    cfg_file = pytestconfig.cache.get("_PBENCH_SERVER_CONFIG", None)
    monkeypatch.setenv("_PBENCH_SERVER_CONFIG", cfg_file)

    server_config = get_server_config()
    return server_config
Exemple #4
0
def main():
    try:
        server_config = get_server_config()
    except (ConfigFileNotSpecified, BadConfig) as e:
        print(e)
        sys.exit(1)
    logger = get_pbench_logger(__name__, server_config)
    try:
        host = str(server_config.get("pbench-server", "bind_host"))
        port = str(server_config.get("pbench-server", "bind_port"))
        db = str(server_config.get("Postgres", "db_uri"))
        workers = str(server_config.get("pbench-server", "workers"))
        worker_timeout = str(
            server_config.get("pbench-server", "worker_timeout"))

        # Multiple gunicorn workers will attempt to connect to the DB; rather
        # than attempt to synchronize them, detect a missing DB (from the
        # postgres URI) and create it here. It's safer to do this here,
        # where we're single-threaded.
        if not database_exists(db):
            logger.info("Postgres DB {} doesn't exist", db)
            create_database(db)
            logger.info("Created DB {}", db)
        Database.init_db(server_config, logger)
    except (NoOptionError, NoSectionError):
        logger.exception(f"{__name__}: ERROR")
        sys.exit(1)

    subprocess.run([
        "gunicorn",
        "--workers",
        workers,
        "--timeout",
        worker_timeout,
        "--pid",
        "/run/pbench-server/gunicorn.pid",
        "--bind",
        f"{host}:{port}",
        "pbench.cli.server.shell:app()",
    ])
Exemple #5
0
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    try:
        server_config = get_server_config()
        logger = get_pbench_logger(__name__, server_config)
    except Exception as e:
        print(e)
        sys.exit(1)

    connectable = create_engine(Database.get_engine_uri(server_config, logger))

    with connectable.connect() as connection:
        context.configure(connection=connection,
                          target_metadata=target_metadata)

        with context.begin_transaction():
            context.run_migrations()
Exemple #6
0
def main():
    try:
        server_config = get_server_config()
    except (ConfigFileNotSpecified, BadConfig) as e:
        print(e)
        sys.exit(1)
    try:
        host = str(server_config.get("pbench-server", "bind_host"))
        port = str(server_config.get("pbench-server", "bind_port"))
        workers = str(server_config.get("pbench-server", "workers"))
    except (NoOptionError, NoSectionError) as e:
        print(f"{__name__}: ERROR: {e.__traceback__}")
        sys.exit(1)

    subprocess.run([
        "gunicorn",
        "--workers",
        workers,
        "--pid",
        "/run/pbench-server/gunicorn.pid",
        "--bind",
        f"{host}:{port}",
        "pbench.cli.server.shell:app()",
    ])
Exemple #7
0
def server_config(pytestconfig, monkeypatch):
    cfg_file = pytestconfig.cache.get("_PBENCH_SERVER_CONFIG", None)
    monkeypatch.setenv("_PBENCH_SERVER_CONFIG", cfg_file)

    server_config = get_server_config()
    return server_config