Exemplo n.º 1
0
def secrets_add(env, file, overwrite, access_token):  # noqa: D301
    """Add secrets from literal string or from file.

    Examples: \n
    \t $ reana-client secrets-add --env PASSWORD=password \n
    \t $ reana-client secrets-add --file ~/.keytab \n
    \t $ reana-client secrets-add --env USER=reanauser \n
    \t                            --env PASSWORD=password \n
    \t                            --file ~/.keytab
    """
    from reana_client.api.client import add_secrets

    secrets_ = {}
    for literal in env:
        secret = parse_secret_from_literal(literal)
        secrets_.update(secret)
    for path in file:
        secret = parse_secret_from_path(path)
        secrets_.update(secret)
    try:
        add_secrets(secrets_, overwrite, access_token)
    except REANASecretAlreadyExists as e:
        logging.debug(str(e), exc_info=True)
        click.echo(
            click.style(
                "One of the secrets already exists. No secrets were added. "
                "If you want to overwrite it use --overwrite option.",
                fg="red",
            ),
            err=True,
        )
        sys.exit(1)
    except Exception as e:
        logging.debug(str(e), exc_info=True)
        click.echo(
            click.style("Something went wrong while uploading secrets",
                        fg="red"),
            err=True,
        )
    else:
        click.echo(
            click.style(
                "Secrets {} were successfully uploaded.".format(", ".join(
                    secrets_.keys())),
                fg="green",
            ))
Exemplo n.º 2
0
def secrets_add(from_literal, from_file, overwrite,
                access_token):  # noqa: D301
    """Add secrets from literal string or from file.

    Examples: \n
    \t $ reana-client secrets-add --from-literal PASSWORD=password \n
    \t $ reana-client secrets-add --from-file ~/.keytab \n
    \t $ reana-client secrets-add --from-literal USER=reanauser \n
    \t                            --from-literal PASSWORD=password \n
    \t                            --from-file ~/.keytab
    """
    if not access_token:
        click.echo(click.style(ERROR_MESSAGES['missing_access_token'],
                               fg='red'),
                   err=True)
        sys.exit(1)

    secrets_ = {}
    for literal in from_literal:
        secret = parse_secret_from_literal(literal)
        secrets_.update(secret)
    for path in from_file:
        secret = parse_secret_from_path(path)
        secrets_.update(secret)
    try:
        add_secrets(secrets_, overwrite, access_token)
    except REANASecretAlreadyExists as e:
        logging.debug(str(e), exc_info=True)
        click.echo(click.style(
            'One of the secrets already exists. No secrets were added. '
            'If you want to overwrite it use --overwrite option.',
            fg='red'),
                   err=True)
        sys.exit(1)
    except Exception as e:
        logging.debug(str(e), exc_info=True)
        click.echo(click.style('Something went wrong while uploading secrets',
                               fg='red'),
                   err=True)
    else:
        click.echo(
            click.style('Secrets {} were successfully uploaded.'.format(
                ', '.join(secrets_.keys())),
                        fg='green'))