feat: stop and start containers

This commit is contained in:
Alexander Navarro 2025-06-25 16:54:31 -04:00
parent bb1784f259
commit 8e89aeba75
4 changed files with 87 additions and 1 deletions

View file

@ -1,3 +1,5 @@
use crate::Error;
use bollard::Docker;
use bollard::models::ContainerSummary;
use std::collections::HashMap;
@ -53,7 +55,51 @@ impl From<&Vec<ContainerSummary>> for Services {
pub async fn manage(containers: &Vec<ContainerSummary>) -> crate::Result<()> {
let services = Services::from(containers);
println!("{:#?}", services);
// TODO: reuse main instance
let docker = Docker::connect_with_local_defaults()?;
// 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| {
let id = container
.id
.as_ref()
.ok_or(Error::Static("Error getting containers id"))?;
eprintln!("Stoping container: {:#?}", id);
let stop_opts = bollard::query_parameters::StopContainerOptionsBuilder::new().build();
docker.stop_container(id, Some(stop_opts)).await?;
Ok::<(), crate::Error>(())
});
futures::future::try_join_all(stop_tasks).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| {
let id = container
.id
.as_ref()
.ok_or(Error::Static("Error getting containers id"))?;
eprintln!("Starting container: {:#?}", id);
let start_opts = bollard::query_parameters::StartContainerOptionsBuilder::new().build();
docker.start_container(id, Some(start_opts)).await?;
Ok::<(), crate::Error>(())
});
futures::future::try_join_all(start_tasks).await?;
}
Ok(())
}