1#![allow(clippy::expect_fun_call)]
2mod docker;
3mod kubernetes;
4mod native;
5pub mod shared;
6
7use std::{
8 collections::HashMap,
9 net::IpAddr,
10 path::{Path, PathBuf},
11 sync::Arc,
12 time::Duration,
13};
14
15use async_trait::async_trait;
16use shared::{
17 constants::LOCALHOST,
18 types::{
19 ExecutionResult, GenerateFilesOptions, ProviderCapabilities, RunCommandOptions,
20 RunScriptOptions, SpawnNodeOptions,
21 },
22};
23use support::fs::FileSystemError;
24
25#[derive(Debug, thiserror::Error)]
26#[allow(missing_docs)]
27pub enum ProviderError {
28 #[error("Failed to create client '{0}': {1}")]
29 CreateClientFailed(String, anyhow::Error),
30
31 #[error("Failed to create namespace '{0}': {1}")]
32 CreateNamespaceFailed(String, anyhow::Error),
33
34 #[error("Failed to spawn node '{0}': {1}")]
35 NodeSpawningFailed(String, anyhow::Error),
36
37 #[error("Error running command '{0}' {1}: {2}")]
38 RunCommandError(String, String, anyhow::Error),
39
40 #[error("Error running script'{0}': {1}")]
41 RunScriptError(String, anyhow::Error),
42
43 #[error("Invalid network configuration field {0}")]
44 InvalidConfig(String),
45
46 #[error("Failed to retrieve node available args using image {0} and command {1}: {2}")]
47 NodeAvailableArgsError(String, String, String),
48
49 #[error("Can not recover node: {0}")]
50 MissingNode(String),
51
52 #[error("Can not recover node: {0} info, field: {1}")]
53 MissingNodeInfo(String, String),
54
55 #[error("Duplicated node name: {0}")]
56 DuplicatedNodeName(String),
57
58 #[error("File generation failed: {0}")]
59 FileGenerationFailed(anyhow::Error),
60
61 #[error(transparent)]
62 FileSystemError(#[from] FileSystemError),
63
64 #[error("Invalid script path for {0}")]
65 InvalidScriptPath(anyhow::Error),
66
67 #[error("Script with path {0} not found")]
68 ScriptNotFound(PathBuf),
69
70 #[error("Failed to retrieve process ID for node '{0}'")]
71 ProcessIdRetrievalFailed(String),
72
73 #[error("Failed to pause node '{0}': {1}")]
74 PauseNodeFailed(String, anyhow::Error),
75
76 #[error("Failed to resume node '{0}': {1}")]
77 ResumeNodeFailed(String, anyhow::Error),
78
79 #[error("Failed to kill node '{0}': {1}")]
80 KillNodeFailed(String, anyhow::Error),
81
82 #[error("Failed to restart node '{0}': {1}")]
83 RestartNodeFailed(String, anyhow::Error),
84
85 #[error("Failed to destroy node '{0}': {1}")]
86 DestroyNodeFailed(String, anyhow::Error),
87
88 #[error("Failed to get logs for node '{0}': {1}")]
89 GetLogsFailed(String, anyhow::Error),
90
91 #[error("Failed to dump logs for node '{0}': {1}")]
92 DumpLogsFailed(String, anyhow::Error),
93
94 #[error("Failed to copy file from node '{0}': {1}")]
95 CopyFileFromNodeError(String, anyhow::Error),
96
97 #[error("Failed to setup fileserver: {0}")]
98 FileServerSetupError(anyhow::Error),
99
100 #[error("Error uploading file: '{0}': {1}")]
101 UploadFile(String, anyhow::Error),
102
103 #[error("Error downloading file: '{0}': {1}")]
104 DownloadFile(String, anyhow::Error),
105
106 #[error("Error sending file '{0}' to {1}: {2}")]
107 SendFile(String, String, anyhow::Error),
108
109 #[error("Error creating port-forward '{0}:{1}': {2}")]
110 PortForwardError(u16, u16, anyhow::Error),
111
112 #[error("Failed to delete namespace '{0}': {1}")]
113 DeleteNamespaceFailed(String, anyhow::Error),
114}
115
116#[async_trait]
117pub trait Provider {
118 fn name(&self) -> &str;
119
120 fn capabilities(&self) -> &ProviderCapabilities;
121
122 async fn namespaces(&self) -> HashMap<String, DynNamespace>;
123
124 async fn create_namespace(&self) -> Result<DynNamespace, ProviderError>;
125
126 async fn create_namespace_with_base_dir(
127 &self,
128 base_dir: &Path,
129 ) -> Result<DynNamespace, ProviderError>;
130}
131
132pub type DynProvider = Arc<dyn Provider + Send + Sync>;
133
134#[async_trait]
135pub trait ProviderNamespace {
136 fn name(&self) -> &str;
137
138 fn base_dir(&self) -> &PathBuf;
139
140 fn capabilities(&self) -> &ProviderCapabilities;
141
142 async fn detach(&self) {
143 warn!("Detach is not implemented for {}", self.name());
145 }
146
147 async fn is_detached(&self) -> bool {
148 false
150 }
151
152 async fn nodes(&self) -> HashMap<String, DynNode>;
153
154 async fn get_node_available_args(
155 &self,
156 options: (String, Option<String>),
157 ) -> Result<String, ProviderError>;
158
159 async fn spawn_node(&self, options: &SpawnNodeOptions) -> Result<DynNode, ProviderError>;
160
161 async fn generate_files(&self, options: GenerateFilesOptions) -> Result<(), ProviderError>;
162
163 async fn destroy(&self) -> Result<(), ProviderError>;
164
165 async fn static_setup(&self) -> Result<(), ProviderError>;
166}
167
168pub type DynNamespace = Arc<dyn ProviderNamespace + Send + Sync>;
169
170#[async_trait]
171pub trait ProviderNode {
172 fn name(&self) -> &str;
173
174 fn args(&self) -> Vec<&str>;
175
176 fn base_dir(&self) -> &PathBuf;
177
178 fn config_dir(&self) -> &PathBuf;
179
180 fn data_dir(&self) -> &PathBuf;
181
182 fn relay_data_dir(&self) -> &PathBuf;
183
184 fn scripts_dir(&self) -> &PathBuf;
185
186 fn log_path(&self) -> &PathBuf;
187
188 fn log_cmd(&self) -> String;
189
190 fn path_in_node(&self, file: &Path) -> PathBuf;
193
194 async fn logs(&self) -> Result<String, ProviderError>;
195
196 async fn dump_logs(&self, local_dest: PathBuf) -> Result<(), ProviderError>;
197
198 async fn ip(&self) -> Result<IpAddr, ProviderError> {
200 Ok(LOCALHOST)
201 }
202
203 async fn create_port_forward(
205 &self,
206 _local_port: u16,
207 _remote_port: u16,
208 ) -> Result<Option<u16>, ProviderError> {
209 Ok(None)
210 }
211
212 async fn run_command(
213 &self,
214 options: RunCommandOptions,
215 ) -> Result<ExecutionResult, ProviderError>;
216
217 async fn run_script(&self, options: RunScriptOptions)
218 -> Result<ExecutionResult, ProviderError>;
219
220 async fn send_file(
221 &self,
222 local_file_path: &Path,
223 remote_file_path: &Path,
224 mode: &str,
225 ) -> Result<(), ProviderError>;
226
227 async fn receive_file(
228 &self,
229 remote_file_path: &Path,
230 local_file_path: &Path,
231 ) -> Result<(), ProviderError>;
232
233 async fn pause(&self) -> Result<(), ProviderError>;
234
235 async fn resume(&self) -> Result<(), ProviderError>;
236
237 async fn restart(&self, after: Option<Duration>) -> Result<(), ProviderError>;
238
239 async fn destroy(&self) -> Result<(), ProviderError>;
240}
241
242pub type DynNode = Arc<dyn ProviderNode + Send + Sync>;
243
244pub use docker::*;
246pub use kubernetes::*;
247pub use native::*;
248pub use shared::{constants, types};
249use tracing::warn;