Able to generate a Containerfile

This commit is contained in:
Gerald Pinder 2023-09-26 23:47:31 -04:00
parent 5361b36238
commit e42cda01ff
3 changed files with 36 additions and 20 deletions

View file

@ -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<PathBuf>,
containerfile: Option<String>,
},
/// 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!();
}

View file

@ -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<Vec<String>>,
pub containerfiles: Option<Containerfiles>,

View file

@ -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 %}