Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/commands/start/curio/db_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::docker::command_logger::run_and_log_command;
use crate::docker::containers::{
lotus_container_name, postgres_container_name, scylla_container_name,
};
use crate::docker::core::docker_command;
use crate::docker::core::{docker_command, get_container_logs, wait_container_exit_code};
use crate::docker::network::{lotus_network_name, pdp_miner_network_name};
use crate::paths::foc_devnet_bin;
use crate::paths::foc_devnet_docker_volumes;
Expand Down Expand Up @@ -263,15 +263,18 @@ fn create_base_cluster(
let _ = docker_command(&["network", "connect", &lotus_network, &container_name]);

// Wait for the command to complete
let output = docker_command(&["wait", &container_name])?;
let exit_code = wait_container_exit_code(&container_name)?;

// Collect logs before removing the container
let logs = get_container_logs(&container_name).unwrap_or_default();

// Clean up container
let _ = docker_command(&["rm", "-f", &container_name]);

if !output.status.success() {
if exit_code != 0 {
return Err(format!(
"Failed to create base cluster for miner {}: command failed",
miner_id
"Failed to create base cluster for miner {}: container exited with code {}\n{}",
miner_id, exit_code, logs
)
.into());
}
Expand Down Expand Up @@ -376,13 +379,20 @@ fn create_pdp_layer(context: &SetupContext, sp_index: usize) -> Result<(), Box<d
let _ = docker_command(&["network", "connect", &lotus_network, &container_name]);

// Wait for the command to complete
let output = docker_command(&["wait", &container_name])?;
let exit_code = wait_container_exit_code(&container_name)?;

// Collect logs before removing the container
let logs = get_container_logs(&container_name).unwrap_or_default();

// Clean up container
let _ = docker_command(&["rm", "-f", &container_name]);

if !output.status.success() {
return Err("Failed to create PDP layer configuration: command failed".into());
if exit_code != 0 {
return Err(format!(
"Failed to create PDP layer configuration: container exited with code {}\n{}",
exit_code, logs
)
.into());
}

info!("PDP layer configuration created");
Expand Down
45 changes: 45 additions & 0 deletions src/docker/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ pub fn docker_command(args: &[&str]) -> Result<Output, Box<dyn Error>> {
run_command("docker", args)
}

/// Wait for a container to exit and return its exit code.
///
/// `docker wait` itself exits 0 and prints the container's exit code on
/// stdout, so checking the command's [`std::process::ExitStatus`] would
/// always report success regardless of how the container's command ended.
pub fn wait_container_exit_code(container_name: &str) -> Result<i32, Box<dyn Error>> {
let output = docker_command(&["wait", container_name])?;
parse_container_exit_code(&String::from_utf8_lossy(&output.stdout)).map_err(|e| {
format!(
"Failed to read exit code of container {}: {}",
container_name, e
)
.into()
})
}

/// Parse the numeric exit code printed by `docker wait`.
fn parse_container_exit_code(stdout: &str) -> Result<i32, String> {
let trimmed = stdout.trim();
trimmed.parse::<i32>().map_err(|_| {
format!(
"unexpected `docker wait` output (expected an exit code): {:?}",
trimmed
)
})
}

/// Get logs from a Docker container.
///
/// Collects both stdout and stderr. Go programs (e.g. Curio)
Expand Down Expand Up @@ -243,3 +270,21 @@ pub fn chown_command(args: &[&str]) -> Result<Output, Box<dyn Error>> {
command.args(args);
command.output().map_err(|e| e.into())
}

#[cfg(test)]
mod tests {
use super::parse_container_exit_code;

#[test]
fn parses_docker_wait_exit_codes() {
assert_eq!(parse_container_exit_code("0\n").unwrap(), 0);
assert_eq!(parse_container_exit_code("2\n").unwrap(), 2);
assert_eq!(parse_container_exit_code("137").unwrap(), 137);
}

#[test]
fn rejects_non_numeric_docker_wait_output() {
assert!(parse_container_exit_code("").is_err());
assert!(parse_container_exit_code("error: no such container\n").is_err());
}
}