docker-image-pusher 0.5.6

A memory-optimized Docker image transfer tool for handling large images efficiently
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::path::Path;
use std::sync::Arc;

use serde_json::Value;
use tokio::{
    sync::mpsc,
    task,
    time::{self, Duration},
};

use crate::{
    common::{
        report::report_upload_summary,
        display::docker_like_progress_reporter,
        state,
    },
    import::tar::{
        TarExtraction, TarRepoInfo, build_target_from_tar, extract_tar_archive_with_sender,
        infer_target_from_history, tar_repo_info_from_path,
    },
    CHUNKED_LAYER_SIZE_BYTES, ESTIMATED_SPEED_MBPS, LARGE_LAYER_PROGRESS_INTERVAL_SECS,
    LARGE_LAYER_THRESHOLD_BYTES, LARGE_LAYER_THRESHOLD_MB, MAX_CHUNKED_LAYER_SIZE_BYTES,
    NORMAL_LAYER_PROGRESS_INTERVAL_SECS, PusherError, RATE_LIMIT_DELAY_MS,
};

const DEFAULT_UPLOAD_CONCURRENCY: usize = 3;
const MEDIUM_LAYER_THRESHOLD_MB: f64 = 250.0;
use oci_core::{
    auth::RegistryAuth,
    blobs::{LayerUploadOptions, LocalLayer, ProgressOptions},
    client::Client,
    manifest::{OciDescriptor, OciImageManifest},
    reference::Reference,
    workflows::push::{
        check_and_filter_layers, check_config_exists, upload_config_if_needed,
        upload_layers_concurrent, push_manifest_bytes,
    },
};

/// Captures everything needed to execute a push once analysis is finished.
#[derive(Debug)]
struct PushPlan {
    source_tar: String,
    target: String,
    target_ref: Reference,
    username: String,
    password: String,
}

/// Coordinates the push workflow so helper functions remain grouped.
pub struct PushWorkflow<'a> {
    client: &'a Client,
    chunk_size_bytes: usize,
    upload_parallelism: usize,
}

impl<'a> PushWorkflow<'a> {
    pub fn new(client: &'a Client, chunk_size_bytes: usize, upload_parallelism: usize) -> Self {
        Self {
            client,
            chunk_size_bytes,
            upload_parallelism: upload_parallelism.max(1),
        }
    }

    /// High-level push orchestration invoked by the CLI entry point.
    pub async fn run(
        &self,
        tar_path: &str,
        target_image: Option<String>,
        username: Option<String>,
        password: Option<String>,
        registry_override: Option<String>,
    ) -> Result<(), PusherError> {
        let tar_path_obj = Path::new(tar_path);
        if !tar_path_obj.is_file() {
            return Err(PusherError::push_error(
                "Push requires a docker-save tar archive. Run `docker-image-pusher save` first.",
            ));
        }
        println!("📦 Preparing to push Docker archive: {}", tar_path);
        let tar_info = tar_repo_info_from_path(tar_path)?;

        let plan = self
            .prepare_push_plan(
                tar_path,
                &tar_info,
                target_image,
                username,
                password,
                registry_override,
            )
            .await?;

        self.execute_push_plan(&plan).await?;
        state::record_push_target(&plan.target).await?;
        println!("✅ Successfully pushed image: {}", plan.target);
        Ok(())
    }

    /// Resolves target, credentials, and metadata before uploading begins.
    async fn prepare_push_plan(
        &self,
        tar_path: &str,
        tar_info: &TarRepoInfo,
        target_image: Option<String>,
        username: Option<String>,
        password: Option<String>,
        registry_override: Option<String>,
    ) -> Result<PushPlan, PusherError> {
        let mut target = self
            .determine_target(target_image, tar_info, registry_override)
            .await?;
        target = Self::ensure_target_from_tar_metadata(target, tar_info).await?;
        println!("🎯 Target image resolved as: {}", target);

        let explicit_credentials_requested = username.is_some() || password.is_some();
        let (final_target, target_ref, resolved_username, resolved_password) =
            Self::resolve_target_credentials(
                target,
                tar_info,
                username,
                password,
                explicit_credentials_requested,
            )
            .await?;

        Ok(PushPlan {
            source_tar: tar_path.to_string(),
            target: final_target,
            target_ref,
            username: resolved_username,
            password: resolved_password,
        })
    }

    /// Performs the actual tar extraction followed by the streaming upload sequence.
    async fn execute_push_plan(&self, plan: &PushPlan) -> Result<(), PusherError> {
        self.push_tar_archive(
            &plan.source_tar,
            &plan.target_ref,
            &plan.username,
            &plan.password,
        )
        .await
    }

