Ejemplo n.º 1
0
def create(
    path: str = typer.Argument(
        None,
        help=(
            "The directory in which the deployment will be created "
            "(will be created if does not exist)"
        ),
    ),
    config_file_name: str = typer.Option(
        "config.toml", help="The configuration file name expected in the provided path"
    ),
    copy_conf: str = typer.Option(
        None, help="The configuration to copy from (e.g. dev_config.toml)"
    ),
    create_conf: bool = typer.Option(
        False,
        help="Enable/disable creation of a default configuration file",
    ),
    dev: bool = typer.Option(
        False,
        help=(
            "Enable/disable dev mode "
            "(fills the database with test data and allows http access)"
        ),
    ),
):
    """Create a new Quetz deployment."""

    logger.info(f"creating new deployment in path {path}")

    abs_path = os.path.abspath(path)
    config_file = os.path.join(path, config_file_name)
    deployments = _get_deployments()

    if os.path.exists(path) and abs_path in deployments:
        delete_ = typer.confirm(f'Quetz deployment exists at {path}.\nOverwrite it?')
        if delete_:
            delete(path, force=True)
            del deployments[abs_path]
        else:
            typer.echo('Use the start command to start a deployment.', err=True)
            raise typer.Abort()

    Path(path).mkdir(parents=True)

    # only authorize path with a config file to avoid deletion of unexpected files
    # when deleting Quetz instance
    if not all(f == config_file_name for f in os.listdir(path)):
        typer.echo(
            f'Quetz deployment not allowed at {path}.\n'
            'The path should not contain more than the configuration file.',
            err=True,
        )
        raise typer.Abort()

    if not os.path.exists(config_file) and not (create_conf or copy_conf):
        typer.echo(
            'No configuration file provided.\n'
            'Use --create-conf or --copy-conf to produce a config file.',
            err=True,
        )
        raise typer.Abort()

    if copy_conf:
        if not os.path.exists(copy_conf):
            typer.echo(f'Config file to copy does not exist {copy_conf}.', err=True)
            raise typer.Abort()

        typer.echo(f"Copying config file from {copy_conf} to {config_file}")
        shutil.copyfile(copy_conf, config_file)

    if not os.path.exists(config_file) and create_conf:
        if dev:
            https = 'false'
        else:
            https = 'true'
        conf = create_config(https=https)
        with open(config_file, 'w') as f:
            f.write(conf)

    os.environ[_env_prefix + _env_config_file] = config_file
    config = Config(config_file)

    os.chdir(path)
    Path('channels').mkdir()
    db = get_session(config.sqlalchemy_database_url)

    _init_db(db, config)

    if dev:
        _fill_test_database(db)

    _store_deployment(abs_path, config_file_name)
