Get cli in basic working order

This commit is contained in:
Gerald Pinder 2023-10-01 15:25:53 -04:00
parent e42cda01ff
commit bd6fabd0de
4 changed files with 135 additions and 98 deletions

View file

@ -1,36 +1,6 @@
use std::{fs, path::PathBuf};
use anyhow::Result;
use clap::{Parser, Subcommand};
use tera::{Context, Tera};
use ublue_rs::{Recipe, DEFAULT_CONTAINERFILE};
#[derive(Parser, Debug)]
#[command(name = "Ublue Builder", author, version, about, long_about = None)]
struct UblueArgs {
#[command(subcommand)]
command: CommandArgs,
}
#[derive(Debug, Subcommand)]
enum CommandArgs {
/// Generate a Containerfile from a recipe
Template {
/// The recipe file to create a template from
#[arg()]
recipe: String,
/// Optional Containerfile to use as a template
#[arg(short, long)]
containerfile: Option<String>,
},
/// Build an image from a Containerfile
Build {
#[arg()]
containerfile: String,
},
}
use clap::Parser;
use ublue_rs::{setup_tera, CommandArgs, UblueArgs};
fn main() -> Result<()> {
let args = UblueArgs::parse();
@ -40,26 +10,7 @@ fn main() -> Result<()> {
recipe,
containerfile: _,
} => {
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 (tera, context) = setup_tera(recipe)?;
let output = tera.render("Containerfile", &context)?;
println!("{output}");
}

View file

@ -1,51 +1,52 @@
use serde::{Deserialize, Serialize};
use std::{fs, path::PathBuf};
use anyhow::Result;
use clap::{Parser, Subcommand};
use recipe::Recipe;
use tera::{Context, Tera};
pub const DEFAULT_CONTAINERFILE: &'static str =
include_str!("../templates/starting_point.template");
#[derive(Serialize, Deserialize, Debug)]
pub struct Recipe {
pub name: String,
pub mod recipe;
#[serde(alias = "base-image")]
pub base_image: String,
#[serde(alias = "fedora-version")]
pub fedora_version: u16,
pub scripts: Scripts,
pub rpm: Rpm,
#[serde(alias = "usr-dir-overlays")]
pub usr_dir_overlays: Option<Vec<String>>,
pub containerfiles: Option<Containerfiles>,
pub firstboot: FirstBoot,
#[derive(Parser, Debug)]
#[command(name = "Ublue Builder", author, version, about, long_about = None)]
pub struct UblueArgs {
#[command(subcommand)]
pub command: CommandArgs,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Scripts {
pub pre: Vec<String>,
pub post: Vec<String>,
#[derive(Debug, Subcommand)]
pub enum CommandArgs {
/// Generate a Containerfile from a recipe
Template {
/// The recipe file to create a template from
#[arg()]
recipe: String,
/// Optional Containerfile to use as a template
#[arg(short, long)]
containerfile: Option<String>,
},
/// Build an image from a Containerfile
Build {
#[arg()]
containerfile: String,
},
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Rpm {
pub repos: Vec<String>,
pub install: Vec<String>,
pub remove: Vec<String>,
}
pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> {
let recipe_de =
serde_yaml::from_str::<Recipe>(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?
.process_repos();
#[derive(Debug, Serialize, Deserialize)]
pub struct FirstBoot {
pub yafti: bool,
pub flatpaks: Vec<String>,
}
let mut context = Context::from_serialize(recipe_de)?;
context.insert("recipe", &recipe);
#[derive(Debug, Serialize, Deserialize)]
pub struct Containerfiles {
pub pre: Vec<String>,
pub post: Vec<String>,
let mut tera = Tera::default();
tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?;
Ok((tera, context))
}

60
src/recipe.rs Normal file
View file

@ -0,0 +1,60 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Recipe {
pub name: String,
#[serde(alias = "base-image")]
pub base_image: String,
#[serde(alias = "fedora-version")]
pub fedora_version: u16,
pub scripts: Scripts,
pub rpm: Rpm,
#[serde(alias = "usr-dir-overlays")]
pub usr_dir_overlays: Option<Vec<String>>,
pub containerfiles: Option<Containerfiles>,
pub firstboot: FirstBoot,
}
impl Recipe {
pub fn process_repos(mut self) -> Self {
self.rpm.repos = self
.rpm
.repos
.iter()
.map(|s| s.replace("%FEDORA_VERSION%", self.fedora_version.to_string().as_str()))
.collect();
self
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Scripts {
pub pre: Vec<String>,
pub post: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Rpm {
pub repos: Vec<String>,
pub install: Vec<String>,
pub remove: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FirstBoot {
pub yafti: bool,
pub flatpaks: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Containerfiles {
pub pre: Vec<String>,
pub post: Vec<String>,
}

View file

@ -23,33 +23,58 @@ COPY --from=docker.io/mikefarah/yq /usr/bin/yq /usr/bin/yq
COPY --from=gcr.io/projectsigstore/cosign /ko-app/cosign /usr/bin/cosign
COPY scripts /tmp/scripts
RUN find /tmp/scripts -type f -exec chmod +x {} \;
{# TODO: Create helper function
{% if continerfiles %}
{% for containerfile in containerfiles.pre %}
{{ print_containerfile(containerfile = containerfile) }}
{% endfor %}
{% endif %}
{% endif %} #}
{% for script in scripts.pre %}
RUN chmod +x /tmp/scripts/{{ script }} && /tmp/scripts/{{ script }} pre
RUN /bin/bash -c '/tmp/scripts/{{ script }} pre'
{% endfor %}
{% 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 override remove {% for app in rpm.remove %}{{ app }} {% endfor %}
ARG FIRSTBOOT_DATA="/usr/share/ublue-os/firstboot"
ARG FIRSTBOOT_LINK="/usr/etc/profile.d/ublue-firstboot.sh"
{% if firstboot.yafti -%}
RUN echo "-- firstboot: Installing and enabling \"yafti\" --"; \
pip install --prefix=/usr yafti; \
mkdir -p "$(dirname "${FIRSTBOOT_LINK}")"; \
ln -s "${FIRSTBOOT_DATA}/launcher/login-profile.sh" "${FIRSTBOOT_LINK}"
ARG YAFTI_FILE="${FIRSTBOOT_DATA}/yafti.yml"
RUN echo "-- yafti: Adding Flatpaks defined in recipe.yml --"; \
yq -i '.screens.applications.values.groups.Custom.description = "Flatpaks suggested by the image maintainer."' "${YAFTI_FILE}"; \
yq -i '.screens.applications.values.groups.Custom.default = true' "${YAFTI_FILE}"; \
{%- for pkg in firstboot.flatpaks %}
echo "Adding to yafti: {{ pkg }}"; \
yq -i ".screens.applications.values.groups.Custom.packages += [{\"{{ pkg }}\": \"{{ pkg }}\"}]" "${YAFTI_FILE}"; \
{%- endfor %}
echo "Done setting up Yafti"
{% else %}
RUN echo "-- firstboot: Removing all \"firstboot\" components --"; \
rm -f "${FIRSTBOOT_LINK}"; \
rm -rf "${FIRSTBOOT_DATA}"
{% endif %}
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 }} post
{% endfor %}
{% for script in scripts.post -%}
RUN /bin/bash -c '/tmp/scripts/{{ script }} post'
{% endfor -%}
{# TODO: Create helper function
{% if continerfiles %}
{% for containerfile in containerfiles.post %}
{{ print_containerfile(containerfile = containerfile) }}
{% endfor %}
{% endif %}
{% endif %} #}
RUN rm -rf /tmp/* /var/* && ostree container commit