    /// Chooses a destination reference using CLI overrides, history, or tar metadata.
    async fn determine_target(
        &self,
        provided_target: Option<String>,
        tar_info: &TarRepoInfo,
        registry_override: Option<String>,
    ) -> Result<String, PusherError> {
        if let Some(target) = provided_target {
            return Ok(target);
        }

        if let Some(history_target) = infer_target_from_history(tar_info).await? {
            return Ok(history_target);
        }

        let fallback = build_target_from_tar(tar_info, registry_override.as_deref());
        println!(
            "💡 No recent history matched. Using target derived from tar metadata: {}",
            fallback
        );
        Ok(fallback)
    }

    /// Ensures valid username/password material for the chosen registry.
    async fn resolve_target_credentials(
        target: String,
        tar_info: &TarRepoInfo,
        username: Option<String>,
        password: Option<String>,
        explicit_credentials_requested: bool,
    ) -> Result<(String, Reference, String, String), PusherError> {
        loop {
            let target_ref: Reference = target.parse().map_err(|e| {
                PusherError::PushError(format!("Invalid target image reference: {}", e))
            })?;

            match Self::load_credentials_for_registry(
                username.clone(),
                password.clone(),
                target_ref.registry_host(),
            )
            .await
            {
                Ok((resolved_username, resolved_password)) => {
                    return Ok((target, target_ref, resolved_username, resolved_password));
                }
                Err(err) => {
                    if !explicit_credentials_requested && Self::is_missing_credentials_error(&err) {
                        if let Some((replacement_target, creds)) =
                            Self::attempt_registry_inference(tar_info).await?
                        {
                            if replacement_target != target {
                                println!("🔁 Switching to inferred target: {}", replacement_target);
                            }
                            let replacement_ref = replacement_target.parse().map_err(|e| {
                                PusherError::PushError(format!(
                                    "Invalid target image reference: {}",
                                    e
                                ))
                            })?;
                            return Ok((replacement_target, replacement_ref, creds.0, creds.1));
                        }
                    }
                    return Err(err);
                }
            }
        }
    }

    /// Uploads all layers/config extracted from a docker-save tarball with digest table pre-flight.
    async fn push_tar_archive(
        &self,
        tar_path: &str,
        target_ref: &Reference,
        username: &str,
        password: &str,
    ) -> Result<(), PusherError> {
        let auth = Arc::new(RegistryAuth::basic(username, password));
        println!(
            "🔐 Preparing registry session with {}",
            target_ref.registry_host()
        );

        // First extract tar to get digest table
        println!("📦 Extracting tar archive to build digest table...");
        let (layer_tx, _layer_rx) = mpsc::channel::<LocalLayer>(self.upload_parallelism * 2);
        let tar_path_string = tar_path.to_string();
        let extraction_handle = task::spawn_blocking(move || {
            extract_tar_archive_with_sender(&tar_path_string, Some(layer_tx))
        });

        let extraction = extraction_handle.await.map_err(|err| {
            PusherError::push_error(format!("Tar extraction task failed: {}", err))
        })??;

        // Check remote and filter layers
        let check_result = check_and_filter_layers(
            self.client,
            target_ref,
            auth.as_ref(),
            extraction.layers.clone(),
        )
        .await
        .map_err(|e| PusherError::push_error(format!("Failed to check layers: {e}")))?;

        // Check config blob
        let config_exists = check_config_exists(self.client, target_ref, auth.as_ref(), &extraction.config_digest)
            .await
            .map_err(|e| PusherError::push_error(format!("Failed to check config: {e}")))?;

        // Now upload filtered layers
        let upload_options = LayerUploadOptions {
            chunk_size_bytes: self.chunk_size_bytes,
            large_layer_threshold_bytes: LARGE_LAYER_THRESHOLD_BYTES,
            medium_layer_threshold_mb: MEDIUM_LAYER_THRESHOLD_MB,
            rate_limit_delay_ms: RATE_LIMIT_DELAY_MS,
            concurrency: self.upload_parallelism,
            progress: ProgressOptions {
                large_layer_threshold_mb: LARGE_LAYER_THRESHOLD_MB,
                large_interval_secs: LARGE_LAYER_PROGRESS_INTERVAL_SECS,
                normal_interval_secs: NORMAL_LAYER_PROGRESS_INTERVAL_SECS,
                estimated_speed_mbps: ESTIMATED_SPEED_MBPS,
            },
            progress_reporter: Some(docker_like_progress_reporter()),
        };

        let upload_summary = upload_layers_concurrent(
            self.client,
            target_ref,
            Arc::clone(&auth),
            check_result.layers_to_upload,
            upload_options,
        )
        .await
        .map_err(|e| PusherError::push_error(format!("Failed to upload layers: {e}")))?;

        report_upload_summary(upload_summary.uploaded.len(), check_result.skipped_count);

        // Upload config if it doesn't exist
        upload_config_if_needed(
            self.client,
            target_ref,
            auth.as_ref(),
            &extraction.config_digest,
            &extraction.config_contents,
            config_exists,
        )
        .await
        .map_err(|e| PusherError::push_error(format!("Failed to upload config: {e}")))?;

        // Build and push manifest using common function
        let manifest = build_manifest(&extraction);
        let manifest_json = serde_json::to_vec(&manifest)
            .map_err(|e| PusherError::push_error(format!("Failed to serialize manifest: {}", e)))?;
        
        push_manifest_bytes(
            self.client,
            target_ref,
            auth.as_ref(),
            "application/vnd.docker.distribution.manifest.v2+json",
            &manifest_json,
        )
        .await
        .map_err(|e| PusherError::push_error(format!("Failed to push manifest: {e}")))?;

        println!(
            "🎉 Successfully pushed {} layers",
            upload_summary.uploaded.len()
        );
        Ok(())
    }

