feat: add tracing

This commit is contained in:
Alexander Navarro 2025-06-26 12:42:01 -04:00
parent f613efc882
commit 1f194ca7f1
6 changed files with 126 additions and 29 deletions

View file

@ -1,7 +1,10 @@
use crate::docker::Container;
use crate::Error;
use bollard::models::ContainerSummary;
use bollard::Docker;
use futures::future::try_join_all;
use std::collections::HashMap;
use tracing::info;
/// Namespace to manage containers together (like compose projects)
type ServiceGroup = String;
@ -43,6 +46,7 @@ impl From<&Vec<ContainerSummary>> for Services {
}
}
#[tracing::instrument(skip(containers))]
pub async fn manage(containers: &Vec<ContainerSummary>) -> crate::Result<()> {
let services = Services::from(containers);
@ -52,35 +56,35 @@ pub async fn manage(containers: &Vec<ContainerSummary>) -> crate::Result<()> {
// TODO: iterate over groups in parallel
for (group, containers) in services.0.iter() {
// stop containers of service group
let stop_tasks = containers.into_iter().map(async |container| {
eprintln!("Stoping container: {:#?}", container.id);
let stop_opts = bollard::query_parameters::StopContainerOptionsBuilder::new().build();
docker.stop_container(&container.id, Some(stop_opts)).await?;
Ok::<(), crate::Error>(())
});
futures::future::try_join_all(stop_tasks).await?;
try_join_all(containers.into_iter().map(|container | stop_container(&docker, container))).await?;
// create container with the same mounts as each container in the group
// run the new container
// restart the containers
let start_tasks = containers.into_iter().map(async |container| {
eprintln!("Starting container: {:#?}", container.id);
let start_opts = bollard::query_parameters::StartContainerOptionsBuilder::new().build();
docker.start_container(&container.id, Some(start_opts)).await?;
Ok::<(), crate::Error>(())
});
futures::future::try_join_all(start_tasks).await?;
try_join_all(containers.into_iter().map(|container| start_container(&docker, container))).await?;
}
Ok(())
}
async fn stop_container(docker: &Docker, container: &crate::docker::Container) -> crate::Result<()> {
info!("Stoping container: {:#?}", container.id);
let stop_opts = bollard::query_parameters::StopContainerOptionsBuilder::new().build();
docker.stop_container(&container.id, Some(stop_opts)).await?;
Ok::<(), crate::Error>(())
}
async fn start_container(docker: &Docker, container: &crate::docker::Container) -> crate::Result<()> {
info!("Starting container: {:#?}", container.id);
let start_opts = bollard::query_parameters::StartContainerOptionsBuilder::new().build();
docker.start_container(&container.id, Some(start_opts)).await?;
Ok::<(), crate::Error>(())
}