chore: Remove need to update .gitignore by making use of temporary directories

This commit is contained in:
Gerald Pinder 2024-11-06 23:58:28 -05:00
parent 5ff001816c
commit 752c957914
11 changed files with 61 additions and 202 deletions

104
Cargo.lock generated
View file

@ -383,7 +383,7 @@ dependencies = [
"serde_yml",
"shadow-rs",
"syntect",
"tempdir",
"tempfile",
"tokio",
"urlencoding",
"users",
@ -413,7 +413,7 @@ dependencies = [
"oci-distribution",
"once_cell",
"os_pipe",
"rand 0.8.5",
"rand",
"reqwest 0.12.9",
"rstest",
"semver",
@ -421,7 +421,7 @@ dependencies = [
"serde_json",
"signal-hook",
"sigstore",
"tempdir",
"tempfile",
"tokio",
"users",
"uuid",
@ -878,7 +878,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76"
dependencies = [
"generic-array",
"rand_core 0.6.4",
"rand_core",
"subtle",
"zeroize",
]
@ -890,7 +890,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"rand_core 0.6.4",
"rand_core",
"typenum",
]
@ -1121,7 +1121,7 @@ checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871"
dependencies = [
"curve25519-dalek",
"ed25519",
"rand_core 0.6.4",
"rand_core",
"serde",
"sha2",
"subtle",
@ -1149,7 +1149,7 @@ dependencies = [
"hkdf",
"pem-rfc7468",
"pkcs8",
"rand_core 0.6.4",
"rand_core",
"sec1",
"subtle",
"zeroize",
@ -1238,7 +1238,7 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449"
dependencies = [
"rand_core 0.6.4",
"rand_core",
"subtle",
]
@ -1325,12 +1325,6 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1fd087255f739f4f1aeea69f11b72f8080e9c2e7645cd06955dad4a178a49e3"
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "futures"
version = "0.3.31"
@ -1500,7 +1494,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63"
dependencies = [
"ff",
"rand_core 0.6.4",
"rand_core",
"subtle",
]
@ -2421,7 +2415,7 @@ dependencies = [
"log-mdc",
"once_cell",
"parking_lot",
"rand 0.8.5",
"rand",
"serde",
"serde-value",
"serde_json",
@ -2610,7 +2604,7 @@ dependencies = [
"num-integer",
"num-iter",
"num-traits",
"rand 0.8.5",
"rand",
"smallvec",
"zeroize",
]
@ -2708,7 +2702,7 @@ dependencies = [
"chrono",
"getrandom",
"http 0.2.12",
"rand 0.8.5",
"rand",
"reqwest 0.11.27",
"serde",
"serde_json",
@ -2859,7 +2853,7 @@ dependencies = [
"oauth2",
"p256",
"p384",
"rand 0.8.5",
"rand",
"rsa",
"serde",
"serde-value",
@ -2976,7 +2970,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166"
dependencies = [
"base64ct",
"rand_core 0.6.4",
"rand_core",
"subtle",
]
@ -3097,7 +3091,7 @@ checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
dependencies = [
"der",
"pkcs5",
"rand_core 0.6.4",
"rand_core",
"spki",
]
@ -3338,7 +3332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fadfaed2cd7f389d0161bb73eeb07b7b78f8691047a6f3e73caaeae55310a4a6"
dependencies = [
"bytes",
"rand 0.8.5",
"rand",
"ring",
"rustc-hash",
"rustls 0.23.16",
@ -3371,19 +3365,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -3392,7 +3373,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core 0.6.4",
"rand_core",
]
[[package]]
@ -3402,24 +3383,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.4"
@ -3449,15 +3415,6 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
@ -3561,15 +3518,6 @@ version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]]
name = "requestty"
version = "0.5.0"
@ -3781,7 +3729,7 @@ dependencies = [
"num-traits",
"pkcs1",
"pkcs8",
"rand_core 0.6.4",
"rand_core",
"signature",
"spki",
"subtle",
@ -4258,7 +4206,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
dependencies = [
"digest",
"rand_core 0.6.4",
"rand_core",
]
[[package]]
@ -4293,7 +4241,7 @@ dependencies = [
"pem",
"pkcs1",
"pkcs8",
"rand 0.8.5",
"rand",
"regex",
"reqwest 0.11.27",
"reqwest 0.12.9",
@ -4566,16 +4514,6 @@ dependencies = [
"libc",
]
[[package]]
name = "tempdir"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
dependencies = [
"rand 0.4.6",
"remove_dir_all",
]
[[package]]
name = "tempfile"
version = "3.13.0"

View file

@ -26,7 +26,8 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = { version = "0.0.12", package = "serde_yml" }
syntect = { version = "5", default-features = false, features = ["default-fancy"] }
tempdir = "0.3"
# tempdir = "0.3"
tempfile = "3"
tokio = { version = "1", features = ["rt", "rt-multi-thread"] }
users = "0.11"
uuid = { version = "1", features = ["v4"] }
@ -91,7 +92,7 @@ serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
syntect = { workspace = true, optional = true }
tempdir.workspace = true
tempfile.workspace = true
tokio = { workspace = true, optional = true }
bon.workspace = true
users.workspace = true

View file

@ -38,7 +38,7 @@ oci-distribution.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tempdir.workspace = true
tempfile.workspace = true
tokio = { workspace = true, optional = true }
bon.workspace = true
users.workspace = true

View file

@ -174,7 +174,7 @@ mod test {
use std::{fs, path::Path};
use blue_build_utils::constants::{COSIGN_PRIV_PATH, COSIGN_PUB_PATH};
use tempdir::TempDir;
use tempfile::TempDir;
use crate::drivers::{
opts::{CheckKeyPairOpts, GenerateKeyPairOpts},
@ -185,7 +185,7 @@ mod test {
#[test]
fn generate_key_pair() {
let tempdir = TempDir::new("keypair").unwrap();
let tempdir = TempDir::new().unwrap();
let gen_opts = GenerateKeyPairOpts::builder().dir(tempdir.path()).build();
@ -219,7 +219,7 @@ mod test {
fn compatibility() {
use crate::drivers::sigstore_driver::SigstoreDriver;
let tempdir = TempDir::new("keypair").unwrap();
let tempdir = TempDir::new().unwrap();
let gen_opts = GenerateKeyPairOpts::builder().dir(tempdir.path()).build();

View file

@ -18,7 +18,7 @@ use miette::{bail, IntoDiagnostic, Result};
use once_cell::sync::Lazy;
use semver::Version;
use serde::Deserialize;
use tempdir::TempDir;
use tempfile::TempDir;
use crate::{
drivers::{
@ -388,7 +388,7 @@ impl InspectDriver for DockerDriver {
impl RunDriver for DockerDriver {
fn run(opts: &RunOpts) -> std::io::Result<ExitStatus> {
let cid_path = TempDir::new("docker")?;
let cid_path = TempDir::new()?;
let cid_file = cid_path.path().join("cid");
let cid = ContainerId::new(&cid_file, ContainerRuntime::Docker, false);
@ -403,7 +403,7 @@ impl RunDriver for DockerDriver {
}
fn run_output(opts: &RunOpts) -> std::io::Result<std::process::Output> {
let cid_path = TempDir::new("docker")?;
let cid_path = TempDir::new()?;
let cid_file = cid_path.path().join("cid");
let cid = ContainerId::new(&cid_file, ContainerRuntime::Docker, false);

View file

@ -14,7 +14,7 @@ use miette::{bail, miette, IntoDiagnostic, Report, Result};
use oci_distribution::Reference;
use semver::Version;
use serde::Deserialize;
use tempdir::TempDir;
use tempfile::TempDir;
use crate::{
drivers::{
@ -310,7 +310,7 @@ impl RunDriver for PodmanDriver {
fn run(opts: &RunOpts) -> std::io::Result<ExitStatus> {
trace!("PodmanDriver::run({opts:#?})");
let cid_path = TempDir::new("podman")?;
let cid_path = TempDir::new()?;
let cid_file = cid_path.path().join("cid");
let cid = ContainerId::new(&cid_file, ContainerRuntime::Podman, opts.privileged);
@ -332,7 +332,7 @@ impl RunDriver for PodmanDriver {
fn run_output(opts: &RunOpts) -> std::io::Result<std::process::Output> {
trace!("PodmanDriver::run_output({opts:#?})");
let cid_path = TempDir::new("podman")?;
let cid_path = TempDir::new()?;
let cid_file = cid_path.path().join("cid");
let cid = ContainerId::new(&cid_file, ContainerRuntime::Podman, opts.privileged);

View file

@ -235,7 +235,7 @@ mod test {
use std::{fs, path::Path};
use blue_build_utils::constants::{COSIGN_PRIV_PATH, COSIGN_PUB_PATH};
use tempdir::TempDir;
use tempfile::TempDir;
use crate::drivers::{
cosign_driver::CosignDriver,
@ -247,7 +247,7 @@ mod test {
#[test]
fn generate_key_pair() {
let tempdir = TempDir::new("keypair").unwrap();
let tempdir = TempDir::new().unwrap();
let gen_opts = GenerateKeyPairOpts::builder().dir(tempdir.path()).build();
@ -278,7 +278,7 @@ mod test {
#[test]
fn compatibility() {
let tempdir = TempDir::new("keypair").unwrap();
let tempdir = TempDir::new().unwrap();
let gen_opts = GenerateKeyPairOpts::builder().dir(tempdir.path()).build();

View file

@ -1,7 +1,4 @@
use std::{
fs,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};
use blue_build_process_management::{
drivers::{
@ -17,8 +14,8 @@ use blue_build_process_management::{
use blue_build_recipe::Recipe;
use blue_build_utils::{
constants::{
ARCHIVE_SUFFIX, BB_REGISTRY_NAMESPACE, BUILD_ID_LABEL, CONFIG_PATH, CONTAINER_FILE,
GITIGNORE_PATH, LABELED_ERROR_MESSAGE, NO_LABEL_ERROR_MESSAGE, RECIPE_FILE, RECIPE_PATH,
ARCHIVE_SUFFIX, BB_REGISTRY_NAMESPACE, CONFIG_PATH, CONTAINER_FILE, RECIPE_FILE,
RECIPE_PATH,
},
cowstr,
credentials::{Credentials, CredentialsArgs},
@ -27,16 +24,16 @@ use blue_build_utils::{
};
use bon::Builder;
use clap::Args;
use colored::Colorize;
use log::{info, trace, warn};
use miette::{bail, Context, IntoDiagnostic, Result};
use miette::{bail, IntoDiagnostic, Result};
use tempfile::TempDir;
use crate::commands::generate::GenerateCommand;
use super::BlueBuildCommand;
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Args, Builder)]
#[derive(Debug, Args, Builder)]
pub struct BuildCommand {
/// The recipe file to build an image
#[arg()]
@ -129,6 +126,9 @@ pub struct BuildCommand {
#[clap(flatten)]
#[builder(default)]
drivers: DriverArgs,
#[clap(skip)]
temp_dir: Option<TempDir>,
}
impl BlueBuildCommand for BuildCommand {
@ -140,8 +140,6 @@ impl BlueBuildCommand for BuildCommand {
Credentials::init(self.credentials.clone());
self.update_gitignore()?;
if self.push && self.archive.is_some() {
bail!("You cannot use '--archive' and '--push' at the same time");
}
@ -153,6 +151,9 @@ impl BlueBuildCommand for BuildCommand {
Driver::signing_login()?;
}
self.temp_dir = Some(TempDir::new().into_diagnostic()?);
let temp_dir = self.temp_dir.as_ref().unwrap();
#[cfg(feature = "multi-recipe")]
{
use rayon::prelude::*;
@ -174,11 +175,11 @@ impl BlueBuildCommand for BuildCommand {
recipe_paths.par_iter().try_for_each(|recipe| {
GenerateCommand::builder()
.output(if recipe_paths.len() > 1 {
.output(temp_dir.path().join(if recipe_paths.len() > 1 {
blue_build_utils::generate_containerfile_path(recipe)?
} else {
PathBuf::from(CONTAINER_FILE)
})
}))
.platform(self.platform)
.recipe(recipe)
.drivers(self.drivers)
@ -203,7 +204,7 @@ impl BlueBuildCommand for BuildCommand {
});
GenerateCommand::builder()
.output(CONTAINER_FILE)
.output(temp_dir.path().join(CONTAINER_FILE))
.recipe(&recipe_path)
.drivers(self.drivers)
.build()
@ -220,15 +221,16 @@ impl BuildCommand {
use rayon::prelude::*;
trace!("BuildCommand::build_image()");
let temp_dir = self.temp_dir.as_ref().unwrap();
let images = recipe_paths
.par_iter()
.try_fold(Vec::new, |mut images, recipe_path| -> Result<Vec<String>> {
let containerfile = if recipe_paths.len() > 1 {
let containerfile = temp_dir.path().join(if recipe_paths.len() > 1 {
blue_build_utils::generate_containerfile_path(recipe_path)?
} else {
PathBuf::from(CONTAINER_FILE)
};
});
images.extend(self.build(recipe_path, &containerfile)?);
Ok(images)
})
@ -252,8 +254,9 @@ impl BuildCommand {
#[cfg(not(feature = "multi-recipe"))]
fn start(&self, recipe_path: &Path) -> Result<()> {
trace!("BuildCommand::start()");
let temp_dir = self.temp_dir.as_ref().unwrap();
let images = self.build(recipe_path, Path::new(CONTAINER_FILE))?;
let images = self.build(recipe_path, &temp_dir.path().join(CONTAINER_FILE))?;
let color = gen_random_ansi_color();
info!(
@ -339,78 +342,4 @@ impl BuildCommand {
Ok(image_name)
}
fn update_gitignore(&self) -> Result<()> {
// Check if the Containerfile exists
// - If doesn't => *Build*
// - If it does:
// - check entry in .gitignore
// -> If it is => *Build*
// -> If isn't:
// - check if it has the BlueBuild tag (LABEL)
// -> If it does => *Ask* to add to .gitignore and remove from git
// -> If it doesn't => *Ask* to continue and override the file
let container_file_path = Path::new(CONTAINER_FILE);
let label = format!("LABEL {BUILD_ID_LABEL}");
if !self.force && container_file_path.exists() {
let to_ignore_lines = [format!("/{CONTAINER_FILE}"), format!("/{CONTAINER_FILE}.*")];
let gitignore = fs::read_to_string(GITIGNORE_PATH)
.into_diagnostic()
.with_context(|| format!("Failed to read {GITIGNORE_PATH}"))?;
let mut edited_gitignore = gitignore.clone();
to_ignore_lines
.iter()
.filter(|to_ignore| {
!gitignore
.lines()
.any(|line| line.trim() == to_ignore.trim())
})
.try_for_each(|to_ignore| -> Result<()> {
let containerfile = fs::read_to_string(container_file_path)
.into_diagnostic()
.with_context(|| {
format!("Failed to read {}", container_file_path.display())
})?;
let has_label = containerfile
.lines()
.any(|line| line.to_string().trim().starts_with(&label));
let question = requestty::Question::confirm("build")
.message(
if has_label {
LABELED_ERROR_MESSAGE
} else {
NO_LABEL_ERROR_MESSAGE
}
.bright_yellow()
.to_string(),
)
.default(true)
.build();
if let Ok(answer) = requestty::prompt_one(question) {
if answer.as_bool().unwrap_or(false) {
if !edited_gitignore.ends_with('\n') {
edited_gitignore.push('\n');
}
edited_gitignore.push_str(to_ignore);
edited_gitignore.push('\n');
}
}
Ok(())
})?;
if edited_gitignore != gitignore {
fs::write(GITIGNORE_PATH, edited_gitignore.as_str()).into_diagnostic()?;
}
}
Ok(())
}
}

View file

@ -9,7 +9,7 @@ use bon::Builder;
use clap::{Args, Subcommand, ValueEnum};
use miette::{bail, Context, IntoDiagnostic, Result};
use oci_distribution::Reference;
use tempdir::TempDir;
use tempfile::TempDir;
use blue_build_process_management::{
drivers::{opts::RunOpts, Driver, DriverArgs, RunDriver},
@ -122,7 +122,7 @@ impl BlueBuildCommand for GenerateIsoCommand {
fn try_run(&mut self) -> Result<()> {
Driver::init(self.drivers);
let image_out_dir = TempDir::new("build_image").into_diagnostic()?;
let image_out_dir = TempDir::new().into_diagnostic()?;
let output_dir = if let Some(output_dir) = self.output_dir.clone() {
if output_dir.exists() && !output_dir.is_dir() {

View file

@ -18,7 +18,7 @@ use colored::Colorize;
use indicatif::ProgressBar;
use log::{debug, trace, warn};
use miette::{bail, IntoDiagnostic, Result};
use tempdir::TempDir;
use tempfile::TempDir;
use crate::{commands::build::BuildCommand, rpm_ostree_status::RpmOstreeStatus};
@ -63,7 +63,7 @@ impl BlueBuildCommand for SwitchCommand {
bail!("There is a transaction in progress. Please cancel it using `rpm-ostree cancel`");
}
let tempdir = TempDir::new("oci-archive").into_diagnostic()?;
let tempdir = TempDir::new().into_diagnostic()?;
trace!("{tempdir:?}");
#[cfg(feature = "multi-recipe")]

View file

@ -6,7 +6,6 @@ pub const CONTAINER_FILE: &str = "Containerfile";
pub const COSIGN_PUB_PATH: &str = "./cosign.pub";
pub const COSIGN_PRIV_PATH: &str = "./cosign.key";
pub const FILES_PATH: &str = "./files";
pub const GITIGNORE_PATH: &str = ".gitignore";
pub const LOCAL_BUILD: &str = "/etc/bluebuild";
pub const MODULES_PATH: &str = "./config/modules";
pub const RECIPE_FILE: &str = "recipe.yml";
@ -85,13 +84,5 @@ pub const UNKNOWN_TERMINAL: &str = "<unknown terminal>";
pub const GITHUB_CHAR_LIMIT: usize = 8100; // Magic number accepted by Github
// Messages
pub const LABELED_ERROR_MESSAGE: &str =
"It looks you have a BlueBuild-generated Containerfile that is not .gitignored. \
Do you want to remove it from git and add it to .gitignore? (This will still continue the build)";
pub const NO_LABEL_ERROR_MESSAGE: &str =
"It looks you have a Containerfile that has not been generated by BlueBuild. \
Running `build` will override your Containerfile and add an entry to the .gitignore. \
Do you want to continue?";
pub const BUG_REPORT_WARNING_MESSAGE: &str =
"Please copy the above report and open an issue manually.";