Skip to content

Commit

Permalink
plugin: inird: download debs to source dir
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
  • Loading branch information
kubiko committed Nov 9, 2023
1 parent d716e78 commit 712aef9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 55 deletions.
13 changes: 9 additions & 4 deletions snapcraft/parts/plugins/initrd_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@
from pydantic import root_validator

from snapcraft.parts.plugins import kernel_plugin
from snapcraft_legacy.plugins.v2 import _initrd_build
from snapcraft_legacy.plugins.v2 import _kernel_build
from snapcraft_legacy.plugins.v2 import _initrd_build, _kernel_build

logger = logging.getLogger(__name__)


class InitrdPluginProperties(plugins.PluginProperties, plugins.PluginModel):
"""The part properties used by the Initrd plugin."""

Expand Down Expand Up @@ -187,11 +187,15 @@ def __init__(
target_arch = self._part_info.target_arch
self._deb_arch = _kernel_build.get_deb_architecture(target_arch)
if not self.options.initrd_kernel_image_target:
self.kernel_image_target = kernel_plugin.default_kernel_image_target[self._deb_arch]
self.kernel_image_target = kernel_plugin.default_kernel_image_target[
self._deb_arch
]
elif isinstance(self.options.initrd_kernel_image_target, str):
self.kernel_image_target = self.options.initrd_kernel_image_target
elif self._deb_arch in self.options.initrd_kernel_image_target:
self.kernel_image_target = self.options.initrd_kernel_image_target[self._deb_arch]
self.kernel_image_target = self.options.initrd_kernel_image_target[
self._deb_arch
]

@overrides
def get_build_snaps(self) -> Set[str]:
Expand Down Expand Up @@ -254,6 +258,7 @@ def get_build_commands(self) -> List[str]:
initrd_default_compression="zstd -1 -T0",
initrd_include_extra_modules_conf=True,
initrd_tool_pass_root=False,
source_dir="${CRAFT_PART_SRC}",
install_dir="${CRAFT_PART_INSTALL}",
stage_dir="${CRAFT_STAGE}",
)
Expand Down
3 changes: 1 addition & 2 deletions snapcraft/parts/plugins/kernel_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@

