Skip to content

Commit

Permalink
enclave_build: start replacing shiplift with bollard
Browse files Browse the repository at this point in the history
Introduce the image_exists private method utilizing Bollard to check whether
a docker image exists locally.

Signed-off-by: Erdem Meydanli <[email protected]>
  • Loading branch information
meerd committed Mar 19, 2024
1 parent a5235ad commit 1054883
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions enclave_build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::docker::DockerError::CredentialsError;
use base64::{engine::general_purpose, Engine as _};
use bollard::errors::Error;
use futures::stream::StreamExt;
use log::{debug, error, info};
use serde_json::{json, Value};
Expand Down Expand Up @@ -33,6 +34,7 @@ pub enum DockerError {
/// Struct exposing the Docker functionalities to the EIF builder
pub struct DockerUtil {
docker: Docker,
docker2: bollard::Docker,
docker_image: String,
}

Expand All @@ -50,6 +52,8 @@ impl DockerUtil {
// if docker daemon address needs to be substituted.
// By default it tries to connect to 'unix:///var/run/docker.sock'
docker: Docker::new(),
docker2: bollard::Docker::connect_with_socket_defaults()
.expect("Failed to connect to Docker daemon"),
docker_image,
}
}
Expand Down Expand Up @@ -155,23 +159,36 @@ impl DockerUtil {
}
}

async fn image_exists(&self) -> Result<bool, Error> {
/*
self.docker
.images()
.get(&self.docker_image)
.inspect()
.await
.is_ok()
*/
let result = self.docker2.inspect_image(&self.docker_image).await?;

let tag_matches = result
.repo_tags
.as_ref()
.unwrap()
.iter()
.any(|repo_tag| repo_tag == &self.docker_image);

Ok(tag_matches)
}

/// Pull the image, with the tag provided in constructor, from the Docker registry
pub fn pull_image(&self) -> Result<(), DockerError> {
let act = async {
// Check if the Docker image is locally available.
// If available, early exit.
if self
.docker
.images()
.get(&self.docker_image)
.inspect()
.await
.is_ok()
{
if self.image_exists().await.is_ok() {
eprintln!("Using the locally available Docker image...");
return Ok(());
}

let mut pull_options_builder = PullOptions::builder();
pull_options_builder.image(&self.docker_image);

Expand Down

0 comments on commit 1054883

Please sign in to comment.