pub enum OperationMode {
PullAndCache {
repository: String,
reference: String,
},
ExtractAndCache {
tar_file: String,
repository: String,
reference: String,
},
PushFromCacheUsingManifest {
repository: String,
reference: String,
},
PushFromCacheUsingTar {
repository: String,
reference: String,
},
PushFromTar {
tar_file: String,
repository: String,
reference: String,
},
}Expand description
4种核心操作模式
Variants§
PullAndCache
模式1: 从repository拉取并缓存
ExtractAndCache
模式2: 从tar文件提取并缓存
PushFromCacheUsingManifest
模式3: 从缓存推送(基于manifest)
PushFromCacheUsingTar
模式4: 从缓存推送(基于tar) - 实际与模式3相同
PushFromTar
模式5: 直接从tar推送(无缓存)
Implementations§
Source§impl OperationMode
impl OperationMode
Sourcepub fn description(&self) -> &'static str
pub fn description(&self) -> &'static str
获取模式描述
Examples found in repository?
examples/optimized_upload_demo.rs (line 58)
10async fn main() -> Result<()> {
11 println!("Docker Image Pusher - Optimized Upload Demo");
12
13 // Create image manager with optimizations enabled (default)
14 let mut manager = ImageManager::new(None, true)?;
15
16 // Configure pipeline for demonstration
17 let config = PipelineConfig {
18 max_concurrent: 8,
19 buffer_size: 1024,
20 small_blob_threshold: 10 * 1024 * 1024, // 10MB
21 medium_blob_threshold: 100 * 1024 * 1024, // 100MB
22 large_blob_threshold: 500 * 1024 * 1024, // 500MB
23 timeout_seconds: 7200,
24 retry_attempts: 3,
25 memory_limit_mb: 512,
26 enable_compression: true,
27 enable_streaming: true,
28 };
29 manager.configure_pipeline(config);
30
31 // Verify configuration
32 let (optimized, pipeline_config) = manager.get_config();
33 println!("Optimized mode: {}", optimized);
34 println!("Pipeline config: {:?}", pipeline_config);
35
36 // Example registry client (would need real registry URL)
37 let client = RegistryClientBuilder::new("https://registry.example.com".to_string())
38 .with_verbose(true)
39 .build()?;
40
41 // Test connectivity (this would fail with example URL)
42 println!("Testing registry connectivity...");
43 match client.test_connectivity().await {
44 Ok(_) => println!("✓ Registry connectivity successful"),
45 Err(e) => println!(
46 "✗ Registry connectivity failed: {} (expected with example URL)",
47 e
48 ),
49 }
50
51 // Example operation mode for pushing from tar
52 let mode = OperationMode::PushFromTar {
53 tar_file: "example-image.tar".to_string(),
54 repository: "myapp".to_string(),
55 reference: "latest".to_string(),
56 };
57
58 println!("Operation mode: {}", mode.description());
59
60 // In a real scenario, you would call:
61 // manager.execute_operation(&mode, Some(&client), None).await?;
62
63 println!("Demo completed successfully!");
64 println!("\nKey benefits of optimized mode:");
65 println!("• Priority-based upload scheduling (small blobs first)");
66 println!("• Streaming TAR processing with parallel uploads");
67 println!("• Memory-efficient processing of large files");
68 println!("• Configurable pipeline parameters");
69
70 Ok(())
71}More examples
examples/extract_and_cache_demo.rs (line 56)
12async fn main() -> Result<()> {
13 println!("📦 Docker Image Pusher - Extract and Cache Demo");
14 println!("===================================================");
15
16 // 配置参数
17 let tar_file = get_tar_file_path();
18 let cache_dir = ".cache_extract_demo";
19
20 // 从tar文件名推导repository和reference
21 let file_stem = Path::new(&tar_file)
22 .file_stem()
23 .and_then(|s| s.to_str())
24 .unwrap_or("extracted-image");
25 let repository = format!("local/{}", file_stem);
26 let reference = "latest";
27
28 println!("📥 Configuration:");
29 println!(" Tar File: {}", tar_file);
30 println!(" Repository: {}", repository);
31 println!(" Reference: {}", reference);
32 println!(" Cache Directory: {}", cache_dir);
33 println!();
34
35 // 1. 验证tar文件存在
36 if !Path::new(&tar_file).exists() {
37 eprintln!("❌ Tar file not found: {}", tar_file);
38 eprintln!("💡 Please ensure the dufs.tar file exists in the examples/ directory");
39 return Err(docker_image_pusher::error::RegistryError::Validation(
40 format!("Tar file does not exist: {}", tar_file),
41 ));
42 }
43
44 // 2. 创建 ImageManager
45 println!("🔧 Creating ImageManager...");
46 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
47 println!("✅ ImageManager created successfully");
48
49 // 3. 定义操作模式
50 let mode = OperationMode::ExtractAndCache {
51 tar_file: tar_file.clone(),
52 repository: repository.clone(),
53 reference: reference.to_string(),
54 };
55
56 println!("📋 Operation Mode: {}", mode.description());
57 println!();
58
59 // 4. 执行提取和缓存操作
60 println!("🔄 Starting extract and cache operation...");
61 match image_manager.execute_operation(&mode, None, None).await {
62 Ok(()) => {
63 println!("✅ Extract and cache operation completed successfully!");
64 println!();
65 println!("📂 Image extracted and cached to: {}", cache_dir);
66 println!("🔍 You can now inspect the cache contents:");
67 println!(
68 " - Manifests: {}/manifests/{}/{}",
69 cache_dir, repository, reference
70 );
71 println!(" - Blobs: {}/blobs/sha256/", cache_dir);
72 println!(" - Index: {}/index.json", cache_dir);
73 }
74 Err(e) => {
75 eprintln!("❌ Extract and cache operation failed: {}", e);
76 std::process::exit(1);
77 }
78 }
79
80 // 5. 显示提取统计
81 show_extraction_stats(&tar_file, cache_dir).await;
82
83 Ok(())
84}examples/pull_and_cache_demo.rs (line 75)
13async fn main() -> Result<()> {
14 println!("🚀 Docker Image Pusher - Pull and Cache Demo");
15 println!("==================================================");
16
17 // 配置参数 - 支持环境变量
18 let registry = env::var("DOCKER_REGISTRY")
19 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
20 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
21 let reference = env::var("DOCKER_REFERENCE").unwrap_or_else(|_| "yoce".to_string());
22 let cache_dir = ".cache_demo";
23
24 println!("📥 Configuration:");
25 println!(" Registry: {}", registry);
26 println!(" Repository: {}", repository);
27 println!(" Reference: {}", reference);
28 println!(" Cache Directory: {}", cache_dir);
29 println!();
30
31 // 1. 创建 ImageManager
32 println!("🔧 Creating ImageManager...");
33 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
34 println!("✅ ImageManager created successfully");
35
36 // 2. 构建 Registry Client
37 println!("🌐 Building Registry Client...");
38 let client = RegistryClientBuilder::new(registry.to_string())
39 .with_timeout(3600)
40 .with_verbose(true)
41 .build()?;
42 println!("✅ Registry Client built successfully");
43
44 // 3. 获取认证(总是尝试,支持匿名token)
45 println!("🔐 Attempting authentication...");
46 let auth_token = if let (Ok(username), Ok(password)) =
47 (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
48 {
49 println!(" Using provided credentials for user: {}", username);
50 let auth_config = AuthConfig::new(username, password);
51 client
52 .authenticate_for_repository(&auth_config, &repository)
53 .await?
54 } else {
55 println!(" No credentials provided, trying anonymous authentication...");
56 // 使用直接认证方法尝试获取匿名token
57 let auth = docker_image_pusher::registry::auth::Auth::new();
58 let output = docker_image_pusher::logging::Logger::new(true);
59 auth.authenticate_with_registry(®istry, &repository, None, None, &output)
60 .await?
61 };
62
63 if auth_token.is_some() {
64 println!("✅ Authentication successful");
65 } else {
66 println!("ℹ️ No authentication required");
67 }
68
69 // 4. 定义操作模式
70 let mode = OperationMode::PullAndCache {
71 repository: repository.to_string(),
72 reference: reference.to_string(),
73 };
74
75 println!("📋 Operation Mode: {}", mode.description());
76 println!();
77
78 // 5. 执行拉取和缓存操作
79 println!("🔄 Starting pull and cache operation...");
80 match image_manager
81 .execute_operation(&mode, Some(&client), auth_token.as_deref())
82 .await
83 {
84 Ok(()) => {
85 println!("✅ Pull and cache operation completed successfully!");
86 println!();
87 println!("📂 Image cached to: {}", cache_dir);
88 println!("🔍 You can now inspect the cache contents:");
89 println!(
90 " - Manifests: {}/manifests/{}/{}",
91 cache_dir, repository, reference
92 );
93 println!(" - Blobs: {}/blobs/sha256/", cache_dir);
94 println!(" - Index: {}/index.json", cache_dir);
95 }
96 Err(e) => {
97 eprintln!("❌ Pull and cache operation failed: {}", e);
98 std::process::exit(1);
99 }
100 }
101
102 // 6. 显示缓存统计
103 show_cache_stats(cache_dir).await;
104
105 Ok(())
106}examples/push_to_aliyun_demo.rs (line 84)
11async fn main() -> Result<()> {
12 println!("🚀 Docker Image Pusher - Push to Aliyun Registry Demo");
13 println!("=======================================================");
14
15 // 阿里云配置
16 let aliyun_registry = "registry.cn-beijing.aliyuncs.com";
17 let aliyun_username = "canny_best@163.com";
18 let aliyun_password = "ra201222";
19
20 // 源镜像(从缓存)
21 let source_repository = "yoce/cblt";
22 let source_reference = "yoce";
23
24 // 目标镜像(推送到阿里云)- 推送回同一个仓库,不同tag
25 let target_repository = "yoce/cblt";
26 let target_reference = "push-test";
27 let cache_dir = ".cache_demo";
28
29 println!("📥 Configuration:");
30 println!(
31 " Source (Cache): {}/{}",
32 source_repository, source_reference
33 );
34 println!(" Target Registry: {}", aliyun_registry);
35 println!(" Target Repository: {}", target_repository);
36 println!(" Target Reference: {}", target_reference);
37 println!(" Cache Directory: {}", cache_dir);
38 println!(" Username: {}", aliyun_username);
39 println!();
40
41 // 1. 检查缓存是否存在
42 check_cache_exists(cache_dir, source_repository, source_reference).await?;
43
44 // 2. 创建 ImageManager
45 println!("🔧 Creating ImageManager...");
46 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
47 println!("✅ ImageManager created successfully");
48
49 // 3. 复制缓存中的镜像到目标仓库名称
50 println!("📋 Copying cached image to target repository name...");
51 copy_image_in_cache(
52 cache_dir,
53 source_repository,
54 source_reference,
55 target_repository,
56 target_reference,
57 )
58 .await?;
59 println!("✅ Image copied in cache");
60
61 // 4. 构建 Registry Client for 阿里云
62 println!("🌐 Building Registry Client for Aliyun...");
63 let client = RegistryClientBuilder::new(format!("https://{}", aliyun_registry))
64 .with_timeout(3600)
65 .with_skip_tls(false)
66 .with_verbose(true)
67 .build()?;
68 println!("✅ Registry Client built successfully");
69
70 // 4. 认证到阿里云
71 println!("🔐 Authenticating with Aliyun registry...");
72 let auth_config = AuthConfig::new(aliyun_username.to_string(), aliyun_password.to_string());
73 let auth_token = client
74 .authenticate_for_repository(&auth_config, target_repository)
75 .await?;
76 println!("✅ Authentication successful");
77
78 // 5. 定义操作模式 - 推送缓存中的镜像到阿里云
79 let mode = OperationMode::PushFromCacheUsingManifest {
80 repository: target_repository.to_string(),
81 reference: target_reference.to_string(),
82 };
83
84 println!("📋 Operation Mode: {}", mode.description());
85 println!();
86
87 // 6. 执行推送操作
88 println!("🔄 Starting push to Aliyun operation...");
89 println!(
90 "🎯 Target: {}/{}/{}",
91 aliyun_registry, target_repository, target_reference
92 );
93
94 match image_manager
95 .execute_operation(&mode, Some(&client), auth_token.as_deref())
96 .await
97 {
98 Ok(()) => {
99 println!("✅ Push to Aliyun operation completed successfully!");
100 println!(
101 "🎯 Image pushed to: {}/{}/{}",
102 aliyun_registry, target_repository, target_reference
103 );
104 println!("🔍 You can verify the upload in Aliyun Console:");
105 println!(" https://cr.console.aliyun.com");
106
107 // 验证推送结果
108 verify_push_result(&client, target_repository, target_reference, &auth_token).await;
109 }
110 Err(e) => {
111 eprintln!("❌ Push to Aliyun operation failed: {}", e);
112 eprintln!("💡 Possible solutions:");
113 eprintln!(" - Check Aliyun credentials and permissions");
114 eprintln!(" - Verify repository name format (namespace/repo)");
115 eprintln!(" - Check network connectivity to Aliyun registry");
116 eprintln!(" - Ensure the repository exists in Aliyun console");
117 eprintln!(" - Check if the namespace 'yoce' exists");
118 std::process::exit(1);
119 }
120 }
121
122 Ok(())
123}examples/push_from_cache_manifest_demo.rs (line 71)
13async fn main() -> Result<()> {
14 println!("🚀 Docker Image Pusher - Push from Cache (Manifest) Demo");
15 println!("============================================================");
16
17 // 配置参数 - 使用Aliyun registry,推送到已存在的repository
18 let source_repository = "yoce/cblt"; // 从缓存中读取
19 let source_reference = "yoce";
20 let target_registry = "registry.cn-beijing.aliyuncs.com";
21 let target_repository = "yoce/cblt"; // 推送回同一个repository
22 let target_reference = "test-push"; // 使用新的tag
23 let cache_dir = ".cache_demo";
24
25 println!("📥 Configuration:");
26 println!(
27 " Source (Cache): {}/{}",
28 source_repository, source_reference
29 );
30 println!(" Target Registry: {}", target_registry);
31 println!(" Target Repository: {}", target_repository);
32 println!(" Target Reference: {}", target_reference);
33 println!(" Cache Directory: {}", cache_dir);
34 println!();
35
36 // 1. 检查缓存是否存在
37 check_cache_exists(cache_dir, source_repository, source_reference).await?;
38
39 // 2. 创建 ImageManager
40 println!("🔧 Creating ImageManager...");
41 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
42 println!("✅ ImageManager created successfully");
43
44 // 3. 构建 Registry Client - 配置为Aliyun registry
45 println!("🌐 Building Registry Client for Aliyun registry...");
46 let client = RegistryClientBuilder::new(format!("https://{}", target_registry))
47 .with_timeout(3600)
48 .with_skip_tls(false) // Aliyun registry使用TLS
49 .with_verbose(true)
50 .build()?;
51 println!("✅ Registry Client built successfully");
52
53 // 4. 获取认证 - 使用Aliyun registry凭据
54 println!("🔐 Authenticating with Aliyun registry...");
55 let username = env::var("ALIYUN_USERNAME").unwrap_or_else(|_| "canny_best@163.com".to_string());
56 let password = env::var("ALIYUN_PASSWORD").unwrap_or_else(|_| "ra201222".to_string());
57
58 let auth_config = AuthConfig::new(username.clone(), password.clone());
59 let auth_token = client
60 .authenticate_for_repository(&auth_config, target_repository)
61 .await?;
62 println!("✅ Authentication successful with user: {}", username);
63 println!("🔑 Token scope: repository:{}:pull,push", target_repository);
64
65 // 5. 定义操作模式 - 使用 manifest 方式推送
66 let mode = OperationMode::PushFromCacheUsingManifest {
67 repository: target_repository.to_string(),
68 reference: target_reference.to_string(),
69 };
70
71 println!("📋 Operation Mode: {}", mode.description());
72 println!();
73
74 // 6. 执行推送操作
75 println!("🔄 Starting push from cache operation...");
76 match image_manager
77 .execute_operation(&mode, Some(&client), auth_token.as_deref())
78 .await
79 {
80 Ok(()) => {
81 println!("✅ Push from cache operation completed successfully!");
82 println!();
83 println!(
84 "🎯 Image pushed to: {}/{}/{}",
85 target_registry, target_repository, target_reference
86 );
87 println!("🔍 You can now verify the upload:");
88 println!(
89 " curl -H \"Authorization: Bearer <token>\" https://{}/v2/{}/manifests/{}",
90 target_registry, target_repository, target_reference
91 );
92 println!(
93 " curl -H \"Authorization: Bearer <token>\" https://{}/v2/{}/tags/list",
94 target_registry, target_repository
95 );
96 }
97 Err(e) => {
98 eprintln!("❌ Push from cache operation failed: {}", e);
99 eprintln!("💡 Possible solutions:");
100 eprintln!(
101 " - Check if source image exists in cache: {}/{}",
102 source_repository, source_reference
103 );
104 eprintln!(" - Verify Aliyun registry credentials");
105 eprintln!(" - Check network connectivity to Aliyun registry");
106 std::process::exit(1);
107 }
108 }
109
110 // 7. 验证推送结果
111 verify_push_result(&client, target_repository, target_reference, &auth_token).await;
112
113 Ok(())
114}examples/push_from_cache_tar_demo.rs (line 72)
13async fn main() -> Result<()> {
14 println!("📦 Docker Image Pusher - Push from Cache (Tar Reference) Demo");
15 println!("================================================================");
16
17 // 配置参数
18 let target_registry = "registry.cn-beijing.aliyuncs.com";
19 let target_repository = "yoce/cblt"; // 推送到相同的repository
20 let target_reference = "yoce"; // 推送到相同的reference
21 let cache_dir = ".cache_demo"; // 使用已有的缓存
22
23 println!("📥 Configuration:");
24 println!(" Cache Directory: {}", cache_dir);
25 println!(" Target Registry: {}", target_registry);
26 println!(" Target Repository: {}", target_repository);
27 println!(" Target Reference: {}", target_reference);
28 println!();
29
30 println!(
31 "ℹ️ Note: This mode pushes cached image {}/{} to target registry",
32 target_repository, target_reference
33 );
34 println!(" The implementation is identical to manifest-based push");
35 println!();
36
37 // 1. 检查缓存是否存在
38 check_cache_exists(cache_dir, target_repository, target_reference).await?;
39
40 // 2. 创建 ImageManager
41 println!("🔧 Creating ImageManager...");
42 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
43 println!("✅ ImageManager created successfully");
44
45 // 3. 构建 Registry Client
46 println!("🌐 Building Registry Client for Aliyun registry...");
47 let client = RegistryClientBuilder::new(format!("https://{}", target_registry))
48 .with_timeout(3600)
49 .with_skip_tls(false) // Aliyun uses TLS
50 .with_verbose(true)
51 .build()?;
52 println!("✅ Registry Client built successfully");
53
54 // 4. 获取认证(Aliyun credentials)
55 println!("🔐 Authenticating with Aliyun registry...");
56 let auth_config = AuthConfig::new("canny_best@163.com".to_string(), "ra201222".to_string());
57 let auth_token = client
58 .authenticate_for_repository(&auth_config, target_repository)
59 .await?;
60 println!(
61 "✅ Authentication successful with user: {}",
62 auth_config.username
63 );
64 println!("🔑 Token scope: repository:{}:pull,push", target_repository);
65
66 // 5. 定义操作模式 - 使用 tar 引用方式推送(实际上与manifest模式相同)
67 let mode = OperationMode::PushFromCacheUsingTar {
68 repository: target_repository.to_string(),
69 reference: target_reference.to_string(),
70 };
71
72 println!("📋 Operation Mode: {}", mode.description());
73 println!("🔄 Internal Process: Reading from unified cache format (same as manifest mode)");
74 println!();
75
76 // 6. 执行推送操作
77 println!("🔄 Starting push from cache operation...");
78 match image_manager
79 .execute_operation(&mode, Some(&client), auth_token.as_deref())
80 .await
81 {
82 Ok(()) => {
83 println!("✅ Push from cache (tar reference) operation completed successfully!");
84 println!();
85 println!(
86 "🎯 Image pushed to: {}/{}/{}",
87 target_registry, target_repository, target_reference
88 );
89 println!("🔍 You can now verify the upload:");
90 println!(
91 " curl http://{}/v2/{}/manifests/{}",
92 target_registry, target_repository, target_reference
93 );
94 println!(
95 " curl http://{}/v2/{}/tags/list",
96 target_registry, target_repository
97 );
98
99 // 显示模式差异说明
100 show_mode_explanation();
101 }
102 Err(e) => {
103 eprintln!("❌ Push from cache (tar reference) operation failed: {}", e);
104 eprintln!("💡 Possible solutions:");
105 eprintln!(
106 " - Check if target registry is running: docker run -d -p 5000:5000 registry:2"
107 );
108 eprintln!(" - Verify cache contains the source image");
109 eprintln!(" - Check network connectivity to target registry");
110 std::process::exit(1);
111 }
112 }
113
114 // 7. 验证推送结果并对比两种模式
115 verify_push_result_and_compare(&client, target_repository, target_reference, &auth_token).await;
116
117 Ok(())
118}Additional examples can be found in:
Sourcepub fn requires_registry_client(&self) -> bool
pub fn requires_registry_client(&self) -> bool
检查是否需要Registry客户端
Sourcepub fn get_target(&self) -> (&str, &str)
pub fn get_target(&self) -> (&str, &str)
获取目标repository和reference
Sourcepub fn get_source(&self) -> Option<&str>
pub fn get_source(&self) -> Option<&str>
获取源信息(如果适用)
Sourcepub fn is_push_operation(&self) -> bool
pub fn is_push_operation(&self) -> bool
检查是否为推送操作
Sourcepub fn is_cache_operation(&self) -> bool
pub fn is_cache_operation(&self) -> bool
检查是否为缓存操作
Sourcepub fn requires_tar_file(&self) -> bool
pub fn requires_tar_file(&self) -> bool
检查是否需要访问tar文件
Trait Implementations§
Source§impl Clone for OperationMode
impl Clone for OperationMode
Source§fn clone(&self) -> OperationMode
fn clone(&self) -> OperationMode
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for OperationMode
impl Debug for OperationMode
Auto Trait Implementations§
impl Freeze for OperationMode
impl RefUnwindSafe for OperationMode
impl Send for OperationMode
impl Sync for OperationMode
impl Unpin for OperationMode
impl UnwindSafe for OperationMode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.