default_kernel_image_target = {
"amd64": "bzImage",
"i386": "bzImage",
"armhf": "zImage",
"arm64": "Image.gz",
"arm64": "Image",
"powerpc": "uImage",
"ppc64el": "vmlinux.strip",
"s390x": "bzImage",
Expand Down
43 changes: 27 additions & 16 deletions snapcraft_legacy/plugins/v2/_initrd_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,33 @@ def _download_core_initrd_fnc_cmd() -> List[str]:
cmd = textwrap.dedent(
"""
# Helper to download code initrd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_core_initrd() {
apt-get download ubuntu-core-initramfs:${1}
# unpack dep to the target dir
dpkg -x ubuntu-core-initramfs_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then
apt-get download ubuntu-core-initramfs:${1}
mv ubuntu-core-initramfs_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2}
}
"""
)
return [cmd]


def _download_generic_initrd_cmd(target_arch: str) -> List[str]:
def _download_generic_initrd_cmd(target_arch: str, source_dir: str) -> List[str]:
"""Download Ubuntu Core initrd deb."""
cmd = textwrap.dedent(
"""
echo "Getting ubuntu-core-initrd...."
# only download u-c-initrd deb if needed
if [ ! -e ${{UC_INITRD_DEB}} ]; then
download_core_initrd {arch} ${{UC_INITRD_DEB}}
download_core_initrd {arch} ${{UC_INITRD_DEB}} {source_dir}
fi
""".format(
arch=target_arch
arch=target_arch,
source_dir=source_dir
)
)
return [cmd]
Expand All @@ -135,29 +140,34 @@ def _download_snap_bootstrap_fnc_cmd() -> List[str]:
cmd = textwrap.dedent(
"""
# Helper to download snap-bootstrap from snapd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_snap_bootstrap() {
apt-get download snapd:${1}
# unpack dep to the target dir
dpkg -x snapd_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then
apt-get download snapd:${1}
mv snapd_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/snapd_*.deb ${2}
}
"""
)
return [cmd]


def _download_snap_bootstrap_cmd(target_arch: str) -> List[str]:
def _download_snap_bootstrap_cmd(target_arch: str, source_dir: str) -> List[str]:
"""Download snap-bootstrap deb."""
cmd = textwrap.dedent(
"""
echo "Getting snapd deb for snap bootstrap..."
# only download again if files does not exist, otherwise
# assume we are re-running build
if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}}
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {source_dir}
fi
""".format(
arch=target_arch
arch=target_arch,
source_dir=source_dir
)
)
return [cmd]
Expand Down Expand Up @@ -574,6 +584,7 @@ def get_build_commands(
initrd_default_compression: str,
initrd_include_extra_modules_conf: bool,
initrd_tool_pass_root: bool,
source_dir: str,
install_dir: str,
stage_dir: str,
) -> List[str]:
Expand All @@ -587,11 +598,11 @@ def get_build_commands(
"",
*_download_core_initrd_fnc_cmd(),
"",
*_download_generic_initrd_cmd(target_arch=target_arch),
*_download_generic_initrd_cmd(target_arch=target_arch, source_dir=source_dir),
"",
*_download_snap_bootstrap_fnc_cmd(),
"",
*_download_snap_bootstrap_cmd(target_arch=target_arch),
*_download_snap_bootstrap_cmd(target_arch=target_arch, source_dir=source_dir),
"",
*_get_initrd_install_command(
initrd_compression=initrd_compression,
Expand Down
2 changes: 2 additions & 0 deletions snapcraft_legacy/plugins/v2/initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ def get_build_commands(self) -> List[str]:
initrd_overlay=self.options.initrd_overlay,
initrd_stage_firmware=self.options.initrd_stage_firmware,
build_efi_image=False,
kernel_image_target="",
initrd_ko_use_workaround=True,
initrd_default_compression="lz4 -9 -l",
initrd_include_extra_modules_conf=False,
initrd_tool_pass_root=True,
source_dir="${SNAPCRAFT_PART_SRC}",
install_dir="${SNAPCRAFT_PART_INSTALL}",
stage_dir="${SNAPCRAFT_STAGE}",
)
Expand Down
44 changes: 28 additions & 16 deletions tests/legacy/unit/plugins/v2/test_initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,21 @@ def _is_sub_array(array, sub_array):
)
]

_parts_source_dir = "${SNAPCRAFT_PART_SRC}"

_download_initrd_fnc = [
textwrap.dedent(
"""
# Helper to download code initrd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_core_initrd() {
apt-get download ubuntu-core-initramfs:${1}
# unpack dep to the target dir
dpkg -x ubuntu-core-initramfs_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then
apt-get download ubuntu-core-initramfs:${1}
mv ubuntu-core-initramfs_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2}
}
"""
)
Expand All @@ -528,10 +534,11 @@ def _is_sub_array(array, sub_array):
echo "Getting ubuntu-core-initrd...."
# only download u-c-initrd deb if needed
if [ ! -e ${{UC_INITRD_DEB}} ]; then
download_core_initrd {arch} ${{UC_INITRD_DEB}}
download_core_initrd {arch} ${{UC_INITRD_DEB}} {parts_source_dir}
fi
""".format(
arch=_DEB_ARCH_TRANSLATIONS[platform.machine()]
arch=_DEB_ARCH_TRANSLATIONS[platform.machine()],
parts_source_dir=_parts_source_dir,
)
)
]
Expand All @@ -543,10 +550,10 @@ def _is_sub_array(array, sub_array):
echo "Getting ubuntu-core-initrd...."
# only download u-c-initrd deb if needed
if [ ! -e ${{UC_INITRD_DEB}} ]; then
download_core_initrd {arch} ${{UC_INITRD_DEB}}
download_core_initrd {arch} ${{UC_INITRD_DEB}} {parts_source_dir}
fi
""".format(
arch="armhf"
arch="armhf", parts_source_dir=_parts_source_dir
)
)
]
Expand All @@ -555,11 +562,15 @@ def _is_sub_array(array, sub_array):
textwrap.dedent(
"""
# Helper to download snap-bootstrap from snapd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_snap_bootstrap() {
apt-get download snapd:${1}
# unpack dep to the target dir
dpkg -x snapd_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then
apt-get download snapd:${1}
mv snapd_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/snapd_*.deb ${2}
}
"""
)
Expand All @@ -572,10 +583,11 @@ def _is_sub_array(array, sub_array):
# only download again if files does not exist, otherwise
# assume we are re-running build
if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}}
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {parts_source_dir}
fi
""".format(
arch=_DEB_ARCH_TRANSLATIONS[platform.machine()]
arch=_DEB_ARCH_TRANSLATIONS[platform.machine()],
parts_source_dir=_parts_source_dir,
)
)
]
Expand All @@ -587,10 +599,10 @@ def _is_sub_array(array, sub_array):
# only download again if files does not exist, otherwise
# assume we are re-running build
if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}}
download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {parts_source_dir}
fi
""".format(
arch="armhf"
arch="armhf", parts_source_dir=_parts_source_dir
)
)
]
Expand Down
43 changes: 26 additions & 17 deletions tests/unit/parts/plugins/test_initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_check_get_build_command_config_flavour_configs(
"initrd-overlay": "my-overlay",
"initrd-compression": "gz",
"initrd-build-efi-image": "true",
"initrd-kernel-image-target": "Image"
"initrd-kernel-image-target": "Image",
},
)

