Skip to content

Commit

Permalink
enclave_build: Extract stream output handling
Browse files Browse the repository at this point in the history
Extract stream output handling code into handle_stream_output function
and move to utils.rs

Signed-off-by: Erdem Meydanli <[email protected]>
  • Loading branch information
meerd committed Mar 29, 2024
1 parent d750455 commit 66ff6fd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 47 deletions.
56 changes: 9 additions & 47 deletions enclave_build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use crate::docker::DockerError::CredentialsError;
use crate::utils::handle_stream_output;
use base64::{engine::general_purpose, Engine as _};
use bollard::auth::DockerCredentials;
use bollard::image::{BuildImageOptions, CreateImageOptions};
use bollard::secret::ImageInspect;
use bollard::Docker;
use flate2::{write::GzEncoder, Compression};
use futures::stream::StreamExt;
use log::{debug, error, info};
use log::{debug, error};
use serde_json::{json, Value};
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -196,30 +196,11 @@ impl DockerUtil {
}
};

let mut stream =
self.docker
.create_image(Some(create_image_options), None, credentials);

loop {
if let Some(item) = stream.next().await {
match item {
Ok(output) => {
if let Some(err_msg) = &output.error {
error!("{:?}", err_msg);
break Err(DockerError::PullError);
} else {
info!("{:?}", output);
}
}
Err(e) => {
error!("{:?}", e);
break Err(DockerError::PullError);
}
}
} else {
break Ok(());
}
}
let stream = self
.docker
.create_image(Some(create_image_options), None, credentials);

handle_stream_output(stream, DockerError::PullError).await
})
}

Expand All @@ -244,7 +225,7 @@ impl DockerUtil {
let runtime = Runtime::new().map_err(|_| DockerError::RuntimeError)?;

runtime.block_on(async move {
let mut stream = self.docker.build_image(
let stream = self.docker.build_image(
BuildImageOptions {
dockerfile: "Dockerfile".to_string(),
t: self.docker_image.clone(),
Expand All @@ -254,26 +235,7 @@ impl DockerUtil {
Some(Self::build_tarball(dockerfile_dir)?.into()),
);

loop {
if let Some(item) = stream.next().await {
match item {
Ok(output) => {
if let Some(err_msg) = &output.error {
error!("{:?}", err_msg.clone());
break Err(DockerError::BuildError);
} else {
info!("{:?}", output);
}
}
Err(e) => {
error!("{:?}", e);
break Err(DockerError::BuildError);
}
}
} else {
break Ok(());
}
}
handle_stream_output(stream, DockerError::BuildError).await
})
}

Expand Down
1 change: 1 addition & 0 deletions enclave_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::Path;
use std::process::Command;

mod docker;
mod utils;
mod yaml_generator;

use aws_nitro_enclaves_image_format::defs::{EifBuildInfo, EifIdentityInfo, EIF_HDR_ARCH_ARM64};
Expand Down
53 changes: 53 additions & 0 deletions enclave_build/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use bollard::errors::Error;
use bollard::secret::{BuildInfo, CreateImageInfo};
use futures::stream::StreamExt;
use futures::Stream;
use log::{error, info};

pub trait StreamItem {
fn error(&self) -> Option<String>;
}

// Implement StreamItem for CreateImageInfo
impl StreamItem for CreateImageInfo {
fn error(&self) -> Option<String> {
self.error.clone()
}
}

// Implement StreamItem for BuildInfo
impl StreamItem for BuildInfo {
fn error(&self) -> Option<String> {
self.error.clone()
}
}

pub async fn handle_stream_output<T, U>(
mut stream: impl Stream<Item = Result<T, Error>> + Unpin,
error_type: U,
) -> Result<(), U>
where
T: StreamItem + std::fmt::Debug,
{
while let Some(item) = stream.next().await {
match item {
Ok(output) => {
if let Some(err_msg) = output.error() {
error!("{:?}", err_msg);
return Err(error_type);
} else {
info!("{:?}", output);
}
}
Err(e) => {
error!("{:?}", e);
return Err(error_type);
}
}
}

Ok(())
}

0 comments on commit 66ff6fd

Please sign in to comment.