feat(prune)!: Create prune command

This commit is contained in:
Gerald Pinder 2024-11-27 14:00:49 -05:00
parent 879dca348c
commit 1671ea2143
13 changed files with 356 additions and 70 deletions

View file

@ -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()");
@ -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() {
@ -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() {
@ -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(())
}
}

View file

@ -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:#?})");
@ -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()
{
@ -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);

View file

@ -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)]

View file

@ -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() {
@ -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() {
@ -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 {
@ -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);

View file

@ -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