    /// Appends repository/tag info from the docker-save manifest when missing.
    async fn ensure_target_from_tar_metadata(
        target: String,
        info: &TarRepoInfo,
    ) -> Result<String, PusherError> {
        let reference: Reference = target.parse().map_err(|e| {
            PusherError::PushError(format!("Invalid target image reference: {}", e))
        })?;

        if reference.tag.is_some() || reference.digest.is_some() {
            return Ok(target);
        }

        let sanitized_repo = reference.repository.trim_end_matches('/');
        let new_repository = if sanitized_repo.is_empty() {
            info.repository.clone()
        } else if sanitized_repo
            .split('/')
            .last()
            .map(|segment| segment == info.image_name)
            .unwrap_or(false)
        {
            sanitized_repo.to_string()
        } else {
            format!("{}/{}", sanitized_repo, info.image_name)
        };

        let completed = format!("{}/{}:{}", reference.registry, new_repository, info.tag);

        Self::confirm_or_wait_for_target(
            &completed,
            "Target missing tag; inferred repository/tag from tar metadata.",
        )
        .await?;

        Ok(completed)
    }

    /// Suggests alternate registries using stored credentials when lookup fails.
    async fn attempt_registry_inference(
        info: &TarRepoInfo,
    ) -> Result<Option<(String, (String, String))>, PusherError> {
        let mut stored = state::all_credentials().await?;
        if stored.is_empty() {
            println!("⚠️  No stored credentials found. Unable to infer alternate registry target.");
            return Ok(None);
        }

        let mut ordered: Vec<state::StoredCredential> = Vec::new();
        if let Ok(history) = state::recent_targets().await {
            for entry in history {
                if let Ok(reference) = Reference::parse(&entry) {
                    if let Some(pos) = stored
                        .iter()
                        .position(|cred| cred.registry == reference.registry)
                    {
                        ordered.push(stored.remove(pos));
                    }
                }
            }
        }

        ordered.extend(stored.into_iter());

        for credential in ordered {
            let candidate = format!(
                "{}/{}:{}",
                credential.registry.trim_end_matches('/'),
                info.repository,
                info.tag
            );
            println!(
                "🧭 Suggested alternate target '{}' using stored login {}",
                candidate, credential.registry
            );
            Self::confirm_or_wait_for_target(
                &candidate,
                "Detected missing login. Proposing alternate target based on stored credentials.",
            )
            .await?;
            return Ok(Some((
                candidate,
                (credential.username, credential.password),
            )));
        }

        Ok(None)
    }

    /// Gives users a chance to confirm inferred targets before pushing.
    async fn confirm_or_wait_for_target(candidate: &str, reason: &str) -> Result<(), PusherError> {
        match state::recent_targets().await {
            Ok(history) => {
                if history.iter().any(|entry| entry == candidate) {
                    println!(
                        "💡 {} Previously confirmed '{}'. Continuing in 3 seconds... (Ctrl+C to abort)",
                        reason, candidate
                    );
                    time::sleep(Duration::from_secs(3)).await;
                    return Ok(());
                }
            }
            Err(err) => println!(
                "⚠️  Unable to read push history for confirmation checks: {}",
                err
            ),
        }

        let question = format!("{} Proceed with '{}' ? [Y/n]: ", reason, candidate);
        if Self::prompt_yes_no(&question).await? {
            Ok(())
        } else {
            Err(PusherError::push_error(
                "User declined inferred target suggestion",
            ))
        }
    }

    /// Blocking stdin prompt executed on a background thread to avoid stalling the runtime.
    async fn prompt_yes_no(prompt: &str) -> Result<bool, PusherError> {
        let prompt = prompt.to_string();
        task::spawn_blocking(move || {
            use std::io::{self, Write};

            print!("{}", prompt);
            io::stdout().flush().map_err(PusherError::IoError)?;

            let mut input = String::new();
            io::stdin()
                .read_line(&mut input)
                .map_err(PusherError::IoError)?;

            let answer = input.trim().to_lowercase();
            Ok(answer.is_empty() || answer == "y" || answer == "yes")
        })
        .await
        .map_err(|err| PusherError::push_error(format!("Failed to read confirmation: {}", err)))?
    }

