From e42cda01ff57b1717f514d42398aa9de7bbadc59 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Tue, 26 Sep 2023 23:47:31 -0400 Subject: [PATCH] Able to generate a Containerfile --- src/bin/ublue.rs | 38 ++++++++++++++++++++++--------- src/lib.rs | 6 ++--- templates/starting_point.template | 12 +++++----- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/bin/ublue.rs b/src/bin/ublue.rs index 0d3344b..1d82db2 100644 --- a/src/bin/ublue.rs +++ b/src/bin/ublue.rs @@ -1,4 +1,4 @@ -use std::{fs, io, path::PathBuf}; +use std::{fs, path::PathBuf}; use anyhow::Result; use clap::{Parser, Subcommand}; @@ -18,17 +18,17 @@ enum CommandArgs { Template { /// The recipe file to create a template from #[arg()] - recipe: PathBuf, + recipe: String, /// Optional Containerfile to use as a template #[arg(short, long)] - containerfile: Option, + containerfile: Option, }, /// Build an image from a Containerfile Build { #[arg()] - containerfile: PathBuf, + containerfile: String, }, } @@ -38,16 +38,32 @@ fn main() -> Result<()> { match args.command { CommandArgs::Template { recipe, - containerfile, + containerfile: _, } => { - let recipe: Recipe = serde_yaml::from_str(fs::read_to_string(recipe)?.as_str())?; - println!("{:#?}", &recipe); - let context = Context::from_serialize(recipe)?; - dbg!(&context); - let output = Tera::one_off(DEFAULT_CONTAINERFILE, &context, true)?; + let mut recipe_de: Recipe = + serde_yaml::from_str(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?; + + recipe_de.rpm.repos = recipe_de + .rpm + .repos + .iter() + .map(|s| { + s.replace( + "%FEDORA_VERSION%", + recipe_de.fedora_version.to_string().as_str(), + ) + }) + .collect(); + + let mut context = Context::from_serialize(recipe_de)?; + context.insert("recipe", &recipe); + + let mut tera = Tera::default(); + tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?; + let output = tera.render("Containerfile", &context)?; println!("{output}"); } - CommandArgs::Build { containerfile } => { + CommandArgs::Build { containerfile: _ } => { println!("Not yet implemented!"); todo!(); } diff --git a/src/lib.rs b/src/lib.rs index e0a0e1c..712c6d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,17 +7,17 @@ pub const DEFAULT_CONTAINERFILE: &'static str = pub struct Recipe { pub name: String, - #[serde(rename = "base-image")] + #[serde(alias = "base-image")] pub base_image: String, - #[serde(rename = "fedora-version")] + #[serde(alias = "fedora-version")] pub fedora_version: u16, pub scripts: Scripts, pub rpm: Rpm, - #[serde(rename = "usr-dir-overlays")] + #[serde(alias = "usr-dir-overlays")] pub usr_dir_overlays: Option>, pub containerfiles: Option, diff --git a/templates/starting_point.template b/templates/starting_point.template index 407f52f..3628446 100644 --- a/templates/starting_point.template +++ b/templates/starting_point.template @@ -31,19 +31,19 @@ COPY scripts /tmp/scripts {% endif %} {% for script in scripts.pre %} -RUN chmod +x /tmp/scripts/{{ script }} && /tmp/scripts/{{ script }} +RUN chmod +x /tmp/scripts/{{ script }} && /tmp/scripts/{{ script }} pre {% endfor %} -{% for repo in rpm.repo %} -RUN wget {% raw %}"${{% endraw %}{{ repo }}//%FEDORA_VERSION%/{{ fedora_verison }}{% raw %}}"{% endraw %} -P "/etc/yum.repos.d/" +{% for repo in rpm.repos %} +RUN wget "{{ repo }}" -P "/etc/yum.repos.d/" {% endfor %} -RUN rpm-ostree uninstall {% for app in rpm.remove %}{{ app }}{% endfor %} +RUN rpm-ostree uninstall {% for app in rpm.remove %}{{ app }} {% endfor %} -RUN rpm-ostree install {% for app in rpm.install %}{{ app }}{% endfor %} +RUN rpm-ostree install {% for app in rpm.install %}{{ app }} {% endfor %} {% for script in scripts.post %} -RUN chmod +x /tmp/scripts/{{ script }} && /tmp/scripts/{{ script }} +RUN chmod +x /tmp/scripts/{{ script }} && /tmp/scripts/{{ script }} post {% endfor %} {% if continerfiles %}