Skip to content

Commit ec780bb

Browse files
authored
Merge pull request #524 from cgwalters/progress-layers
deploy: Add number of layers to fetch progress
2 parents 916539e + 6eb5718 commit ec780bb

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

lib/src/deploy.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,55 @@ pub(crate) fn check_bootc_label(config: &ostree_ext::oci_spec::image::ImageConfi
113113
}
114114
}
115115

116+
/// Write container fetch progress to standard output.
117+
async fn handle_layer_progress_print(
118+
mut layers: tokio::sync::mpsc::Receiver<ostree_container::store::ImportProgress>,
119+
mut layer_bytes: tokio::sync::watch::Receiver<Option<ostree_container::store::LayerProgress>>,
120+
total_layers: usize,
121+
n_layers_fetched: &mut usize,
122+
) {
123+
let style = indicatif::ProgressStyle::default_bar();
124+
let pb = indicatif::ProgressBar::new(100);
125+
pb.set_style(
126+
style
127+
.template("{prefix} {bytes} [{bar:20}] ({eta}) {msg}")
128+
.unwrap(),
129+
);
130+
loop {
131+
tokio::select! {
132+
// Always handle layer changes first.
133+
biased;
134+
layer = layers.recv() => {
135+
if let Some(l) = layer {
136+
if l.is_starting() {
137+
pb.set_position(0);
138+
} else {
139+
pb.finish();
140+
*n_layers_fetched += 1;
141+
}
142+
pb.set_prefix(format!("[{}/{}]", *n_layers_fetched, total_layers));
143+
pb.set_message(ostree_ext::cli::layer_progress_format(&l));
144+
} else {
145+
// If the receiver is disconnected, then we're done
146+
break
147+
};
148+
},
149+
r = layer_bytes.changed() => {
150+
if r.is_err() {
151+
// If the receiver is disconnected, then we're done
152+
break
153+
}
154+
let bytes = layer_bytes.borrow();
155+
if let Some(bytes) = &*bytes {
156+
pb.set_length(bytes.total);
157+
pb.set_position(bytes.fetched);
158+
}
159+
}
160+
161+
}
162+
}
163+
}
164+
116165
/// Wrapper for pulling a container image, wiring up status output.
117166
#[context("Pulling")]
118167
pub(crate) async fn pull(
@@ -138,8 +187,16 @@ pub(crate) async fn pull(
138187
let printer = (!quiet).then(|| {
139188
let layer_progress = imp.request_progress();
140189
let layer_byte_progress = imp.request_layer_progress();
190+
let total_layers = prep.layers_to_fetch().count();
191+
let mut n_fetched = 0usize;
141192
tokio::task::spawn(async move {
142-
ostree_ext::cli::handle_layer_progress_print(layer_progress, layer_byte_progress).await
193+
handle_layer_progress_print(
194+
layer_progress,
195+
layer_byte_progress,
196+
total_layers,
197+
&mut n_fetched,
198+
)
199+
.await
143200
})
144201
});
145202
let import = imp.import(prep).await;

0 commit comments

Comments
 (0)