Skip to content

Commit aab4dda

Browse files
committed
Add Engine::{pause,resume,delete} methods
1 parent 4024d32 commit aab4dda

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/main.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,51 @@ impl Engine {
3232
Ok(Engine { containers })
3333
}
3434

35-
pub async fn fetch(&mut self, container_name: &str) -> anyhow::Result<()> {
35+
pub async fn create(&self, container_name: &str) -> anyhow::Result<()> {
3636
eprintln!("fetching from dockerhub...");
3737
let fetched_image = OciImage::fetch_from_docker_hub(container_name).await?;
38+
3839
eprintln!("fetched from dockerhub, unpacking...");
3940
let runtime_dir = fetched_image.unpack().await?;
41+
4042
eprintln!("unpacked, creating container...");
4143
let container = Container::create(container_name, runtime_dir).await?;
44+
45+
eprintln!("starting container...");
46+
container.start().await?;
47+
eprintln!("started container");
48+
4249
self.containers.insert(container_name.into(), container);
4350
Ok(())
4451
}
52+
53+
pub async fn pause(&self, container_name: &str) -> anyhow::Result<()> {
54+
match self.containers.get(container_name) {
55+
Some(container) => container.pause().await,
56+
None => return Err(anyhow!("container `{}` does not exist", container_name)),
57+
}
58+
}
59+
60+
pub async fn resume(&self, container_name: &str) -> anyhow::Result<()> {
61+
match self.containers.get(container_name) {
62+
Some(container) => container.resume().await,
63+
None => return Err(anyhow!("container `{}` does not exist", container_name)),
64+
}
65+
}
66+
67+
pub async fn delete(&self, container_name: &str) -> anyhow::Result<()> {
68+
match self.containers.remove(container_name) {
69+
Some((_, container)) => container.delete().await,
70+
None => return Err(anyhow!("container `{}` does not exist", container_name)),
71+
}
72+
}
4573
}
4674

4775
#[tokio::main]
4876
async fn main() -> anyhow::Result<()> {
4977
// TODO: Use `warp` to host REST endpoints.
50-
let mut engine = Engine::new().await?;
51-
engine.fetch("busybox").await?;
78+
let engine = Engine::new().await?;
79+
engine.create("busybox").await?;
5280
tokio::time::sleep(std::time::Duration::from_secs(1000)).await;
5381
Ok(())
5482
}

0 commit comments

Comments
 (0)