1#![warn(missing_docs)]
2
3pub 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
17pub struct RustyDocker {
19 runtime: Arc<ContainerRuntime>,
21 config_manager: Arc<ConfigManager>,
23 config: Arc<DockerConfig>,
25}
26
27pub type Docker = RustyDocker;
29
30impl RustyDocker {
31 pub fn new() -> docker_types::Result<Self> {
33 let config_manager = Arc::new(ConfigManager::new()?);
35 let config = Arc::new(config_manager.get_config()?);
36
37 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 pub fn get_config(&self) -> Arc<DockerConfig> {
49 self.config.clone()
50 }
51
52 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 pub async fn start(&self) {
61 self.runtime.start().await;
62 }
63
64 pub fn get_runtime(&self) -> Arc<ContainerRuntime> {
66 self.runtime.clone()
67 }
68
69 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 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 pub async fn stop_container(&mut self, container_id: &str) -> docker_types::Result<()> {
89 self.runtime.stop_container(container_id).await
90 }
91
92 pub async fn remove_container(&mut self, container_id: &str) -> docker_types::Result<()> {
94 self.runtime.remove_container(container_id).await
95 }
96
97 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 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 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 pub fn delete_config(&self, config_id: &str) -> docker_types::Result<()> {
128 self.config_manager.delete_config(config_id)
129 }
130
131 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 pub fn list_configs(&self) -> docker_types::Result<Vec<ConfigInfo>> {
138 self.config_manager.list_configs()
139 }
140
141 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 pub fn delete_secret(&self, secret_id: &str) -> docker_types::Result<()> {
153 self.config_manager.delete_secret(secret_id)
154 }
155
156 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 pub fn list_secrets(&self) -> docker_types::Result<Vec<SecretInfo>> {
163 self.config_manager.list_secrets()
164 }
165}