Skip to main content

docker_types/
lib.rs

1#![warn(missing_docs)]
2
3//! Docker 共享数据结构
4//!
5//! 包含 Docker 相关的所有数据结构定义,供其他组件使用。
6
7pub mod errors;
8
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11use std::time::SystemTime;
12
13/// 错误类型
14pub use crate::errors::*;
15
16/// 向后兼容:RustyDockerError 类型别名
17#[deprecated(note = "请使用 DockerError 替代")]
18pub type RustyDockerError = DockerError;
19
20/// 结果类型别名
21pub type Result<T> = std::result::Result<T, DockerError>;
22
23/// 容器状态
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub enum ContainerStatus {
26    /// 创建中
27    Creating,
28    /// 运行中
29    Running,
30    /// 已停止
31    Stopped,
32    /// 已暂停
33    Paused,
34    /// 错误
35    Error(String),
36}
37
38/// 资源限制
39#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
40pub struct ResourceLimits {
41    /// CPU 限制(核心数)
42    pub cpu_limit: f64,
43    /// 内存限制(MB)
44    pub memory_limit: u32,
45    /// 存储限制(GB)
46    pub storage_limit: u32,
47    /// 网络带宽限制(MB/s)
48    pub network_limit: u32,
49}
50
51/// 重启策略
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub enum RestartPolicy {
54    /// 无重启策略
55    None,
56    /// 总是重启
57    Always,
58    /// 失败时重启
59    OnFailure(u32),
60    /// 除非被手动停止,否则总是重启
61    UnlessStopped,
62}
63
64/// 健康检查配置
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct HealthCheck {
67    /// 健康检查命令
68    pub test: Vec<String>,
69    /// 健康检查间隔时间(秒)
70    pub interval: u32,
71    /// 健康检查超时时间(秒)
72    pub timeout: u32,
73    /// 健康检查失败重试次数
74    pub retries: u32,
75    /// 健康检查启动超时时间(秒)
76    pub start_period: u32,
77}
78
79/// 部署配置
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct DeployConfig {
82    /// 复制数量
83    pub replicas: u32,
84    /// 资源限制
85    pub resources: ResourceLimits,
86    /// 重启策略
87    pub restart_policy: RestartPolicy,
88    /// 滚动更新配置
89    pub update_config: Option<UpdateConfig>,
90}
91
92/// 更新配置
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct UpdateConfig {
95    /// 并行更新数量
96    pub parallelism: u32,
97    /// 失败后是否继续
98    pub failure_action: String,
99    /// 监控更新的时间(秒)
100    pub monitor: u32,
101    /// 最大失败比例
102    pub max_failure_ratio: f64,
103    /// 排序方法
104    pub order: String,
105}
106
107/// 容器配置
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct ContainerConfig {
110    /// 容器名称
111    pub name: String,
112    /// 镜像名称
113    pub image: String,
114    /// 命令
115    pub command: Vec<String>,
116    /// 环境变量
117    pub environment: HashMap<String, String>,
118    /// 端口映射
119    pub ports: HashMap<u16, u16>,
120    /// 挂载卷
121    pub volumes: Vec<VolumeMount>,
122    /// 资源限制
123    pub resources: ResourceLimits,
124    /// 网络配置
125    pub network: NetworkConfig,
126    /// 重启策略
127    pub restart_policy: Option<RestartPolicy>,
128    /// 健康检查配置
129    pub healthcheck: Option<HealthCheck>,
130    /// 部署配置
131    pub deploy: Option<DeployConfig>,
132}
133
134/// 挂载类型
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub enum MountType {
137    /// 绑定挂载
138    Bind,
139    /// 卷
140    Volume,
141    /// tmpfs
142    Tmpfs,
143}
144
145/// 绑定挂载一致性选项
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub enum Consistency {
148    /// 默认一致性
149    Default,
150    /// 一致
151    Consistent,
152    /// 委托
153    Delegated,
154    /// 缓存
155    Cached,
156}
157
158/// 卷挂载
159#[derive(Debug, Clone, Serialize, Deserialize)]
160pub struct VolumeMount {
161    /// 挂载类型
162    pub mount_type: MountType,
163    /// 主机路径(对于绑定挂载)
164    pub host_path: Option<String>,
165    /// 卷名称(对于卷挂载)
166    pub volume_name: Option<String>,
167    /// 容器路径
168    pub container_path: String,
169    /// 读写模式
170    pub read_only: bool,
171    /// 驱动名称(对于卷挂载)
172    pub driver: Option<String>,
173    /// 卷标签(对于卷挂载)
174    pub labels: Option<std::collections::HashMap<String, String>>,
175    /// 绑定挂载一致性选项
176    pub consistency: Option<Consistency>,
177    /// tmpfs 大小(对于 tmpfs 挂载)
178    pub tmpfs_size: Option<u64>,
179    /// tmpfs 模式(对于 tmpfs 挂载)
180    pub tmpfs_mode: Option<u32>,
181}
182
183/// 网络配置
184#[derive(Debug, Clone, Serialize, Deserialize)]
185pub struct NetworkConfig {
186    /// 网络名称
187    pub network_name: String,
188    /// 静态 IP
189    pub static_ip: Option<String>,
190    /// 主机名
191    pub hostname: Option<String>,
192    /// 网络别名
193    pub aliases: Option<Vec<String>>,
194    /// 网络模式
195    pub network_mode: Option<String>,
196    /// 是否启用 IPv6
197    pub enable_ipv6: bool,
198}
199
200/// 容器信息
201#[derive(Debug, Clone, Serialize, Deserialize)]
202pub struct ContainerInfo {
203    /// 容器 ID
204    pub id: String,
205    /// 容器名称
206    pub name: String,
207    /// 镜像名称
208    pub image: String,
209    /// 状态
210    pub status: ContainerStatus,
211    /// 配置
212    pub config: ContainerConfig,
213    /// 创建时间
214    pub created_at: SystemTime,
215    /// 启动时间
216    pub started_at: Option<SystemTime>,
217    /// 停止时间
218    pub stopped_at: Option<SystemTime>,
219    /// 进程 ID
220    pub pid: Option<u32>,
221    /// 网络信息
222    pub network_info: NetworkInfo,
223}
224
225/// 网络信息
226#[derive(Debug, Clone, Serialize, Deserialize)]
227pub struct NetworkInfo {
228    /// IP 地址
229    pub ip_address: Option<String>,
230    /// 端口映射
231    pub ports: HashMap<u16, u16>,
232    /// 网络名称
233    pub network_name: String,
234}
235
236/// 镜像信息
237#[derive(Debug, Clone, Serialize, Deserialize)]
238pub struct ImageInfo {
239    /// 镜像 ID
240    pub id: String,
241    /// 镜像名称
242    pub name: String,
243    /// 标签
244    pub tags: Vec<String>,
245    /// 大小
246    pub size: u64,
247    /// 创建时间
248    pub created_at: SystemTime,
249    /// 架构
250    pub architecture: String,
251    /// 操作系统
252    pub os: String,
253}
254
255/// 网络配置信息
256#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct NetworkConfigInfo {
258    /// 网络名称
259    pub name: String,
260    /// 网络类型
261    pub network_type: String,
262    /// 子网
263    pub subnet: String,
264    /// 网关
265    pub gateway: String,
266    /// 容器列表
267    pub containers: Vec<String>,
268}
269
270/// 全局配置
271#[derive(Debug, Clone, Serialize, Deserialize)]
272pub struct DockerConfig {
273    /// 数据目录
274    pub data_dir: String,
275    /// 镜像存储目录
276    pub image_dir: String,
277    /// 容器存储目录
278    pub container_dir: String,
279    /// 网络配置目录
280    pub network_dir: String,
281    /// 默认网络名称
282    pub default_network: String,
283    /// 默认资源限制
284    pub default_resources: ResourceLimits,
285    /// 日志配置
286    pub log_config: LogConfig,
287}
288
289/// 日志配置
290#[derive(Debug, Clone, Serialize, Deserialize)]
291pub struct LogConfig {
292    /// 日志级别
293    pub log_level: String,
294    /// 日志文件路径
295    pub log_file: String,
296    /// 日志大小限制(MB)
297    pub max_log_size: u32,
298}
299
300/// 卷信息
301#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct VolumeInfo {
303    /// 卷 ID
304    pub id: String,
305    /// 卷名称
306    pub name: String,
307    /// 卷大小(字节)
308    pub size: u64,
309    /// 创建时间
310    pub created_at: SystemTime,
311    /// 挂载点
312    pub mount_point: String,
313    /// 驱动
314    pub driver: String,
315    /// 标签
316    pub labels: HashMap<String, String>,
317    /// 被使用的容器
318    pub used_by: Vec<String>,
319}
320
321/// 系统资源使用情况
322#[derive(Debug, Clone, Serialize, Deserialize)]
323pub struct SystemResourceUsage {
324    /// CPU 使用率(百分比)
325    pub cpu_usage: f64,
326    /// 内存使用量(MB)
327    pub memory_used: u32,
328    /// 内存总量(MB)
329    pub memory_total: u32,
330    /// 存储使用量(GB)
331    pub storage_used: u32,
332    /// 存储总量(GB)
333    pub storage_total: u32,
334    /// 网络发送量(MB)
335    pub network_sent: u32,
336    /// 网络接收量(MB)
337    pub network_received: u32,
338}
339
340/// 系统信息
341#[derive(Debug, Clone, Serialize, Deserialize)]
342pub struct SystemInfo {
343    /// 操作系统类型
344    pub os_type: String,
345    /// 操作系统版本
346    pub os_version: String,
347    /// 内核版本
348    pub kernel_version: String,
349    /// 架构
350    pub architecture: String,
351    /// 主机名
352    pub hostname: String,
353    /// 处理器核心数
354    pub cpu_cores: u32,
355    /// 总内存(MB)
356    pub total_memory: u32,
357}
358
359/// Docker 守护进程状态
360#[derive(Debug, Clone, Serialize, Deserialize)]
361pub enum DockerDaemonStatus {
362    /// 运行中
363    Running,
364    /// 停止
365    Stopped,
366    /// 错误
367    Error(String),
368}
369
370/// 系统状态
371#[derive(Debug, Clone, Serialize, Deserialize)]
372pub struct SystemStatus {
373    /// Docker 守护进程状态
374    pub daemon_status: DockerDaemonStatus,
375    /// 系统资源使用情况
376    pub resource_usage: SystemResourceUsage,
377    /// 系统信息
378    pub system_info: SystemInfo,
379    /// 容器统计信息
380    pub container_stats: ContainerStats,
381}
382
383/// 容器统计信息
384#[derive(Debug, Clone, Serialize, Deserialize)]
385pub struct ContainerStats {
386    /// 运行中的容器数
387    pub running: u32,
388    /// 已停止的容器数
389    pub stopped: u32,
390    /// 总容器数
391    pub total: u32,
392}
393
394/// Swarm 服务状态
395#[derive(Debug, Clone, Serialize, Deserialize)]
396pub enum ServiceStatus {
397    /// 已创建
398    Created,
399    /// 运行中
400    Running,
401    /// 更新中
402    Updating,
403    /// 错误
404    Error(String),
405}
406
407/// Swarm 服务信息
408#[derive(Debug, Clone, Serialize, Deserialize)]
409pub struct ServiceInfo {
410    /// 服务 ID
411    pub id: String,
412    /// 服务名称
413    pub name: String,
414    /// 服务状态
415    pub status: ServiceStatus,
416    /// 镜像名称
417    pub image: String,
418    /// 副本数
419    pub replicas: u32,
420    /// 端口映射
421    pub ports: HashMap<u16, u16>,
422    /// 环境变量
423    pub environment: HashMap<String, String>,
424    /// 挂载卷
425    pub volumes: Vec<VolumeMount>,
426    /// 创建时间
427    pub created_at: SystemTime,
428    /// 更新时间
429    pub updated_at: SystemTime,
430}
431
432/// Swarm 节点角色
433#[derive(Debug, Clone, Serialize, Deserialize)]
434pub enum NodeRole {
435    /// 管理节点
436    Manager,
437    /// 工作节点
438    Worker,
439}
440
441/// Swarm 节点可用性
442#[derive(Debug, Clone, Serialize, Deserialize)]
443pub enum NodeAvailability {
444    /// 活跃
445    Active,
446    /// 暂停
447    Pause,
448    /// 排空
449    Drain,
450}
451
452/// Swarm 节点状态
453#[derive(Debug, Clone, Serialize, Deserialize)]
454pub enum NodeStatus {
455    /// 就绪
456    Ready,
457    /// 不可用
458    Down,
459    /// 未知
460    Unknown,
461}
462
463/// Swarm 节点信息
464#[derive(Debug, Clone, Serialize, Deserialize)]
465pub struct NodeInfo {
466    /// 节点 ID
467    pub id: String,
468    /// 节点名称
469    pub name: String,
470    /// 节点角色
471    pub role: NodeRole,
472    /// 节点可用性
473    pub availability: NodeAvailability,
474    /// 节点状态
475    pub status: NodeStatus,
476    /// 节点地址
477    pub address: String,
478    /// 节点版本
479    pub version: String,
480    /// 运行的容器数
481    pub containers_running: u32,
482    /// 节点标签
483    pub labels: HashMap<String, String>,
484}
485
486/// Swarm 集群信息
487#[derive(Debug, Clone, Serialize, Deserialize)]
488pub struct SwarmInfo {
489    /// 集群 ID
490    pub id: String,
491    /// 集群名称
492    pub name: Option<String>,
493    /// 管理节点数量
494    pub managers: u32,
495    /// 工作节点数量
496    pub workers: u32,
497    /// 服务数量
498    pub services: u32,
499    /// 任务数量
500    pub tasks: u32,
501    /// 集群版本
502    pub version: String,
503    /// 集群创建时间
504    pub created_at: SystemTime,
505}
506
507/// 配置信息
508#[derive(Debug, Clone, Serialize, Deserialize)]
509pub struct ConfigInfo {
510    /// 配置 ID
511    pub id: String,
512    /// 配置名称
513    pub name: String,
514    /// 配置数据
515    pub data: String,
516    /// 创建时间
517    pub created_at: SystemTime,
518    /// 标签
519    pub labels: HashMap<String, String>,
520}
521
522/// 密钥信息
523#[derive(Debug, Clone, Serialize, Deserialize)]
524pub struct SecretInfo {
525    /// 密钥 ID
526    pub id: String,
527    /// 密钥名称
528    pub name: String,
529    /// 创建时间
530    pub created_at: SystemTime,
531    /// 标签
532    pub labels: HashMap<String, String>,
533    /// 密钥摘要(用于验证密钥完整性)
534    pub digest: String,
535}
536
537/// 端点类型
538#[derive(Debug, Clone, Serialize, Deserialize)]
539pub enum EndpointType {
540    /// 本地 Docker 环境
541    Local,
542    /// 远程 Docker 环境
543    Remote,
544    /// 云 Docker 环境
545    Cloud,
546}
547
548/// 端点状态
549#[derive(Debug, Clone, Serialize, Deserialize)]
550pub enum EndpointStatus {
551    /// 已连接
552    Connected,
553    /// 连接中
554    Connecting,
555    /// 连接失败
556    Failed(String),
557    /// 未连接
558    Disconnected,
559}
560
561/// 端点配置
562#[derive(Debug, Clone, Serialize, Deserialize)]
563pub struct EndpointConfig {
564    /// 端点 ID
565    pub id: String,
566    /// 端点名称
567    pub name: String,
568    /// 端点类型
569    pub endpoint_type: EndpointType,
570    /// 端点 URL
571    pub url: String,
572    /// 是否使用 TLS
573    pub use_tls: bool,
574    /// TLS 证书路径(可选)
575    pub tls_cert_path: Option<String>,
576    /// TLS 密钥路径(可选)
577    pub tls_key_path: Option<String>,
578    /// TLS CA 证书路径(可选)
579    pub tls_ca_path: Option<String>,
580    /// 认证令牌(可选)
581    pub auth_token: Option<String>,
582    /// 标签
583    pub labels: HashMap<String, String>,
584}
585
586/// 端点信息
587#[derive(Debug, Clone, Serialize, Deserialize)]
588pub struct EndpointInfo {
589    /// 端点配置
590    pub config: EndpointConfig,
591    /// 端点状态
592    pub status: EndpointStatus,
593    /// 创建时间
594    pub created_at: SystemTime,
595    /// 最后连接时间
596    pub last_connected_at: Option<SystemTime>,
597    /// 连接信息
598    pub connection_info: Option<SystemInfo>,
599}