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 anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -18,17 +18,17 @@ enum CommandArgs {
Template { Template {
/// The recipe file to create a template from /// The recipe file to create a template from
#[arg()] #[arg()]
recipe: PathBuf, recipe: String,
/// Optional Containerfile to use as a template /// Optional Containerfile to use as a template
#[arg(short, long)] #[arg(short, long)]
containerfile: Option<PathBuf>, containerfile: Option<String>,
}, },
/// Build an image from a Containerfile /// Build an image from a Containerfile
Build { Build {
#[arg()] #[arg()]
containerfile: PathBuf, containerfile: String,
}, },
} }
@ -38,16 +38,32 @@ fn main() -> Result<()> {
match args.command { match args.command {
CommandArgs::Template { CommandArgs::Template {
recipe, recipe,
containerfile, containerfile: _,
} => { } => {
let recipe: Recipe = serde_yaml::from_str(fs::read_to_string(recipe)?.as_str())?; let mut recipe_de: Recipe =
println!("{:#?}", &recipe); serde_yaml::from_str(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?;
let context = Context::from_serialize(recipe)?;
dbg!(&context); recipe_de.rpm.repos = recipe_de
let output = Tera::one_off(DEFAULT_CONTAINERFILE, &context, true)?; .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}"); println!("{output}");
} }
CommandArgs::Build { containerfile } => { CommandArgs::Build { containerfile: _ } => {
println!("Not yet implemented!"); println!("Not yet implemented!");
todo!(); todo!();
} }

View file

@ -7,17 +7,17 @@ pub const DEFAULT_CONTAINERFILE: &'static str =
pub struct Recipe { pub struct Recipe {
pub name: String, pub name: String,
#[serde(rename = "base-image")] #[serde(alias = "base-image")]
pub base_image: String, pub base_image: String,
#[serde(rename = "fedora-version")] #[serde(alias = "fedora-version")]
pub fedora_version: u16, pub fedora_version: u16,
pub scripts: Scripts, pub scripts: Scripts,
pub rpm: Rpm, pub rpm: Rpm,
#[serde(rename = "usr-dir-overlays")] #[serde(alias = "usr-dir-overlays")]
pub usr_dir_overlays: Option<Vec<String>>, pub usr_dir_overlays: Option<Vec<String>>,
pub containerfiles: Option<Containerfiles>, pub containerfiles: Option<Containerfiles>,

View file

@ -31,19 +31,19 @@ COPY scripts /tmp/scripts
{% endif %} {% endif %}
{% for script in scripts.pre %} {% 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 %} {% endfor %}
{% for repo in rpm.repo %} {% for repo in rpm.repos %}
RUN wget {% raw %}"${{% endraw %}{{ repo }}//%FEDORA_VERSION%/{{ fedora_verison }}{% raw %}}"{% endraw %} -P "/etc/yum.repos.d/" RUN wget "{{ repo }}" -P "/etc/yum.repos.d/"
{% endfor %} {% 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 %} {% 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 %} {% endfor %}
{% if continerfiles %} {% if continerfiles %}