From 10548839a184f683208353940fb80142751f0a38 Mon Sep 17 00:00:00 2001 From: Erdem Meydanli Date: Tue, 19 Mar 2024 08:44:21 +0000 Subject: [PATCH] enclave_build: start replacing shiplift with bollard Introduce the image_exists private method utilizing Bollard to check whether a docker image exists locally. Signed-off-by: Erdem Meydanli --- enclave_build/src/docker.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/enclave_build/src/docker.rs b/enclave_build/src/docker.rs index 31436ca6a..6b760bd7a 100644 --- a/enclave_build/src/docker.rs +++ b/enclave_build/src/docker.rs @@ -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}; @@ -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, } @@ -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, } } @@ -155,23 +159,36 @@ impl DockerUtil { } } + async fn image_exists(&self) -> Result { +/* + 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);