Skip to content

Commit 7e15545

Browse files
committed
feat: Add services to machine-config
1 parent 6c88a91 commit 7e15545

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

fly-http/src/machine.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub(crate) mod ser {
2020
use crate::activity_flyio::fly_http::regions::Region;
2121
use crate::exports::activity_flyio::fly_http::machines::{
2222
CpuKind, ExecResponse, GuestConfig, HostStatus, InitConfig, Machine, MachineConfig,
23-
MachineRestart, Mount, RestartPolicy, StopConfig,
23+
MachineRestart, Mount, PortConfig, PortHandler, RestartPolicy, ServiceConfig,
24+
ServiceProtocol, StopConfig,
2425
};
2526
use serde::de::DeserializeOwned;
2627
use serde::{Deserialize, Serialize};
@@ -78,6 +79,7 @@ pub(crate) mod ser {
7879
restart: Option<MachineRestartSer>,
7980
stop_config: Option<StopConfig>,
8081
mounts: Option<Vec<Mount>>,
82+
services: Option<Vec<ServiceConfigSer>>,
8183
}
8284

8385
#[derive(Serialize, Deserialize, Debug)]
@@ -161,6 +163,53 @@ pub(crate) mod ser {
161163
path: String,
162164
}
163165

166+
#[derive(Debug, Serialize, Deserialize)]
167+
pub(crate) struct PortConfigSer {
168+
port: u16,
169+
handlers: Vec<ToLowerWrapper<PortHandler>>,
170+
}
171+
impl From<PortConfig> for PortConfigSer {
172+
fn from(wit: PortConfig) -> Self {
173+
PortConfigSer {
174+
port: wit.port,
175+
handlers: wit.handlers.into_iter().map(ToLowerWrapper).collect(),
176+
}
177+
}
178+
}
179+
impl From<PortConfigSer> for PortConfig {
180+
fn from(ser: PortConfigSer) -> PortConfig {
181+
PortConfig {
182+
port: ser.port,
183+
handlers: ser.handlers.into_iter().map(|wrapper| wrapper.0).collect(),
184+
}
185+
}
186+
}
187+
188+
#[derive(Debug, Serialize, Deserialize)]
189+
pub(crate) struct ServiceConfigSer {
190+
internal_port: u16,
191+
protocol: ToLowerWrapper<ServiceProtocol>,
192+
ports: Vec<PortConfigSer>,
193+
}
194+
impl From<ServiceConfig> for ServiceConfigSer {
195+
fn from(wit: ServiceConfig) -> Self {
196+
ServiceConfigSer {
197+
internal_port: wit.internal_port,
198+
protocol: ToLowerWrapper(wit.protocol),
199+
ports: wit.ports.into_iter().map(PortConfigSer::from).collect(),
200+
}
201+
}
202+
}
203+
impl From<ServiceConfigSer> for ServiceConfig {
204+
fn from(ser: ServiceConfigSer) -> ServiceConfig {
205+
ServiceConfig {
206+
internal_port: ser.internal_port,
207+
protocol: ser.protocol.0,
208+
ports: ser.ports.into_iter().map(PortConfig::from).collect(),
209+
}
210+
}
211+
}
212+
164213
#[derive(Deserialize)]
165214
pub(crate) struct MachineCreateResponseSer {
166215
pub(crate) id: String,
@@ -205,7 +254,8 @@ pub(crate) mod ser {
205254

206255
use std::fmt::Debug;
207256
#[derive(derive_more::Debug)]
208-
#[debug("{_0:?}")]
257+
#[debug("{_0:?}")] // Transparent debug
258+
// FIXME: Remove once wit-bindgen supports path-specific derives
209259
pub(crate) struct ToLowerWrapper<T: Debug + Serialize + DeserializeOwned>(pub(crate) T);
210260

211261
impl<T: Debug + Serialize + DeserializeOwned> Serialize for ToLowerWrapper<T> {
@@ -302,6 +352,9 @@ pub(crate) mod ser {
302352
init,
303353
stop_config: wit.stop_config,
304354
mounts: wit.mounts,
355+
services: wit
356+
.services
357+
.map(|vec| vec.into_iter().map(ServiceConfigSer::from).collect()),
305358
}
306359
}
307360
}
@@ -350,6 +403,9 @@ pub(crate) mod ser {
350403
init,
351404
stop_config,
352405
mounts: ser.mounts,
406+
services: ser
407+
.services
408+
.map(|vec| vec.into_iter().map(ServiceConfig::from).collect()),
353409
}
354410
}
355411
}

fly-http/wit/activity-flyio_fly-http@1.0.0-beta/fly.wit

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ interface apps {
1111
/// Idempotently create a new fly.io app.
1212
/// Using "personal" alias for org-slug works but will return an error on retry: "already exists but belongs to different organization"
1313
put: func(org-slug: string, app-name: string) -> result<app, string>;
14+
15+
/// List all application within an organization.
1416
%list: func(org-slug: string) -> result<list<app>, string>;
17+
18+
/// Delete application with all its associated resources.
1519
delete: func(app-name: string, force: bool) -> result<_, string>;
1620
}
1721

@@ -60,6 +64,7 @@ interface machines {
6064
restart: option<machine-restart>,
6165
stop-config: option<stop-config>,
6266
mounts: option<list<mount>>,
67+
services: option<list<service-config>>,
6368
}
6469

6570
record mount {
@@ -111,6 +116,33 @@ interface machines {
111116
stdout: option<string>,
112117
}
113118

119+
/// Configuration for a single service (port forwarding rule)
120+
record service-config {
121+
internal-port: u16,
122+
protocol: service-protocol,
123+
ports: list<port-config>,
124+
}
125+
126+
/// Protocol for a service
127+
enum service-protocol {
128+
tcp,
129+
udp,
130+
}
131+
132+
/// Configuration for an external port
133+
record port-config {
134+
port: u16,
135+
handlers: list<port-handler>,
136+
}
137+
138+
/// Handlers for a port
139+
enum port-handler {
140+
http,
141+
tls,
142+
pg, // PostgreSQL
143+
}
144+
145+
/// List machines
114146
%list: func(app-name: string) -> result<list<machine>, string>;
115147

116148
/// Create and start a machine under `app-name` with name `machine-name` and return its ID.
@@ -203,5 +235,3 @@ world exports {
203235
export secrets;
204236
export volumes;
205237
}
206-
207-

0 commit comments

Comments
 (0)