Skip to content

Commit

Permalink
feat: Add support for NuShell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Dec 24, 2024
1 parent f85a761 commit 8325de0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bacon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ command = ["cargo", "install", "--path", ".", "--debug", "--locked", "--color",
need_stdout = false
allow_warnings = true
default_watch = false
watch = ["src", "process", "recipe", "template", "utils", "Cargo.toml", "build.rs"]
watch = ["src", "process", "recipe", "template", "utils", "scripts", "Cargo.toml", "build.rs"]

[jobs.install-all]
command = ["cargo", "install", "--all-features", "--path", ".", "--debug", "--locked", "--color", "always"]
need_stdout = false
allow_warnings = true
default_watch = false
watch = ["src", "process", "recipe", "template", "utils", "Cargo.toml", "build.rs"]
watch = ["src", "process", "recipe", "template", "utils", "scripts", "Cargo.toml", "build.rs"]

# You may define here keybindings that would be specific to
# a project, for example a shortcut to launch a specific job.
Expand Down
7 changes: 7 additions & 0 deletions recipe/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ impl<'a> ModuleRequiredFields<'a> {
}
}

#[must_use]
pub fn is_local_source(&self) -> bool {
self.source
.as_deref()
.is_some_and(|source| source == "local")
}

#[must_use]
pub fn generate_akmods_info(&'a self, os_version: &u64) -> AkmodsInfo {
#[derive(Debug, Default, Copy, Clone)]
Expand Down
41 changes: 38 additions & 3 deletions scripts/run_module.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,51 @@ print_banner() {
printf '%*.*s%s%*.*s\n' 0 "$padlen" "$padding" "$text" 0 "$padlen" "$padding"
}

get_script_path() {
local script_name="$1"
local extensions=("sh" "bash" "nu")
local base_script_path="/tmp/modules/${script_name}/${script_name}"
local tried_scripts=()

# See if
if [[ -f "${base_script_path}" ]]; then
echo "${base_script_path}"
return 0
fi
tried_scripts+=("${script_name}")

# Iterate through each extension and check if the file exists
for ext in "${extensions[@]}"; do
local script_path="${base_script_path}.${ext}"
tried_scripts+=("${script_name}.${ext}")

if [[ -f "$script_path" ]]; then
# Output only the script path without extra information
echo "$script_path"
return 0 # Exit the function when the first matching file is found
fi
done

# If no matching file was found
echo "Failed to find scripts matching: ${tried_scripts[*]}" >&2
return 1
}

module="$1"
params="$2"
script_path="/tmp/modules/${module}/${module}.sh"
script_path="$(get_script_path "$module")"

color_string "$(print_banner "Start '${module}' Module")" "33"
chmod +x ${script_path}
chmod +x "${script_path}"

if ${script_path} "${params}"; then
if "${script_path}" "${params}"; then
color_string "$(print_banner "End '${module}' Module")" "32"

else
color_string "$(print_banner "Failed '${module}' Module")" "31"
exit 1
fi

if command -v ostree > /dev/null; then
ostree container commit
fi
11 changes: 7 additions & 4 deletions template/templates/modules/modules.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ RUN \
{%- endif %}
{%- if let Some(source) = module.get_non_local_source() %}
--mount=type=bind,from={{ source }},src=/modules,dst=/tmp/modules,rw \
{%- else %}
{%- else if module.is_local_source() %}
--mount=type=bind,from=stage-modules,src=/modules,dst=/tmp/modules,rw \
{%- else %}
--mount=type=bind,from=ghcr.io/blue-build/modules/{{ module.module_type }}:latest,src=/modules,dst=/tmp/modules,rw \
{%- endif %}
{%- if module.module_type == "akmods" %}
--mount=type=bind,from=stage-akmods-{{ module.generate_akmods_info(os_version).stage_name }},src=/rpms,dst=/tmp/rpms,rw \
{%- endif %}
--mount=type=bind,from={{ build_scripts_image }},src=/scripts/,dst=/tmp/scripts/ \
--mount=type=cache,dst=/var/cache/rpm-ostree,id=rpm-ostree-cache-{{ recipe.name }}-{{ recipe.image_version }},sharing=locked \
--mount=type=cache,dst=/var/cache/libdnf5,id=dnf-cache-{{ recipe.name }}-{{ recipe.image_version }},sharing=locked \
/tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module|json|safe }}' \
&& ostree container commit
/tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module|json|safe }}'
{%- endif %}
{%- endif %}
{%- endfor %}
Expand Down Expand Up @@ -57,8 +58,10 @@ RUN \
{%- endif %}
{%- if let Some(source) = module.get_non_local_source() %}
--mount=type=bind,from={{ source }},src=/modules,dst=/tmp/modules,rw \
{%- else %}
{%- else if module.is_local_source() %}
--mount=type=bind,from=stage-modules,src=/modules,dst=/tmp/modules,rw \
{%- else %}
--mount=type=bind,from=ghcr.io/blue-build/modules/{{ module.module_type }}:latest,src=/modules,dst=/tmp/modules,rw \
{%- endif %}
--mount=type=bind,from={{ build_scripts_image }},src=/scripts/,dst=/tmp/scripts/ \
/tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module|json|safe }}'
Expand Down
6 changes: 3 additions & 3 deletions template/templates/stages.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ FROM scratch AS stage-config
COPY ./config /config
{% endif %}

{%- if self::modules_exists() %}
# Copy modules
# The default modules are inside blue-build/modules
# Custom modules overwrite defaults
FROM scratch AS stage-modules
COPY --from=ghcr.io/blue-build/modules:latest /modules /modules
{%- if self::modules_exists() %}
COPY ./modules /modules
{% endif %}

Expand All @@ -25,7 +24,8 @@ COPY ./modules /modules
# can be added to the ostree commits.
FROM scratch AS stage-bins
COPY --from={{ blue_build_utils::constants::COSIGN_IMAGE }} /ko-app/cosign /bins/cosign
COPY --from=ghcr.io/blue-build/cli:
COPY --from={{ blue_build_utils::constants::NUSHELL_IMAGE }} /nu/nu* /bins/
COPY --from={{ blue_build_utils::constants::BLUE_BULID_IMAGE_REF }}:
{%- if let Some(tag) = recipe.blue_build_tag -%}
{{ tag }}
{%- else -%}
Expand Down
2 changes: 2 additions & 0 deletions utils/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ pub const XDG_RUNTIME_DIR: &str = "XDG_RUNTIME_DIR";

// Misc
pub const BUILD_SCRIPTS_IMAGE_REF: &str = "ghcr.io/blue-build/cli/build-scripts";
pub const BLUE_BULID_IMAGE_REF: &str = "ghcr.io/blue-build/cli";
pub const COSIGN_IMAGE: &str = "ghcr.io/sigstore/cosign/cosign:v2.4.1";
pub const NUSHELL_IMAGE: &str = "ghcr.io/blue-build/nushell-image:latest";
pub const OCI_ARCHIVE: &str = "oci-archive";
pub const OSTREE_IMAGE_SIGNED: &str = "ostree-image-signed";
pub const OSTREE_UNVERIFIED_IMAGE: &str = "ostree-unverified-image";
Expand Down

0 comments on commit 8325de0

Please sign in to comment.