mirror of
https://github.com/vyos/vyos-build.git
synced 2025-10-01 20:28:40 +02:00
build: T3664: improve support for custom build hooks
This commit is contained in:
parent
ead4cc2eba
commit
3fe55e7813
@ -95,7 +95,7 @@ else:
|
|||||||
import utils
|
import utils
|
||||||
import raw_image
|
import raw_image
|
||||||
|
|
||||||
from utils import cmd
|
from utils import cmd, rc_cmd
|
||||||
|
|
||||||
## Check if there are missing build dependencies
|
## Check if there are missing build dependencies
|
||||||
deps = {
|
deps = {
|
||||||
@ -192,7 +192,8 @@ if __name__ == "__main__":
|
|||||||
'vyos-mirror': ('VyOS package mirror', None),
|
'vyos-mirror': ('VyOS package mirror', None),
|
||||||
'build-type': ('Build type, release or development', lambda x: x in ['release', 'development']),
|
'build-type': ('Build type, release or development', lambda x: x in ['release', 'development']),
|
||||||
'version': ('Version number (release builds only)', None),
|
'version': ('Version number (release builds only)', None),
|
||||||
'build-comment': ('Optional build comment', None)
|
'build-comment': ('Optional build comment', None),
|
||||||
|
'build-hook-opts': ('Custom options for the post-build hook', None)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create the option parser
|
# Create the option parser
|
||||||
@ -630,21 +631,15 @@ Pin-Priority: 600
|
|||||||
# Copy the image
|
# Copy the image
|
||||||
shutil.copy("live-image-{0}.hybrid.iso".format(build_config["architecture"]), iso_file)
|
shutil.copy("live-image-{0}.hybrid.iso".format(build_config["architecture"]), iso_file)
|
||||||
|
|
||||||
# Build additional flavors from the ISO,
|
# If the flavor has `image_format = "iso"`, then the work is done.
|
||||||
# if the flavor calls for them
|
# If not, build additional flavors from the ISO.
|
||||||
if build_config["image_format"] != ["iso"]:
|
if build_config["image_format"] != ["iso"]:
|
||||||
|
# For all non-iso formats, we always build a raw image first
|
||||||
raw_image = raw_image.create_raw_image(build_config, iso_file, "tmp/")
|
raw_image = raw_image.create_raw_image(build_config, iso_file, "tmp/")
|
||||||
manifest['artifacts'].append(raw_image)
|
manifest['artifacts'].append(raw_image)
|
||||||
|
|
||||||
if has_nonempty_key(build_config, "post_build_hook"):
|
# If there are other formats in the flavor, the assumptions is that
|
||||||
# Some flavors require special procedures that aren't covered by qemu-img
|
# they can be produced from a raw image with `qemu-img convert`
|
||||||
# (most notably, the VMware OVA that requires a custom tool to make and sign the image).
|
|
||||||
# For those cases, we support running a post-build hook on the raw image.
|
|
||||||
# The image_format field should be 'raw' if a post-build hook is used.
|
|
||||||
hook_path = build_config["post_build_hook"]
|
|
||||||
cmd(f"{hook_path} {raw_image}")
|
|
||||||
else:
|
|
||||||
# Most other formats, thankfully, can be produced with just `qemu-img convert`
|
|
||||||
other_formats = filter(lambda x: x not in ["iso", "raw"], build_config["image_format"])
|
other_formats = filter(lambda x: x not in ["iso", "raw"], build_config["image_format"])
|
||||||
for f in other_formats:
|
for f in other_formats:
|
||||||
image_ext = build_config.get("image_ext", f)
|
image_ext = build_config.get("image_ext", f)
|
||||||
@ -654,5 +649,25 @@ Pin-Priority: 600
|
|||||||
cmd(f"qemu-img convert -f raw -O {f} {image_opts} {raw_image} {target}")
|
cmd(f"qemu-img convert -f raw -O {f} {image_opts} {raw_image} {target}")
|
||||||
manifest['artifacts'].append(target)
|
manifest['artifacts'].append(target)
|
||||||
|
|
||||||
|
# Finally, there are formats that require special procedures.
|
||||||
|
# For example, a VMware OVA needs a custom tool for image building and signing.
|
||||||
|
# For those cases, we support custom build hooks that run on raw images.
|
||||||
|
# A post-build hook is a script embedded in the flavor file
|
||||||
|
# that must return the artifact name via stdout.
|
||||||
|
if has_nonempty_key(build_config, "build_hook"):
|
||||||
|
hook_source = build_config["build_hook"]
|
||||||
|
|
||||||
|
with open('build_hook', 'w') as f:
|
||||||
|
f.write(hook_source)
|
||||||
|
os.chmod('build_hook', 0o755)
|
||||||
|
|
||||||
|
if has_nonempty_key(build_config, "build_hook_opts"):
|
||||||
|
hook_opts = build_config["build_hook_opts"]
|
||||||
|
else:
|
||||||
|
hook_opts = ""
|
||||||
|
custom_image = rc_cmd(f"./build_hook {raw_image} {build_config['version']} \
|
||||||
|
{build_config['architecture']} {hook_opts}")
|
||||||
|
manifest['artifacts'].append(custom_image)
|
||||||
|
|
||||||
with open('manifest.json', 'w') as f:
|
with open('manifest.json', 'w') as f:
|
||||||
f.write(json.dumps(manifest))
|
f.write(json.dumps(manifest))
|
||||||
|
|||||||
@ -82,3 +82,10 @@ def cmd(command):
|
|||||||
res = vyos.utils.process.call(command, shell=True)
|
res = vyos.utils.process.call(command, shell=True)
|
||||||
if res > 0:
|
if res > 0:
|
||||||
raise OSError(f"Command '{command}' failed")
|
raise OSError(f"Command '{command}' failed")
|
||||||
|
|
||||||
|
def rc_cmd(command):
|
||||||
|
code, out = vyos.utils.process.rc_cmd(command, shell=True)
|
||||||
|
if code > 0:
|
||||||
|
raise OSError(f"Command '{command}' failed")
|
||||||
|
else:
|
||||||
|
return out
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user