From e1e5c712724548b753a5384f5b6feef5f42728db Mon Sep 17 00:00:00 2001 From: gerblesh <101901964+gerblesh@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:32:16 -0700 Subject: [PATCH] feat: add support for nested modules (#152) * feat: add support for nested modules * chore: test out module nesting * fix: made build function * revert: move back to old recipe.yml configuration * fix: formatting in recipe.yml * style: add newline between modules --- build.sh | 55 +++++++++++++++++++++++++++-------------------- config/README.md | 8 +++---- config/recipe.yml | 11 +++++----- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/build.sh b/build.sh index 7d33c221a0..2ee040d2c0 100644 --- a/build.sh +++ b/build.sh @@ -14,11 +14,41 @@ MODULE_DIRECTORY="/tmp/modules" # https://mikefarah.gitbook.io/yq/usage/tips-and-tricks#yq-in-a-bash-loop get_yaml_array() { - # creates array $1 with content at key $2 from $3 + # creates array $1 with content at key $2 from $3 readarray "$1" < <(echo "$3" | yq -I=0 "$2") } export -f get_yaml_array # this makes the function available to all modules +run_module() { + MODULE="$1" + TYPE=$(echo "$MODULE" | yq '.type') + if [[ "$TYPE" != "null" ]]; then + # If type is found, that means that the module config + # has been declared inline, and thus is safe to pass to the module + echo "=== Launching module of type: $TYPE ===" + bash "$MODULE_DIRECTORY/$TYPE/$TYPE.sh" "$MODULE" + else + # If the type is not found, that means that the module config + # is in a separate file, and has to be read from it + FILE=$(echo "$MODULE" | yq '.from-file') + run_modules "$CONFIG_DIRECTORY/$FILE" + fi + echo "======" +} + +run_modules() { + MODULES_FILE="$1" + readarray MODULES < <(yq -o=j -I=0 '.modules[]' "$MODULES_FILE" ) + if [[ ${#MODULES[@]} -gt 0 ]]; then + for MODULE in "${MODULES[@]}"; do + run_module "$MODULE" + done + else + MODULE=$(yq -o=j -I=0 '.' "$MODULES_FILE") + run_module "$MODULE" + fi +} + # Declare dynamically generated variables as exported declare -x IMAGE_NAME BASE_IMAGE OS_VERSION @@ -32,25 +62,4 @@ OS_VERSION="$(grep -Po '(?<=VERSION_ID=)\d+' /usr/lib/os-release)" # Welcome. echo "Building $IMAGE_NAME from $BASE_IMAGE:$OS_VERSION." -# Run each module -readarray MODULES < <(yq -o=j -I=0 '.modules[]' "$RECIPE_FILE" ) - -for MODULE in "${MODULES[@]}"; do - TYPE=$(echo "$MODULE" | yq '.type') - if [[ "$TYPE" != "null" ]]; then - # If type is found, that means that the module config - # has been declared inline, and thus is safe to pass to the module - echo "=== Launching module of type: $TYPE ===" - bash "$MODULE_DIRECTORY/$TYPE/$TYPE.sh" "$MODULE" - else - # If the type is not found, that means that the module config - # is in a separate file, and has to be read from it - FILE=$(echo "$MODULE" | yq '.from-file') - MODULE_CONFIG=$(yq -o=j -I=0 '.' "$CONFIG_DIRECTORY/$FILE") - - TYPE=$(echo "$MODULE_CONFIG" | yq '.type') - echo "=== Launching module of type: $TYPE ===" - bash "$MODULE_DIRECTORY/$TYPE/$TYPE.sh" "$MODULE_CONFIG" - fi - echo "======" -done +run_modules "$RECIPE_FILE" diff --git a/config/README.md b/config/README.md index 23337d6d5b..946c73f133 100644 --- a/config/README.md +++ b/config/README.md @@ -1,6 +1,6 @@ # Configuring your image -The main file of your is *the recipe file*. You can have multiple recipe files, and the ones to build are declared in the matrix section of [build.yml](../.github/workflows/build.yml). +The main file of your is *the recipe file*. You can have multiple recipe files, and the ones to build are declared in the matrix section of [build.yml](../.github/workflows/build.yml). ## Basic options @@ -8,9 +8,9 @@ At the top of the recipe, there are four *mandatory* configuration options. `name:` is the name of the image that is used when rebasing to it. For example, the name "sapphire" would result in the final URL of the container being `ghcr.io//sapphire`. -`description:` is a short description of your image that will be attached to your image's metadata. +`description:` is a short description of your image that will be attached to your image's metadata. -`base-image:` is the URL of the image your image will be built upon. +`base-image:` is the URL of the image your image will be built upon. `image-version:` is the version tag of the `base-image` that will be pulled. For example, Universal Blue's images build with Fedora version tags (`38`, `39`), with the `latest` tag for the latest major version, and [many other tags](https://github.com/ublue-os/main/pkgs/container/base-main/versions?filters%5Bversion_type%5D=tagged). @@ -39,4 +39,4 @@ install: - dunst - rofi - kitty -``` +``` \ No newline at end of file diff --git a/config/recipe.yml b/config/recipe.yml index 5a50f8f0af..3dd0e51a48 100644 --- a/config/recipe.yml +++ b/config/recipe.yml @@ -10,16 +10,15 @@ image-version: 38 # latest is also supported if you want new updates ASAP # module configuration, executed in order # you can include multiple instances of the same module modules: - - type: files files: - - usr: /usr # copy static configurations - # + - usr: /usr # copy static configurations + # # copies config/files/usr into your image's /usr # - # configuration you wish to end up in /etc/ on the booted system - # should be added into /usr/etc/ as that is the proper "distro" - # config directory on ostree read more in the files module's README + # configuration you wish to end up in /etc/ on the booted system + # should be added into /usr/etc/ as that is the proper "distro" + # config directory on ostree. Read more in the files module's README - type: rpm-ostree repos: