Skip to main content

docker_config/
lib.rs

1#![warn(missing_docs)]
2
3//! Docker 核心运行时
4//!
5//! 包含容器运行时、命名空间管理、控制组管理等核心功能。
6
7pub mod config;
8pub mod runtime;
9
10use std::collections::HashMap;
11use std::sync::Arc;
12
13use config::ConfigManager;
14use docker_types::{ConfigInfo, DockerConfig, SecretInfo};
15use runtime::ContainerRuntime;
16
17/// Rusty Docker 服务
18pub struct RustyDocker {
19    /// 容器运行时
20    runtime: Arc<ContainerRuntime>,
21    /// 配置管理器
22    config_manager: Arc<ConfigManager>,
23    /// 全局配置
24    config: Arc<DockerConfig>,
25}
26
27/// Docker 服务别名
28pub type Docker = RustyDocker;
29
30impl RustyDocker {
31    /// 创建新的 Rusty Docker 服务
32    pub fn new() -> docker_types::Result<Self> {
33        // 初始化配置管理器
34        let config_manager = Arc::new(ConfigManager::new()?);
35        let config = Arc::new(config_manager.get_config()?);
36
37        // 使用配置初始化容器运行时
38        let runtime = Arc::new(ContainerRuntime::new_with_config(config.clone())?);
39
40        Ok(Self {
41            runtime,
42            config_manager,
43            config,
44        })
45    }
46
47    /// 获取全局配置
48    pub fn get_config(&self) -> Arc<DockerConfig> {
49        self.config.clone()
50    }
51
52    /// 重新加载配置
53    pub fn reload_config(&mut self) -> docker_types::Result<()> {
54        let new_config = self.config_manager.get_config()?;
55        self.config = Arc::new(new_config);
56        Ok(())
57    }
58
59    /// 启动服务
60    pub async fn start(&self) {
61        self.runtime.start().await;
62    }
63
64    /// 获取容器运行时
65    pub fn get_runtime(&self) -> Arc<ContainerRuntime> {
66        self.runtime.clone()
67    }
68
69    /// 运行容器
70    pub async fn run(
71        &mut self,
72        image: String,
73        name: Option<String>,
74        ports: Vec<String>,
75    ) -> docker_types::Result<docker_types::ContainerInfo> {
76        self.runtime.run_container(image, name, ports).await
77    }
78
79    /// 列出容器
80    pub async fn list_containers(
81        &self,
82        all: bool,
83    ) -> docker_types::Result<Vec<docker_types::ContainerInfo>> {
84        self.runtime.list_containers(all).await
85    }
86
87    /// 停止容器
88    pub async fn stop_container(&mut self, container_id: &str) -> docker_types::Result<()> {
89        self.runtime.stop_container(container_id).await
90    }
91
92    /// 删除容器
93    pub async fn remove_container(&mut self, container_id: &str) -> docker_types::Result<()> {
94        self.runtime.remove_container(container_id).await
95    }
96
97    /// 克隆 Docker 实例
98    pub fn clone(&self) -> Self {
99        Self {
100            runtime: self.runtime.clone(),
101            config_manager: self.config_manager.clone(),
102            config: self.config.clone(),
103        }
104    }
105
106    /// 创建配置
107    pub fn create_config(
108        &self,
109        name: &str,
110        data: &str,
111        labels: HashMap<String, String>,
112    ) -> docker_types::Result<ConfigInfo> {
113        self.config_manager.create_config(name, data, labels)
114    }
115
116    /// 更新配置
117    pub fn update_config(
118        &self,
119        config_id: &str,
120        data: &str,
121        labels: HashMap<String, String>,
122    ) -> docker_types::Result<ConfigInfo> {
123        self.config_manager.update_config(config_id, data, labels)
124    }
125
126    /// 删除配置
127    pub fn delete_config(&self, config_id: &str) -> docker_types::Result<()> {
128        self.config_manager.delete_config(config_id)
129    }
130
131    /// 获取配置详细信息
132    pub fn get_config_info(&self, config_id: &str) -> docker_types::Result<ConfigInfo> {
133        self.config_manager.get_config_info(config_id)
134    }
135
136    /// 列出所有配置
137    pub fn list_configs(&self) -> docker_types::Result<Vec<ConfigInfo>> {
138        self.config_manager.list_configs()
139    }
140
141    /// 创建密钥
142    pub fn create_secret(
143        &self,
144        name: &str,
145        data: &str,
146        labels: HashMap<String, String>,
147    ) -> docker_types::Result<SecretInfo> {
148        self.config_manager.create_secret(name, data, labels)
149    }
150
151    /// 删除密钥
152    pub fn delete_secret(&self, secret_id: &str) -> docker_types::Result<()> {
153        self.config_manager.delete_secret(secret_id)
154    }
155
156    /// 获取密钥详细信息
157    pub fn get_secret_info(&self, secret_id: &str) -> docker_types::Result<SecretInfo> {
158        self.config_manager.get_secret_info(secret_id)
159    }
160
161    /// 列出所有密钥
162    pub fn list_secrets(&self) -> docker_types::Result<Vec<SecretInfo>> {
163        self.config_manager.list_secrets()
164    }
165}