Skip to content

Commit 613837c

Browse files
committed
Build the orchestrator in Docker
1 parent f9d4d34 commit 613837c

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

compiler/base/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
asm-cleanup/target
22
modify-cargo-toml/target
3+
orchestrator/target

compiler/base/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,39 @@ FROM bare-sources as munge
5959
ADD --chown=playground modify-cargo-toml /playground/modify-cargo-toml
6060
RUN cargo build --release --manifest-path=/playground/modify-cargo-toml/Cargo.toml
6161

62+
# Set up cargo-chef for faster builds
63+
64+
FROM bare-sources as chef-available
65+
66+
RUN cargo install cargo-chef
67+
68+
WORKDIR /orchestrator
69+
70+
# Prepare the orchestrator's dependencies
71+
72+
FROM chef-available as prepare-orchestrator
73+
74+
COPY --chown=playground --link orchestrator /orchestrator
75+
RUN cargo chef prepare
76+
77+
# Build the orchestrator
78+
79+
FROM chef-available as build-orchestrator
80+
81+
COPY --chown=playground --link asm-cleanup /asm-cleanup
82+
COPY --chown=playground --link modify-cargo-toml /modify-cargo-toml
83+
COPY --chown=playground --link --from=prepare-orchestrator /orchestrator/recipe.json /orchestrator/recipe.json
84+
RUN cargo chef cook --release
85+
86+
COPY --chown=playground --link orchestrator /orchestrator
87+
RUN cargo install --path .
88+
6289
# Compiler and sources
6390

6491
FROM bare-sources as sources
6592

6693
COPY --from=munge /playground/modify-cargo-toml/target/release/modify-cargo-toml /playground/.cargo/bin
94+
COPY --link --from=build-orchestrator /playground/.cargo/bin/worker /playground/.cargo/bin/worker
6795

6896
# Compiler and pre-compiled crates
6997

compiler/base/entrypoint.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
set -eu
44

5-
timeout=${PLAYGROUND_TIMEOUT:-10}
5+
if [[ -z "${PLAYGROUND_ORCHESTRATOR:-}" ]]; then
6+
timeout=${PLAYGROUND_TIMEOUT:-10}
67

7-
modify-cargo-toml
8+
modify-cargo-toml
89

9-
# Don't use `exec` here. The shell is what prints out the useful
10-
# "Killed" message
11-
timeout --signal=KILL ${timeout} "$@"
10+
# Don't use `exec` here. The shell is what prints out the useful
11+
# "Killed" message
12+
timeout --signal=KILL ${timeout} "$@"
13+
else
14+
exec "$@"
15+
fi

compiler/base/orchestrator/src/coordinator.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ where
346346
}
347347
}
348348

349+
impl Coordinator<DockerBackend> {
350+
pub async fn new_docker() -> Result<Self, Error> {
351+
Self::new(DockerBackend(())).await
352+
}
353+
}
354+
349355
#[derive(Debug)]
350356
struct Container {
351357
task: JoinHandle<Result<()>>,
@@ -819,6 +825,63 @@ where
819825
}
820826
}
821827

828+
macro_rules! docker_command {
829+
($($arg:expr),* $(,)?) => ({
830+
let mut cmd = Command::new("docker");
831+
$( cmd.arg($arg); )*
832+
cmd
833+
});
834+
}
835+
836+
#[cfg(target_arch = "x86_64")]
837+
const DOCKER_ARCH: &str = "linux/amd64";
838+
839+
#[cfg(target_arch = "aarch64")]
840+
const DOCKER_ARCH: &str = "linux/arm64";
841+
842+
fn basic_secure_docker_command() -> Command {
843+
docker_command!(
844+
"run",
845+
"--platform",
846+
DOCKER_ARCH,
847+
"--cap-drop=ALL",
848+
"--net",
849+
"none",
850+
"--memory",
851+
"512m",
852+
"--memory-swap",
853+
"640m",
854+
"--pids-limit",
855+
"512",
856+
)
857+
}
858+
859+
pub struct DockerBackend(());
860+
861+
impl Backend for DockerBackend {
862+
fn prepare_worker_command(&self, channel: Channel) -> Command {
863+
let mut command = basic_secure_docker_command();
864+
command
865+
.arg("-i")
866+
.args(["-a", "stdin", "-a", "stdout", "-a", "stderr"])
867+
.arg("--rm")
868+
.arg(channel.to_container_name())
869+
.arg("worker")
870+
.arg("/playground");
871+
command
872+
}
873+
}
874+
875+
impl Channel {
876+
fn to_container_name(self) -> &'static str {
877+
match self {
878+
Channel::Stable => "rust-stable",
879+
Channel::Beta => "rust-beta",
880+
Channel::Nightly => "rust-nightly",
881+
}
882+
}
883+
}
884+
822885
pub type Result<T, E = Error> = ::std::result::Result<T, E>;
823886

824887
#[derive(Debug, Snafu)]
@@ -988,6 +1051,7 @@ mod tests {
9881051

9891052
async fn new_coordinator() -> Result<Coordinator<impl Backend>> {
9901053
Coordinator::new(TestBackend::new()).await
1054+
//Coordinator::new_docker().await
9911055
}
9921056

9931057
fn new_compile_request() -> CompileRequest {
@@ -1207,6 +1271,29 @@ mod tests {
12071271
Ok(())
12081272
}
12091273

1274+
#[tokio::test]
1275+
#[snafu::report]
1276+
async fn test_compile_wasm() -> Result<()> {
1277+
// cargo-wasm only exists inside the container
1278+
let coordinator = Coordinator::new_docker().await?;
1279+
1280+
let response = coordinator
1281+
.compile(new_compile_wasm_request())
1282+
.with_timeout()
1283+
.await
1284+
.unwrap();
1285+
1286+
assert!(response.success, "stderr: {}", response.stderr);
1287+
assert_contains!(
1288+
response.code,
1289+
r#"(func $inc (export "inc") (type $t0) (param $p0 i32) (result i32)"#
1290+
);
1291+
1292+
coordinator.shutdown().await?;
1293+
1294+
Ok(())
1295+
}
1296+
12101297
trait TimeoutExt: Future + Sized {
12111298
#[allow(clippy::type_complexity)]
12121299
fn with_timeout(

0 commit comments

Comments
 (0)