A Docker image whose build and dependencies are managed by mzbuild. An image corresponds to a directory in a repository that contains a `mzbuild.yml` file. This directory is called an "mzbuild context." Attributes: name: The name of the image. publish: Whether the image
| 866 | |
| 867 | |
| 868 | class Image: |
| 869 | """A Docker image whose build and dependencies are managed by mzbuild. |
| 870 | |
| 871 | An image corresponds to a directory in a repository that contains a |
| 872 | `mzbuild.yml` file. This directory is called an "mzbuild context." |
| 873 | |
| 874 | Attributes: |
| 875 | name: The name of the image. |
| 876 | publish: Whether the image should be pushed to Docker Hub. |
| 877 | depends_on: The names of the images upon which this image depends. |
| 878 | root: The path to the root of the associated `Repository`. |
| 879 | path: The path to the directory containing the `mzbuild.yml` |
| 880 | configuration file. |
| 881 | pre_images: Optional actions to perform before running `docker build`. |
| 882 | build_args: An optional list of --build-arg to pass to the dockerfile |
| 883 | """ |
| 884 | |
| 885 | _DOCKERFILE_MZFROM_RE = re.compile(rb"^MZFROM\s*(\S+)") |
| 886 | |
| 887 | _context_files_cache: set[str] | None |
| 888 | |
| 889 | def __init__(self, rd: RepositoryDetails, path: Path): |
| 890 | self.rd = rd |
| 891 | self.path = path |
| 892 | self._context_files_cache = None |
| 893 | self.pre_images: list[PreImage] = [] |
| 894 | with open(self.path / "mzbuild.yml") as f: |
| 895 | data = yaml.safe_load(f) |
| 896 | self.name: str = data.pop("name") |
| 897 | self.publish: bool = data.pop("publish", True) |
| 898 | self.description: str | None = data.pop("description", None) |
| 899 | self.mainline: bool = data.pop("mainline", True) |
| 900 | for pre_image in data.pop("pre-image", []): |
| 901 | typ = pre_image.pop("type", None) |
| 902 | if typ == "cargo-build": |
| 903 | self.pre_images.append(CargoBuild(self.rd, self.path, pre_image)) |
| 904 | elif typ == "copy": |
| 905 | self.pre_images.append(Copy(self.rd, self.path, pre_image)) |
| 906 | else: |
| 907 | raise ValueError( |
| 908 | f"mzbuild config in {self.path} has unknown pre-image type" |
| 909 | ) |
| 910 | self.build_args = data.pop("build-args", {}) |
| 911 | |
| 912 | if re.search(r"[^A-Za-z0-9\-]", self.name): |
| 913 | raise ValueError( |
| 914 | f"mzbuild image name {self.name} contains invalid character; only alphanumerics and hyphens allowed" |
| 915 | ) |
| 916 | |
| 917 | self.depends_on: list[str] = [] |
| 918 | with open(self.path / "Dockerfile", "rb") as f: |
| 919 | for line in f: |
| 920 | match = self._DOCKERFILE_MZFROM_RE.match(line) |
| 921 | if match: |
| 922 | self.depends_on.append(match.group(1).decode()) |
| 923 | |
| 924 | def sync_description(self) -> None: |
| 925 | """Sync the description to Docker Hub if the image is publishable |