    fn is_missing_credentials_error(err: &PusherError) -> bool {
        matches!(
            err,
            PusherError::PushError(message) if message.contains("Credentials for registry")
        )
    }

    /// Loads stored credentials unless the CLI explicitly overrides them.
    async fn load_credentials_for_registry(
        username: Option<String>,
        password: Option<String>,
        registry: &str,
    ) -> Result<(String, String), PusherError> {
        match (username, password) {
            (Some(user), Some(pass)) => Ok((user, pass)),
            (Some(_), None) | (None, Some(_)) => Err(PusherError::push_error(
                "Both username and password must be provided when using CLI flags",
            )),
            (None, None) => match state::load_credentials(registry).await {
                Ok(Some((stored_user, stored_pass))) => {
                    println!("🔑 Using stored credentials for {}", registry);
                    Ok((stored_user, stored_pass))
                }
                Ok(None) => Err(PusherError::push_error(format!(
                    "Credentials for registry '{}' not found. Run 'login' or pass --username/--password",
                    registry
                ))),
                Err(err) => Err(PusherError::push_error(format!(
                    "Failed to load stored credentials: {}",
                    err
                ))),
            },
        }
    }
}

/// Entry point for the push command. Handles tar imports, target inference, credential
/// resolution, and delegates to the low-level upload helpers.
pub async fn run_push(
    client: &Client,
    input: &str,
    target_image: Option<String>,
    username: Option<String>,
    password: Option<String>,
    registry_override: Option<String>,
    blob_chunk: Option<usize>,
) -> Result<(), PusherError> {
    let chunk_size_bytes = match blob_chunk {
        Some(0) => {
            return Err(PusherError::push_error(
                "--blob-chunk must be greater than 0 MB",
            ));
        }
        Some(mb) => {
            let requested = mb.checked_mul(1024 * 1024).ok_or_else(|| {
                PusherError::push_error("--blob-chunk value is too large for this platform")
            })?;
            if requested > MAX_CHUNKED_LAYER_SIZE_BYTES {
                return Err(PusherError::push_error(format!(
                    "--blob-chunk cannot exceed {} MB",
                    MAX_CHUNKED_LAYER_SIZE_BYTES / (1024 * 1024)
                )));
            }
            requested
        }
        None => CHUNKED_LAYER_SIZE_BYTES,
    };

    PushWorkflow::new(client, chunk_size_bytes, DEFAULT_UPLOAD_CONCURRENCY)
        .run(input, target_image, username, password, registry_override)
        .await
}

/// Rebuilds the OCI manifest describing the image extracted from the tarball.
fn build_manifest(extraction: &TarExtraction) -> OciImageManifest {
    let diff_ids = collect_layer_diff_ids(&extraction.config_contents);
    if !diff_ids.is_empty() && diff_ids.len() != extraction.layers.len() {
        println!(
            "⚠️  Config reports {} diff_ids but archive has {} layers",
            diff_ids.len(),
            extraction.layers.len()
        );
    }

    let config_descriptor = OciDescriptor {
        media_type: "application/vnd.docker.container.image.v1+json".to_string(),
        digest: extraction.config_digest.clone(),
        size: extraction.config_contents.len() as i64,
        urls: Vec::new(),
    };

    let mut layer_descriptors = Vec::with_capacity(extraction.layers.len());
    for (index, layer) in extraction.layers.iter().enumerate() {
        if index < diff_ids.len() {
            println!(
                "   🧩 Layer {}/{} maps diff_id {}",
                index + 1,
                extraction.layers.len(),
                diff_ids[index]
            );
        }
        layer_descriptors.push(OciDescriptor {
            media_type: layer.media_type.clone(),
            digest: layer.digest.clone(),
            size: layer.size as i64,
            urls: Vec::new(),
        });
    }

    OciImageManifest {
        schema_version: 2,
        media_type: "application/vnd.docker.distribution.manifest.v2+json".to_string(),
        config: config_descriptor,
        layers: layer_descriptors,
    }
}

/// Parses `rootfs.diff_ids` out of the config JSON for debug logging.
fn collect_layer_diff_ids(config_bytes: &[u8]) -> Vec<String> {
    match serde_json::from_slice::<Value>(config_bytes) {
        Ok(value) => value["rootfs"]["diff_ids"]
            .as_array()
            .map(|diffs| {
                diffs
                    .iter()
                    .filter_map(|entry| entry.as_str().map(|s| s.to_string()))
                    .collect()
            })
            .unwrap_or_default(),
        Err(err) => {
            println!("⚠️  Unable to inspect config diff_ids: {}", err);
            Vec::new()
        }
    }
}