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 append_only_vec::AppendOnlyVec;
9use async_trait::async_trait;
10use hydro_deploy_integration::ServerBindConfig;
11use rust_crate::build::BuildOutput;
12use rust_crate::tracing_options::TracingOptions;
13use tokio::sync::{mpsc, oneshot};
14
15pub mod deployment;
16pub use deployment::Deployment;
17
18pub mod progress;
19
20pub mod localhost;
21pub use localhost::LocalhostHost;
22
23pub mod ssh;
24
25pub mod gcp;
26pub use gcp::GcpComputeEngineHost;
27
28pub mod azure;
29pub use azure::AzureHost;
30
31pub mod aws;
32pub use aws::{AwsEc2Host, AwsNetwork};
33
34pub mod rust_crate;
35pub use rust_crate::RustCrate;
36
37pub mod custom_service;
38pub use custom_service::CustomService;
39
40pub mod terraform;
41
42pub mod util;
43
44#[derive(Default)]
45pub struct ResourcePool {
46 pub terraform: terraform::TerraformPool,
47}
48
49pub struct ResourceBatch {
50 pub terraform: terraform::TerraformBatch,
51}
52
53impl ResourceBatch {
54 fn new() -> ResourceBatch {
55 ResourceBatch {
56 terraform: terraform::TerraformBatch::default(),
57 }
58 }
59
60 async fn provision(
61 self,
62 pool: &mut ResourcePool,
63 last_result: Option<Arc<ResourceResult>>,
64 ) -> Result<ResourceResult> {
65 Ok(ResourceResult {
66 terraform: self.terraform.provision(&mut pool.terraform).await?,
67 _last_result: last_result,
68 })
69 }
70}
71
72#[derive(Debug)]
73pub struct ResourceResult {
74 pub terraform: terraform::TerraformResult,
75 _last_result: Option<Arc<ResourceResult>>,
76}
77
78#[cfg(feature = "profile-folding")]
79#[derive(Clone, Debug)]
80pub struct TracingResults {
81 pub folded_data: Vec<u8>,
82}
83
84#[async_trait]
85pub trait LaunchedBinary: Send + Sync {
86 fn stdin(&self) -> mpsc::UnboundedSender<String>;
87
88 fn deploy_stdout(&self) -> oneshot::Receiver<String>;
93
94 fn stdout(&self) -> mpsc::UnboundedReceiver<String>;
95 fn stderr(&self) -> mpsc::UnboundedReceiver<String>;
96 fn stdout_filter(&self, prefix: String) -> mpsc::UnboundedReceiver<String>;
97 fn stderr_filter(&self, prefix: String) -> mpsc::UnboundedReceiver<String>;
98
99 #[cfg(feature = "profile-folding")]
100 fn tracing_results(&self) -> Option<&TracingResults>;
101
102 fn exit_code(&self) -> Option<i32>;
103
104 async fn wait(&self) -> Result<i32>;
106 async fn stop(&self) -> Result<()>;
108}
109
110#[async_trait]
111pub trait LaunchedHost: Send + Sync {
112 fn base_server_config(&self, strategy: &BaseServerStrategy) -> ServerBindConfig;
115
116 fn server_config(&self, strategy: &ServerStrategy) -> ServerBindConfig {
117 match strategy {
118 ServerStrategy::Direct(b) => self.base_server_config(b),
119 ServerStrategy::Many(b) => {
120 ServerBindConfig::MultiConnection(Box::new(self.base_server_config(b)))
121 }
122 ServerStrategy::Demux(demux) => ServerBindConfig::Demux(
123 demux
124 .iter()
125 .map(|(key, underlying)| (*key, self.server_config(underlying)))
126 .collect::<HashMap<_, _>>(),
127 ),
128 ServerStrategy::Merge(merge) => ServerBindConfig::Merge(
129 merge
130 .iter()
131 .map(|underlying| self.server_config(underlying))
132 .collect(),
133 ),
134 ServerStrategy::Tagged(underlying, id) => {
135 ServerBindConfig::Tagged(Box::new(self.server_config(underlying)), *id)
136 }
137 ServerStrategy::Null => ServerBindConfig::Null,
138 }
139 }
140
141 async fn copy_binary(&self, binary: &BuildOutput) -> Result<()>;
142
143 async fn launch_binary(
144 &self,
145 id: String,
146 binary: &BuildOutput,
147 args: &[String],
148 perf: Option<TracingOptions>,
149 ) -> Result<Box<dyn LaunchedBinary>>;
150
151 async fn forward_port(&self, addr: &SocketAddr) -> Result<SocketAddr>;
152}
153
154pub enum BaseServerStrategy {
155 UnixSocket,
156 InternalTcpPort(Option<u16>),
157 ExternalTcpPort(
158 u16,
160 ),
161}
162
163pub enum ServerStrategy {
165 Direct(BaseServerStrategy),
166 Many(BaseServerStrategy),
167 Demux(HashMap<u32, ServerStrategy>),
168 Merge(Box<AppendOnlyVec<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.iter() {
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(&self, resource_result: &Arc<ResourceResult>) -> Result<()>;
278
279 async fn ready(&self) -> Result<()>;
282
283 async fn start(&self) -> Result<()>;
285
286 async fn stop(&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, This: FnOnce(usize, Arc<dyn Host>) -> S> ServiceBuilder for This {
296 type Service = S;
297 fn build(self, id: usize, on: Arc<dyn Host>) -> Self::Service {
298 (self)(id, on)
299 }
300}