From 2cd887849012a705ee77adf0416b178963714a5f Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sun, 1 Oct 2023 18:54:23 -0400 Subject: [PATCH] Create autorun script capabilities --- src/lib.rs | 35 ++++++++++++++++++++++++++++++- templates/starting_point.template | 4 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c0a0756..a2ab78b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - fs::{self, read_to_string}, + fs::{self, read_dir, read_to_string}, path::PathBuf, }; @@ -57,6 +57,38 @@ fn print_containerfile() -> impl Function { ) } +fn print_autorun_scripts() -> impl Function { + Box::new( + |args: &HashMap| -> tera::Result { + match args.get("mode") { + Some(v) => match from_value::(v.clone()) { + Ok(mode) if mode == "pre" || mode == "post" => { + Ok(read_dir(format!("scripts/{mode}"))? + .fold(String::from(""), |mut acc: String, script| match script { + Ok(entry) => { + let file_name = entry.file_name(); + if let Some(file_name) = file_name.to_str() { + if file_name.ends_with(".sh") { + acc += format!( + "RUN /tmp/scripts/{mode}/{file_name} {mode}\n" + ) + .as_str(); + } + } + acc + } + Err(_) => acc, + }) + .into()) + } + _ => Err("Mode must be pre/post".into()), + }, + None => Err("Need arg 'mode' set with 'pre' or 'post'".into()), + } + }, + ) +} + pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> { let recipe_de = serde_yaml::from_str::(fs::read_to_string(PathBuf::from(&recipe))?.as_str())? @@ -68,6 +100,7 @@ pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> { let mut tera = Tera::default(); tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?; tera.register_function("print_containerfile", print_containerfile()); + tera.register_function("print_autorun_scripts", print_autorun_scripts()); Ok((tera, context)) } diff --git a/templates/starting_point.template b/templates/starting_point.template index 450b0cb..5262eac 100644 --- a/templates/starting_point.template +++ b/templates/starting_point.template @@ -32,6 +32,8 @@ RUN find /tmp/scripts -type f -exec chmod +x {} \; {% endfor %} {% endif %} +{{ print_autorun_scripts(mode = "pre") }} + {% for script in scripts.pre %} RUN /bin/bash -c '/tmp/scripts/{{ script }} pre' {% endfor %} @@ -66,6 +68,8 @@ RUN echo "-- firstboot: Removing all \"firstboot\" components --"; \ RUN rpm-ostree install {% for app in rpm.install %}{{ app }} {% endfor %} +{{ print_autorun_scripts(mode = "post") }} + {% for script in scripts.post -%} RUN /bin/bash -c '/tmp/scripts/{{ script }} post' {% endfor -%}