push_operations = dict()
for service_name, service in compose_file.services.items():
    if "build" in service:
        compose_image = "{}_{}".format(project_name, service_name)
        registry_image = "{}/{}:{}".format(registry, compose_image, version)
        # Re-tag the image so that it can be uploaded to the registry.
        subprocess.check_call(["docker", "tag", compose_image, registry_image])
        # Spawn "docker push" to upload the image.
        push_operations[service_name] = subprocess.Popen(
            ["docker", "push", registry_image])
        # Replace the "build" definition by an "image" definition,
        # using the name of the image on the registry.
        del service["build"]
        service["image"] = registry_image

# Wait for push operations to complete.
for service_name, popen_object in push_operations.items():
    print("Waiting for {} push to complete...".format(service_name))
    popen_object.wait()
    print("Done.")

# Write the new docker-compose.yml file.
if "COMPOSE_FILE" not in os.environ:
    os.environ["COMPOSE_FILE"] = "docker-compose-p.yml-{}".format(version)
    print("Writing to new Compose file:")
else:
    print("Writing to provided Compose file:")

print("COMPOSE_FILE={}".format(os.environ["COMPOSE_FILE"]))
compose_file.save()
def generate_local_addr():
    last_byte = 2
    while last_byte < 255:
        yield "127.127.0.{}".format(last_byte)
        last_byte += 1


for service_name, service in config.services.items():
    if "links" in service:
        for link, local_addr in zip(service["links"], generate_local_addr()):
            if link not in ports:
                print(
                    "Skipping link {} in service {} "
                    "(no port mapping defined). "
                    "Your code will probably break.".format(link, service_name)
                )
                continue
            if "extra_hosts" not in service:
                service["extra_hosts"] = {}
            service["extra_hosts"][link] = local_addr
        del service["links"]
    if "ports" in service:
        del service["ports"]
    if "volumes" in service:
        del service["volumes"]
    if service_name in ports:
        service["ports"] = [ports[service_name]]

config.save()
push_operations = dict()
for service_name, service in compose_file.services.items():
    if "build" in service:
        compose_image = "{}_{}".format(project_name, service_name)
        registry_image = "{}/{}_{}:{}".format(registry, project_name, service_name, version)
        # Re-tag the image so that it can be uploaded to the registry.
        subprocess.check_call(["docker", "tag", compose_image, registry_image])
        # Spawn "docker push" to upload the image.
        push_operations[service_name] = subprocess.Popen(["docker", "push", registry_image])
        # Replace the "build" definition by an "image" definition,
        # using the name of the image on the registry.
        del service["build"]
        service["image"] = registry_image

# Wait for push operations to complete.
for service_name, popen_object in push_operations.items():
    print("Waiting for {} push to complete...".format(service_name))
    popen_object.wait()
    print("Done.")

# Write the new docker-compose.yml file.
if "COMPOSE_FILE" not in os.environ:
    os.environ["COMPOSE_FILE"] = "docker-compose.yml-{}".format(version)
    print("Writing to new Compose file:")
else:
    print("Writing to provided Compose file:")

print("COMPOSE_FILE={}".format(os.environ["COMPOSE_FILE"]))
compose_file.save()


def generate_local_addr():
    last_byte = 2
    while last_byte < 255:
        yield "127.127.0.{}".format(last_byte)
        last_byte += 1


for service_name, service in config.services.items():
    if "links" in service:
        for link, local_addr in zip(service["links"], generate_local_addr()):
            if link not in ports:
                print("Skipping link {} in service {} "
                      "(no port mapping defined). "
                      "Your code will probably break.".format(
                          link, service_name))
                continue
            if "extra_hosts" not in service:
                service["extra_hosts"] = {}
            service["extra_hosts"][link] = local_addr
        del service["links"]
    if "ports" in service:
        del service["ports"]
    if "volumes" in service:
        del service["volumes"]
    if service_name in ports:
        service["ports"] = [ports[service_name]]

config.save()