1use std::any::Any;
2use std::collections::HashMap;
3use std::fmt::Debug;
4use std::net::SocketAddr;
5use std::sync::Arc;
6
7use anyhow::Result;
8use async_trait::async_trait;
9use hydro_deploy_integration::ServerBindConfig;
10use rust_crate::build::BuildOutput;
11use rust_crate::tracing_options::TracingOptions;
12use tokio::sync::{mpsc, oneshot};
13
14pub mod deployment;
15pub use deployment::Deployment;
16
17pub mod progress;
18
19pub mod localhost;
20pub use localhost::LocalhostHost;
21
22pub mod ssh;
23
24pub mod gcp;
25pub use gcp::GcpComputeEngineHost;
26
27pub mod azure;
28pub use azure::AzureHost;
29
30pub mod aws;
31pub use aws::{AwsEc2Host, AwsNetwork};
32
33pub mod rust_crate;
34pub use rust_crate::RustCrate;
35
36pub mod custom_service;
37pub use custom_service::CustomService;
38
39pub mod terraform;
40
41pub mod util;
42
43#[derive(Default)]
44pub struct ResourcePool {
45 pub terraform: terraform::TerraformPool,
46}
47
48pub struct ResourceBatch {
49 pub terraform: terraform::TerraformBatch,
50}
51
52impl ResourceBatch {
53 fn new() -> ResourceBatch {
54 ResourceBatch {
55 terraform: terraform::TerraformBatch::default(),
56 }
57 }
58
59 async fn provision(
60 self,
61 pool: &mut ResourcePool,
62 last_result: Option<Arc<ResourceResult>>,
63 ) -> Result<ResourceResult> {
64 Ok(ResourceResult {
65 terraform: self.terraform.provision(&mut pool.terraform).await?,
66 _last_result: last_result,
67 })
68 }
69}
70
71#[derive(Debug)]
72pub struct ResourceResult {
73 pub terraform: terraform::TerraformResult,
74 _last_result: Option<Arc<ResourceResult>>,
75}
76
77#[derive(Clone)]
78pub struct TracingResults {
79 pub folded_data: Vec<u8>,
80}
81
82#[async_trait]
83pub trait LaunchedBinary: Send + Sync {
84 fn stdin(&self) -> mpsc::UnboundedSender<String>;
85
86 fn deploy_stdout(&self) -> oneshot::Receiver<String>;
91
92 fn stdout(&self) -> mpsc::UnboundedReceiver<String>;
93 fn stderr(&self) -> mpsc::UnboundedReceiver<String>;
94 fn stdout_filter(&self, prefix: String) -> mpsc::UnboundedReceiver<String>;
95 fn stderr_filter(&self, prefix: String) -> mpsc::UnboundedReceiver<String>;
96
97 fn tracing_results(&self) -> Option<&TracingResults>;
98
99 fn exit_code(&self) -> Option<i32>;
100
101 async fn wait(&mut self) -> Result<i32>;
103 async fn stop(&mut self) -> Result<()>;
105}
106
107#[async_trait]
108pub trait LaunchedHost: Send + Sync {
109 fn base_server_config(&self, strategy: &BaseServerStrategy) -> ServerBindConfig;
112
113 fn server_config(&self, strategy: &ServerStrategy) -> ServerBindConfig {
114 match strategy {
115 ServerStrategy::Direct(b) => self.base_server_config(b),
116 ServerStrategy::Many(b) => {
117 ServerBindConfig::MultiConnection(Box::new(self.base_server_config(b)))
118 }
119 ServerStrategy::Demux(demux) => {
120 let mut config_map = HashMap::new();
121 for (key, underlying) in demux {
122 config_map.insert(*key, self.server_config(underlying));
123 }
124
125 ServerBindConfig::Demux(config_map)
126 }
127 ServerStrategy::Merge(merge) => {
128 let mut configs = vec![];
129 for underlying in merge {
130 configs.push(self.server_config(underlying));
131 }
132
133 ServerBindConfig::Merge(configs)
134 }
135 ServerStrategy::Tagged(underlying, id) => {
136 ServerBindConfig::Tagged(Box::new(self.server_config(underlying)), *id)
137 }
138 ServerStrategy::Null => ServerBindConfig::Null,
139 }
140 }
141
142 async fn copy_binary(&self, binary: &BuildOutput) -> Result<()>;
143
144 async fn launch_binary(
145 &self,
146 id: String,
147 binary: &BuildOutput,
148 args: &[String],
149 perf: Option<TracingOptions>,
150 ) -> Result<Box<dyn LaunchedBinary>>;
151
152 async fn forward_port(&self, addr: &SocketAddr) -> Result<SocketAddr>;
153}
154
155pub enum BaseServerStrategy {
156 UnixSocket,
157 InternalTcpPort(Option<u16>),
158 ExternalTcpPort(
159 u16,
161 ),
162}
163
164pub enum ServerStrategy {
166 Direct(BaseServerStrategy),
167 Many(BaseServerStrategy),
168 Demux(HashMap<u32, ServerStrategy>),
169 Merge(Vec<ServerStrategy>),
170 Tagged(Box<ServerStrategy>, u32),
171 Null,
172}
173
174pub enum ClientStrategy<'a> {
176 UnixSocket(
177 usize,
179 ),
180 InternalTcpPort(
181 &'a dyn Host,
183 ),
184 ForwardedTcpPort(
185 &'a dyn Host,
187 ),
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
191pub enum HostTargetType {
192 Local,
193 Linux(LinuxCompileType),
194}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
197pub enum LinuxCompileType {
198 Glibc,
199 Musl,
200}
201
202#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
203pub enum PortNetworkHint {
204 Auto,
205 TcpPort(Option<u16>),
206}
207
208pub type HostStrategyGetter = Box<dyn FnOnce(&dyn Any) -> BaseServerStrategy>;
209
210pub trait Host: Any + Send + Sync + Debug {
211 fn target_type(&self) -> HostTargetType;
212
213 fn request_port_base(&self, bind_type: &BaseServerStrategy);
214
215 fn request_port(&self, bind_type: &ServerStrategy) {
216 match bind_type {
217 ServerStrategy::Direct(base) => self.request_port_base(base),
218 ServerStrategy::Many(base) => self.request_port_base(base),
219 ServerStrategy::Demux(demux) => {
220 for bind_type in demux.values() {
221 self.request_port(bind_type);
222 }
223 }
224 ServerStrategy::Merge(merge) => {
225 for bind_type in merge {
226 self.request_port(bind_type);
227 }
228 }
229 ServerStrategy::Tagged(underlying, _) => {
230 self.request_port(underlying);
231 }
232 ServerStrategy::Null => {}
233 }
234 }
235
236 fn id(&self) -> usize;
238
239 fn request_custom_binary(&self);
241
242 fn collect_resources(&self, resource_batch: &mut ResourceBatch);
246
247 fn provision(&self, resource_result: &Arc<ResourceResult>) -> Arc<dyn LaunchedHost>;
251
252 fn launched(&self) -> Option<Arc<dyn LaunchedHost>>;
253
254 fn strategy_as_server<'a>(
257 &'a self,
258 connection_from: &dyn Host,
259 server_tcp_port_hint: PortNetworkHint,
260 ) -> Result<(ClientStrategy<'a>, HostStrategyGetter)>;
261
262 fn can_connect_to(&self, typ: ClientStrategy) -> bool;
264}
265
266#[async_trait]
267pub trait Service: Send + Sync {
268 fn collect_resources(&self, resource_batch: &mut ResourceBatch);
275
276 async fn deploy(&mut self, resource_result: &Arc<ResourceResult>) -> Result<()>;
278
279 async fn ready(&mut self) -> Result<()>;
282
283 async fn start(&mut self) -> Result<()>;
285
286 async fn stop(&mut self) -> Result<()>;
288}
289
290pub trait ServiceBuilder {
291 type Service: Service + 'static;
292 fn build(self, id: usize, on: Arc<dyn Host>) -> Self::Service;
293}
294
295impl<S: Service + 'static, T: FnOnce(usize) -> S> ServiceBuilder for T {
296 type Service = S;
297 fn build(self, id: usize, _on: Arc<dyn Host>) -> Self::Service {
298 self(id)
299 }
300}