Example #1
0
 def aux(ctx, service_name, service):
     compiler, _ = get_compiler(config, service)
     service_tfs = compiler.compile(ctx, service_name, service, barts)
     ctx = ctx + [service_name]
     for child_name, child in compiler.children(service):
         util.merge_into(service_tfs, aux(ctx, child_name, child))
     return service_tfs
Example #2
0
 def aux(ctx, service_name, service):
     compiler, _ = get_compiler(config, service)
     service_tfs = compiler.compile(ctx, service_name, service, barts)
     ctx = ctx + [service_name]
     for child_name, child in compiler.children(service):
         util.merge_into(service_tfs, aux(ctx, child_name, child))
     return service_tfs
Example #3
0
 def aux(ctx, service_name, service):
     compiler, environment = get_compiler(config, service)
     new_service, service_barts = compiler.getBuildArtefacts(environment, ctx, service)
     ctx = ctx + [service_name]
     for child_name, child in compiler.children(service):
         new_child, child_barts = aux(ctx, child_name, child)
         util.merge_into(service_barts, child_barts)
         new_service[child_name] = new_child
     return new_service, service_barts
Example #4
0
 def aux(ctx, service_name, service):
     compiler, environment = get_compiler(config, service)
     new_service, service_barts = compiler.getBuildArtefacts(
         environment, ctx, service)
     ctx = ctx + [service_name]
     for child_name, child in compiler.children(service):
         new_child, child_barts = aux(ctx, child_name, child)
         util.merge_into(service_barts, child_barts)
         new_service[child_name] = new_child
     return new_service, service_barts
Example #5
0
 def add_file(self, path: str) -> None:
     with open(path, 'r') as stream:
         try:
             source = yaml.load(stream.read())
         except yaml.YAMLError as e:
             raise UserError(
                 f"illegal config: malformed variables file at '{path}': {e}"
             ) from e
         merge_into(self._data,
                    util.post_process(value=source, context=self.data))
Example #6
0
def test_merge_into():
    dst = {'k4': {'v41': 'vv41'}}
    src1 = {'k1': 'v1'}
    src2 = {'k2': 'v2'}
    src3 = {'k3': {'v3': 'vv3'}}
    src4 = {'k4': {'v4': 'vv4'}}
    result = merge_into(dst, src1, src2, src3, src4)
    assert result is dst
    assert result['k1'] == 'v1'
    assert result['k2'] == 'v2'
    assert isinstance(result['k3'], dict)
    assert result['k3']['v3'] == 'vv3'
    assert result['k4']['v41'] == 'vv41'
    assert result['k4']['v4'] == 'vv4'

    result = merge(src1, src2, src3)
    assert result is not dst
    assert result is not src1
    assert result is not src2
    assert result is not src3
    assert result['k1'] == 'v1'
    assert result['k2'] == 'v2'
    assert isinstance(result['k3'], dict)
    assert result['k3']['v3'] == 'vv3'
    assert 'k1' in src1
    assert 'k1' not in src2
    assert 'k1' not in src3
    assert 'k2' not in src1
    assert 'k2' in src2
    assert 'k2' not in src3
    assert 'k3' not in src1
    assert 'k3' not in src2
    assert 'k3' in src3
Example #7
0
def compile(config, barts):
    def aux(ctx, service_name, service):
        compiler, _ = get_compiler(config, service)
        service_tfs = compiler.compile(ctx, service_name, service, barts)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            util.merge_into(service_tfs, aux(ctx, child_name, child))
        return service_tfs

    tfs = {}

    for service_name, service in services(config):
        util.merge_into(tfs, aux([], service_name, service))

    for environment_name, environment in config['environments'].iteritems():
        compiler = service_compilers[environment['kind']]
        tf_dict = compiler.compileProvider(environment_name, environment)
        util.merge_into(tfs, tf_dict)

    # Avoid a warning from Terraform that there are no tf files
    if not len(tfs):
        tfs['empty.tf.json'] = {}

    # Clean up the output JSON files because Terraform is quite picky.
    for tfname, tf in tfs.iteritems():
        if 'resource' in tf:
            new_resources = {}
            for rtype_name, rtype_dict in list(tf['resource'].iteritems()):
                for r_name, r_dict in rtype_dict.iteritems():
                    # Ensure depends_on is always a list.
                    # TODO: Doesn't seem too hard to make sure all configs just provide it as a
                    # list.
                    if 'depends_on' in r_dict and isinstance(
                            r_dict['depends_on'], basestring):
                        r_dict['depends_on'] = [r_dict['depends_on']]

                # Remove types that have no resources.
                if not rtype_dict:
                    del tf['resource'][rtype_name]

        # Remove empty dicts at the top level.
        if 'resource' in tf and not tf['resource']:
            del tf['resource']
        if 'output' in tf and not tf['output']:
            del tf['output']
    return tfs
Example #8
0
def get_build_artefacts(config):
    """Create all required build artefacts, modify config to refer to them."""
    def aux(ctx, service_name, service):
        compiler, environment = get_compiler(config, service)
        new_service, service_barts = compiler.getBuildArtefacts(
            environment, ctx, service)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            new_child, child_barts = aux(ctx, child_name, child)
            util.merge_into(service_barts, child_barts)
            new_service[child_name] = new_child
        return new_service, service_barts

    barts = {}
    new_config = {
        'environments': config['environments'],
    }
    for service_name, service in services(config):
        new_service, service_barts = aux([], service_name, service)
        new_config[service_name] = new_service
        util.merge_into(barts, service_barts)
    return new_config, barts
