diff --git a/Cargo.lock b/Cargo.lock index b90dd1d09..021dae973 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2667,6 +2667,7 @@ dependencies = [ "async-trait", "bollard", "futures-util", + "log", "tokio", ] diff --git a/crates/jstzd/Cargo.toml b/crates/jstzd/Cargo.toml index b081273b1..4270d7d91 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -12,7 +12,9 @@ async-trait.workspace = true bollard.workspace = true futures-util.workspace = true tokio.workspace = true +log.workspace = true [[bin]] name = "jstzd" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" + diff --git a/crates/jstzd/src/docker/image.rs b/crates/jstzd/src/docker/image.rs index b55179829..0ebd0b6c6 100644 --- a/crates/jstzd/src/docker/image.rs +++ b/crates/jstzd/src/docker/image.rs @@ -1,3 +1,12 @@ +use anyhow::Result; +use bollard::{ + image::{CreateImageOptions, ListImagesOptions}, + Docker, +}; +use futures_util::StreamExt; +use log::info; +use std::{collections::HashMap, sync::Arc}; +#[async_trait::async_trait] pub trait Image: Sized { const LATEST_TAG: &'static str = "latest"; fn image_name(&self) -> &str; @@ -15,6 +24,48 @@ pub trait Image: Sized { fn exposed_ports(&self) -> &[u16] { &[] } + async fn pull_image(&self, client: Arc) -> Result<()> { + if Self::image_exists(self, client.clone()).await { + info!("Image: {:?} already exists ", self.image_uri()); + return Ok(()); + } + self.create_image(client.clone()).await + } + async fn image_exists(&self, client: Arc) -> bool { + let filters = [("reference".to_string(), vec![self.image_uri()])] + .into_iter() + .collect::>(); + let images = &client + .list_images(Some(ListImagesOptions:: { + all: true, + filters, + ..Default::default() + })) + .await; + match images { + Ok(images) => !images.is_empty(), + Err(_) => false, + } + } + async fn create_image(&self, client: Arc) -> anyhow::Result<()> { + let options = Some(CreateImageOptions { + from_image: self.image_name(), + tag: self.image_tag(), + ..Default::default() + }); + let mut stream = client.create_image(options, None, None); + while let Some(create_info) = stream.next().await { + match create_info { + Ok(info) => { + if let Some(status) = info.status { + println!("{:?}", status) + } + } + Err(e) => return Err(e.into()), + } + } + Ok(()) + } } pub struct GenericImage { diff --git a/crates/jstzd/tests/pull_image.rs b/crates/jstzd/tests/pull_image.rs new file mode 100644 index 000000000..e7599aecf --- /dev/null +++ b/crates/jstzd/tests/pull_image.rs @@ -0,0 +1,41 @@ +use std::{collections::HashMap, sync::Arc}; + +use anyhow::Result; +use bollard::{image::ListImagesOptions, secret::ImageSummary, Docker}; +use jstzd::docker::{GenericImage, Image}; + +// search image locally +async fn search_local_image( + image_uri: String, + client: Arc, +) -> Result> { + let filters = [("reference".to_string(), vec![image_uri])] + .into_iter() + .collect::>(); + let images = &client + .list_images(Some(ListImagesOptions:: { + all: true, + filters, + ..Default::default() + })) + .await; + match images { + Ok(images) => Ok(images.clone()), + Err(_) => Err(anyhow::anyhow!("Image not found")), + } +} + +#[tokio::test] +async fn test_pull_image() -> Result<()> { + let docker = Docker::connect_with_socket_defaults().unwrap(); + let docker = Arc::new(docker); + let image = GenericImage::new("busybox").with_tag("stable"); + let _ = docker.remove_image(&image.image_uri(), None, None).await; + image.pull_image(docker.clone()).await?; + let expected_image_digest = + "sha256:7db2ddde018a2a56e929855445bc7f30bc83db514a23404bd465a07d2770ac5f"; + let images = search_local_image(image.image_uri(), docker.clone()).await?; + assert!(images.iter().any(|image| image.id == expected_image_digest)); + let _ = docker.remove_image(&image.image_uri(), None, None).await; + Ok(()) +}