From 459cc417167784cdcf61ea90a5e64b92f4c640f6 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Mon, 2 Sep 2024 01:15:49 -0400 Subject: [PATCH] Implement recipe build process --- process/drivers/docker_driver.rs | 12 ++++---- process/drivers/opts/run.rs | 4 +-- process/drivers/podman_driver.rs | 20 +++++++++---- src/commands/build.rs | 8 +++--- src/commands/generate.rs | 4 +-- src/commands/generate_iso.rs | 48 ++++++++++++++++++++++++-------- 6 files changed, 66 insertions(+), 30 deletions(-) diff --git a/process/drivers/docker_driver.rs b/process/drivers/docker_driver.rs index 051a9991..a05922e0 100644 --- a/process/drivers/docker_driver.rs +++ b/process/drivers/docker_driver.rs @@ -349,9 +349,8 @@ impl RunDriver for DockerDriver { add_cid(&cid); - let command = docker_run(opts, &cid_file); - trace!("{command:?}"); - let status = command.status_image_ref_progress(&*opts.image, "Running container")?; + let status = docker_run(opts, &cid_file) + .status_image_ref_progress(&*opts.image, "Running container")?; remove_cid(&cid); @@ -374,7 +373,7 @@ impl RunDriver for DockerDriver { } fn docker_run(opts: &RunOpts, cid_file: &Path) -> Command { - cmd!( + let command = cmd!( "docker", "run", format!("--cidfile={}", cid_file.display()), @@ -398,5 +397,8 @@ fn docker_run(opts: &RunOpts, cid_file: &Path) -> Command { }, &*opts.image, for opts.args, - ) + ); + trace!("{command:?}"); + + command } diff --git a/process/drivers/opts/run.rs b/process/drivers/opts/run.rs index bf63c184..6c0971a6 100644 --- a/process/drivers/opts/run.rs +++ b/process/drivers/opts/run.rs @@ -48,7 +48,7 @@ pub struct RunOptsVolume<'scope> { macro_rules! run_volumes { ($($host:expr => $container:expr),+ $(,)?) => { { - [ + vec![ $($crate::drivers::opts::RunOptsVolume::builder() .path_or_vol_name($host) .container_path($container) @@ -71,7 +71,7 @@ pub struct RunOptsEnv<'scope> { macro_rules! run_envs { ($($key:expr => $value:expr),+ $(,)?) => { { - [ + vec![ $($crate::drivers::opts::RunOptsEnv::builder() .key($key) .value($value) diff --git a/process/drivers/podman_driver.rs b/process/drivers/podman_driver.rs index d8eb5ab6..3405c6d6 100644 --- a/process/drivers/podman_driver.rs +++ b/process/drivers/podman_driver.rs @@ -227,8 +227,12 @@ impl RunDriver for PodmanDriver { add_cid(&cid); - let status = podman_run(opts, &cid_file) - .status_image_ref_progress(&*opts.image, "Running container")?; + let status = if opts.privileged { + podman_run(opts, &cid_file).status()? + } else { + podman_run(opts, &cid_file) + .status_image_ref_progress(&*opts.image, "Running container")? + }; remove_cid(&cid); @@ -254,7 +258,7 @@ impl RunDriver for PodmanDriver { } fn podman_run(opts: &RunOpts, cid_file: &Path) -> Command { - cmd!( + let command = cmd!( if opts.privileged { warn!( "Running 'podman' in privileged mode requires '{}'", @@ -267,7 +271,10 @@ fn podman_run(opts: &RunOpts, cid_file: &Path) -> Command { if opts.privileged => "podman", "run", format!("--cidfile={}", cid_file.display()), - if opts.privileged => "--privileged", + if opts.privileged => [ + "--privileged", + "--network=host", + ], if opts.remove => "--rm", if opts.pull => "--pull=always", for volume in opts.volumes => [ @@ -280,5 +287,8 @@ fn podman_run(opts: &RunOpts, cid_file: &Path) -> Command { ], &*opts.image, for opts.args, - ) + ); + trace!("{command:?}"); + + command } diff --git a/src/commands/build.rs b/src/commands/build.rs index 0d075dcd..31a08862 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -211,13 +211,13 @@ impl BuildCommand { } else { PathBuf::from(CONTAINER_FILE) }; - let image_name = self.generate_full_image_name(&recipe)?; let tags = Driver::generate_tags( &GenerateTagsOpts::builder() - .oci_ref(&image_name) + .oci_ref(&recipe.base_image_ref()?) .alt_tags(recipe.alt_tags()) .build(), )?; + let image_name = self.generate_full_image_name(&recipe)?; let opts = if let Some(archive_dir) = self.archive.as_ref() { BuildTagPushOpts::builder() @@ -270,13 +270,13 @@ impl BuildCommand { let recipe = Recipe::parse(recipe_path)?; let containerfile = PathBuf::from(CONTAINER_FILE); - let image_name = self.generate_full_image_name(&recipe)?; let tags = Driver::generate_tags( &GenerateTagsOpts::builder() - .oci_ref(&image_name) + .oci_ref(&recipe.base_image_ref()?) .alt_tags(recipe.alt_tags()) .build(), )?; + let image_name = self.generate_full_image_name(&recipe)?; let opts = if let Some(archive_dir) = self.archive.as_ref() { BuildTagPushOpts::builder() diff --git a/src/commands/generate.rs b/src/commands/generate.rs index 1c884db7..c3581f11 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -109,9 +109,7 @@ impl GenerateCommand { info!("Templating for recipe at {}", recipe_path.display()); let template = ContainerFileTemplate::builder() - .os_version(Driver::get_os_version( - &recipe.base_image.parse().into_diagnostic()?, - )?) + .os_version(Driver::get_os_version(&recipe.base_image_ref()?)?) .build_id(Driver::get_build_id()) .recipe(&recipe) .recipe_path(recipe_path.as_path()) diff --git a/src/commands/generate_iso.rs b/src/commands/generate_iso.rs index 992bdce4..8c8c6996 100644 --- a/src/commands/generate_iso.rs +++ b/src/commands/generate_iso.rs @@ -3,7 +3,8 @@ use std::{ path::{self, PathBuf}, }; -use blue_build_utils::string_vec; +use blue_build_recipe::Recipe; +use blue_build_utils::{constants::ARCHIVE_SUFFIX, string_vec}; use clap::{Args, Subcommand, ValueEnum}; use miette::{bail, Context, IntoDiagnostic, Result}; use oci_distribution::Reference; @@ -125,16 +126,29 @@ impl BlueBuildCommand for GenerateIsoCommand { fs::create_dir(&output_dir).into_diagnostic()?; } - output_dir + path::absolute(output_dir).into_diagnostic()? } else { env::current_dir().into_diagnostic()? }; if let GenIsoSubcommand::Recipe { recipe } = &self.command { - // BuildCommand::builder().recipe( - - // ) - todo!() + #[cfg(feature = "multi-recipe")] + let mut build_command = { + BuildCommand::builder() + .recipe(vec![recipe.clone()]) + .archive(image_out_dir.path()) + .build() + }; + #[cfg(not(feature = "multi-recipe"))] + let mut build_command = { + BuildCommand::builder() + .recipe(recipe.to_path_buf()) + .output_dir(image_out_dir.path()) + .archive(image_out_dir.path()) + .build() + }; + + build_command.try_run()?; } let iso_name = self @@ -154,6 +168,10 @@ impl BlueBuildCommand for GenerateIsoCommand { format!("SECURE_BOOT_KEY_URL={}", self.secure_boot_url), format!("ENROLLMENT_PASSWORD={}", self.enrollment_password), ]; + let mut vols = run_volumes![ + output_dir.display().to_string() => "/build-container-installer/build", + "dnf-cache" => "/cache/dnf/", + ]; match &self.command { GenIsoSubcommand::Image { image } => { @@ -180,7 +198,18 @@ impl BlueBuildCommand for GenerateIsoCommand { ]); } GenIsoSubcommand::Recipe { recipe } => { - todo!() + let recipe = Recipe::parse(recipe)?; + + args.extend([ + format!("IMAGE_SRC=/img_src/{}.{ARCHIVE_SUFFIX}", recipe.name), + format!( + "VERSION={}", + Driver::get_os_version(&recipe.base_image_ref()?)? + ), + ]); + vols.extend(run_volumes![ + image_out_dir.path().display().to_string() => "/img_src/" + ]); } } @@ -190,10 +219,7 @@ impl BlueBuildCommand for GenerateIsoCommand { .privileged(true) .remove(true) .args(&args) - .volumes(run_volumes![ - path::absolute(output_dir).into_diagnostic()?.display().to_string() => "/build-container-installer/build", - "dnf-cache" => "/cache/dnf/", - ]) + .volumes(vols) .build(); let status = Driver::run(&opts).into_diagnostic()?;