Skip to main content

docker_registry/
lib.rs

1#![warn(missing_docs)]
2
3//! Docker Registry 镜像管理库
4//!
5//! 支持从多种镜像仓库拉取和推送镜像,实现并行下载和续点续传功能。
6//!
7//! ## 主要功能
8//! - 支持多种镜像仓库类型(Docker Hub、私有仓库、GCR、ECR、ACR)
9//! - 并行下载镜像层,提高下载速度
10//! - 支持续点续传,避免网络中断导致的重复下载
11//! - 提供下载进度查询
12
13pub mod client;
14pub mod downloader;
15pub mod types;
16
17use std::sync::Arc;
18
19use client::DockerHubClient;
20use docker_types::{ImageInfo, Result};
21use downloader::ImageDownloader;
22
23/// 镜像仓库类型
24///
25/// 定义了支持的镜像仓库类型,用于创建相应的仓库客户端。
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum RegistryType {
28    /// Docker Hub 官方仓库
29    DockerHub,
30    /// 私有镜像仓库
31    Private,
32    /// Google Container Registry
33    GCR,
34    /// Amazon Elastic Container Registry
35    ECR,
36    /// Azure Container Registry
37    ACR,
38}
39
40/// 镜像仓库服务
41///
42/// 提供镜像的拉取和推送功能,支持多种仓库类型。
43pub struct RegistryService {
44    /// 仓库客户端,用于与镜像仓库 API 交互
45    client: Arc<DockerHubClient>,
46    /// 镜像下载器,用于并行下载镜像层
47    downloader: Arc<ImageDownloader>,
48    /// 仓库类型,标识当前服务使用的仓库类型
49    registry_type: RegistryType,
50}
51
52impl RegistryService {
53    /// 创建新的仓库服务
54    ///
55    /// # 参数
56    /// - `registry_type`: 仓库类型
57    /// - `endpoint`: 可选的仓库端点 URL
58    ///
59    /// # 返回
60    /// - `Result<Self>`: 成功返回服务实例,失败返回错误
61    pub fn new(registry_type: RegistryType, endpoint: Option<&str>) -> Result<Self> {
62        let client = match registry_type {
63            RegistryType::DockerHub => Arc::new(DockerHubClient::new()?),
64            _ => {
65                // 对于其他类型的仓库,这里可以实现相应的客户端
66                Arc::new(DockerHubClient::new()?)
67            }
68        };
69
70        let downloader = Arc::new(ImageDownloader::new(client.clone())?);
71
72        Ok(Self {
73            client,
74            downloader,
75            registry_type,
76        })
77    }
78
79    /// 从 Docker Hub 创建服务(向后兼容)
80    ///
81    /// # 返回
82    /// - `Result<Self>`: 成功返回 Docker Hub 服务实例,失败返回错误
83    pub fn new_docker_hub() -> Result<Self> {
84        Self::new(RegistryType::DockerHub, None)
85    }
86
87    /// 拉取镜像
88    ///
89    /// # 参数
90    /// - `image`: 镜像名称
91    /// - `tag`: 镜像标签
92    ///
93    /// # 返回
94    /// - `Result<ImageInfo>`: 成功返回镜像信息,失败返回错误
95    pub async fn pull_image(&self, image: &str, tag: &str) -> Result<ImageInfo> {
96        self.downloader.download_image(image, tag).await
97    }
98
99    /// 推送镜像
100    ///
101    /// # 参数
102    /// - `image`: 镜像名称
103    /// - `tag`: 镜像标签
104    ///
105    /// # 返回
106    /// - `Result<ImageInfo>`: 成功返回镜像信息,失败返回错误
107    pub async fn push_image(&self, image: &str, tag: &str) -> Result<ImageInfo> {
108        // 模拟推送镜像
109        Ok(ImageInfo {
110            id: uuid::Uuid::new_v4().to_string(),
111            name: format!("{}", image),
112            tags: vec![format!("{}:{}", image, tag)],
113            size: 1024 * 1024 * 100, // 100MB
114            created_at: std::time::SystemTime::now(),
115            architecture: "amd64".to_string(),
116            os: "linux".to_string(),
117        })
118    }
119
120    /// 获取仓库客户端
121    ///
122    /// # 返回
123    /// - `Arc<DockerHubClient>`: 仓库客户端实例
124    pub fn get_client(&self) -> Arc<DockerHubClient> {
125        self.client.clone()
126    }
127
128    /// 获取镜像下载器
129    ///
130    /// # 返回
131    /// - `Arc<ImageDownloader>`: 镜像下载器实例
132    pub fn get_downloader(&self) -> Arc<ImageDownloader> {
133        self.downloader.clone()
134    }
135
136    /// 获取仓库类型
137    ///
138    /// # 返回
139    /// - `RegistryType`: 当前服务的仓库类型
140    pub fn get_registry_type(&self) -> RegistryType {
141        self.registry_type.clone()
142    }
143}
144
145/// Docker Hub 服务(向后兼容)
146pub type DockerHubService = RegistryService;
147
148/// 创建 Docker Hub 服务(向后兼容)
149///
150/// # 返回
151/// - `Result<DockerHubService>`: 成功返回 Docker Hub 服务实例,失败返回错误
152pub fn new_docker_hub_service() -> Result<DockerHubService> {
153    RegistryService::new_docker_hub()
154}