Ejemplo n.º 2
0
Archivo: cli.py Proyecto: beenje/quetz
def create(
    path: str = typer.Argument(
        None,
        help=("The directory in which the deployment will be created "
              "(will be created if does not exist)"),
    ),
    copy_conf: str = typer.Option(
        None, help="The configuration to copy from (e.g. dev_config.toml)"),
    create_conf: bool = typer.Option(
        False,
        help="Enable/disable creation of a default configuration file",
    ),
    delete: bool = typer.Option(
        False,
        help="Delete the the deployment if it exists. "
        "Must be specified with --copy-conf or --create-conf",
    ),
    exists_ok: bool = typer.Option(
        False, help="Skip the creation if deployment already exists."),
    dev: bool = typer.Option(
        False,
        help=("Enable/disable dev mode "
              "(fills the database with test data and allows http access)"),
    ),
):
    """Create a new Quetz deployment."""

    logger.info(f"creating new deployment in path {path}")
    deployment_folder = Path(path).resolve()
    config_file = deployment_folder / "config.toml"

    if _is_deployment(deployment_folder):
        if exists_ok:
            logger.info(
                f'Quetz deployment already exists at {deployment_folder}.\n'
                f'Skipping creation.')
            return
        if delete and (copy_conf or create_conf):
            shutil.rmtree(deployment_folder)
        else:
            typer.echo(
                'Use the start command to start a deployment '
                'or specify --delete with --copy-conf or --create-conf.',
                err=True,
            )
            raise typer.Abort()

    deployment_folder.mkdir(parents=True, exist_ok=True)

    # only authorize path with a config file to avoid deletion of unexpected files
    # when deleting Quetz instance
    if any(f != config_file for f in deployment_folder.iterdir()):
        typer.echo(
            f'Quetz deployment not allowed at {path}.\n'
            'The path should not contain more than the configuration file.',
            err=True,
        )
        raise typer.Abort()

    if not config_file.exists() and not create_conf and not copy_conf:
        typer.echo(
            'No configuration file provided.\n'
            'Use --create-conf or --copy-conf to produce a config file.',
            err=True,
        )
        raise typer.Abort()

    if copy_conf:
        if not os.path.exists(copy_conf):
            typer.echo(f'Config file to copy does not exist {copy_conf}.',
                       err=True)
            raise typer.Abort()

        typer.echo(f"Copying config file from {copy_conf} to {config_file}")
        shutil.copyfile(copy_conf, config_file)

    if not config_file.exists() and create_conf:
        https = 'false' if dev else 'true'
        conf = create_config(https=https)
        with open(config_file, 'w') as f:
            f.write(conf)

    os.environ[_env_prefix + _env_config_file] = str(config_file.resolve())
    config = Config(str(config_file))

    deployment_folder.joinpath('channels').mkdir(exist_ok=True)
    with working_directory(path):
        db = get_session(config.sqlalchemy_database_url)
        _run_migrations(config.sqlalchemy_database_url)
        if dev:
            _fill_test_database(db)
        _set_user_roles(db, config)
Ejemplo n.º 3
0
def create(path: str,
           config_file_name: str = "config.toml",
           create_conf: bool = False,
           dev: bool = False) -> NoReturn:
    """ Create a new Quetz deployment.

    Parameters
    ----------
    path : str
        The path where to create the deployment (will be created if does not exist)
    config_file_name : str, optional
        The configuration file name expected in the provided path {default="config.toml"}
    create_conf : bool, optional
        Whether to create a default configuration file if not found in the path, or not {default=False}
    dev : bool, optional
        Whether to activate the dev mode, or not (includes filling the database with test data, http instead of https)
    """

    abs_path = os.path.abspath(path)
    config_file = os.path.join(path, config_file_name)
    deployments = _get_deployments()

    if os.path.exists(path):
        if abs_path in deployments:
            delete_ = typer.confirm(
                'Quetz deployement exists at {}.\n'.format(path) +
                'Overwrite it?')
            if delete_:
                delete(path, force=True)
                create(path, config_file_name, create_conf, dev)
                return
            else:
                typer.echo('Use the start command to start a deployement.')
                raise typer.Abort()

        # only authorize path with a config file to avoid deletion of unexpected files
        # when deleting Quetz instance
        if not all([f in [config_file_name] for f in os.listdir(path)]):
            typer.echo(
                'Quetz deployement not allowed at {}.\n'.format(path) +
                'The path should not contain more than the configuration file.'
            )
            raise typer.Abort()

        if not os.path.exists(config_file) and not create_conf:
            typer.echo(
                'Config file "{}" does not exist at {}.\n'.format(
                    config_file_name, path) +
                'Use --create-conf option to generate a default config file.')
            raise typer.Abort()
    else:
        if not create_conf:
            typer.echo(
                'No configuration file provided.\n' +
                'Use --create-conf option to generate a default config file.')
            raise typer.Abort()

        Path(path).mkdir(parents=True)

    if not os.path.exists(config_file):
        if dev:
            https = 'false'
        else:
            https = 'true'
        conf = create_config(https=https)
        with open(config_file, 'w') as f:
            f.write(conf)

    os.environ['QUETZ_CONFIG_FILE'] = config_file
    config = Config(config_file)

    os.chdir(path)
    Path('channels').mkdir()
    init_db(config.sqlalchemy_database_url)

    if dev:
        _fill_test_database(config.sqlalchemy_database_url)

    _store_deployement(abs_path, config_file_name)