Example #9
0
def get_build_artefacts(config):
    """Create all required build artefacts, modify config to refer to them."""

    def aux(ctx, service_name, service):
        compiler, environment = get_compiler(config, service)
        new_service, service_barts = compiler.getBuildArtefacts(environment, ctx, service)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            new_child, child_barts = aux(ctx, child_name, child)
            util.merge_into(service_barts, child_barts)
            new_service[child_name] = new_child
        return new_service, service_barts

    barts = {}
    new_config = {
        'environments': config['environments'],
    }
    for service_name, service in services(config):
        new_service, service_barts = aux([], service_name, service)
        new_config[service_name] = new_service
        util.merge_into(barts, service_barts)
    return new_config, barts
Example #10
0
def compile(config, barts):
    def aux(ctx, service_name, service):
        compiler, _ = get_compiler(config, service)
        service_tfs = compiler.compile(ctx, service_name, service, barts)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            util.merge_into(service_tfs, aux(ctx, child_name, child))
        return service_tfs

    tfs = {}

    for service_name, service in services(config):
        util.merge_into(tfs, aux([], service_name, service))

    for environment_name, environment in config['environments'].iteritems():
        compiler = service_compilers[environment['kind']]
        tf_dict = compiler.compileProvider(environment_name, environment)
        util.merge_into(tfs, tf_dict)

    # Avoid a warning from Terraform that there are no tf files
    if not len(tfs):
        tfs['empty.tf'] = {}

    for tfname, tf in tfs.iteritems():
        if 'resource' in tf:
            new_resources = {}
            for rtype_name, rtype_dict in tf['resource'].iteritems():
                if rtype_dict:
                    new_resources[rtype_name] = rtype_dict
                    for r_name, r_dict in rtype_dict.iteritems():
                        # depends_on changed to always be a list.
                        if 'depends_on' in r_dict and isinstance(
                                r_dict['depends_on'], basestring):
                            r_dict['depends_on'] = [r_dict['depends_on']]
            # Remove empty resource dicts, workaround for
            # https://github.com/hashicorp/terraform/issues/6368
            tf['resource'] = new_resources
    return tfs
Example #11
0
def compile(config, barts):
    def aux(ctx, service_name, service):
        compiler, _ = get_compiler(config, service)
        service_tfs = compiler.compile(ctx, service_name, service, barts)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            util.merge_into(service_tfs, aux(ctx, child_name, child))
        return service_tfs

    tfs = {}

    for service_name, service in services(config):
        util.merge_into(tfs, aux([], service_name, service))

    for environment_name, environment in config['environments'].iteritems():
        compiler = service_compilers[environment['kind']]
        tf_dict = compiler.compileProvider(environment_name, environment)
        util.merge_into(tfs, tf_dict)

    # Avoid a warning from Terraform that there are no tf files
    if not len(tfs):
        tfs['empty.tf'] = {}

    for tfname, tf in tfs.iteritems():
        if 'resource' in tf:
            new_resources = {}
            for rtype_name, rtype_dict in tf['resource'].iteritems():
                if rtype_dict:
                    new_resources[rtype_name] = rtype_dict
                    for r_name, r_dict in rtype_dict.iteritems():
                        # depends_on changed to always be a list.
                        if 'depends_on' in r_dict and isinstance(r_dict['depends_on'], basestring):
                            r_dict['depends_on'] = [r_dict['depends_on']]
            # Remove empty resource dicts, workaround for
            # https://github.com/hashicorp/terraform/issues/6368
            tf['resource'] = new_resources
    return tfs
Example #12
0
def compile(config, barts):
    def aux(ctx, service_name, service):
        compiler, _ = get_compiler(config, service)
        service_tfs = compiler.compile(ctx, service_name, service, barts)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            util.merge_into(service_tfs, aux(ctx, child_name, child))
        return service_tfs

    tfs = {}

    for service_name, service in services(config):
        util.merge_into(tfs, aux([], service_name, service))

    for environment_name, environment in config['environments'].iteritems():
        compiler = service_compilers[environment['kind']]
        tf_dict = compiler.compileProvider(environment_name, environment)
        util.merge_into(tfs, tf_dict)

    # Avoid a warning from Terraform that there are no tf files
    if not len(tfs):
        tfs['empty.tf'] = {}

    return tfs
Example #13
0
def compile(config, barts):
    def aux(ctx, service_name, service):
        compiler, _ = get_compiler(config, service)
        service_tfs = compiler.compile(ctx, service_name, service, barts)
        ctx = ctx + [service_name]
        for child_name, child in compiler.children(service):
            util.merge_into(service_tfs, aux(ctx, child_name, child))
        return service_tfs

    tfs = {}

    for service_name, service in services(config):
        util.merge_into(tfs, aux([], service_name, service))

    for environment_name, environment in config['environments'].iteritems():
        compiler = service_compilers[environment['kind']]
        tf_dict = compiler.compileProvider(environment_name, environment)
        util.merge_into(tfs, tf_dict)

    # Avoid a warning from Terraform that there are no tf files
    if not len(tfs):
        tfs['empty.tf'] = {}

    return tfs