Skip to content

Commit

Permalink
fix: check image and build envs exists in builds
Browse files Browse the repository at this point in the history
Check container image and build environment exist or fail with appropriate
error at the beginning of build tasks.

fix #17
  • Loading branch information
rezib committed Jun 19, 2024
1 parent 4de70c8 commit bf40af4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
console on client side (`fatbuildrctl` and `fatbuildrweb`) with error message.
- Check OSI artifact checksum file is properly created by mkosi or raise task
execution error to report in task journal.
- Check container image and build environment exist or fail with appropriate
error at the beginning of build tasks (#17).
- docs:
- Add missing path parameter in REST API to retrieve artifact information.
- Add missing optional `architectures` parameter in instances pipelines
Expand Down
29 changes: 29 additions & 0 deletions fatbuildr/builds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ def patches(self):
]
return patches

def check_image_exists(self):
"""Check container image for the build format exists or fail with
FatbuildrTaskExecutionError."""
if not self.image.exists:
raise FatbuildrTaskExecutionError(
f"Image {self.format} does not exist, it must be created."
)

def run(self):
logger.info("Running build %s", self.id)
self.prepare()
Expand Down Expand Up @@ -540,6 +548,27 @@ def prewrapper_path(self):
"""Returns path to the prescript wrapper script."""
return self.image.common_libdir.joinpath('pre-wrapper.sh')

def check_build_env_exists(self, architecture=None):
"""Check build environment exists or raise FatbuildrTaskExecutionError.
If architecture is None, it checks for existence of native build
environment. Else it checks existence of the build environment for the
given architecture."""

if architecture is None:
env = self.native_env
else:
env = self.instance.images_mgr.build_env(
self.format, self.env_name, architecture
)

# First check native build environment exists or fail
if not env.exists():
raise FatbuildrTaskExecutionError(
f"Environment {env.environment} for architecture "
f"{env.architecture} in {self.format} image does not exist, it "
"must be created."
)

def prescript_get_token_lines(self, token):
"""Iterate over the lines of prescript file and generate all lines that
matches the provided token with an additional item at least."""
Expand Down
2 changes: 2 additions & 0 deletions fatbuildr/builds/formats/deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def supp_tarball_path(self, subdir):
)

def build(self):
self.check_image_exists()
self._build_src()
for architecture in self.architectures:
self._build_bin(architecture)
Expand Down Expand Up @@ -252,6 +253,7 @@ def _build_src(self):
def _build_bin(self, architecture):
"""Build deb packages binary package."""

self.check_build_env_exists(architecture)
env = self.instance.images_mgr.build_env(
self.format, self.env_name, architecture
)
Expand Down
3 changes: 3 additions & 0 deletions fatbuildr/builds/formats/osi.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def __init__(
def build(self):
"""Build the OS image using mkosi"""

if self.image.format_conf.containerized:
self.check_image_exists()

logger.info("Building the OS image based %s", self.artifact)

def_path = self.place.joinpath(self.format, self.artifact + '.mkosi')
Expand Down
3 changes: 3 additions & 0 deletions fatbuildr/builds/formats/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ def supp_tarball_path(self, subdir):
)

def build(self):
self.check_image_exists()
self._build_src()
for architecture in self.architectures:
self._build_bin(architecture)
self._static_check()

def _build_src(self):
"""Build source SRPM"""
self.check_build_env_exists()

logger.info(
"Building source RPM for %s in build environment %s",
Expand Down Expand Up @@ -340,6 +342,7 @@ def _build_src(self):
def _build_bin(self, architecture):
"""Build binary RPM"""

self.check_build_env_exists(architecture)
env = self.instance.images_mgr.build_env(
self.format, self.env_name, architecture
)
Expand Down
13 changes: 12 additions & 1 deletion fatbuildr/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import shlex
import tempfile
from io import BytesIO
from pathlib import Path

from .templates import Templeter
from .utils import current_user_group
Expand Down Expand Up @@ -179,7 +180,7 @@ def __str__(self):
def path(self):
env_path = self.image.format_conf.env_path
if env_path:
return Templeter().srender(env_path, name=self.name)
return Path(Templeter().srender(env_path, name=self.name))

@property
def base(self):
Expand All @@ -193,6 +194,16 @@ def name(self):
def native_architecture(self):
return ArchMap(self.image.format).native(self.architecture)

def exists(self):
"""Return True if the build environment exists in the container
image."""
return (
self.path is not None
and self.image.path.joinpath(
self.path.relative_to(self.path.anchor)
).exists()
)

def create(self, task):
logger.info(
"Creating build environment %s for architecture %s in %s image",
Expand Down

0 comments on commit bf40af4

Please sign in to comment.