pub struct RegistryClientBuilder { /* private fields */ }Implementations§
Source§impl RegistryClientBuilder
impl RegistryClientBuilder
Sourcepub fn new(address: String) -> Self
pub fn new(address: String) -> Self
Examples found in repository?
examples/optimized_upload_demo.rs (line 37)
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/pull_and_cache_demo.rs (line 38)
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/auth_test_demo.rs (line 25)
9async fn main() -> Result<()> {
10 println!("🔐 Docker Registry API v2 Authentication Test");
11 println!("==============================================");
12
13 // 配置参数
14 let registry = env::var("DOCKER_REGISTRY")
15 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
16 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
17
18 println!("📋 Configuration:");
19 println!(" Registry: {}", registry);
20 println!(" Repository: {}", repository);
21 println!();
22
23 // 构建 Registry Client
24 println!("🌐 Building Registry Client...");
25 let client = RegistryClientBuilder::new(registry.clone())
26 .with_timeout(300)
27 .with_verbose(true)
28 .build()?;
29 println!("✅ Registry Client built successfully");
30
31 // 测试无凭据的情况
32 println!();
33 println!("🔍 Test 1: Testing registry authentication challenge...");
34 let auth = docker_image_pusher::registry::auth::Auth::new();
35 let output = docker_image_pusher::logging::Logger::new(true);
36
37 // 直接调用新的认证方法
38 match auth
39 .authenticate_with_registry(®istry, &repository, None, None, &output)
40 .await
41 {
42 Ok(token) => {
43 if let Some(token) = token {
44 println!(
45 "✅ Received authentication token: {}...",
46 &token[..20.min(token.len())]
47 );
48 } else {
49 println!("ℹ️ Registry does not require authentication");
50 }
51 }
52 Err(e) => {
53 println!("❌ Authentication test failed: {}", e);
54 println!(" This is expected if the registry requires credentials");
55 }
56 }
57
58 // 测试有凭据的情况(如果提供)
59 if let (Ok(username), Ok(password)) = (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
60 {
61 println!();
62 println!("🔍 Test 2: Testing with provided credentials...");
63 println!(" Username: {}", username);
64
65 match auth
66 .authenticate_with_registry(
67 ®istry,
68 &repository,
69 Some(&username),
70 Some(&password),
71 &output,
72 )
73 .await
74 {
75 Ok(token) => {
76 if let Some(token) = token {
77 println!("✅ Successfully authenticated with credentials");
78 println!(" Token: {}...", &token[..50.min(token.len())]);
79
80 // 测试token是否能用于访问manifest
81 println!();
82 println!("🔍 Test 3: Testing token with manifest access...");
83 match client
84 .pull_manifest(&repository, "yoce", &Some(token))
85 .await
86 {
87 Ok(manifest) => {
88 println!("✅ Successfully pulled manifest using token");
89 println!(" Manifest size: {} bytes", manifest.len());
90 }
91 Err(e) => {
92 println!("❌ Failed to pull manifest with token: {}", e);
93 }
94 }
95 } else {
96 println!("ℹ️ Authentication successful but no token required");
97 }
98 }
99 Err(e) => {
100 println!("❌ Authentication with credentials failed: {}", e);
101 }
102 }
103 } else {
104 println!();
105 println!("ℹ️ No credentials provided via DOCKER_USERNAME/DOCKER_PASSWORD");
106 println!(" Set these environment variables to test credential-based authentication");
107 }
108
109 println!();
110 println!("🏁 Authentication test completed");
111
112 Ok(())
113}examples/push_to_aliyun_demo.rs (line 63)
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 46)
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 47)
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:
pub fn with_auth(self, auth_config: Option<AuthConfig>) -> Self
Sourcepub fn with_timeout(self, timeout: u64) -> Self
pub fn with_timeout(self, timeout: u64) -> Self
Examples found in repository?
examples/pull_and_cache_demo.rs (line 39)
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}More examples
examples/auth_test_demo.rs (line 26)
9async fn main() -> Result<()> {
10 println!("🔐 Docker Registry API v2 Authentication Test");
11 println!("==============================================");
12
13 // 配置参数
14 let registry = env::var("DOCKER_REGISTRY")
15 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
16 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
17
18 println!("📋 Configuration:");
19 println!(" Registry: {}", registry);
20 println!(" Repository: {}", repository);
21 println!();
22
23 // 构建 Registry Client
24 println!("🌐 Building Registry Client...");
25 let client = RegistryClientBuilder::new(registry.clone())
26 .with_timeout(300)
27 .with_verbose(true)
28 .build()?;
29 println!("✅ Registry Client built successfully");
30
31 // 测试无凭据的情况
32 println!();
33 println!("🔍 Test 1: Testing registry authentication challenge...");
34 let auth = docker_image_pusher::registry::auth::Auth::new();
35 let output = docker_image_pusher::logging::Logger::new(true);
36
37 // 直接调用新的认证方法
38 match auth
39 .authenticate_with_registry(®istry, &repository, None, None, &output)
40 .await
41 {
42 Ok(token) => {
43 if let Some(token) = token {
44 println!(
45 "✅ Received authentication token: {}...",
46 &token[..20.min(token.len())]
47 );
48 } else {
49 println!("ℹ️ Registry does not require authentication");
50 }
51 }
52 Err(e) => {
53 println!("❌ Authentication test failed: {}", e);
54 println!(" This is expected if the registry requires credentials");
55 }
56 }
57
58 // 测试有凭据的情况(如果提供)
59 if let (Ok(username), Ok(password)) = (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
60 {
61 println!();
62 println!("🔍 Test 2: Testing with provided credentials...");
63 println!(" Username: {}", username);
64
65 match auth
66 .authenticate_with_registry(
67 ®istry,
68 &repository,
69 Some(&username),
70 Some(&password),
71 &output,
72 )
73 .await
74 {
75 Ok(token) => {
76 if let Some(token) = token {
77 println!("✅ Successfully authenticated with credentials");
78 println!(" Token: {}...", &token[..50.min(token.len())]);
79
80 // 测试token是否能用于访问manifest
81 println!();
82 println!("🔍 Test 3: Testing token with manifest access...");
83 match client
84 .pull_manifest(&repository, "yoce", &Some(token))
85 .await
86 {
87 Ok(manifest) => {
88 println!("✅ Successfully pulled manifest using token");
89 println!(" Manifest size: {} bytes", manifest.len());
90 }
91 Err(e) => {
92 println!("❌ Failed to pull manifest with token: {}", e);
93 }
94 }
95 } else {
96 println!("ℹ️ Authentication successful but no token required");
97 }
98 }
99 Err(e) => {
100 println!("❌ Authentication with credentials failed: {}", e);
101 }
102 }
103 } else {
104 println!();
105 println!("ℹ️ No credentials provided via DOCKER_USERNAME/DOCKER_PASSWORD");
106 println!(" Set these environment variables to test credential-based authentication");
107 }
108
109 println!();
110 println!("🏁 Authentication test completed");
111
112 Ok(())
113}examples/push_to_aliyun_demo.rs (line 64)
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 47)
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 48)
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}examples/comprehensive_demo.rs (line 70)
56async fn run_comprehensive_demo(cache_dir: &str, target_registry: &str) -> Result<()> {
57 // =========================================================================
58 // 模式1: PullAndCache - 从registry拉取并缓存
59 // =========================================================================
60 println!("📥 MODE 1: Pull and Cache from Registry");
61 println!("========================================");
62
63 let pull_start = Instant::now();
64
65 // 创建ImageManager
66 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
67
68 // 构建registry client
69 let source_client = RegistryClientBuilder::new("https://registry-1.docker.io".to_string())
70 .with_timeout(3600)
71 .with_verbose(true)
72 .build()?;
73
74 // 定义拉取操作
75 let pull_mode = OperationMode::PullAndCache {
76 repository: "library/hello-world".to_string(),
77 reference: "latest".to_string(),
78 };
79
80 println!("🔄 Executing: {}", pull_mode.description());
81 image_manager
82 .execute_operation(&pull_mode, Some(&source_client), None)
83 .await?;
84
85 let pull_duration = pull_start.elapsed();
86 println!("✅ Mode 1 completed in {:.2}s", pull_duration.as_secs_f64());
87 println!();
88
89 // =========================================================================
90 // 模式2: ExtractAndCache - 从tar文件提取并缓存
91 // =========================================================================
92 println!("📦 MODE 2: Extract and Cache from Tar File");
93 println!("===========================================");
94
95 let extract_start = Instant::now();
96
97 // 创建示例tar文件
98 let tar_file = "nginx-demo.tar";
99 create_demo_tar_file(tar_file, "nginx:alpine").await?;
100
101 // 定义提取操作
102 let extract_mode = OperationMode::ExtractAndCache {
103 tar_file: tar_file.to_string(),
104 repository: "local/nginx".to_string(),
105 reference: "alpine".to_string(),
106 };
107
108 println!("🔄 Executing: {}", extract_mode.description());
109 image_manager
110 .execute_operation(&extract_mode, None, None)
111 .await?;
112
113 let extract_duration = extract_start.elapsed();
114 println!(
115 "✅ Mode 2 completed in {:.2}s",
116 extract_duration.as_secs_f64()
117 );
118 println!();
119
120 // =========================================================================
121 // 准备目标registry客户端
122 // =========================================================================
123 println!("🌐 Setting up target registry...");
124 ensure_target_registry_running(target_registry).await;
125
126 let target_client = RegistryClientBuilder::new(format!("http://{}", target_registry))
127 .with_timeout(3600)
128 .with_skip_tls(true)
129 .with_verbose(true)
130 .build()?;
131
132 // =========================================================================
133 // 模式3: PushFromCacheUsingManifest - 从缓存推送(manifest方式)
134 // =========================================================================
135 println!("🚀 MODE 3: Push from Cache using Manifest");
136 println!("==========================================");
137
138 let push_manifest_start = Instant::now();
139
140 // 定义推送操作(manifest方式)
141 let push_manifest_mode = OperationMode::PushFromCacheUsingManifest {
142 repository: "demo/hello-world-manifest".to_string(),
143 reference: "v1.0".to_string(),
144 };
145
146 println!("🔄 Executing: {}", push_manifest_mode.description());
147 image_manager
148 .execute_operation(&push_manifest_mode, Some(&target_client), None)
149 .await?;
150
151 let push_manifest_duration = push_manifest_start.elapsed();
152 println!(
153 "✅ Mode 3 completed in {:.2}s",
154 push_manifest_duration.as_secs_f64()
155 );
156 println!();
157
158 // =========================================================================
159 // 模式4: PushFromCacheUsingTar - 从缓存推送(tar引用方式)
160 // =========================================================================
161 println!("📦 MODE 4: Push from Cache using Tar Reference");
162 println!("===============================================");
163
164 let push_tar_start = Instant::now();
165
166 // 定义推送操作(tar引用方式)
167 let push_tar_mode = OperationMode::PushFromCacheUsingTar {
168 repository: "demo/nginx-tar-ref".to_string(),
169 reference: "alpine".to_string(),
170 };
171
172 println!("🔄 Executing: {}", push_tar_mode.description());
173 image_manager
174 .execute_operation(&push_tar_mode, Some(&target_client), None)
175 .await?;
176
177 let push_tar_duration = push_tar_start.elapsed();
178 println!(
179 "✅ Mode 4 completed in {:.2}s",
180 push_tar_duration.as_secs_f64()
181 );
182 println!();
183
184 // 验证所有推送结果
185 verify_all_pushes(&target_client, target_registry).await;
186
187 Ok(())
188}Additional examples can be found in:
Sourcepub fn with_skip_tls(self, skip_tls: bool) -> Self
pub fn with_skip_tls(self, skip_tls: bool) -> Self
Examples found in repository?
examples/push_to_aliyun_demo.rs (line 65)
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}More examples
examples/push_from_cache_manifest_demo.rs (line 48)
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 49)
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}examples/comprehensive_demo.rs (line 128)
56async fn run_comprehensive_demo(cache_dir: &str, target_registry: &str) -> Result<()> {
57 // =========================================================================
58 // 模式1: PullAndCache - 从registry拉取并缓存
59 // =========================================================================
60 println!("📥 MODE 1: Pull and Cache from Registry");
61 println!("========================================");
62
63 let pull_start = Instant::now();
64
65 // 创建ImageManager
66 let mut image_manager = ImageManager::new(Some(cache_dir), true)?;
67
68 // 构建registry client
69 let source_client = RegistryClientBuilder::new("https://registry-1.docker.io".to_string())
70 .with_timeout(3600)
71 .with_verbose(true)
72 .build()?;
73
74 // 定义拉取操作
75 let pull_mode = OperationMode::PullAndCache {
76 repository: "library/hello-world".to_string(),
77 reference: "latest".to_string(),
78 };
79
80 println!("🔄 Executing: {}", pull_mode.description());
81 image_manager
82 .execute_operation(&pull_mode, Some(&source_client), None)
83 .await?;
84
85 let pull_duration = pull_start.elapsed();
86 println!("✅ Mode 1 completed in {:.2}s", pull_duration.as_secs_f64());
87 println!();
88
89 // =========================================================================
90 // 模式2: ExtractAndCache - 从tar文件提取并缓存
91 // =========================================================================
92 println!("📦 MODE 2: Extract and Cache from Tar File");
93 println!("===========================================");
94
95 let extract_start = Instant::now();
96
97 // 创建示例tar文件
98 let tar_file = "nginx-demo.tar";
99 create_demo_tar_file(tar_file, "nginx:alpine").await?;
100
101 // 定义提取操作
102 let extract_mode = OperationMode::ExtractAndCache {
103 tar_file: tar_file.to_string(),
104 repository: "local/nginx".to_string(),
105 reference: "alpine".to_string(),
106 };
107
108 println!("🔄 Executing: {}", extract_mode.description());
109 image_manager
110 .execute_operation(&extract_mode, None, None)
111 .await?;
112
113 let extract_duration = extract_start.elapsed();
114 println!(
115 "✅ Mode 2 completed in {:.2}s",
116 extract_duration.as_secs_f64()
117 );
118 println!();
119
120 // =========================================================================
121 // 准备目标registry客户端
122 // =========================================================================
123 println!("🌐 Setting up target registry...");
124 ensure_target_registry_running(target_registry).await;
125
126 let target_client = RegistryClientBuilder::new(format!("http://{}", target_registry))
127 .with_timeout(3600)
128 .with_skip_tls(true)
129 .with_verbose(true)
130 .build()?;
131
132 // =========================================================================
133 // 模式3: PushFromCacheUsingManifest - 从缓存推送(manifest方式)
134 // =========================================================================
135 println!("🚀 MODE 3: Push from Cache using Manifest");
136 println!("==========================================");
137
138 let push_manifest_start = Instant::now();
139
140 // 定义推送操作(manifest方式)
141 let push_manifest_mode = OperationMode::PushFromCacheUsingManifest {
142 repository: "demo/hello-world-manifest".to_string(),
143 reference: "v1.0".to_string(),
144 };
145
146 println!("🔄 Executing: {}", push_manifest_mode.description());
147 image_manager
148 .execute_operation(&push_manifest_mode, Some(&target_client), None)
149 .await?;
150
151 let push_manifest_duration = push_manifest_start.elapsed();
152 println!(
153 "✅ Mode 3 completed in {:.2}s",
154 push_manifest_duration.as_secs_f64()
155 );
156 println!();
157
158 // =========================================================================
159 // 模式4: PushFromCacheUsingTar - 从缓存推送(tar引用方式)
160 // =========================================================================
161 println!("📦 MODE 4: Push from Cache using Tar Reference");
162 println!("===============================================");
163
164 let push_tar_start = Instant::now();
165
166 // 定义推送操作(tar引用方式)
167 let push_tar_mode = OperationMode::PushFromCacheUsingTar {
168 repository: "demo/nginx-tar-ref".to_string(),
169 reference: "alpine".to_string(),
170 };
171
172 println!("🔄 Executing: {}", push_tar_mode.description());
173 image_manager
174 .execute_operation(&push_tar_mode, Some(&target_client), None)
175 .await?;
176
177 let push_tar_duration = push_tar_start.elapsed();
178 println!(
179 "✅ Mode 4 completed in {:.2}s",
180 push_tar_duration.as_secs_f64()
181 );
182 println!();
183
184 // 验证所有推送结果
185 verify_all_pushes(&target_client, target_registry).await;
186
187 Ok(())
188}examples/check_aliyun_namespace.rs (line 32)
8async fn main() -> Result<()> {
9 println!("🔍 Aliyun Registry Namespace Checker");
10 println!("=====================================");
11
12 // 阿里云配置
13 let aliyun_registry = "registry.cn-beijing.aliyuncs.com";
14 let aliyun_username = "canny_best@163.com";
15 let aliyun_password = "ra201222";
16
17 // 要检查的namespace/repository
18 let namespace = "canny_best";
19 let repository = "canny_best/test-repo";
20
21 println!("📊 Configuration:");
22 println!(" Registry: {}", aliyun_registry);
23 println!(" Username: {}", aliyun_username);
24 println!(" Namespace: {}", namespace);
25 println!(" Repository: {}", repository);
26 println!();
27
28 // 构建客户端
29 println!("🌐 Building Registry Client...");
30 let client = RegistryClientBuilder::new(format!("https://{}", aliyun_registry))
31 .with_timeout(3600)
32 .with_skip_tls(false)
33 .with_verbose(true)
34 .build()?;
35
36 // 测试连接性
37 println!("🔗 Testing registry connectivity...");
38 match client.test_connectivity().await {
39 Ok(_) => println!("✅ Registry is accessible"),
40 Err(e) => {
41 println!("⚠️ Registry connectivity test failed: {}", e);
42 println!(" This may be normal for some registries");
43 }
44 }
45
46 // 认证
47 println!("🔐 Authenticating...");
48 let auth_config = AuthConfig::new(aliyun_username.to_string(), aliyun_password.to_string());
49
50 match client.authenticate(&auth_config).await {
51 Ok(Some(token)) => {
52 println!("✅ Authentication successful (token received)");
53
54 // 尝试访问repository
55 println!("📦 Checking repository access...");
56 match client.list_tags(repository, &Some(token.clone())).await {
57 Ok(tags) => {
58 println!("✅ Repository {} is accessible", repository);
59 println!("🏷️ Available tags: {:?}", tags);
60 }
61 Err(e) => {
62 println!("❌ Repository {} is not accessible: {}", repository, e);
63
64 println!("\n💡 To fix this issue:");
65 println!(" 1. Login to Aliyun Console: https://cr.console.aliyun.com/");
66 println!(" 2. Create namespace '{}' if it doesn't exist", namespace);
67 println!(
68 " 3. Create repository '{}' in the namespace",
69 repository.split('/').nth(1).unwrap_or("unknown")
70 );
71 println!(" 4. Ensure your account has push/pull permissions");
72
73 return Ok(());
74 }
75 }
76
77 // 尝试检查认证的repository访问
78 println!("🔐 Testing repository-specific authentication...");
79 match client
80 .authenticate_for_repository(&auth_config, repository)
81 .await
82 {
83 Ok(Some(repo_token)) => {
84 println!("✅ Repository-specific authentication successful");
85
86 // 检查一个不存在的镜像
87 println!("🔍 Testing image existence check...");
88 match client
89 .check_image_exists(repository, "non-existent-tag", &Some(repo_token))
90 .await
91 {
92 Ok(exists) => {
93 if exists {
94 println!("⚠️ Image unexpectedly exists");
95 } else {
96 println!(
97 "✅ Image existence check works (image doesn't exist as expected)"
98 );
99 }
100 }
101 Err(e) => {
102 println!("⚠️ Image existence check failed: {}", e);
103 }
104 }
105 }
106 Ok(None) => {
107 println!("✅ Repository-specific authentication successful (no token)");
108 }
109 Err(e) => {
110 println!("❌ Repository-specific authentication failed: {}", e);
111 }
112 }
113 }
114 Ok(None) => {
115 println!("✅ Authentication successful (no token required)");
116 }
117 Err(e) => {
118 println!("❌ Authentication failed: {}", e);
119 println!("\n💡 Please check:");
120 println!(" - Username: {}", aliyun_username);
121 println!(" - Password is correct");
122 println!(" - Account has access to Aliyun Container Registry");
123 return Ok(());
124 }
125 }
126
127 println!("\n🎉 All checks completed!");
128 println!(
129 " Repository {} appears to be ready for push operations",
130 repository
131 );
132
133 Ok(())
134}Sourcepub fn with_verbose(self, verbose: bool) -> Self
pub fn with_verbose(self, verbose: bool) -> Self
Examples found in repository?
examples/optimized_upload_demo.rs (line 38)
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/pull_and_cache_demo.rs (line 40)
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/auth_test_demo.rs (line 27)
9async fn main() -> Result<()> {
10 println!("🔐 Docker Registry API v2 Authentication Test");
11 println!("==============================================");
12
13 // 配置参数
14 let registry = env::var("DOCKER_REGISTRY")
15 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
16 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
17
18 println!("📋 Configuration:");
19 println!(" Registry: {}", registry);
20 println!(" Repository: {}", repository);
21 println!();
22
23 // 构建 Registry Client
24 println!("🌐 Building Registry Client...");
25 let client = RegistryClientBuilder::new(registry.clone())
26 .with_timeout(300)
27 .with_verbose(true)
28 .build()?;
29 println!("✅ Registry Client built successfully");
30
31 // 测试无凭据的情况
32 println!();
33 println!("🔍 Test 1: Testing registry authentication challenge...");
34 let auth = docker_image_pusher::registry::auth::Auth::new();
35 let output = docker_image_pusher::logging::Logger::new(true);
36
37 // 直接调用新的认证方法
38 match auth
39 .authenticate_with_registry(®istry, &repository, None, None, &output)
40 .await
41 {
42 Ok(token) => {
43 if let Some(token) = token {
44 println!(
45 "✅ Received authentication token: {}...",
46 &token[..20.min(token.len())]
47 );
48 } else {
49 println!("ℹ️ Registry does not require authentication");
50 }
51 }
52 Err(e) => {
53 println!("❌ Authentication test failed: {}", e);
54 println!(" This is expected if the registry requires credentials");
55 }
56 }
57
58 // 测试有凭据的情况(如果提供)
59 if let (Ok(username), Ok(password)) = (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
60 {
61 println!();
62 println!("🔍 Test 2: Testing with provided credentials...");
63 println!(" Username: {}", username);
64
65 match auth
66 .authenticate_with_registry(
67 ®istry,
68 &repository,
69 Some(&username),
70 Some(&password),
71 &output,
72 )
73 .await
74 {
75 Ok(token) => {
76 if let Some(token) = token {
77 println!("✅ Successfully authenticated with credentials");
78 println!(" Token: {}...", &token[..50.min(token.len())]);
79
80 // 测试token是否能用于访问manifest
81 println!();
82 println!("🔍 Test 3: Testing token with manifest access...");
83 match client
84 .pull_manifest(&repository, "yoce", &Some(token))
85 .await
86 {
87 Ok(manifest) => {
88 println!("✅ Successfully pulled manifest using token");
89 println!(" Manifest size: {} bytes", manifest.len());
90 }
91 Err(e) => {
92 println!("❌ Failed to pull manifest with token: {}", e);
93 }
94 }
95 } else {
96 println!("ℹ️ Authentication successful but no token required");
97 }
98 }
99 Err(e) => {
100 println!("❌ Authentication with credentials failed: {}", e);
101 }
102 }
103 } else {
104 println!();
105 println!("ℹ️ No credentials provided via DOCKER_USERNAME/DOCKER_PASSWORD");
106 println!(" Set these environment variables to test credential-based authentication");
107 }
108
109 println!();
110 println!("🏁 Authentication test completed");
111
112 Ok(())
113}examples/push_to_aliyun_demo.rs (line 66)
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 49)
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 50)
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 build(self) -> Result<RegistryClient>
pub fn build(self) -> Result<RegistryClient>
Examples found in repository?
examples/optimized_upload_demo.rs (line 39)
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/pull_and_cache_demo.rs (line 41)
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/auth_test_demo.rs (line 28)
9async fn main() -> Result<()> {
10 println!("🔐 Docker Registry API v2 Authentication Test");
11 println!("==============================================");
12
13 // 配置参数
14 let registry = env::var("DOCKER_REGISTRY")
15 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
16 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
17
18 println!("📋 Configuration:");
19 println!(" Registry: {}", registry);
20 println!(" Repository: {}", repository);
21 println!();
22
23 // 构建 Registry Client
24 println!("🌐 Building Registry Client...");
25 let client = RegistryClientBuilder::new(registry.clone())
26 .with_timeout(300)
27 .with_verbose(true)
28 .build()?;
29 println!("✅ Registry Client built successfully");
30
31 // 测试无凭据的情况
32 println!();
33 println!("🔍 Test 1: Testing registry authentication challenge...");
34 let auth = docker_image_pusher::registry::auth::Auth::new();
35 let output = docker_image_pusher::logging::Logger::new(true);
36
37 // 直接调用新的认证方法
38 match auth
39 .authenticate_with_registry(®istry, &repository, None, None, &output)
40 .await
41 {
42 Ok(token) => {
43 if let Some(token) = token {
44 println!(
45 "✅ Received authentication token: {}...",
46 &token[..20.min(token.len())]
47 );
48 } else {
49 println!("ℹ️ Registry does not require authentication");
50 }
51 }
52 Err(e) => {
53 println!("❌ Authentication test failed: {}", e);
54 println!(" This is expected if the registry requires credentials");
55 }
56 }
57
58 // 测试有凭据的情况(如果提供)
59 if let (Ok(username), Ok(password)) = (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
60 {
61 println!();
62 println!("🔍 Test 2: Testing with provided credentials...");
63 println!(" Username: {}", username);
64
65 match auth
66 .authenticate_with_registry(
67 ®istry,
68 &repository,
69 Some(&username),
70 Some(&password),
71 &output,
72 )
73 .await
74 {
75 Ok(token) => {
76 if let Some(token) = token {
77 println!("✅ Successfully authenticated with credentials");
78 println!(" Token: {}...", &token[..50.min(token.len())]);
79
80 // 测试token是否能用于访问manifest
81 println!();
82 println!("🔍 Test 3: Testing token with manifest access...");
83 match client
84 .pull_manifest(&repository, "yoce", &Some(token))
85 .await
86 {
87 Ok(manifest) => {
88 println!("✅ Successfully pulled manifest using token");
89 println!(" Manifest size: {} bytes", manifest.len());
90 }
91 Err(e) => {
92 println!("❌ Failed to pull manifest with token: {}", e);
93 }
94 }
95 } else {
96 println!("ℹ️ Authentication successful but no token required");
97 }
98 }
99 Err(e) => {
100 println!("❌ Authentication with credentials failed: {}", e);
101 }
102 }
103 } else {
104 println!();
105 println!("ℹ️ No credentials provided via DOCKER_USERNAME/DOCKER_PASSWORD");
106 println!(" Set these environment variables to test credential-based authentication");
107 }
108
109 println!();
110 println!("🏁 Authentication test completed");
111
112 Ok(())
113}examples/push_to_aliyun_demo.rs (line 67)
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 50)
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 51)
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:
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RegistryClientBuilder
impl RefUnwindSafe for RegistryClientBuilder
impl Send for RegistryClientBuilder
impl Sync for RegistryClientBuilder
impl Unpin for RegistryClientBuilder
impl UnwindSafe for RegistryClientBuilder
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