fix: extremely robust handling for YAML data fetching

This new technique is way easier to use, more maintainable, and safely handles special characters and spaces in the YAML data. It will also enable other features, such as the rpm-ostree improvement coming in the next commit.

The function name is `get_yaml_array()` because it literally reads it into a Bash array, by the way. Don't change the function name in the future. We may need other functions to read values into strings, etc.
This commit is contained in:
Arcitec
2023-05-09 16:56:23 +02:00
committed by Eino Rauhala
parent 20e0a06588
commit 07cbe2cc08

View File

@@ -3,47 +3,59 @@
# Tell build process to exit if there are any errors. # Tell build process to exit if there are any errors.
set -oue pipefail set -oue pipefail
# Helper functions.
get_yaml_array() {
mapfile -t "$1" < <(yq "$2" < /usr/etc/ublue-recipe.yml)
}
# Add custom repos. # Add custom repos.
repos=$(yq '.extrarepos[]' < /usr/etc/ublue-recipe.yml) get_yaml_array repos '.extrarepos[]'
if [[ -n "$repos" ]]; then if [[ ${#repos[@]} -gt 0 ]]; then
echo "-- Adding repos defined in recipe.yml --" echo "-- Adding repos defined in recipe.yml --"
for repo in $(echo -e "$repos"); do \ for repo in "${repos[@]}"; do
wget $repo -P /etc/yum.repos.d/; \ wget "$repo" -P /etc/yum.repos.d/
done done
echo "---" echo "---"
fi fi
# Run scripts. # Run scripts.
get_yaml_array buildscripts '.scripts[]'
if [[ ${#buildscripts[@]} -gt 0 ]]; then
echo "-- Running scripts defined in recipe.yml --" echo "-- Running scripts defined in recipe.yml --"
buildscripts=$(yq '.scripts[]' < /usr/etc/ublue-recipe.yml) for script in "${buildscripts[@]}"; do
for script in $(echo -e "$buildscripts"); do \ echo "Running: ${script}"
echo "Running: ${script}" && \ /tmp/scripts/"$script"
/tmp/scripts/$script; \
done done
echo "---" echo "---"
fi
# Remove the default firefox (from fedora) in favor of the flatpak. # Remove the default firefox (from fedora) in favor of the flatpak.
rpm-ostree override remove firefox firefox-langpacks rpm-ostree override remove firefox firefox-langpacks
# Install RPMs. # Install RPMs.
get_yaml_array install_rpms '.rpms[]'
if [[ ${#install_rpms[@]} -gt 0 ]]; then
echo "-- Installing RPMs defined in recipe.yml --" echo "-- Installing RPMs defined in recipe.yml --"
rpm_packages=$(yq '.rpms[]' < /usr/etc/ublue-recipe.yml) for pkg in "${install_rpms[@]}"; do
for pkg in $(echo -e "$rpm_packages"); do \ echo "Installing: ${pkg}"
echo "Installing: ${pkg}" && \ rpm-ostree install $pkg
rpm-ostree install $pkg; \
done done
echo "---" echo "---"
fi
# Install yafti to install flatpaks on first boot, https://github.com/ublue-os/yafti. # Install yafti to install flatpaks on first boot, https://github.com/ublue-os/yafti.
pip install --prefix=/usr yafti pip install --prefix=/usr yafti
# Add a package group for yafti using the packages defined in recipe.yml. # Add a new yafti "package group" called Custom, for the packages defined in recipe.yml.
flatpaks=$(yq '.flatpaks[]' < /tmp/ublue-recipe.yml) # Only adds the package group if some flatpaks are defined in the recipe.
# Only try to add package groups if some flatpaks are defined in the recipe. get_yaml_array flatpaks '.flatpaks[]'
if [[ -n "$flatpaks" ]]; then 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.description = "Flatpaks defined by the image maintainer"' /usr/etc/yafti.yml
yq -i '.screens.applications.values.groups.Custom.default = true' /usr/etc/yafti.yml yq -i '.screens.applications.values.groups.Custom.default = true' /usr/etc/yafti.yml
for pkg in $(echo -e "$flatpaks"); do \ for pkg in "${flatpaks[@]}"; do
echo "Adding to yafti: ${pkg}"
yq -i ".screens.applications.values.groups.Custom.packages += [{\"$pkg\": \"$pkg\"}]" /usr/etc/yafti.yml yq -i ".screens.applications.values.groups.Custom.packages += [{\"$pkg\": \"$pkg\"}]" /usr/etc/yafti.yml
done done
echo "---"
fi fi