Skip to content

Commit

Permalink
Removing container_engine option and adding auto-detection
Browse files Browse the repository at this point in the history
- Instead of requiring the user to specify which container engine they
  would prefer via the container_engine option, cross will now check to see
  which one is installed and use it. Defaults to docker if both are installed.
  • Loading branch information
ostrosco committed Nov 13, 2019
1 parent 42f2410 commit 91223e4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 66 deletions.
34 changes: 27 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libc = "0.2.18"
rustc_version = "0.2"
semver = "0.9"
toml = "0.5"
which = "3.1.0"

[target.'cfg(not(windows))'.dependencies]
nix = "0.15"
Expand Down
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ This project is developed and maintained by the [Tools team][team].

[binfmt_misc]: https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html

One of these container engines is required:
One of these container engines is required. If both are installed, `cross` will
default to `docker`.

- [Docker](https://www.docker.com/). Note that on Linux non-sudo users need to be in the
`docker` group. Read the official [post-installation steps][post]. Requires version
Expand Down Expand Up @@ -82,18 +83,6 @@ $ cross rustc --target powerpc-unknown-linux-gnu --release -- -C lto
You can place a `Cross.toml` file in the root of your Cargo project to tweak
`cross`'s behavior:

### Using an Alternative Container Engine

`cross` allows the user to specify the container engine they would like to use.
Currently, only Docker and Podman are supported. `cross` will default to using
Docker. For other container engines, you can use the
`target.{{TARGET}}.container_engine` field in `Cross.toml`:

``` toml
[target.aarch64-unknown-linux-gnu]
container_engine = "podman"
```

### Custom Docker images

`cross` provides default Docker images for the targets listed below. However, it
Expand Down
49 changes: 25 additions & 24 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ use crate::id;

const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs"));
const DOCKER: &str = "docker";
const PODMAN: &str = "podman";

pub fn docker_command(container_engine: &str, subcommand: &str) -> Command {
let mut docker = Command::new(container_engine);
docker.arg(subcommand);
docker.args(&["--userns", "host"]);
docker
fn get_container_engine() -> Option<&'static str> {
if which::which(DOCKER).is_ok() {
Some(DOCKER)
} else if which::which(PODMAN).is_ok() {
Some(PODMAN)
} else {
None
}
}

pub fn docker_command(subcommand: &str) -> Result<Command> {
if let Some(ce) = get_container_engine() {
let mut command = Command::new(ce);
command.arg(subcommand);
command.args(&["--userns", "host"]);
Ok(command)
} else {
Err("no container engine found; install docker or podman".into())
}
}

/// Register binfmt interpreters
pub fn register(target: &Target, toml: Option<&Toml>, verbose: bool) -> Result<()> {
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 && \
Expand All @@ -32,14 +47,7 @@ pub fn register(target: &Target, toml: Option<&Toml>, verbose: bool) -> Result<(
binfmt-support qemu-user-static"
};

let mut container_engine = DOCKER.to_string();
if let Some(toml) = toml {
if let Some(engine) = toml.container_engine(target)? {
container_engine = engine;
}
}

docker_command(&container_engine, "run")
docker_command("run")?
.arg("--privileged")
.arg("--rm")
.arg("ubuntu:16.04")
Expand Down Expand Up @@ -80,22 +88,15 @@ pub fn run(target: &Target,
// container doesn't have write access to the root of the Cargo project
let cargo_toml = root.join("Cargo.toml");

let mut runner = None;
let mut container_engine = DOCKER.to_string();
let runner = None;

Command::new("cargo").args(&["fetch",
"--manifest-path",
&cargo_toml.display().to_string()])
.run(verbose)
.chain_err(|| "couldn't generate Cargo.lock")?;

if let Some(toml) = toml {
runner = toml.runner(target)?;
if let Some(engine) = toml.container_engine(target)? {
container_engine = engine;
}
}
let mut docker = docker_command(&container_engine, "run");
let mut docker = docker_command("run")?;

if let Some(toml) = toml {
for var in toml.env_passthrough(target)? {
Expand All @@ -118,7 +119,7 @@ pub fn run(target: &Target,
docker.arg("--rm");

// We need to specify the user for Docker, but not for Podman.
if container_engine == DOCKER {
if let Some(DOCKER) = get_container_engine() {
docker.args(&["--user", &format!("{}:{}", id::user(), id::group())]);
}

Expand Down
23 changes: 1 addition & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ fn run() -> Result<ExitStatus> {
needs_interpreter &&
target.needs_interpreter() &&
!interpreter::is_registered(&target)? {
docker::register(&target, toml.as_ref(), verbose)?
docker::register(&target, verbose)?
}

return docker::run(&target,
Expand Down Expand Up @@ -329,27 +329,6 @@ impl Toml {
}
}

/// Returns the `target.{}.container_engine` part of `Cross.toml`
pub fn container_engine(&self, target: &Target) -> Result<Option<String>> {
let triple = target.triple();

if let Some(value) = self
.table
.get("target")
.and_then(|t| t.get(triple))
.and_then(|t| t.get("container_engine"))
{
let value = value
.as_str()
.ok_or_else(|| format!("target.{}.container_engine must be a string", triple))?
.to_string();
Ok(Some(value))
} else {
Ok(None)
}
}


/// Returns the `build.image` or the `target.{}.xargo` part of `Cross.toml`
pub fn xargo(&self, target: &Target) -> Result<Option<bool>> {
let triple = target.triple();
Expand Down

0 comments on commit 91223e4

Please sign in to comment.