Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
feat: add support for nested modules (blue-build#152)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gerblesh authored and vietchinh committed Sep 19, 2023
1 parent 6d9bea4 commit e1e5c71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
55 changes: 32 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
8 changes: 4 additions & 4 deletions config/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# 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

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).

Expand Down Expand Up @@ -39,4 +39,4 @@ install:
- dunst
- rofi
- kitty
```
```
11 changes: 5 additions & 6 deletions config/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e1e5c71

Please sign in to comment.