ftb-docker/docker.py

53 lines
2.3 KiB
Python
Raw Normal View History

2022-11-21 16:33:29 +01:00
from subprocess import run
from typing import Literal, get_args
Platforms = Literal["linux/amd64", "linux/amd64/v2", "linux/amd64/v3", "linux/arm64", "linux/riscv64", "linux/ppc64le", "linux/s390x", "linux/386", "linux/mips64le", "linux/mips64", "linux/arm/v7", "linux/arm/v6"]
supported_platforms: list[Platforms] = list(get_args(Platforms))
Progress = Literal["auto", "plain", "tty"]
def semver_tags(version: str):
parts = version.split('.')
return list('.'.join(parts[:i]) for i in range(1, len(parts) + 1))
def pull(repository: str, tag: str):
label = f"{repository}:{tag}"
run(["docker", "pull", label], check=True)
return label
def push_image(repository: str, tag: str):
label = f"{repository}:{tag}"
run(["docker", "push", label], check=True)
return label
def create_manifest(repository: str, manifest_tag: str, image_tags: list[str], push: bool = True, write_command: bool = False):
raise Exception("Creation of manifests is not yet supported!")
manifest = f"{repository}:{manifest_tag}"
images = [f"{repository}:{tag}" for tag in image_tags]
for image_tag in image_tags:
pull(repository, image_tag)
command = ["docker", "manifest", "create", manifest, *images]
if write_command:
print(" ".join(command))
run(command, check=True)
if push:
return push_image(repository, manifest_tag)
return manifest
def buildx(repository: str, tags: list[str], build_platforms: list[Platforms], dockerfile: str = "Dockerfile", build_args: dict[str, str] | None = None, directory: str = ".", push: bool = True, pull: bool = False, progress: Progress = "auto", write_command: bool = False):
if build_args is None:
build_args = dict()
labels = [f"{repository}:{tag}" for tag in tags]
command = list(filter(None, ["docker", "buildx", "build",
"--platform", ",".join(build_platforms),
*[t for (key, value) in build_args.items() for t in ("--build-arg", f"{key}={value}")],
"--file", dockerfile,
*[t for label in labels for t in ("--tag", label)],
f"--progress={progress}",
"--pull" if pull else None,
"--push" if push else None,
directory]))
if write_command:
print(" ".join(command))
run(command, check=True)