Ejemplo n.º 1
0
def test_deep_merge():
    dict_a = dict(foo=dict(a=1, b=2), bar=dict(a=3))
    dict_b = dict(foo=dict(b=3), baz=4)
    dict_c = dict(foo=dict(b=9), bar=dict(c=9))

    assert (deep_merge(dict_a, dict_b, dict_c) == dict(foo=dict(a=1, b=9),
                                                       baz=4,
                                                       bar=dict(a=3, c=9)))

    assert (deep_merge(dict_a, dict_c, dict_b) == dict(foo=dict(a=1, b=3),
                                                       baz=4,
                                                       bar=dict(a=3, c=9)))

    assert (deep_merge(dict_c, dict_b, dict_a) == dict(foo=dict(a=1, b=2),
                                                       baz=4,
                                                       bar=dict(a=3, c=9)))
Ejemplo n.º 2
0
def load_all(root: str, cluster: str, environment: str,
             method: str) -> Dict[str, Any]:
    configs: List[str] = find_files(root,
                                    cluster,
                                    environment,
                                    "config.yaml",
                                    dir_ok=False)

    LOGGER.debug("using config files: %s", configs)

    return deep_merge(*[load_yaml(f) for f in configs], method=method)
Ejemplo n.º 3
0
def get_values(directory, method, value_files, cli_values, cname, ename,
               output_format):  # pylint: disable=redefined-outer-name
    vals = deep_merge(  # pylint: disable=redefined-outer-name
        values.load_all(directory, cname, ename, method),
        *(load_yaml(p) for p in value_files),
        dict(load_cli_value(k, v) for k, v in cli_values),
        envvalues(),
        method=method,
    )

    if output_format == "json":
        print(to_json(vals))
    else:
        print(to_yaml(vals))
Ejemplo n.º 4
0
def cli_validate(method, value_files, cli_values, cname, ename, suffixes,
                 template_overrides, directory):
    vals = deep_merge(  # pylint: disable=redefined-outer-name
        values.load_all(directory, cname, ename, method),
        *(load_yaml(p) for p in value_files),
        dict(load_cli_value(k, v) for k, v in cli_values),
        envvalues(),
        method=method,
    )
    config.CONFIG = config.load_all(directory, cname, ename, method)

    eng = build(directory, cname, ename, template_overrides)

    templates = eng.list_templates()  # pylint: disable=redefined-outer-name

    if suffixes:
        templates = [
            name for name in templates if os.path.splitext(name)[1] in suffixes
        ]

    all_validated = True

    for template_path in templates:
        errors = set()

        undefined, _, invalid, secrets = analyze(template_path, vals, eng)

        if undefined or invalid:
            for var in undefined:
                errors.add("undefined variable: {}".format(var))

            for var in invalid:
                errors.add("invalid variable: {}".format(var))

        if secrets:
            if "secrets" not in config.CONFIG or "provider" not in config.CONFIG[
                    "secrets"]:
                errors.add("No secrets provider configured")

        if errors:
            all_validated = False

            click.secho("{}: ✗".format(template_path), fg="red", err=True)

            for error in errors:
                click.echo("- {}".format(error))
        else:
            click.secho("{}: ✔".format(template_path), fg="green")

    sys.exit(not all_validated)
Ejemplo n.º 5
0
def cli_gen(method, value_files, cli_values, cname, ename, suffixes,
            secret_provider, template_overrides, directory):  # pylint: disable=redefined-outer-name,too-many-arguments
    vals = deep_merge(  # pylint: disable=redefined-outer-name
        values.load_all(directory, cname, ename, method),
        *(load_yaml(p) for p in value_files),
        dict(load_cli_value(k, v) for k, v in cli_values),
        envvalues(),
        method=method,
    )

    config.CONFIG = config.load_all(directory, cname, ename, method)

    if secret_provider is not None:
        if 'secrets' not in config.CONFIG:
            config.CONFIG['secrets'] = dict()

        config.CONFIG['secrets']['provider'] = secret_provider

    eng = build(directory, cname, ename, template_overrides)

    templates = eng.list_templates()  # pylint: disable=redefined-outer-name

    if suffixes:
        templates = [
            name for name in templates if os.path.splitext(name)[1] in suffixes
        ]

    validated = True

    for template_path in templates:
        if not validate(template_path, vals, eng):
            click.echo("Failed to validate template {}".format(template_path))

            validated = False

    if not validated:
        sys.exit(1)

    try:
        for template_path in templates:
            click.echo("---")
            click.echo("# Source: {}".format(template_path))

            template_output = render(template_path, vals, eng)
            click.echo(template_output)
    except (UndefinedError, YamlValidationError) as err:
        click.secho("✗ -> {}".format(err), fg="red", err=True)
        sys.exit(1)
Ejemplo n.º 6
0
def load_all(root: str, cluster: str, environment: str,
             method: str) -> Dict[str, Any]:
    values: List[str] = find_files(root,
                                   cluster,
                                   environment,
                                   "values.yaml",
                                   dir_ok=False)

    LOGGER.debug("using value files: %s", values)

    base = dict()

    if cluster is not None:
        base['cluster'] = cluster

    if environment is not None:
        base['environment'] = environment

    return deep_merge(base, *[load_yaml(f) for f in values], method=method)