Files
secureblue/build.sh
Arcitec 55ff6363be feat: implement a very flexible runner for "pre" and "post" scripts
This new functionality now makes it possible to execute scripts at the start or end of the build process, while also being super simple to expand to add further script stages in the future.

It also supports effortless reuse of scripts for multiple stages, since the scripts are now executed with the "current stage" as their 1st argument, to allow them to easily determine which stage they're running in.
2023-05-10 18:44:25 +03:00

87 lines
2.7 KiB
Bash

#!/usr/bin/env bash
# Tell build process to exit if there are any errors.
set -oue pipefail
# Helper functions.
RECIPE_FILE="/usr/etc/ublue-recipe.yml"
get_yaml_array() {
mapfile -t "$1" < <(yq -- "$2" "$RECIPE_FILE")
}
get_yaml_string() {
yq -- "$1" "$RECIPE_FILE"
}
# Automatically determine which Fedora version we're building.
FEDORA_VERSION="$(cat /usr/lib/os-release | grep '^VERSION_ID=' | head -1 | sed 's,^VERSION_ID=,,')"
# Read configuration variables.
base_image="$(get_yaml_string '.base-image')"
# Welcome.
echo "Building custom Fedora ${FEDORA_VERSION} from image: \"${base_image}\"."
# Add custom repos.
get_yaml_array repos '.rpm.repos[]'
if [[ ${#repos[@]} -gt 0 ]]; then
echo "-- Adding repos defined in recipe.yml --"
for repo in "${repos[@]}"; do
repo="${repo//%FEDORA_VERSION%/${FEDORA_VERSION}}"
wget "$repo" -P /etc/yum.repos.d/
done
echo "---"
fi
# Run "pre" scripts.
run_scripts() {
script_mode="$1"
get_yaml_array buildscripts ".scripts.${script_mode}[]"
if [[ ${#buildscripts[@]} -gt 0 ]]; then
echo "-- Running [${script_mode}] scripts defined in recipe.yml --"
for script in "${buildscripts[@]}"; do
echo "Running [${script_mode}]: ${script}"
/tmp/scripts/"$script" "${script_mode}"
done
echo "---"
fi
}
run_scripts "pre"
# Remove RPMs.
get_yaml_array remove_rpms '.rpm.remove[]'
if [[ ${#remove_rpms[@]} -gt 0 ]]; then
echo "-- Removing RPMs defined in recipe.yml --"
echo "Removing: ${remove_rpms[@]}"
rpm-ostree override remove "${remove_rpms[@]}"
echo "---"
fi
# Install RPMs.
get_yaml_array install_rpms '.rpm.install[]'
if [[ ${#install_rpms[@]} -gt 0 ]]; then
echo "-- Installing RPMs defined in recipe.yml --"
echo "Installing: ${install_rpms[@]}"
rpm-ostree install "${install_rpms[@]}"
echo "---"
fi
# Install yafti to install flatpaks on first boot, https://github.com/ublue-os/yafti.
pip install --prefix=/usr yafti
# Add a new yafti "package group" called Custom, for the packages defined in recipe.yml.
# Only adds the package group if some flatpaks are defined in the recipe.
get_yaml_array flatpaks '.flatpaks[]'
if [[ ${#flatpaks[@]} -gt 0 ]]; then
echo "-- yafti: Adding Flatpaks defined in recipe.yml --"
yq -i '.screens.applications.values.groups.Custom.description = "Flatpaks defined by the image maintainer"' /usr/etc/yafti.yml
yq -i '.screens.applications.values.groups.Custom.default = true' /usr/etc/yafti.yml
for pkg in "${flatpaks[@]}"; do
echo "Adding to yafti: ${pkg}"
yq -i ".screens.applications.values.groups.Custom.packages += [{\"$pkg\": \"$pkg\"}]" /usr/etc/yafti.yml
done
echo "---"
fi
# Run "post" scripts.
run_scripts "post"