Skip to content

Commit

Permalink
Add custom docker image support
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Nov 24, 2020
1 parent 31c864e commit cffd56f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
26 changes: 23 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ schemamama = "0.3"
schemamama_postgres = "0.3"
systemstat = "0.1.4"
prometheus = { version = "0.10.0", default-features = false }
rustwide = "0.11"
rustwide = { path = "../rustwide" } # "0.11"
mime_guess = "2"
dotenv = "0.15"
zstd = "0.5"
Expand Down
9 changes: 9 additions & 0 deletions crates/metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub enum MetadataError {
/// targets = [ "x86_64-apple-darwin", "x86_64-pc-windows-msvc" ]
/// rustc-args = [ "--example-rustc-arg" ]
/// rustdoc-args = [ "--example-rustdoc-arg" ]
/// docker-image = "rustops/crates-build-env"
/// ```
///
/// You can define one or more fields in your `Cargo.toml`.
Expand Down Expand Up @@ -128,6 +129,9 @@ pub struct Metadata {
/// List of command line arguments for `rustdoc`.
#[serde(default)]
rustdoc_args: Vec<String>,

/// Custom docker image.
docker_image: Option<String>,
}

/// The targets that should be built for a crate.
Expand Down Expand Up @@ -277,6 +281,11 @@ impl Metadata {
map.insert("DOCS_RS", "1".into());
map
}

/// Return the custom docker image, if provided.
pub fn docker_image(&self) -> Option<String> {
self.docker_image.clone()
}
}

impl std::str::FromStr for Metadata {
Expand Down
20 changes: 12 additions & 8 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ impl RustwideBuilder {
self.skip_build_if_exists = should;
}

fn prepare_sandbox(&self, limits: &Limits) -> SandboxBuilder {
SandboxBuilder::new()
fn prepare_sandbox(&self, metadata: &Metadata, limits: &Limits) -> Result<SandboxBuilder> {
let mut builder = SandboxBuilder::new()
.cpu_limit(self.config.build_cpu_limit.map(|limit| limit as f32))
.memory_limit(Some(limits.memory()))
.enable_networking(limits.networking())
.enable_networking(limits.networking());
if let Some(image) = metadata.docker_image() {
builder = builder.image(SandboxImage::remote(&image)?)
}
Ok(builder)
}

pub fn update_toolchain(&mut self) -> Result<()> {
Expand Down Expand Up @@ -208,11 +212,11 @@ impl RustwideBuilder {
let krate = Crate::crates_io(DUMMY_CRATE_NAME, DUMMY_CRATE_VERSION);
krate.fetch(&self.workspace)?;

let metadata = Metadata::from_crate_root(&build_dir.build_dir())?;

build_dir
.build(&self.toolchain, &krate, self.prepare_sandbox(&limits))
.build(&self.toolchain, &krate, self.prepare_sandbox(&metadata, &limits)?)
.run(|build| {
let metadata = Metadata::from_crate_root(&build.host_source_dir())?;

let res = self.execute_build(HOST_TARGET, true, build, &limits, &metadata)?;
if !res.result.successful {
failure::bail!("failed to build dummy crate for {}", self.rustc_version);
Expand Down Expand Up @@ -322,14 +326,14 @@ impl RustwideBuilder {

let local_storage = tempfile::Builder::new().prefix("docsrs-docs").tempdir()?;

let metadata = Metadata::from_crate_root(&build_dir.build_dir())?;
let res = build_dir
.build(&self.toolchain, &krate, self.prepare_sandbox(&limits))
.build(&self.toolchain, &krate, self.prepare_sandbox(&metadata, &limits)?)
.run(|build| {
use docsrs_metadata::BuildTargets;

let mut has_docs = false;
let mut successful_targets = Vec::new();
let metadata = Metadata::from_crate_root(&build.host_source_dir())?;
let BuildTargets {
default_target,
other_targets,
Expand Down

0 comments on commit cffd56f

Please sign in to comment.