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
This commit is contained in:
gerblesh 2023-09-18 23:32:16 -07:00 committed by GitHub
parent 9c8e1e2cb2
commit aee351fcbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 33 deletions

View File

@ -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"

View File

@ -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/<yourusername>/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
```
```

View File

@ -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: