refactor: Rename strategies to drivers

This will make the terms similar to the terms of the dependent tools i.e. docker.
This commit is contained in:
Gerald Pinder 2024-03-16 12:47:02 -04:00
parent f619c4c47c
commit 8006af3ec9
10 changed files with 65 additions and 69 deletions

View file

@ -18,7 +18,7 @@ use colorized::{Color, Colors};
use log::{debug, info, trace, warn}; use log::{debug, info, trace, warn};
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
use crate::{commands::template::TemplateCommand, strategies::Strategy}; use crate::{commands::template::TemplateCommand, credentials, drivers::Driver};
use super::BlueBuildCommand; use super::BlueBuildCommand;
@ -142,7 +142,7 @@ impl BlueBuildCommand for BuildCommand {
fn try_run(&mut self) -> Result<()> { fn try_run(&mut self) -> Result<()> {
trace!("BuildCommand::try_run()"); trace!("BuildCommand::try_run()");
Strategy::builder() Driver::builder()
.username(self.username.as_ref()) .username(self.username.as_ref())
.password(self.password.as_ref()) .password(self.password.as_ref())
.registry(self.registry.as_ref()) .registry(self.registry.as_ref())
@ -230,7 +230,7 @@ impl BuildCommand {
trace!("BuildCommand::build_image()"); trace!("BuildCommand::build_image()");
let recipe = Recipe::parse(&recipe_path)?; let recipe = Recipe::parse(&recipe_path)?;
let os_version = Strategy::get_os_version(&recipe)?; let os_version = Driver::get_os_version(&recipe)?;
let tags = recipe.generate_tags(&os_version); let tags = recipe.generate_tags(&os_version);
let image_name = self.generate_full_image_name(&recipe)?; let image_name = self.generate_full_image_name(&recipe)?;
@ -249,7 +249,7 @@ impl BuildCommand {
trace!("BuildCommand::login()"); trace!("BuildCommand::login()");
info!("Attempting to login to the registry"); info!("Attempting to login to the registry");
let credentials = Strategy::get_credentials()?; let credentials = credentials::get()?;
let (registry, username, password) = ( let (registry, username, password) = (
&credentials.registry, &credentials.registry,
@ -258,7 +258,7 @@ impl BuildCommand {
); );
info!("Logging into the registry, {registry}"); info!("Logging into the registry, {registry}");
Strategy::get_build_strategy().login()?; Driver::get_build_driver().login()?;
trace!("cosign login -u {username} -p [MASKED] {registry}"); trace!("cosign login -u {username} -p [MASKED] {registry}");
let login_output = Command::new("cosign") let login_output = Command::new("cosign")
@ -355,7 +355,7 @@ impl BuildCommand {
fn run_build(&self, image_name: &str, tags: &[String]) -> Result<()> { fn run_build(&self, image_name: &str, tags: &[String]) -> Result<()> {
trace!("BuildCommand::run_build({image_name}, {tags:#?})"); trace!("BuildCommand::run_build({image_name}, {tags:#?})");
let strat = Strategy::get_build_strategy(); let strat = Driver::get_build_driver();
let full_image = if self.archive.is_some() { let full_image = if self.archive.is_some() {
image_name.to_string() image_name.to_string()

View file

@ -8,7 +8,7 @@ use clap::Args;
use log::{debug, info, trace}; use log::{debug, info, trace};
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
use crate::strategies::Strategy; use crate::drivers::Driver;
use super::BlueBuildCommand; use super::BlueBuildCommand;
@ -53,8 +53,8 @@ impl TemplateCommand {
trace!("recipe_de: {recipe_de:#?}"); trace!("recipe_de: {recipe_de:#?}");
let template = ContainerFileTemplate::builder() let template = ContainerFileTemplate::builder()
.os_version(Strategy::get_os_version(&recipe_de)?) .os_version(Driver::get_os_version(&recipe_de)?)
.build_id(Strategy::get_build_id()) .build_id(Driver::get_build_id())
.recipe(&recipe_de) .recipe(&recipe_de)
.recipe_path(recipe_path.as_path()) .recipe_path(recipe_path.as_path())
.build(); .build();

View file

@ -101,6 +101,9 @@ static ENV_CREDENTIALS: Lazy<Option<Credentials>> = Lazy::new(|| {
/// Be sure to call this before trying to use /// Be sure to call this before trying to use
/// any strategy that requires credentials as /// any strategy that requires credentials as
/// the environment credentials are lazy allocated. /// the environment credentials are lazy allocated.
///
/// # Errors
/// Will error if it can't lock the mutex.
pub fn set_user_creds( pub fn set_user_creds(
username: Option<&String>, username: Option<&String>,
password: Option<&String>, password: Option<&String>,

View file

@ -18,7 +18,6 @@ use blue_build_utils::constants::{
IMAGE_VERSION_LABEL, RUN_PODMAN_SOCK, VAR_RUN_PODMAN_PODMAN_SOCK, VAR_RUN_PODMAN_SOCK, IMAGE_VERSION_LABEL, RUN_PODMAN_SOCK, VAR_RUN_PODMAN_PODMAN_SOCK, VAR_RUN_PODMAN_SOCK,
XDG_RUNTIME_DIR, XDG_RUNTIME_DIR,
}; };
pub use credentials::Credentials;
use log::{debug, error, info, trace}; use log::{debug, error, info, trace};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
@ -31,22 +30,21 @@ use podman_api::Podman;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
#[cfg(feature = "builtin-podman")] #[cfg(feature = "builtin-podman")]
use crate::strategies::podman_api_strategy::PodmanApiStrategy; use podman_api_driver::PodmanApiDriver;
use crate::image_inspection::ImageInspection; use crate::{credentials, image_inspection::ImageInspection};
use self::{ use self::{
buildah_strategy::BuildahStrategy, docker_strategy::DockerStrategy, buildah_driver::BuildahDriver, docker_driver::DockerDriver, podman_driver::PodmanDriver,
podman_strategy::PodmanStrategy, skopeo_strategy::SkopeoStrategy, skopeo_driver::SkopeoDriver,
}; };
mod buildah_strategy; mod buildah_driver;
mod credentials; mod docker_driver;
mod docker_strategy;
#[cfg(feature = "builtin-podman")] #[cfg(feature = "builtin-podman")]
mod podman_api_strategy; mod podman_api_driver;
mod podman_strategy; mod podman_driver;
mod skopeo_strategy; mod skopeo_driver;
/// Stores the build strategy. /// Stores the build strategy.
/// ///
@ -59,8 +57,8 @@ mod skopeo_strategy;
/// ///
/// This will cause the program to exit if a build strategy could /// This will cause the program to exit if a build strategy could
/// not be determined. /// not be determined.
static BUILD_STRATEGY: Lazy<Arc<dyn BuildStrategy>> = static BUILD_STRATEGY: Lazy<Arc<dyn BuildDriver>> =
Lazy::new(|| match Strategy::determine_build_strategy() { Lazy::new(|| match Driver::determine_build_driver() {
Err(e) => { Err(e) => {
error!("{e}"); error!("{e}");
process::exit(1); process::exit(1);
@ -79,8 +77,8 @@ static BUILD_STRATEGY: Lazy<Arc<dyn BuildStrategy>> =
/// ///
/// This will cause the program to exit if a build strategy could /// This will cause the program to exit if a build strategy could
/// not be determined. /// not be determined.
static INSPECT_STRATEGY: Lazy<Arc<dyn InspectStrategy>> = static INSPECT_STRATEGY: Lazy<Arc<dyn InspectDriver>> =
Lazy::new(|| match Strategy::determine_inspect_strategy() { Lazy::new(|| match Driver::determine_inspect_driver() {
Err(e) => { Err(e) => {
error!("{e}"); error!("{e}");
process::exit(1); process::exit(1);
@ -96,7 +94,7 @@ static OS_VERSION: Lazy<Mutex<HashMap<String, String>>> = Lazy::new(|| Mutex::ne
/// Allows agnostic building, tagging /// Allows agnostic building, tagging
/// pushing, and login. /// pushing, and login.
pub trait BuildStrategy: Sync + Send { pub trait BuildDriver: Sync + Send {
/// Runs the build logic for the strategy. /// Runs the build logic for the strategy.
/// ///
/// # Errors /// # Errors
@ -123,7 +121,7 @@ pub trait BuildStrategy: Sync + Send {
} }
/// Allows agnostic inspection of images. /// Allows agnostic inspection of images.
pub trait InspectStrategy: Sync + Send { pub trait InspectDriver: Sync + Send {
/// Gets the labels on an image tag. /// Gets the labels on an image tag.
/// ///
/// # Errors /// # Errors
@ -132,7 +130,7 @@ pub trait InspectStrategy: Sync + Send {
} }
#[derive(Debug, TypedBuilder)] #[derive(Debug, TypedBuilder)]
pub struct Strategy<'a> { pub struct Driver<'a> {
#[builder(default)] #[builder(default)]
username: Option<&'a String>, username: Option<&'a String>,
@ -143,7 +141,7 @@ pub struct Strategy<'a> {
registry: Option<&'a String>, registry: Option<&'a String>,
} }
impl<'a> Strategy<'a> { impl Driver<'_> {
/// Initializes the Strategy with user provided credentials. /// Initializes the Strategy with user provided credentials.
/// ///
/// If you want to take advantage of a user's credentials, /// If you want to take advantage of a user's credentials,
@ -164,23 +162,15 @@ impl<'a> Strategy<'a> {
} }
/// Gets the current run's build strategy /// Gets the current run's build strategy
pub fn get_build_strategy() -> Arc<dyn BuildStrategy> { pub fn get_build_driver() -> Arc<dyn BuildDriver> {
BUILD_STRATEGY.clone() BUILD_STRATEGY.clone()
} }
/// Gets the current run's inspectioin strategy /// Gets the current run's inspectioin strategy
pub fn get_inspection_strategy() -> Arc<dyn InspectStrategy> { pub fn get_inspection_driver() -> Arc<dyn InspectDriver> {
INSPECT_STRATEGY.clone() INSPECT_STRATEGY.clone()
} }
/// Get the current environment credentials.
///
/// # Errors
/// Will error if credentials don't exist.
pub fn get_credentials() -> Result<&'static Credentials> {
credentials::get()
}
/// Retrieve the `os_version` for an image. /// Retrieve the `os_version` for an image.
/// ///
/// This gets cached for faster resolution if it's required /// This gets cached for faster resolution if it's required
@ -228,7 +218,7 @@ impl<'a> Strategy<'a> {
Ok(os_version) Ok(os_version)
} }
fn determine_inspect_strategy() -> Result<Arc<dyn InspectStrategy>> { fn determine_inspect_driver() -> Result<Arc<dyn InspectDriver>> {
trace!("Strategy::determine_inspect_strategy()"); trace!("Strategy::determine_inspect_strategy()");
Ok( Ok(
@ -237,15 +227,15 @@ impl<'a> Strategy<'a> {
blue_build_utils::check_command_exists("docker"), blue_build_utils::check_command_exists("docker"),
blue_build_utils::check_command_exists("podman"), blue_build_utils::check_command_exists("podman"),
) { ) {
(Ok(_skopeo), _, _) => Arc::new(SkopeoStrategy), (Ok(_skopeo), _, _) => Arc::new(SkopeoDriver),
(_, Ok(_docker), _) => Arc::new(DockerStrategy), (_, Ok(_docker), _) => Arc::new(DockerDriver),
(_, _, Ok(_podman)) => Arc::new(PodmanStrategy), (_, _, Ok(_podman)) => Arc::new(PodmanDriver),
_ => bail!("Could not determine inspection strategy. You need either skopeo, docker, or podman"), _ => bail!("Could not determine inspection strategy. You need either skopeo, docker, or podman"),
} }
) )
} }
fn determine_build_strategy() -> Result<Arc<dyn BuildStrategy>> { fn determine_build_driver() -> Result<Arc<dyn BuildDriver>> {
trace!("Strategy::determine_build_strategy()"); trace!("Strategy::determine_build_strategy()");
Ok( Ok(
@ -263,7 +253,7 @@ impl<'a> Strategy<'a> {
if PathBuf::from(format!("{xdg_runtime}/podman/podman.sock")).exists() => if PathBuf::from(format!("{xdg_runtime}/podman/podman.sock")).exists() =>
{ {
Arc::new( Arc::new(
PodmanApiStrategy::builder() PodmanApiDriver::builder()
.client( .client(
Podman::unix(PathBuf::from(format!( Podman::unix(PathBuf::from(format!(
"{xdg_runtime}/podman/podman.sock" "{xdg_runtime}/podman/podman.sock"
@ -277,7 +267,7 @@ impl<'a> Strategy<'a> {
#[cfg(feature = "builtin-podman")] #[cfg(feature = "builtin-podman")]
(_, run_podman_podman_sock, _, _, _, _, _) if run_podman_podman_sock.exists() => { (_, run_podman_podman_sock, _, _, _, _, _) if run_podman_podman_sock.exists() => {
Arc::new( Arc::new(
PodmanApiStrategy::builder() PodmanApiDriver::builder()
.client(Podman::unix(run_podman_podman_sock).into()) .client(Podman::unix(run_podman_podman_sock).into())
.rt(Runtime::new()?) .rt(Runtime::new()?)
.build(), .build(),
@ -288,7 +278,7 @@ impl<'a> Strategy<'a> {
if var_run_podman_podman_sock.exists() => if var_run_podman_podman_sock.exists() =>
{ {
Arc::new( Arc::new(
PodmanApiStrategy::builder() PodmanApiDriver::builder()
.client(Podman::unix(var_run_podman_podman_sock).into()) .client(Podman::unix(var_run_podman_podman_sock).into())
.rt(Runtime::new()?) .rt(Runtime::new()?)
.build(), .build(),
@ -297,16 +287,16 @@ impl<'a> Strategy<'a> {
#[cfg(feature = "builtin-podman")] #[cfg(feature = "builtin-podman")]
(_, _, _, var_run_podman_sock, _, _, _) if var_run_podman_sock.exists() => { (_, _, _, var_run_podman_sock, _, _, _) if var_run_podman_sock.exists() => {
Arc::new( Arc::new(
PodmanApiStrategy::builder() PodmanApiDriver::builder()
.client(Podman::unix(var_run_podman_sock).into()) .client(Podman::unix(var_run_podman_sock).into())
.rt(Runtime::new()?) .rt(Runtime::new()?)
.build(), .build(),
) )
} }
// (_, _, _, _, Ok(_docker), _, _) if !oci_required => { // (_, _, _, _, Ok(_docker), _, _) if !oci_required => {
(_, _, _, _, Ok(_docker), _, _) => Arc::new(DockerStrategy), (_, _, _, _, Ok(_docker), _, _) => Arc::new(DockerDriver),
(_, _, _, _, _, Ok(_podman), _) => Arc::new(PodmanStrategy), (_, _, _, _, _, Ok(_podman), _) => Arc::new(PodmanDriver),
(_, _, _, _, _, _, Ok(_buildah)) => Arc::new(BuildahStrategy), (_, _, _, _, _, _, Ok(_buildah)) => Arc::new(BuildahDriver),
_ => bail!( _ => bail!(
"Could not determine strategy, need either docker, podman, or buildah to continue" "Could not determine strategy, need either docker, podman, or buildah to continue"
), ),

View file

@ -3,12 +3,14 @@ use std::process::Command;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use log::{info, trace}; use log::{info, trace};
use super::{credentials, BuildStrategy}; use crate::credentials;
use super::BuildDriver;
#[derive(Debug)] #[derive(Debug)]
pub struct BuildahStrategy; pub struct BuildahDriver;
impl BuildStrategy for BuildahStrategy { impl BuildDriver for BuildahDriver {
fn build(&self, image: &str) -> Result<()> { fn build(&self, image: &str) -> Result<()> {
trace!("buildah build -t {image}"); trace!("buildah build -t {image}");
let status = Command::new("buildah") let status = Command::new("buildah")

View file

@ -9,12 +9,12 @@ use log::{info, trace};
use crate::image_inspection::ImageInspection; use crate::image_inspection::ImageInspection;
use super::{credentials, BuildStrategy, InspectStrategy}; use super::{credentials, BuildDriver, InspectDriver};
#[derive(Debug)] #[derive(Debug)]
pub struct DockerStrategy; pub struct DockerDriver;
impl BuildStrategy for DockerStrategy { impl BuildDriver for DockerDriver {
fn build(&self, image: &str) -> Result<()> { fn build(&self, image: &str) -> Result<()> {
trace!("docker"); trace!("docker");
let mut command = Command::new("docker"); let mut command = Command::new("docker");
@ -103,7 +103,7 @@ impl BuildStrategy for DockerStrategy {
} }
} }
impl InspectStrategy for DockerStrategy { impl InspectDriver for DockerDriver {
fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> { fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> {
let url = format!("docker://{image_name}:{tag}"); let url = format!("docker://{image_name}:{tag}");

View file

@ -21,17 +21,17 @@ use tokio::{
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
use crate::strategies::BUILD_ID; use crate::drivers::BUILD_ID;
use super::{credentials, BuildStrategy}; use super::{credentials, BuildDriver};
#[derive(Debug, TypedBuilder)] #[derive(Debug, TypedBuilder)]
pub struct PodmanApiStrategy { pub struct PodmanApiDriver {
client: Arc<Podman>, client: Arc<Podman>,
rt: Runtime, rt: Runtime,
} }
impl BuildStrategy for PodmanApiStrategy { impl BuildDriver for PodmanApiDriver {
fn build(&self, image: &str) -> Result<()> { fn build(&self, image: &str) -> Result<()> {
trace!("PodmanApiStrategy::build({image})"); trace!("PodmanApiStrategy::build({image})");

View file

@ -6,12 +6,12 @@ use log::{debug, info, trace};
use crate::image_inspection::ImageInspection; use crate::image_inspection::ImageInspection;
use super::{credentials, BuildStrategy, InspectStrategy}; use super::{credentials, BuildDriver, InspectDriver};
#[derive(Debug)] #[derive(Debug)]
pub struct PodmanStrategy; pub struct PodmanDriver;
impl BuildStrategy for PodmanStrategy { impl BuildDriver for PodmanDriver {
fn build(&self, image: &str) -> Result<()> { fn build(&self, image: &str) -> Result<()> {
trace!("podman build . -t {image}"); trace!("podman build . -t {image}");
let status = Command::new("podman") let status = Command::new("podman")
@ -81,7 +81,7 @@ impl BuildStrategy for PodmanStrategy {
} }
} }
impl InspectStrategy for PodmanStrategy { impl InspectDriver for PodmanDriver {
fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> { fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> {
let url = format!("docker://{image_name}:{tag}"); let url = format!("docker://{image_name}:{tag}");

View file

@ -5,12 +5,12 @@ use log::{debug, trace};
use crate::image_inspection::ImageInspection; use crate::image_inspection::ImageInspection;
use super::InspectStrategy; use super::InspectDriver;
#[derive(Debug)] #[derive(Debug)]
pub struct SkopeoStrategy; pub struct SkopeoDriver;
impl InspectStrategy for SkopeoStrategy { impl InspectDriver for SkopeoDriver {
fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> { fn get_labels(&self, image_name: &str, tag: &str) -> Result<ImageInspection> {
let url = format!("docker://{image_name}:{tag}"); let url = format!("docker://{image_name}:{tag}");

View file

@ -5,5 +5,6 @@
shadow_rs::shadow!(shadow); shadow_rs::shadow!(shadow);
pub mod commands; pub mod commands;
pub mod credentials;
pub mod drivers;
pub mod image_inspection; pub mod image_inspection;
pub mod strategies;