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("File generation failed: {0}")]
56 FileGenerationFailed(anyhow::Error),
57
58 #[error(transparent)]
59 FileSystemError(#[from] FileSystemError),
60
61 #[error("Invalid script path for {0}")]
62 InvalidScriptPath(anyhow::Error),
63
64 #[error("Script with path {0} not found")]
65 ScriptNotFound(PathBuf),
66
67 #[error("Failed to retrieve process ID for node '{0}'")]
68 ProcessIdRetrievalFailed(String),
69
70 #[error("Failed to pause node '{0}': {1}")]
71 PauseNodeFailed(String, anyhow::Error),
72
73 #[error("Failed to resume node '{0}': {1}")]
74 ResumeNodeFailed(String, anyhow::Error),
75
76 #[error("Failed to kill node '{0}': {1}")]
77 KillNodeFailed(String, anyhow::Error),
78
79 #[error("Failed to restart node '{0}': {1}")]
80 RestartNodeFailed(String, anyhow::Error),
81
82 #[error("Failed to destroy node '{0}': {1}")]
83 DestroyNodeFailed(String, anyhow::Error),
84
85 #[error("Failed to get logs for node '{0}': {1}")]
86 GetLogsFailed(String, anyhow::Error),
87
88 #[error("Failed to dump logs for node '{0}': {1}")]
89 DumpLogsFailed(String, anyhow::Error),
90
91 #[error("Failed to copy file from node '{0}': {1}")]
92 CopyFileFromNodeError(String, anyhow::Error),
93
94 #[error("Failed to setup fileserver: {0}")]
95 FileServerSetupError(anyhow::Error),
96
97 #[error("Error uploading file: '{0}': {1}")]
98 UploadFile(String, anyhow::Error),
99
100 #[error("Error downloading file: '{0}': {1}")]
101 DownloadFile(String, anyhow::Error),
102
103 #[error("Error sending file '{0}' to {1}: {2}")]
104 SendFile(String, String, anyhow::Error),
105
106 #[error("Error creating port-forward '{0}:{1}': {2}")]
107 PortForwardError(u16, u16, anyhow::Error),
108
109 #[error("Failed to delete namespace '{0}': {1}")]
110 DeleteNamespaceFailed(String, anyhow::Error),
111}
112
113#[async_trait]
114pub trait Provider {
115 fn name(&self) -> &str;
116
117 fn capabilities(&self) -> &ProviderCapabilities;
118
119 async fn namespaces(&self) -> HashMap<String, DynNamespace>;
120
121 async fn create_namespace(&self) -> Result<DynNamespace, ProviderError>;
122
123 async fn create_namespace_with_base_dir(
124 &self,
125 base_dir: &Path,
126 ) -> Result<DynNamespace, ProviderError>;
127}
128
129pub type DynProvider = Arc<dyn Provider + Send + Sync>;
130
131#[async_trait]
132pub trait ProviderNamespace {
133 fn name(&self) -> &str;
134
135 fn base_dir(&self) -> &PathBuf;
136
137 fn capabilities(&self) -> &ProviderCapabilities;
138
139 async fn detach(&self) {
140 warn!("Detach is not implemented for {}", self.name());
142 }
143
144 async fn is_detached(&self) -> bool {
145 false
147 }
148
149 async fn nodes(&self) -> HashMap<String, DynNode>;
150
151 async fn get_node_available_args(
152 &self,
153 options: (String, Option<String>),
154 ) -> Result<String, ProviderError>;
155
156 async fn spawn_node(&self, options: &SpawnNodeOptions) -> Result<DynNode, ProviderError>;
157
158 async fn generate_files(&self, options: GenerateFilesOptions) -> Result<(), ProviderError>;
159
160 async fn destroy(&self) -> Result<(), ProviderError>;
161
162 async fn static_setup(&self) -> Result<(), ProviderError>;
163}
164
165pub type DynNamespace = Arc<dyn ProviderNamespace + Send + Sync>;
166
167#[async_trait]
168pub trait ProviderNode {
169 fn name(&self) -> &str;
170
171 fn args(&self) -> Vec<&str>;
172
173 fn base_dir(&self) -> &PathBuf;
174
175 fn config_dir(&self) -> &PathBuf;
176
177 fn data_dir(&self) -> &PathBuf;
178
179 fn relay_data_dir(&self) -> &PathBuf;
180
181 fn scripts_dir(&self) -> &PathBuf;
182
183 fn log_path(&self) -> &PathBuf;
184
185 fn log_cmd(&self) -> String;
186
187 fn path_in_node(&self, file: &Path) -> PathBuf;
190
191 async fn logs(&self) -> Result<String, ProviderError>;
192
193 async fn dump_logs(&self, local_dest: PathBuf) -> Result<(), ProviderError>;
194
195 async fn ip(&self) -> Result<IpAddr, ProviderError> {
197 Ok(LOCALHOST)
198 }
199
200 async fn create_port_forward(
202 &self,
203 _local_port: u16,
204 _remote_port: u16,
205 ) -> Result<Option<u16>, ProviderError> {
206 Ok(None)
207 }
208
209 async fn run_command(
210 &self,
211 options: RunCommandOptions,
212 ) -> Result<ExecutionResult, ProviderError>;
213
214 async fn run_script(&self, options: RunScriptOptions)
215 -> Result<ExecutionResult, ProviderError>;
216
217 async fn send_file(
218 &self,
219 local_file_path: &Path,
220 remote_file_path: &Path,
221 mode: &str,
222 ) -> Result<(), ProviderError>;
223
224 async fn receive_file(
225 &self,
226 remote_file_path: &Path,
227 local_file_path: &Path,
228 ) -> Result<(), ProviderError>;
229
230 async fn pause(&self) -> Result<(), ProviderError>;
231
232 async fn resume(&self) -> Result<(), ProviderError>;
233
234 async fn restart(&self, after: Option<Duration>) -> Result<(), ProviderError>;
235
236 async fn destroy(&self) -> Result<(), ProviderError>;
237}
238
239pub type DynNode = Arc<dyn ProviderNode + Send + Sync>;
240
241pub use docker::*;
243pub use kubernetes::*;
244pub use native::*;
245pub use shared::{constants, types};
246use tracing::warn;