use anyhow::Result;
use podbox::podman::{ContainerState, query_state};
use podbox::systemd;
pub mod clone;
pub mod context;
pub mod create;
pub mod create_init;
pub mod definition;
pub mod diff;
pub mod export;
pub mod history;
pub mod inspect;
pub mod lifecycle;
pub mod list;
pub mod migrate;
pub mod pull;
pub mod recover;
pub mod runtime;
pub mod serve;
pub mod stats;
pub mod translate;
pub const DEFAULT_START_TIMEOUT_SECS: u64 = 30;
pub fn ensure_running(name: &str, dry_run: bool, timeout_secs: u64) -> Result<()> {
match query_state(name)? {
ContainerState::Running => Ok(()),
ContainerState::Stopped | ContainerState::Missing => {
if dry_run {
println!("podman start {name}");
return Ok(());
}
if systemd::is_available() {
systemd::start_unit_friendly(name, timeout_secs)
} else {
let args = podbox::process::args(&["start", name]);
podbox::process::spawn_interactive("podman", &args)?;
wait_for_running(name, timeout_secs)
}
}
}
}
fn wait_for_running(name: &str, timeout_secs: u64) -> Result<()> {
use std::time::{Duration, Instant};
let deadline = Instant::now() + Duration::from_secs(timeout_secs);
loop {
match query_state(name)? {
ContainerState::Running => return Ok(()),
_ if Instant::now() >= deadline => {
let state = query_state(name)?;
anyhow::bail!(
"Container '{name}' did not become ready within {timeout_secs}s \
(final state: {state:?})",
);
}
_ => {
std::thread::sleep(Duration::from_millis(300));
}
}
}
}