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

1
.idea/epoch.iml generated
View file

@ -2,6 +2,7 @@
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>

38
Cargo.lock generated
View file

@ -182,6 +182,7 @@ name = "epoch"
version = "0.1.0"
dependencies = [
"bollard",
"futures",
"thiserror",
"tokio",
]
@ -207,6 +208,21 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
@ -214,6 +230,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -222,6 +239,23 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
@ -251,9 +285,13 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",

View file

@ -5,5 +5,6 @@ edition = "2024"
[dependencies]
bollard = "0.19.1"
futures = "0.3.31"
thiserror = "2.0.12"
tokio = { version = "1.45.1", features = ["macros", "rt", "rt-multi-thread"] }

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(())
}