Skip to content

Commit

Permalink
feat(prune)!: Create prune command
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Nov 27, 2024
1 parent 879dca3 commit 1671ea2
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 76 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ validate = [
"cached/async",
"blue-build-process-management/validate"
]
prune = [
"blue-build-process-management/prune"
]

[dev-dependencies]
rusty-hook = "0.11"
Expand Down
1 change: 1 addition & 0 deletions process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ workspace = true
[features]
sigstore = ["dep:tokio", "dep:sigstore"]
validate = ["dep:tokio"]
prune = []
5 changes: 5 additions & 0 deletions process/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ impl BuildDriver for Driver {
impl_build_driver!(login())
}

#[cfg(feature = "prune")]
fn prune(opts: &opts::PruneOpts) -> Result<()> {
impl_build_driver!(prune(opts))
}

fn build_tag_push(opts: &BuildTagPushOpts) -> Result<Vec<String>> {
impl_build_driver!(build_tag_push(opts))
}
Expand Down
29 changes: 27 additions & 2 deletions process/drivers/buildah_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ pub struct BuildahDriver;
impl DriverVersion for BuildahDriver {
// RUN mounts for bind, cache, and tmpfs first supported in 1.24.0
// https://buildah.io/releases/#changes-for-v1240
#[cfg(not(feature = "prune"))]
const VERSION_REQ: &'static str = ">=1.24";

// The prune command wasn't present until 1.29
#[cfg(feature = "prune")]
const VERSION_REQ: &'static str = ">=1.29";

fn version() -> Result<Version> {
trace!("BuildahDriver::version()");

Expand Down Expand Up @@ -64,7 +69,7 @@ impl BuildDriver for BuildahDriver {

trace!("{command:?}");
let status = command
.status_image_ref_progress(&opts.image, "Building Image")
.build_status(&opts.image, "Building Image")
.into_diagnostic()?;

if status.success() {
Expand Down Expand Up @@ -104,7 +109,7 @@ impl BuildDriver for BuildahDriver {

trace!("{command:?}");
let status = command
.status_image_ref_progress(&opts.image, "Pushing Image")
.build_status(&opts.image, "Pushing Image")
.into_diagnostic()?;

if status.success() {
Expand Down Expand Up @@ -159,4 +164,24 @@ impl BuildDriver for BuildahDriver {
}
Ok(())
}

#[cfg(feature = "prune")]
fn prune(opts: &super::opts::PruneOpts) -> Result<()> {
trace!("PodmanDriver::prune({opts:?})");

let status = cmd!(
"buildah",
"prune",
"--force",
if opts.all => "-all",
)
.message_status("buildah prune", "Pruning Buildah System")
.into_diagnostic()?;

if !status.success() {
bail!("Failed to prune buildah");
}

Ok(())
}
}
58 changes: 55 additions & 3 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,59 @@ impl BuildDriver for DockerDriver {
Ok(())
}

#[cfg(feature = "prune")]
fn prune(opts: &super::opts::PruneOpts) -> Result<()> {
trace!("DockerDriver::prune({opts:?})");

let (system, buildx) = std::thread::scope(
|scope| -> std::thread::Result<(Result<ExitStatus>, Result<ExitStatus>)> {
let system = scope.spawn(|| {
cmd!(
"docker",
"system",
"prune",
"--force",
if opts.all => "--all",
if opts.volumes => "--volumes",
)
.message_status("docker system prune", "Pruning Docker System")
.into_diagnostic()
});

let buildx = scope.spawn(|| {
cmd!(
"docker",
"buildx",
"prune",
"--force",
|command|? {
if !env::var(DOCKER_HOST).is_ok_and(|dh| !dh.is_empty()) {
Self::setup()?;
cmd!(command, "--builder=bluebuild");
}
},
if opts.all => "--all",
)
.message_status("docker buildx prune", "Pruning Docker Buildx")
.into_diagnostic()
});

Ok((system.join()?, buildx.join()?))
},
)
.map_err(|e| miette!("{e:?}"))?;

if !system?.success() {
bail!("Failed to prune docker system");
}

if !buildx?.success() {
bail!("Failed to prune docker buildx");
}

Ok(())
}

fn build_tag_push(opts: &BuildTagPushOpts) -> Result<Vec<String>> {
trace!("DockerDriver::build_tag_push({opts:#?})");

Expand Down Expand Up @@ -305,7 +358,7 @@ impl BuildDriver for DockerDriver {

trace!("{command:?}");
if command
.status_image_ref_progress(display_image, "Building Image")
.build_status(display_image, "Building Image")
.into_diagnostic()?
.success()
{
Expand Down Expand Up @@ -383,8 +436,7 @@ impl RunDriver for DockerDriver {

add_cid(&cid);

let status = docker_run(opts, &cid_file)
.status_image_ref_progress(&*opts.image, "Running container")?;
let status = docker_run(opts, &cid_file).build_status(&*opts.image, "Running container")?;

remove_cid(&cid);

Expand Down
7 changes: 7 additions & 0 deletions process/drivers/opts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ pub struct PushOpts<'scope> {
pub compression_type: Option<CompressionType>,
}

#[derive(Debug, Clone, Builder)]
#[cfg(feature = "prune")]
pub struct PruneOpts {
pub all: bool,
pub volumes: bool,
}

/// Options for building, tagging, and pusing images.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Builder)]
Expand Down
29 changes: 25 additions & 4 deletions process/drivers/podman_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl BuildDriver for PodmanDriver {

trace!("{command:?}");
let status = command
.status_image_ref_progress(&opts.image, "Building Image")
.build_status(&opts.image, "Building Image")
.into_diagnostic()?;

if status.success() {
Expand Down Expand Up @@ -188,7 +188,7 @@ impl BuildDriver for PodmanDriver {

trace!("{command:?}");
let status = command
.status_image_ref_progress(&opts.image, "Pushing Image")
.build_status(&opts.image, "Pushing Image")
.into_diagnostic()?;

if status.success() {
Expand Down Expand Up @@ -243,6 +243,28 @@ impl BuildDriver for PodmanDriver {
}
Ok(())
}

#[cfg(feature = "prune")]
fn prune(opts: &super::opts::PruneOpts) -> Result<()> {
trace!("PodmanDriver::prune({opts:?})");

let status = cmd!(
"podman",
"system",
"prune",
"--force",
if opts.all => "-all",
if opts.volumes => "--volumes",
)
.message_status("podman system prune", "Pruning Podman System")
.into_diagnostic()?;

if !status.success() {
bail!("Failed to prune podman");
}

Ok(())
}
}

impl InspectDriver for PodmanDriver {
Expand Down Expand Up @@ -326,8 +348,7 @@ impl RunDriver for PodmanDriver {
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")?
podman_run(opts, &cid_file).build_status(&*opts.image, "Running container")?
};

remove_cid(&cid);
Expand Down
7 changes: 7 additions & 0 deletions process/drivers/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ pub trait BuildDriver: PrivateDriver {
/// Will error if login fails.
fn login() -> Result<()>;

/// Runs prune commands for the driver.
///
/// # Errors
/// Will error if the driver fails to prune.
#[cfg(feature = "prune")]
fn prune(opts: &super::opts::PruneOpts) -> Result<()>;

/// Runs the logic for building, tagging, and pushing an image.
///
/// # Errors
Expand Down
Loading

0 comments on commit 1671ea2

Please sign in to comment.