From 93bdeec423140fe4efc4a01b2dd31e39b817da1b Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Fri, 12 Mar 2021 03:18:33 +0100 Subject: Add ImageBuildChunk Signed-off-by: Matthias Beyer --- src/image.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/image.rs b/src/image.rs index addf4ee..20a6886 100644 --- a/src/image.rs +++ b/src/image.rs @@ -862,6 +862,47 @@ pub enum Status { Deleted(String), } +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +/// Represents a response chunk from Docker api when building, pulling or importing an image. +pub enum ImageBuildChunk { + Update { + stream: String, + }, + Error { + error: String, + #[serde(rename = "errorDetail")] + error_detail: ErrorDetail, + }, + Digest { + aux: Aux, + }, + PullStatus { + status: String, + id: Option, + progress: Option, + #[serde(rename = "progressDetail")] + progress_detail: Option, + }, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Aux { + #[serde(rename = "ID")] + id: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct ErrorDetail { + message: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct ProgressDetail { + current: Option, + total: Option, +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 From 4514c2566b8f328b5a883e408cf09510a0a97efe Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Fri, 12 Mar 2021 03:18:58 +0100 Subject: Use ImageBuildChunk in endpoints Signed-off-by: Matthias Beyer --- src/docker.rs | 8 ++++---- src/image.rs | 16 ++++++---------- 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/docker.rs b/src/docker.rs index edb97e4..1b7f603 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -7,8 +7,7 @@ use std::{collections::HashMap, env, io, path::Path}; use futures_util::{stream::Stream, TryStreamExt}; use hyper::{client::HttpConnector, Body, Client, Method}; use mime::Mime; -use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde::{de, Deserialize, Serialize}; use url::form_urlencoded; use crate::{ @@ -355,14 +354,15 @@ impl Docker { /// Send a streaming post request that returns a stream of JSON values /// /// Assumes that each received chunk contains one or more JSON values - pub(crate) fn stream_post_into_values<'a, H>( + pub(crate) fn stream_post_into<'a, H, T>( &'a self, endpoint: impl AsRef + 'a, body: Option<(Body, Mime)>, headers: Option, - ) -> impl Stream> + 'a + ) -> impl Stream> + 'a where H: IntoIterator + 'a, + T: de::DeserializeOwned, { self.stream_post(endpoint, body, headers) .and_then(|chunk| async move { diff --git a/src/image.rs b/src/image.rs index 20a6886..d32c472 100644 --- a/src/image.rs +++ b/src/image.rs @@ -7,7 +7,6 @@ use std::{collections::HashMap, io::Read, iter}; use futures_util::{stream::Stream, TryFutureExt, TryStreamExt}; use hyper::Body; use serde::{Deserialize, Serialize}; -use serde_json::Value; use url::form_urlencoded; use crate::{docker::Docker, errors::Result, tarball, transport::tar}; @@ -111,7 +110,7 @@ impl<'docker> Images<'docker> { pub fn build( &self, opts: &BuildOptions, - ) -> impl Stream> + Unpin + 'docker { + ) -> impl Stream> + Unpin + 'docker { let mut endpoint = vec!["/build".to_owned()]; if let Some(query) = opts.serialize() { endpoint.push(query) @@ -131,7 +130,7 @@ impl<'docker> Images<'docker> { // Bubble up error inside the stream for backwards compatability tar_result?; - let value_stream = docker.stream_post_into_values( + let value_stream = docker.stream_post_into( endpoint.join("?"), Some((Body::from(bytes), tar())), None::>, @@ -191,7 +190,7 @@ impl<'docker> Images<'docker> { pub fn pull( &self, opts: &PullOptions, - ) -> impl Stream> + Unpin + 'docker { + ) -> impl Stream> + Unpin + 'docker { let mut path = vec!["/images/create".to_owned()]; if let Some(query) = opts.serialize() { path.push(query); @@ -200,10 +199,7 @@ impl<'docker> Images<'docker> { .auth_header() .map(|a| iter::once(("X-Registry-Auth", a))); - Box::pin( - self.docker - .stream_post_into_values(path.join("?"), None, headers), - ) + Box::pin(self.docker.stream_post_into(path.join("?"), None, headers)) } /// exports a collection of named images, @@ -230,7 +226,7 @@ impl<'docker> Images<'docker> { pub fn import( self, mut tarball: R, - ) -> impl Stream> + Unpin + 'docker + ) -> impl Stream> + Unpin + 'docker where R: Read + Send + 'docker, { @@ -240,7 +236,7 @@ impl<'docker> Images<'docker> { tarball.read_to_end(&mut bytes)?; - let value_stream = self.docker.stream_post_into_values( + let value_stream = self.docker.stream_post_into( "/images/load", Some((Body::from(bytes), tar())), None::>, -- cgit v1.2.3