mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-10-10 12:19:37 +00:00

* Use Docker BuildKit to build addons * Improve error message as suggested by CodeRabbit * Fix container.remove() tests missing v=True * Ignore squash rather than falling back to legacy builder * Use version rather than tag to avoid confusion in run_command() * Fix tests differently * Use PropertyMock like other tests * Restore position of fix_label fn * Exempt addon builder image from unsupported checks * Refactor tests * Fix tests expecting wrong builder image * Remove harcoded paths * Fix tests * Remove get_addon_host_path() function * Use docker buildx build rather than docker build Co-authored-by: Stefan Agner <stefan@agner.ch> --------- Co-authored-by: Stefan Agner <stefan@agner.ch>
188 lines
5.6 KiB
Python
188 lines
5.6 KiB
Python
"""Supervisor add-on build environment."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from functools import cached_property
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from awesomeversion import AwesomeVersion
|
|
|
|
from ..const import (
|
|
ATTR_ARGS,
|
|
ATTR_BUILD_FROM,
|
|
ATTR_LABELS,
|
|
ATTR_SQUASH,
|
|
FILE_SUFFIX_CONFIGURATION,
|
|
META_ADDON,
|
|
SOCKET_DOCKER,
|
|
)
|
|
from ..coresys import CoreSys, CoreSysAttributes
|
|
from ..docker.interface import MAP_ARCH
|
|
from ..exceptions import ConfigurationFileError, HassioArchNotFound
|
|
from ..utils.common import FileConfiguration, find_one_filetype
|
|
from .validate import SCHEMA_BUILD_CONFIG
|
|
|
|
if TYPE_CHECKING:
|
|
from .manager import AnyAddon
|
|
|
|
|
|
class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|
"""Handle build options for add-ons."""
|
|
|
|
def __init__(self, coresys: CoreSys, addon: AnyAddon) -> None:
|
|
"""Initialize Supervisor add-on builder."""
|
|
self.coresys: CoreSys = coresys
|
|
self.addon = addon
|
|
|
|
# Search for build file later in executor
|
|
super().__init__(None, SCHEMA_BUILD_CONFIG)
|
|
|
|
def _get_build_file(self) -> Path:
|
|
"""Get build file.
|
|
|
|
Must be run in executor.
|
|
"""
|
|
try:
|
|
return find_one_filetype(
|
|
self.addon.path_location, "build", FILE_SUFFIX_CONFIGURATION
|
|
)
|
|
except ConfigurationFileError:
|
|
return self.addon.path_location / "build.json"
|
|
|
|
async def read_data(self) -> None:
|
|
"""Load data from file."""
|
|
if not self._file:
|
|
self._file = await self.sys_run_in_executor(self._get_build_file)
|
|
|
|
await super().read_data()
|
|
|
|
async def save_data(self):
|
|
"""Ignore save function."""
|
|
raise RuntimeError()
|
|
|
|
@cached_property
|
|
def arch(self) -> str:
|
|
"""Return arch of the add-on."""
|
|
return self.sys_arch.match([self.addon.arch])
|
|
|
|
@property
|
|
def base_image(self) -> str:
|
|
"""Return base image for this add-on."""
|
|
if not self._data[ATTR_BUILD_FROM]:
|
|
return f"ghcr.io/home-assistant/{self.sys_arch.default}-base:latest"
|
|
|
|
if isinstance(self._data[ATTR_BUILD_FROM], str):
|
|
return self._data[ATTR_BUILD_FROM]
|
|
|
|
# Evaluate correct base image
|
|
if self.arch not in self._data[ATTR_BUILD_FROM]:
|
|
raise HassioArchNotFound(
|
|
f"Add-on {self.addon.slug} is not supported on {self.arch}"
|
|
)
|
|
return self._data[ATTR_BUILD_FROM][self.arch]
|
|
|
|
@property
|
|
def squash(self) -> bool:
|
|
"""Return True or False if squash is active."""
|
|
return self._data[ATTR_SQUASH]
|
|
|
|
@property
|
|
def additional_args(self) -> dict[str, str]:
|
|
"""Return additional Docker build arguments."""
|
|
return self._data[ATTR_ARGS]
|
|
|
|
@property
|
|
def additional_labels(self) -> dict[str, str]:
|
|
"""Return additional Docker labels."""
|
|
return self._data[ATTR_LABELS]
|
|
|
|
def get_dockerfile(self) -> Path:
|
|
"""Return Dockerfile path.
|
|
|
|
Must be run in executor.
|
|
"""
|
|
if self.addon.path_location.joinpath(f"Dockerfile.{self.arch}").exists():
|
|
return self.addon.path_location.joinpath(f"Dockerfile.{self.arch}")
|
|
return self.addon.path_location.joinpath("Dockerfile")
|
|
|
|
async def is_valid(self) -> bool:
|
|
"""Return true if the build env is valid."""
|
|
|
|
def build_is_valid() -> bool:
|
|
return all(
|
|
[
|
|
self.addon.path_location.is_dir(),
|
|
self.get_dockerfile().is_file(),
|
|
]
|
|
)
|
|
|
|
try:
|
|
return await self.sys_run_in_executor(build_is_valid)
|
|
except HassioArchNotFound:
|
|
return False
|
|
|
|
def get_docker_args(
|
|
self, version: AwesomeVersion, image_tag: str
|
|
) -> dict[str, Any]:
|
|
"""Create a dict with Docker run args."""
|
|
dockerfile_path = self.get_dockerfile().relative_to(self.addon.path_location)
|
|
|
|
build_cmd = [
|
|
"docker",
|
|
"buildx",
|
|
"build",
|
|
".",
|
|
"--tag",
|
|
image_tag,
|
|
"--file",
|
|
str(dockerfile_path),
|
|
"--platform",
|
|
MAP_ARCH[self.arch],
|
|
"--pull",
|
|
]
|
|
|
|
labels = {
|
|
"io.hass.version": version,
|
|
"io.hass.arch": self.arch,
|
|
"io.hass.type": META_ADDON,
|
|
"io.hass.name": self._fix_label("name"),
|
|
"io.hass.description": self._fix_label("description"),
|
|
**self.additional_labels,
|
|
}
|
|
|
|
if self.addon.url:
|
|
labels["io.hass.url"] = self.addon.url
|
|
|
|
for key, value in labels.items():
|
|
build_cmd.extend(["--label", f"{key}={value}"])
|
|
|
|
build_args = {
|
|
"BUILD_FROM": self.base_image,
|
|
"BUILD_VERSION": version,
|
|
"BUILD_ARCH": self.sys_arch.default,
|
|
**self.additional_args,
|
|
}
|
|
|
|
for key, value in build_args.items():
|
|
build_cmd.extend(["--build-arg", f"{key}={value}"])
|
|
|
|
# The addon path will be mounted from the host system
|
|
addon_extern_path = self.sys_config.local_to_extern_path(
|
|
self.addon.path_location
|
|
)
|
|
|
|
return {
|
|
"command": build_cmd,
|
|
"volumes": {
|
|
SOCKET_DOCKER: {"bind": "/var/run/docker.sock", "mode": "rw"},
|
|
addon_extern_path: {"bind": "/addon", "mode": "ro"},
|
|
},
|
|
"working_dir": "/addon",
|
|
}
|
|
|
|
def _fix_label(self, label_name: str) -> str:
|
|
"""Remove characters they are not supported."""
|
|
label = getattr(self.addon, label_name, "")
|
|
return label.replace("'", "")
|