Expand Down Expand Up @@ -698,37 +698,42 @@ def _is_sub_array(array, sub_array):
textwrap.dedent(
"""
# Helper to download code initrd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_core_initrd() {
apt-get download ubuntu-core-initramfs:${1}
# unpack dep to the target dir
dpkg -x ubuntu-core-initramfs_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then
apt-get download ubuntu-core-initramfs:${1}
mv ubuntu-core-initramfs_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2}
}
"""
)
]

_machine_arch = _DEB_ARCH_TRANSLATIONS[platform.machine()]
_parts_source_dir = "${CRAFT_PART_SRC}"

_get_initrd_cmd = [
textwrap.dedent(
f"""
echo "Getting ubuntu-core-initrd...."
# only download u-c-initrd deb if needed
if [ ! -e ${{UC_INITRD_DEB}} ]; then
download_core_initrd {_machine_arch} ${{UC_INITRD_DEB}}
download_core_initrd {_machine_arch} ${{UC_INITRD_DEB}} {_parts_source_dir}
fi
"""
)
]

_get_initrd_armhf_cmd = [
textwrap.dedent(
"""
f"""
echo "Getting ubuntu-core-initrd...."
# only download u-c-initrd deb if needed
if [ ! -e ${UC_INITRD_DEB} ]; then
download_core_initrd armhf ${UC_INITRD_DEB}
if [ ! -e ${{UC_INITRD_DEB}} ]; then
download_core_initrd armhf ${{UC_INITRD_DEB}} {_parts_source_dir}
fi
"""
)
Expand All @@ -738,11 +743,15 @@ def _is_sub_array(array, sub_array):
textwrap.dedent(
"""
# Helper to download snap-bootstrap from snapd deb package
# 1: arch, 2: output dir
# 1: arch, 2: output dir 3: source dir
download_snap_bootstrap() {
apt-get download snapd:${1}
# unpack dep to the target dir
dpkg -x snapd_*.deb ${2}
# skip download if file already exist
if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then
apt-get download snapd:${1}
mv snapd_*.deb ${3}
fi
# unpack dep to the target dir
dpkg -x ${3}/snapd_*.deb ${2}
}
"""
)
Expand All @@ -755,20 +764,20 @@ def _is_sub_array(array, sub_array):
# only download again if files does not exist, otherwise
# assume we are re-running build
if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then
download_snap_bootstrap {_machine_arch} ${{UC_INITRD_DEB}}
download_snap_bootstrap {_machine_arch} ${{UC_INITRD_DEB}} {_parts_source_dir}
fi
"""
)
]

_get_snapd_armhf_cmd = [
textwrap.dedent(
"""
f"""
echo "Getting snapd deb for snap bootstrap..."
# only download again if files does not exist, otherwise
# assume we are re-running build
if [ ! -e ${UC_INITRD_DEB}/usr/lib/snapd ]; then
download_snap_bootstrap armhf ${UC_INITRD_DEB}
if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then
download_snap_bootstrap armhf ${{UC_INITRD_DEB}} {_parts_source_dir}
fi
"""
)
Expand Down

0 comments on commit 712aef9

Please sign in to comment.