Skip to content

Commit

Permalink
Implement recipe build process
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Sep 2, 2024
1 parent f6900de commit 459cc41
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 30 deletions.
12 changes: 7 additions & 5 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()),
Expand All @@ -398,5 +397,8 @@ fn docker_run(opts: &RunOpts, cid_file: &Path) -> Command {
},
&*opts.image,
for opts.args,
)
);
trace!("{command:?}");

command
}
4 changes: 2 additions & 2 deletions process/drivers/opts/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions process/drivers/podman_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 '{}'",
Expand All @@ -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 => [
Expand All @@ -280,5 +287,8 @@ fn podman_run(opts: &RunOpts, cid_file: &Path) -> Command {
],
&*opts.image,
for opts.args,
)
);
trace!("{command:?}");

command
}
8 changes: 4 additions & 4 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
48 changes: 37 additions & 11 deletions src/commands/generate_iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 } => {
Expand All @@ -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/"
]);
}
}

Expand All @@ -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()?;
Expand Down

0 comments on commit 459cc41

Please sign in to comment.