-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathdocker.rs
504 lines (440 loc) · 16.9 KB
/
docker.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use std::{env, fs};
use crate::cargo::Root;
use crate::errors::*;
use crate::extensions::{CommandExt, SafeCommand};
use crate::id;
use crate::{Config, Target};
use atty::Stream;
use eyre::bail;
const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs"));
const CROSS_IMAGE: &str = "ghcr.io/cross-rs";
const DOCKER: &str = "docker";
const PODMAN: &str = "podman";
fn get_container_engine() -> Result<std::path::PathBuf, which::Error> {
if let Ok(ce) = env::var("CROSS_CONTAINER_ENGINE") {
which::which(ce)
} else {
which::which(DOCKER).or_else(|_| which::which(PODMAN))
}
}
pub fn docker_command(subcommand: &str) -> Result<Command> {
let ce = get_container_engine()
.map_err(|_| eyre::eyre!("no container engine found"))
.with_suggestion(|| "is docker or podman installed?")?;
let mut command = Command::new(ce);
command.arg(subcommand);
command.args(&["--userns", "host"]);
Ok(command)
}
/// Register binfmt interpreters
pub fn register(target: &Target, verbose: bool) -> Result<()> {
let cmd = if target.is_windows() {
// https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
"mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc && \
echo ':wine:M::MZ::/usr/bin/run-detectors:' > /proc/sys/fs/binfmt_misc/register"
} else {
"apt-get update && apt-get install --no-install-recommends --assume-yes \
binfmt-support qemu-user-static"
};
docker_command("run")?
.arg("--privileged")
.arg("--rm")
.arg("ubuntu:16.04")
.args(&["sh", "-c", cmd])
.run(verbose)
}
#[allow(clippy::too_many_arguments)] // TODO: refactor
pub fn run(
target: &Target,
args: &[String],
target_dir: &Option<PathBuf>,
root: &Root,
config: &Config,
uses_xargo: bool,
sysroot: &Path,
verbose: bool,
docker_in_docker: bool,
) -> Result<ExitStatus> {
let mount_finder = if docker_in_docker {
MountFinder::new(docker_read_mount_paths()?)
} else {
MountFinder::default()
};
let root = root.path();
let home_dir = home::home_dir().ok_or_else(|| eyre::eyre!("could not find home directory"))?;
let cargo_dir = home::cargo_home()?;
let xargo_dir = env::var_os("XARGO_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home_dir.join(".xargo"));
let nix_store_dir = env::var_os("NIX_STORE").map(PathBuf::from);
let target_dir = target_dir.clone().unwrap_or_else(|| root.join("target"));
// create the directories we are going to mount before we mount them,
// otherwise `docker` will create them but they will be owned by `root`
fs::create_dir(&target_dir).ok();
fs::create_dir(&cargo_dir).ok();
fs::create_dir(&xargo_dir).ok();
// update paths to the host mounts path.
let cargo_dir = mount_finder.find_mount_path(cargo_dir);
let xargo_dir = mount_finder.find_mount_path(xargo_dir);
let target_dir = mount_finder.find_mount_path(target_dir);
let host_root = mount_finder.find_mount_path(root);
let mount_root: PathBuf;
#[cfg(target_os = "windows")]
{
// On Windows, we can not mount the directory name directly. Instead, we use wslpath to convert the path to a linux compatible path.
mount_root = wslpath(&host_root, verbose)?;
}
#[cfg(not(target_os = "windows"))]
{
mount_root = host_root.clone();
}
let sysroot = mount_finder.find_mount_path(sysroot);
let mut cmd = if uses_xargo {
SafeCommand::new("xargo")
} else {
SafeCommand::new("cargo")
};
cmd.args(args);
let runner = config.runner(target)?;
let mut docker = docker_command("run")?;
let validate_env_var = |var: &str| -> Result<()> {
if var.contains('=') {
bail!("environment variable names must not contain the '=' character");
}
if var == "CROSS_RUNNER" {
bail!("CROSS_RUNNER environment variable name is reserved and cannot be pass through");
}
Ok(())
};
for ref var in config.env_passthrough(target)? {
validate_env_var(var)?;
// Only specifying the environment variable name in the "-e"
// flag forwards the value from the parent shell
docker.args(&["-e", var]);
}
let mut env_volumes = false;
for ref var in config.env_volumes(target)? {
validate_env_var(var)?;
if let Ok(val) = env::var(var) {
let host_path: PathBuf;
let mount_path: PathBuf;
#[cfg(target_os = "windows")]
{
// Docker does not support UNC paths, this will try to not use UNC paths
host_path = dunce::canonicalize(&val)
.wrap_err_with(|| format!("when canonicalizing path `{val}`"))?;
// On Windows, we can not mount the directory name directly. Instead, we use wslpath to convert the path to a linux compatible path.
mount_path = wslpath(&host_path, verbose)?;
}
#[cfg(not(target_os = "windows"))]
{
host_path = Path::new(&val)
.canonicalize()
.wrap_err_with(|| format!("when canonicalizing path `{val}`"))?;
mount_path = host_path.clone();
}
docker.args(&[
"-v",
&format!("{}:{}", host_path.display(), mount_path.display()),
]);
docker.args(&["-e", &format!("{}={}", var, mount_path.display())]);
env_volumes = true;
}
}
docker.args(&["-e", "PKG_CONFIG_ALLOW_CROSS=1"]);
docker.arg("--rm");
if target.needs_docker_privileged() {
docker.arg("--privileged");
}
// We need to specify the user for Docker, but not for Podman.
if let Ok(ce) = get_container_engine() {
if ce.ends_with(DOCKER) {
docker.args(&[
"--user",
&format!(
"{}:{}",
env::var("CROSS_CONTAINER_UID").unwrap_or_else(|_| id::user().to_string()),
env::var("CROSS_CONTAINER_GID").unwrap_or_else(|_| id::group().to_string()),
),
]);
}
}
docker
.args(&["-e", "XARGO_HOME=/xargo"])
.args(&["-e", "CARGO_HOME=/cargo"])
.args(&["-e", "CARGO_TARGET_DIR=/target"]);
if let Some(username) = id::username().unwrap() {
docker.args(&["-e", &format!("USER={username}")]);
}
if let Ok(value) = env::var("QEMU_STRACE") {
docker.args(&["-e", &format!("QEMU_STRACE={value}")]);
}
if let Ok(value) = env::var("CROSS_DEBUG") {
docker.args(&["-e", &format!("CROSS_DEBUG={value}")]);
}
if let Ok(value) = env::var("DOCKER_OPTS") {
let opts: Vec<&str> = value.split(' ').collect();
docker.args(&opts);
}
docker
.args(&[
"-e",
&format!("CROSS_RUNNER={}", runner.unwrap_or_default()),
])
.args(&["-v", &format!("{}:/xargo:Z", xargo_dir.display())])
.args(&["-v", &format!("{}:/cargo:Z", cargo_dir.display())])
// Prevent `bin` from being mounted inside the Docker container.
.args(&["-v", "/cargo/bin"]);
if env_volumes {
docker.args(&[
"-v",
&format!("{}:{}:Z", host_root.display(), mount_root.display()),
]);
} else {
docker.args(&["-v", &format!("{}:/project:Z", host_root.display())]);
}
docker
.args(&["-v", &format!("{}:/rust:Z,ro", sysroot.display())])
.args(&["-v", &format!("{}:/target:Z", target_dir.display())]);
if env_volumes {
docker.args(&["-w", &mount_root.display().to_string()]);
} else {
docker.args(&["-w", "/project"]);
}
// When running inside NixOS or using Nix packaging we need to add the Nix
// Store to the running container so it can load the needed binaries.
if let Some(nix_store) = nix_store_dir {
docker.args(&[
"-v",
&format!("{}:{}:Z", nix_store.display(), nix_store.display()),
]);
}
if atty::is(Stream::Stdin) {
docker.arg("-i");
if atty::is(Stream::Stdout) && atty::is(Stream::Stderr) {
docker.arg("-t");
}
}
docker
.arg(&image(config, target)?)
.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)])
.run_and_get_status(verbose)
}
pub fn image(config: &Config, target: &Target) -> Result<String> {
if let Some(image) = config.image(target)? {
return Ok(image);
}
if !DOCKER_IMAGES.contains(&target.triple()) {
bail!(
"`cross` does not provide a Docker image for target {target}, \
specify a custom image in `Cross.toml`."
);
}
let version = if include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")).is_empty() {
env!("CARGO_PKG_VERSION")
} else {
"main"
};
Ok(format!("{CROSS_IMAGE}/{target}:{version}"))
}
#[cfg(target_os = "windows")]
fn wslpath(path: &Path, verbose: bool) -> Result<PathBuf> {
let wslpath = which::which("wsl.exe")
.map_err(|_| eyre::eyre!("could not find wsl.exe"))
.warning("usage of `env.volumes` requires WSL on Windows")
.suggestion("is WSL installed on the host?")?;
Command::new(wslpath)
.arg("-e")
.arg("wslpath")
.arg("-a")
.arg(path)
.run_and_get_stdout(verbose)
.wrap_err_with(|| {
format!(
"could not get linux compatible path for `{}`",
path.display()
)
})
.map(|s| s.trim().into())
}
fn docker_read_mount_paths() -> Result<Vec<MountDetail>> {
let hostname = env::var("HOSTNAME").wrap_err("HOSTNAME environment variable not found")?;
let docker_path = which::which(DOCKER)?;
let mut docker: Command = {
let mut command = Command::new(docker_path);
command.arg("inspect");
command.arg(hostname);
command
};
let output = docker.run_and_get_stdout(false)?;
let info = serde_json::from_str(&output).wrap_err("failed to parse docker inspect output")?;
dockerinfo_parse_mounts(&info)
}
fn dockerinfo_parse_mounts(info: &serde_json::Value) -> Result<Vec<MountDetail>> {
let mut mounts = dockerinfo_parse_user_mounts(info);
let root_info = dockerinfo_parse_root_mount_path(info)?;
mounts.push(root_info);
Ok(mounts)
}
fn dockerinfo_parse_root_mount_path(info: &serde_json::Value) -> Result<MountDetail> {
let driver_name = info
.pointer("/0/GraphDriver/Name")
.and_then(|v| v.as_str())
.ok_or_else(|| eyre::eyre!("no driver name found"))?;
if driver_name == "overlay2" {
let path = info
.pointer("/0/GraphDriver/Data/MergedDir")
.and_then(|v| v.as_str())
.ok_or_else(|| eyre::eyre!("No merge directory found"))?;
Ok(MountDetail {
source: PathBuf::from(&path),
destination: PathBuf::from("/"),
})
} else {
eyre::bail!("want driver overlay2, got {driver_name}")
}
}
fn dockerinfo_parse_user_mounts(info: &serde_json::Value) -> Vec<MountDetail> {
info.pointer("/0/Mounts")
.and_then(|v| v.as_array())
.map(|v| {
let make_path = |v: &serde_json::Value| PathBuf::from(&v.as_str().unwrap());
let mut mounts = vec![];
for details in v {
let source = make_path(&details["Source"]);
let destination = make_path(&details["Destination"]);
mounts.push(MountDetail {
source,
destination,
});
}
mounts
})
.unwrap_or_else(Vec::new)
}
#[derive(Debug, Default)]
struct MountFinder {
mounts: Vec<MountDetail>,
}
#[derive(Debug, Clone, PartialEq)]
struct MountDetail {
source: PathBuf,
destination: PathBuf,
}
impl MountFinder {
fn new(mounts: Vec<MountDetail>) -> MountFinder {
// sort by length (reverse), to give mounts with more path components a higher priority;
let mut mounts = mounts;
mounts.sort_by(|a, b| {
let la = a.destination.as_os_str().len();
let lb = b.destination.as_os_str().len();
la.cmp(&lb).reverse()
});
MountFinder { mounts }
}
fn find_mount_path(&self, path: impl AsRef<Path>) -> PathBuf {
let path = path.as_ref();
for info in &self.mounts {
if let Ok(stripped) = path.strip_prefix(&info.destination) {
return info.source.join(stripped);
}
}
path.to_path_buf()
}
}
#[cfg(test)]
mod tests {
use super::*;
mod mount_finder {
use super::*;
#[test]
fn test_default_finder_returns_original() {
let finder = MountFinder::default();
assert_eq!(
PathBuf::from("/test/path"),
finder.find_mount_path("/test/path"),
);
}
#[test]
fn test_longest_destination_path_wins() {
let finder = MountFinder::new(vec![
MountDetail {
source: PathBuf::from("/project/path"),
destination: PathBuf::from("/project"),
},
MountDetail {
source: PathBuf::from("/target/path"),
destination: PathBuf::from("/project/target"),
},
]);
assert_eq!(
PathBuf::from("/target/path/test"),
finder.find_mount_path("/project/target/test")
)
}
#[test]
fn test_adjust_multiple_paths() {
let finder = MountFinder::new(vec![
MountDetail {
source: PathBuf::from("/var/lib/docker/overlay2/container-id/merged"),
destination: PathBuf::from("/"),
},
MountDetail {
source: PathBuf::from("/home/project/path"),
destination: PathBuf::from("/project"),
},
]);
assert_eq!(
PathBuf::from("/var/lib/docker/overlay2/container-id/merged/container/path"),
finder.find_mount_path("/container/path")
);
assert_eq!(
PathBuf::from("/home/project/path"),
finder.find_mount_path("/project")
);
assert_eq!(
PathBuf::from("/home/project/path/target"),
finder.find_mount_path("/project/target")
);
}
}
mod parse_docker_inspect {
use super::*;
use serde_json::json;
#[test]
fn test_parse_container_root() {
let actual = dockerinfo_parse_root_mount_path(&json!([{
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/f107af83b37bc0a182d3d2661f3d84684f0fffa1a243566b338a388d5e54bef4-init/diff:/var/lib/docker/overlay2/dfe81d459bbefada7aa897a9d05107a77145b0d4f918855f171ee85789ab04a0/diff:/var/lib/docker/overlay2/1f704696915c75cd081a33797ecc66513f9a7a3ffab42d01a3f17c12c8e2dc4c/diff:/var/lib/docker/overlay2/0a4f6cb88f4ace1471442f9053487a6392c90d2c6e206283d20976ba79b38a46/diff:/var/lib/docker/overlay2/1ee3464056f9cdc968fac8427b04e37ec96b108c5050812997fa83498f2499d1/diff:/var/lib/docker/overlay2/0ec5a47f1854c0f5cfe0e3f395b355b5a8bb10f6e622710ce95b96752625f874/diff:/var/lib/docker/overlay2/f24c8ad76303838b49043d17bf2423fe640836fd9562d387143e68004f8afba0/diff:/var/lib/docker/overlay2/462f89d5a0906805a6f2eec48880ed1e48256193ed506da95414448d435db2b7/diff",
"MergedDir": "/var/lib/docker/overlay2/f107af83b37bc0a182d3d2661f3d84684f0fffa1a243566b338a388d5e54bef4/merged",
"UpperDir": "/var/lib/docker/overlay2/f107af83b37bc0a182d3d2661f3d84684f0fffa1a243566b338a388d5e54bef4/diff",
"WorkDir": "/var/lib/docker/overlay2/f107af83b37bc0a182d3d2661f3d84684f0fffa1a243566b338a388d5e54bef4/work"
},
"Name": "overlay2"
},
}])).unwrap();
let want = MountDetail {
source: PathBuf::from("/var/lib/docker/overlay2/f107af83b37bc0a182d3d2661f3d84684f0fffa1a243566b338a388d5e54bef4/merged"),
destination: PathBuf::from("/"),
};
assert_eq!(want, actual);
}
#[test]
fn test_parse_empty_user_mounts() {
let actual = dockerinfo_parse_user_mounts(&json!([{
"Mounts": [],
}]));
assert_eq!(Vec::<MountDetail>::new(), actual);
}
#[test]
fn test_parse_missing_user_moutns() {
let actual = dockerinfo_parse_user_mounts(&json!([{
"Id": "test",
}]));
assert_eq!(Vec::<MountDetail>::new(), actual);
}
}
}