-
Notifications
You must be signed in to change notification settings - Fork 63
/
deploy.rs
172 lines (155 loc) · 5.27 KB
/
deploy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::convert::TryFrom;
use std::io::Cursor;
use std::net::{AddrParseError, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DeployResult {
pub valid: Result<String, String>,
#[serde(default)]
pub vols: Vec<ContainerVolume>,
#[serde(default)]
pub start_mode: StartMode,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ContainerVolume {
pub name: String,
pub path: String,
}
#[non_exhaustive]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ContainerEndpoint {
UnixStream(PathBuf),
UnixDatagram(PathBuf),
UdpDatagram(SocketAddr),
TcpListener(SocketAddr),
TcpStream(SocketAddr),
}
impl std::fmt::Display for ContainerEndpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnixStream(path) | Self::UnixDatagram(path) => write!(f, "{}", path.display()),
Self::UdpDatagram(addr) | Self::TcpListener(addr) | Self::TcpStream(addr) => {
write!(f, "{}", addr)
}
}
}
}
impl From<ContainerEndpoint> for PathBuf {
fn from(e: ContainerEndpoint) -> Self {
match e {
ContainerEndpoint::UnixStream(p) | ContainerEndpoint::UnixDatagram(p) => p,
ContainerEndpoint::UdpDatagram(a) => PathBuf::from(format!("udp://{}", a)),
ContainerEndpoint::TcpListener(a) => PathBuf::from(format!("tcp-connect://{}", a)),
ContainerEndpoint::TcpStream(a) => PathBuf::from(format!("tcp-listen://{}", a)),
}
}
}
impl<'a> TryFrom<&'a crate::server::NetworkEndpoint> for ContainerEndpoint {
type Error = String;
fn try_from(endpoint: &'a crate::server::NetworkEndpoint) -> Result<Self, Self::Error> {
match endpoint {
crate::server::NetworkEndpoint::UnixStream(s) => Ok(Self::UnixStream(PathBuf::from(s))),
crate::server::NetworkEndpoint::UnixDatagram(s) => {
Ok(Self::UnixDatagram(PathBuf::from(s)))
}
crate::server::NetworkEndpoint::UdpDatagram(s) => {
Ok(Self::UdpDatagram(to_socket_addr(s)?))
}
crate::server::NetworkEndpoint::TcpListener(s) => {
Ok(Self::TcpListener(to_socket_addr(s)?))
}
crate::server::NetworkEndpoint::TcpStream(s) => Ok(Self::TcpStream(to_socket_addr(s)?)),
}
}
}
impl TryFrom<url::Url> for ContainerEndpoint {
type Error = String;
fn try_from(url: url::Url) -> Result<Self, Self::Error> {
match url.scheme() {
"unix" => {
let url = url.to_string().replacen("unix://", "", 1);
Ok(Self::UnixStream(PathBuf::from(url)))
}
"udp" => {
let url = url.to_string().replacen("udp://", "", 1);
let addr: SocketAddr = url.parse().map_err(|e: AddrParseError| e.to_string())?;
Ok(Self::UdpDatagram(addr))
}
"tcp-connect" => {
let url = url.to_string().replacen("tcp-connect://", "", 1);
let addr: SocketAddr = url.parse().map_err(|e: AddrParseError| e.to_string())?;
Ok(Self::TcpStream(addr))
}
"tcp-listen" => Ok(Self::TcpListener(SocketAddr::new(
Ipv4Addr::new(127, 0, 0, 1).into(),
0,
))),
scheme => Err(format!("Unknown scheme: {scheme}")),
}
}
}
fn to_socket_addr(s: &str) -> Result<SocketAddr, String> {
s.parse().map_err(|e: AddrParseError| e.to_string())
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub enum StartMode {
#[default]
Empty,
Blocking,
}
impl DeployResult {
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> anyhow::Result<DeployResult> {
let b: &[u8] = bytes.as_ref();
if b.is_empty() {
log::warn!("empty descriptor");
let vols = Default::default();
return Ok(DeployResult {
valid: Ok(Default::default()),
vols,
start_mode: Default::default(),
});
}
if let Some(idx) = b.iter().position(|&ch| ch == b'{') {
let b = &b[idx..];
Ok(serde_json::from_reader(Cursor::new(b))?)
} else {
let text = String::from_utf8_lossy(b);
anyhow::bail!("invalid deploy response: {}", text);
}
}
}
#[cfg(test)]
mod test {
use super::DeployResult;
fn parse_bytes<T: AsRef<[u8]>>(b: T) -> DeployResult {
let result = DeployResult::from_bytes(b).unwrap();
eprintln!("result={:?}", result);
result
}
#[test]
fn test_wasi_deploy() {
parse_bytes(
r#"{
"valid": {"Ok": "success"}
}"#,
);
parse_bytes(
r#"{
"valid": {"Err": "bad image format"}
}"#,
);
parse_bytes(
r#"{
"valid": {"Ok": "success"},
"vols": [
{"name": "vol-9a0c1c4a", "path": "/in"},
{"name": "vol-a68672e0", "path": "/out"}
]
}"#,
);
}
}