Skip to content

Commit

Permalink
feat: Create RunDriver (#196)
Browse files Browse the repository at this point in the history
This will be used for running containers for various tasks. There will
be a way to take all output from the process and a way to display output
from a running container like our builds have.
  • Loading branch information
gmpinder authored Jul 5, 2024
1 parent 1a348f8 commit 784be98
Show file tree
Hide file tree
Showing 16 changed files with 515 additions and 103 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ once_cell = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
tempdir = "0.3"
typed-builder = "0.18"
uuid = { version = "1", features = ["v4"] }

Expand Down Expand Up @@ -69,7 +70,6 @@ rayon = { version = "1.10.0", optional = true }
requestty = { version = "0.5", features = ["macros", "termion"] }
semver = { version = "1", features = ["serde"] }
shadow-rs = "0.26"
tempdir = "0.3"
urlencoding = "2"
users = "0.11"

Expand All @@ -85,6 +85,7 @@ once_cell.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
tempdir.workspace = true
typed-builder.workspace = true
uuid.workspace = true

Expand Down
23 changes: 12 additions & 11 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,6 @@ pub enum CommandArgs {

#[derive(Default, Clone, Copy, Debug, TypedBuilder, Args)]
pub struct DriverArgs {
/// Runs all instructions inside one layer of the final image.
///
/// WARN: This doesn't work with the
/// docker driver as it has been deprecated.
///
/// NOTE: Squash has a performance benefit for
/// podman and buildah when running inside a container.
#[arg(short, long)]
#[builder(default)]
squash: bool,

/// Select which driver to use to build
/// your image.
#[builder(default)]
Expand All @@ -146,3 +135,15 @@ pub struct DriverArgs {
#[arg(short = 'I', long)]
inspect_driver: Option<InspectDriverType>,
}

#[cfg(test)]
mod test {
use clap::CommandFactory;

use super::BlueBuildArgs;

#[test]
fn test_cli() {
BlueBuildArgs::command().debug_assert();
}
}
25 changes: 18 additions & 7 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ pub struct BuildCommand {
#[builder(default)]
no_sign: bool,

/// Runs all instructions inside one layer of the final image.
///
/// WARN: This doesn't work with the
/// docker driver as it has been deprecated.
///
/// NOTE: Squash has a performance benefit for
/// podman and buildah when running inside a container.
#[arg(short, long)]
#[builder(default)]
squash: bool,

#[clap(flatten)]
#[builder(default)]
drivers: DriverArgs,
Expand All @@ -134,7 +145,7 @@ impl BlueBuildCommand for BuildCommand {
.build_driver(self.drivers.build_driver)
.inspect_driver(self.drivers.inspect_driver)
.build()
.init()?;
.init();

self.update_gitignore()?;

Expand Down Expand Up @@ -171,7 +182,7 @@ impl BlueBuildCommand for BuildCommand {
GenerateCommand::builder()
.output(generate_containerfile_path(recipe)?)
.recipe(recipe)
.drivers(DriverArgs::builder().squash(self.drivers.squash).build())
.drivers(self.drivers)
.build()
.try_run()
})?;
Expand All @@ -195,7 +206,7 @@ impl BlueBuildCommand for BuildCommand {
GenerateCommand::builder()
.output(generate_containerfile_path(&recipe_path)?)
.recipe(&recipe_path)
.drivers(DriverArgs::builder().squash(self.drivers.squash).build())
.drivers(self.drivers)
.build()
.try_run()?;

Expand Down Expand Up @@ -227,7 +238,7 @@ impl BuildCommand {
archive_dir.to_string_lossy().trim_end_matches('/'),
recipe.name.to_lowercase().replace('/', "_"),
))
.squash(self.drivers.squash)
.squash(self.squash)
.build()
} else {
BuildTagPushOpts::builder()
Expand All @@ -238,7 +249,7 @@ impl BuildCommand {
.no_retry_push(self.no_retry_push)
.retry_count(self.retry_count)
.compression(self.compression_format)
.squash(self.drivers.squash)
.squash(self.squash)
.build()
};

Expand Down Expand Up @@ -273,7 +284,7 @@ impl BuildCommand {
archive_dir.to_string_lossy().trim_end_matches('/'),
recipe.name.to_lowercase().replace('/', "_"),
))
.squash(self.drivers.squash)
.squash(self.squash)
.build()
} else {
BuildTagPushOpts::builder()
Expand All @@ -284,7 +295,7 @@ impl BuildCommand {
.no_retry_push(self.no_retry_push)
.retry_count(self.retry_count)
.compression(self.compression_format)
.squash(self.drivers.squash)
.squash(self.squash)
.build()
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl BlueBuildCommand for GenerateCommand {
.build_driver(self.drivers.build_driver)
.inspect_driver(self.drivers.inspect_driver)
.build()
.init()?;
.init();

self.template_file()
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl BlueBuildCommand for SwitchCommand {
.build_driver(self.drivers.build_driver)
.inspect_driver(self.drivers.inspect_driver)
.build()
.init()?;
.init();

let status = RpmOstreeStatus::try_new()?;
trace!("{status:?}");
Expand Down
15 changes: 4 additions & 11 deletions src/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{env, sync::Mutex};

use anyhow::{anyhow, Result};
use blue_build_utils::constants::{
CI_REGISTRY, CI_REGISTRY_PASSWORD, CI_REGISTRY_USER, GITHUB_ACTIONS, GITHUB_ACTOR, GITHUB_TOKEN,
};
Expand Down Expand Up @@ -107,29 +106,23 @@ static ENV_CREDENTIALS: Lazy<Option<Credentials>> = Lazy::new(|| {
/// any strategy that requires credentials as
/// the environment credentials are lazy allocated.
///
/// # Errors
/// Will error if it can't lock the mutex.
/// # Panics
/// Will panic if it can't lock the mutex.
pub fn set_user_creds(
username: Option<&String>,
password: Option<&String>,
registry: Option<&String>,
) -> Result<()> {
) {
trace!("credentials::set({username:?}, password, {registry:?})");
let mut creds_lock = USER_CREDS
.lock()
.map_err(|e| anyhow!("Failed to set credentials: {e}"))?;
let mut creds_lock = USER_CREDS.lock().expect("Must lock USER_CREDS");
creds_lock.username = username.map(ToOwned::to_owned);
creds_lock.password = password.map(ToOwned::to_owned);
creds_lock.registry = registry.map(ToOwned::to_owned);
drop(creds_lock);
let _ = ENV_CREDENTIALS.as_ref();
Ok(())
}

/// Get the credentials for the current set of actions.
///
/// # Errors
/// Will error if there aren't any credentials available.
pub fn get() -> Option<&'static Credentials> {
trace!("credentials::get()");
ENV_CREDENTIALS.as_ref()
Expand Down
Loading

0 comments on commit 784be98

Please sign in to comment.