-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcompose.rs
More file actions
282 lines (226 loc) · 8.43 KB
/
Copy pathcompose.rs
File metadata and controls
282 lines (226 loc) · 8.43 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::io::Write;
use std::process::Command;
use launcher_interface::types::DockerSha256Digest;
use crate::constants::{DSTACK_UNIX_SOCKET, MPC_CONFIG_SHARED_PATH, MPC_CONTAINER_NAME};
use crate::error::LauncherError;
use crate::types::{Platform, PortMapping};
const COMPOSE_TEMPLATE: &str = include_str!("../assets/mpc-node-docker-compose.template.yml");
const COMPOSE_TEE_TEMPLATE: &str =
include_str!("../assets/mpc-node-docker-compose.tee.template.yml");
pub fn render_compose_file(
platform: Platform,
port_mappings: &[PortMapping],
image_name: &str,
manifest_digest: &DockerSha256Digest,
) -> Result<tempfile::NamedTempFile, LauncherError> {
let template = match platform {
Platform::Tee => COMPOSE_TEE_TEMPLATE,
Platform::NonTee => COMPOSE_TEMPLATE,
};
let ports: Vec<String> = port_mappings
.iter()
.map(PortMapping::docker_compose_value)
.collect();
let ports_json = serde_json::to_string(&ports).expect("port list is serializable");
let rendered = template
.replace("{{IMAGE_NAME}}", image_name)
.replace("{{MANIFEST_DIGEST}}", &manifest_digest.to_string())
.replace("{{CONTAINER_NAME}}", MPC_CONTAINER_NAME)
.replace("{{MPC_CONFIG_SHARED_PATH}}", MPC_CONFIG_SHARED_PATH)
.replace("{{DSTACK_UNIX_SOCKET}}", DSTACK_UNIX_SOCKET)
.replace("{{PORTS}}", &ports_json);
tracing::info!(compose = %rendered, "rendered docker-compose file");
let mut file = tempfile::NamedTempFile::new().map_err(LauncherError::TempFileCreate)?;
file.write_all(rendered.as_bytes())
.map_err(|source| LauncherError::FileWrite {
path: file.path().display().to_string(),
source,
})?;
Ok(file)
}
pub fn launch_mpc_container(
platform: Platform,
manifest_digest: &DockerSha256Digest,
image_name: &str,
port_mappings: &[PortMapping],
) -> Result<(), LauncherError> {
tracing::info!(?manifest_digest, "launching MPC node");
let compose_file = render_compose_file(platform, port_mappings, image_name, manifest_digest)?;
let compose_path = compose_file.path().display().to_string();
// The compose file pins a stable top-level project `name`, so the
// container is reused across restarts rather than recreated, preserving its
// logs. Compose recreates it only when the rendered config (e.g. image
// digest) changes.
let run_output = Command::new("docker")
.args(["compose", "-f", &compose_path, "up", "-d"])
.output()
.map_err(|inner| LauncherError::DockerRunFailed {
image_hash: manifest_digest.clone(),
inner,
})?;
if !run_output.status.success() {
let stderr = String::from_utf8_lossy(&run_output.stderr);
let stdout = String::from_utf8_lossy(&run_output.stdout);
tracing::error!(%stderr, %stdout, "docker compose up failed");
return Err(LauncherError::DockerRunFailedExitStatus {
image_hash: manifest_digest.clone(),
output: stderr.into_owned(),
});
}
tracing::info!("MPC launched successfully.");
Ok(())
}
#[cfg(test)]
mod tests {
use std::num::NonZeroU16;
use launcher_interface::types::DockerSha256Digest;
use super::*;
const SAMPLE_IMAGE_NAME: &str = "nearone/mpc-node";
fn render(
platform: Platform,
port_mappings: &[PortMapping],
digest: &DockerSha256Digest,
) -> String {
let file = render_compose_file(platform, port_mappings, SAMPLE_IMAGE_NAME, digest).unwrap();
std::fs::read_to_string(file.path()).unwrap()
}
fn digest(hex_char: char) -> DockerSha256Digest {
format!(
"sha256:{}",
std::iter::repeat_n(hex_char, 64).collect::<String>()
)
.parse()
.unwrap()
}
fn sample_digest() -> DockerSha256Digest {
digest('a')
}
fn empty_port_mappings() -> Vec<PortMapping> {
vec![]
}
fn port_mappings_with_port() -> Vec<PortMapping> {
vec![PortMapping {
host: NonZeroU16::new(11780).unwrap(),
container: NonZeroU16::new(11780).unwrap(),
}]
}
#[test]
fn tee_mode_includes_dstack_env_and_volume() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::Tee, &port_mappings, &digest);
// then
assert!(rendered.contains(&format!("DSTACK_ENDPOINT={DSTACK_UNIX_SOCKET}")));
assert!(rendered.contains(&format!("{DSTACK_UNIX_SOCKET}:{DSTACK_UNIX_SOCKET}")));
}
#[test]
fn nontee_mode_excludes_dstack() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(!rendered.contains("DSTACK_ENDPOINT"));
assert!(!rendered.contains(DSTACK_UNIX_SOCKET));
}
#[test]
fn includes_security_opts_and_required_volumes() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(rendered.contains("no-new-privileges:true"));
assert!(rendered.contains("/tapp:/tapp:ro"));
assert!(rendered.contains("shared-volume:/mnt/shared"));
assert!(rendered.contains("mpc-data:/data"));
assert!(rendered.contains(&format!("container_name: \"{MPC_CONTAINER_NAME}\"")));
}
#[test]
fn tee_mode_includes_restart_on_failure() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::Tee, &port_mappings, &digest);
// then
assert!(rendered.contains("restart: on-failure"));
}
#[test]
fn nontee_mode_includes_restart_on_failure() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(rendered.contains("restart: on-failure"));
}
#[test]
fn mounts_config_file_read_only() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then — config is on the shared volume, referenced in the command
assert!(rendered.contains(MPC_CONFIG_SHARED_PATH));
}
#[test]
fn includes_start_with_config_file_command() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(rendered.contains("/app/mpc-node"));
assert!(rendered.contains(MPC_CONFIG_SHARED_PATH));
}
#[test]
fn image_is_set() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(rendered.contains(&format!("image: \"{SAMPLE_IMAGE_NAME}@{digest}\"")));
}
#[test]
fn includes_ports() {
// given
let port_mappings = port_mappings_with_port();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(rendered.contains("11780:11780"));
}
#[test]
fn no_env_section_in_nontee_mode() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::NonTee, &port_mappings, &digest);
// then
assert!(!rendered.contains("environment:"));
}
#[test]
fn includes_stable_project_name() {
// given
let port_mappings = empty_port_mappings();
let digest = sample_digest();
// when
let rendered = render(Platform::Tee, &port_mappings, &digest);
// then — a stable top-level project name keeps Compose reusing the
// container across restarts instead of recreating it (which would wipe
// its logs)
assert!(rendered.contains("name: mpc-node"));
}
}