mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-19 07:06:30 +00:00
Support arch specific dockerfiles (#4022)
This commit is contained in:
parent
fad75810ab
commit
2ddb5ca53f
@ -1,6 +1,7 @@
|
|||||||
"""Supervisor add-on build environment."""
|
"""Supervisor add-on build environment."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from functools import cached_property
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ from ..const import (
|
|||||||
)
|
)
|
||||||
from ..coresys import CoreSys, CoreSysAttributes
|
from ..coresys import CoreSys, CoreSysAttributes
|
||||||
from ..docker.interface import MAP_ARCH
|
from ..docker.interface import MAP_ARCH
|
||||||
from ..exceptions import ConfigurationFileError
|
from ..exceptions import ConfigurationFileError, HassioArchNotFound
|
||||||
from ..utils.common import FileConfiguration, find_one_filetype
|
from ..utils.common import FileConfiguration, find_one_filetype
|
||||||
from .validate import SCHEMA_BUILD_CONFIG
|
from .validate import SCHEMA_BUILD_CONFIG
|
||||||
|
|
||||||
@ -45,6 +46,11 @@ class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|||||||
"""Ignore save function."""
|
"""Ignore save function."""
|
||||||
raise RuntimeError()
|
raise RuntimeError()
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def arch(self) -> str:
|
||||||
|
"""Return arch of the add-on."""
|
||||||
|
return self.sys_arch.match(self.addon.arch)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def base_image(self) -> str:
|
def base_image(self) -> str:
|
||||||
"""Return base image for this add-on."""
|
"""Return base image for this add-on."""
|
||||||
@ -55,8 +61,18 @@ class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|||||||
return self._data[ATTR_BUILD_FROM]
|
return self._data[ATTR_BUILD_FROM]
|
||||||
|
|
||||||
# Evaluate correct base image
|
# Evaluate correct base image
|
||||||
arch = self.sys_arch.match(list(self._data[ATTR_BUILD_FROM].keys()))
|
if self.arch not in self._data[ATTR_BUILD_FROM]:
|
||||||
return self._data[ATTR_BUILD_FROM][arch]
|
raise HassioArchNotFound(
|
||||||
|
f"Add-on {self.addon.slug} is not supported on {self.arch}"
|
||||||
|
)
|
||||||
|
return self._data[ATTR_BUILD_FROM][self.arch]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dockerfile(self) -> Path:
|
||||||
|
"""Return Dockerfile path."""
|
||||||
|
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")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def squash(self) -> bool:
|
def squash(self) -> bool:
|
||||||
@ -76,25 +92,29 @@ class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|||||||
@property
|
@property
|
||||||
def is_valid(self) -> bool:
|
def is_valid(self) -> bool:
|
||||||
"""Return true if the build env is valid."""
|
"""Return true if the build env is valid."""
|
||||||
return all(
|
try:
|
||||||
[
|
return all(
|
||||||
self.addon.path_location.is_dir(),
|
[
|
||||||
Path(self.addon.path_location, "Dockerfile").is_file(),
|
self.addon.path_location.is_dir(),
|
||||||
]
|
self.dockerfile.is_file(),
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
except HassioArchNotFound:
|
||||||
|
return False
|
||||||
|
|
||||||
def get_docker_args(self, version: AwesomeVersion):
|
def get_docker_args(self, version: AwesomeVersion):
|
||||||
"""Create a dict with Docker build arguments."""
|
"""Create a dict with Docker build arguments."""
|
||||||
args = {
|
args = {
|
||||||
"path": str(self.addon.path_location),
|
"path": str(self.addon.path_location),
|
||||||
"tag": f"{self.addon.image}:{version!s}",
|
"tag": f"{self.addon.image}:{version!s}",
|
||||||
|
"dockerfile": str(self.dockerfile),
|
||||||
"pull": True,
|
"pull": True,
|
||||||
"forcerm": not self.sys_dev,
|
"forcerm": not self.sys_dev,
|
||||||
"squash": self.squash,
|
"squash": self.squash,
|
||||||
"platform": MAP_ARCH[self.sys_arch.match(self.addon.arch)],
|
"platform": MAP_ARCH[self.arch],
|
||||||
"labels": {
|
"labels": {
|
||||||
"io.hass.version": version,
|
"io.hass.version": version,
|
||||||
"io.hass.arch": self.sys_arch.default,
|
"io.hass.arch": self.arch,
|
||||||
"io.hass.type": META_ADDON,
|
"io.hass.type": META_ADDON,
|
||||||
"io.hass.name": self._fix_label("name"),
|
"io.hass.name": self._fix_label("name"),
|
||||||
"io.hass.description": self._fix_label("description"),
|
"io.hass.description": self._fix_label("description"),
|
||||||
|
@ -19,3 +19,57 @@ async def test_platform_set(coresys: CoreSys, install_addon_ssh: Addon):
|
|||||||
args = build.get_docker_args(AwesomeVersion("latest"))
|
args = build.get_docker_args(AwesomeVersion("latest"))
|
||||||
|
|
||||||
assert args["platform"] == "linux/amd64"
|
assert args["platform"] == "linux/amd64"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_dockerfile_evaluation(coresys: CoreSys, install_addon_ssh: Addon):
|
||||||
|
"""Test platform set in docker args."""
|
||||||
|
build = AddonBuild(coresys, install_addon_ssh)
|
||||||
|
with patch.object(
|
||||||
|
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
||||||
|
), patch.object(
|
||||||
|
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
||||||
|
):
|
||||||
|
args = build.get_docker_args(AwesomeVersion("latest"))
|
||||||
|
|
||||||
|
assert args["dockerfile"].endswith("fixtures/addons/local/ssh/Dockerfile")
|
||||||
|
assert str(build.dockerfile).endswith("fixtures/addons/local/ssh/Dockerfile")
|
||||||
|
assert build.arch == "amd64"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_dockerfile_evaluation_arch(coresys: CoreSys, install_addon_ssh: Addon):
|
||||||
|
"""Test platform set in docker args."""
|
||||||
|
build = AddonBuild(coresys, install_addon_ssh)
|
||||||
|
with patch.object(
|
||||||
|
type(coresys.arch), "supported", new=PropertyMock(return_value=["aarch64"])
|
||||||
|
), patch.object(
|
||||||
|
type(coresys.arch), "default", new=PropertyMock(return_value="aarch64")
|
||||||
|
):
|
||||||
|
args = build.get_docker_args(AwesomeVersion("latest"))
|
||||||
|
|
||||||
|
assert args["dockerfile"].endswith("fixtures/addons/local/ssh/Dockerfile.aarch64")
|
||||||
|
assert str(build.dockerfile).endswith(
|
||||||
|
"fixtures/addons/local/ssh/Dockerfile.aarch64"
|
||||||
|
)
|
||||||
|
assert build.arch == "aarch64"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_build_valid(coresys: CoreSys, install_addon_ssh: Addon):
|
||||||
|
"""Test platform set in docker args."""
|
||||||
|
build = AddonBuild(coresys, install_addon_ssh)
|
||||||
|
with patch.object(
|
||||||
|
type(coresys.arch), "supported", new=PropertyMock(return_value=["aarch64"])
|
||||||
|
), patch.object(
|
||||||
|
type(coresys.arch), "default", new=PropertyMock(return_value="aarch64")
|
||||||
|
):
|
||||||
|
assert build.is_valid
|
||||||
|
|
||||||
|
|
||||||
|
async def test_build_invalid(coresys: CoreSys, install_addon_ssh: Addon):
|
||||||
|
"""Test platform set in docker args."""
|
||||||
|
build = AddonBuild(coresys, install_addon_ssh)
|
||||||
|
with patch.object(
|
||||||
|
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
||||||
|
), patch.object(
|
||||||
|
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
||||||
|
):
|
||||||
|
assert not build.is_valid
|
||||||
|
0
tests/fixtures/addons/local/ssh/Dockerfile.aarch64
vendored
Normal file
0
tests/fixtures/addons/local/ssh/Dockerfile.aarch64
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user