Skip to content

Commit

Permalink
feat(jstzd): implement pull image
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 12, 2024
1 parent 605ae93 commit 1982bf9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ url = "2.4.1"
urlpattern = "0.2.0"
wasm-bindgen = "0.2.92"
bollard = "0.16.1"
# async-dropper = { version = "0.3.1", features = ["tokio", "simple"] }
# async-trait = "0.1.82"
# async-scoped = "0.9.0"
async-trait = "0.1.82"

[workspace.dependencies.tezos-smart-rollup]
version = "0.2.2"
Expand Down
5 changes: 5 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ repository.workspace = true

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
bollard.workspace = true
env_logger.workspace = true
futures-util.workspace = true
tokio.workspace = true
log.workspace = true
51 changes: 51 additions & 0 deletions crates/jstzd/src/docker/image.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +24,48 @@ pub trait Image: Sized {
fn exposed_ports(&self) -> &[u16] {
&[]
}
async fn pull_image(&self, client: Arc<Docker>) -> Result<()> {
if Self::image_exists(self, client.clone()).await {
info!("Image: {:?} already exists ", self.image_name());
return Ok(());
}
self.create_image(client.clone()).await
}
async fn image_exists(&self, client: Arc<Docker>) -> bool {
let filters = [("reference".to_string(), vec![self.image_uri()])]
.into_iter()
.collect::<HashMap<_, _>>();
let images = &client
.list_images(Some(ListImagesOptions::<String> {
all: true,
filters,
..Default::default()
}))
.await;
match images {
Ok(images) => !images.is_empty(),
Err(_) => false,
}
}
async fn create_image(&self, client: Arc<Docker>) -> 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 {
Expand Down
22 changes: 22 additions & 0 deletions crates/jstzd/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bollard::Docker;
use env_logger::Env;
use jstzd::docker::{GenericImage, Image};
use std::sync::Arc;

pub async fn example() -> anyhow::Result<()> {
let docker = Docker::connect_with_socket_defaults().unwrap();
let docker = Arc::new(docker);
let image = GenericImage::new("busybox").with_tag("latest");
image.pull_image(docker.clone()).await?;

Ok(())
}

#[tokio::main]
async fn main() {
env_logger::init_from_env(Env::default().default_filter_or("info"));
match example().await {
Ok(_) => log::info!("Success"),
Err(e) => log::error!("Error: {}", e),
}
}

0 comments on commit 1982bf9

Please sign in to comment.