Skip to content

Commit

Permalink
Split off an internal "bootc-utils" crate
Browse files Browse the repository at this point in the history
In general the codebase is starting to get to the size
where some internal crates make sense. Let's start
with the inevitable catchall "utils" crate which
starts off just holding our helper traits for
subprocesses.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Aug 14, 2024
1 parent 2aa8c6a commit d7c3f9f
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 12 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include = ["/src", "LICENSE-APACHE", "LICENSE-MIT"]
anstream = "0.6.13"
anstyle = "1.0.6"
anyhow = { workspace = true }
bootc-utils = { path = "../utils" }
camino = { workspace = true, features = ["serde1"] }
ostree-ext = { version = "0.14.0" }
chrono = { workspace = true, features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use fn_error_context::context;
use regex::Regex;
use serde::Deserialize;

use crate::cmdutils::CommandRunExt;
use crate::install::run_in_host_mountns;
use crate::task::Task;
use bootc_utils::CommandRunExt;

#[derive(Debug, Deserialize)]
struct DevicesOutput {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
//! APIs for operating on container images in the bootc storage.

use anyhow::{Context, Result};
use bootc_utils::CommandRunExt;
use fn_error_context::context;
use ostree_ext::container::{ImageReference, Transport};

use crate::{cmdutils::CommandRunExt, imgstorage::Storage};
use crate::imgstorage::Storage;

/// The name of the image we push to containers-storage if nothing is specified.
const IMAGE_DEFAULT: &str = "localhost/bootc";
Expand Down
3 changes: 1 addition & 2 deletions lib/src/imgstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::process::{Command, Stdio};
use std::sync::Arc;

use anyhow::{Context, Result};
use bootc_utils::{AsyncCommandRunExt, CommandRunExt, ExitStatusExt};
use camino::Utf8Path;
use cap_std_ext::cap_std;
use cap_std_ext::cap_std::fs::Dir;
Expand All @@ -22,8 +23,6 @@ use fn_error_context::context;
use std::os::fd::OwnedFd;
use tokio::process::Command as AsyncCommand;

use crate::cmdutils::{AsyncCommandRunExt, CommandRunExt, ExitStatusExt};

// Pass only 100 args at a time just to avoid potentially overflowing argument
// vectors; not that this should happen in reality, but just in case.
const SUBCMD_ARGV_CHUNKING: usize = 100;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::time::Duration;

use anyhow::Ok;
use anyhow::{anyhow, Context, Result};
use bootc_utils::CommandRunExt;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use cap_std::fs::{Dir, MetadataExt};
Expand All @@ -43,7 +44,6 @@ use rustix::fs::{FileTypeExt, MetadataExt as _};
use serde::{Deserialize, Serialize};

use self::baseline::InstallBlockDeviceOpts;
use crate::cmdutils::CommandRunExt;
use crate::containerenv::ContainerExecutionInfo;
use crate::mount::Filesystem;
use crate::spec::ImageReference;
Expand Down
1 change: 0 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

mod boundimage;
pub mod cli;
mod cmdutils;
pub(crate) mod deploy;
pub(crate) mod generator;
mod image;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use std::process::Command;

use anyhow::{anyhow, Result};
use bootc_utils::CommandRunExt;
use camino::Utf8Path;
use fn_error_context::context;
use serde::Deserialize;

use crate::{cmdutils::CommandRunExt, task::Task};
use crate::task::Task;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
Expand Down
2 changes: 1 addition & 1 deletion lib/src/podman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct ImageListEntry {
/// Given an image ID, return its manifest digest
#[cfg(feature = "install")]
pub(crate) fn imageid_to_digest(imgid: &str) -> Result<String> {
use crate::cmdutils::CommandRunExt;
use bootc_utils::CommandRunExt;
let o: Vec<Inspect> = crate::install::run_in_host_mountns("podman")
.args(["inspect", imgid])
.run_and_parse_json()?;
Expand Down
22 changes: 22 additions & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "bootc-utils"
publish = false
version = "0.0.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/containers/bootc"

[dependencies]
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tempfile = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true, features = ["process"] }

[dev-dependencies]
similar-asserts = { version = "1.5.0" }
static_assertions = "1.1.0"

[lints]
workspace = true
8 changes: 4 additions & 4 deletions lib/src/cmdutils.rs → utils/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::{
use anyhow::{Context, Result};

/// Helpers intended for [`std::process::Command`].
pub(crate) trait CommandRunExt {
pub trait CommandRunExt {
fn run(&mut self) -> Result<()>;
/// Execute the child process, parsing its stdout as JSON.
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;
}

/// Helpers intended for [`std::process::ExitStatus`].
pub(crate) trait ExitStatusExt {
pub trait ExitStatusExt {
/// If the exit status signals it was not successful, return an error.
/// Note that we intentionally *don't* include the command string
/// in the output; we leave it to the caller to add that if they want,
Expand Down Expand Up @@ -82,8 +82,8 @@ impl CommandRunExt for Command {
}

/// Helpers intended for [`tokio::process::Command`].
#[allow(dead_code)]
pub(crate) trait AsyncCommandRunExt {
#[allow(async_fn_in_trait)]
pub trait AsyncCommandRunExt {
async fn run(&mut self) -> Result<()>;
}

Expand Down
6 changes: 6 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! The inevitable catchall "utils" crate. Generally only add
//! things here that only depend on the standard library and
//! "core" crates.
//!
mod command;
pub use command::*;

0 comments on commit d7c3f9f

Please sign in to comment.