Skip to content

Commit

Permalink
fix: Fix shell commands on alpine linux and add instructions to build…
Browse files Browse the repository at this point in the history
… runner from docker container
  • Loading branch information
johnnyeric committed Apr 19, 2019
1 parent 5b9def9 commit ff3fbef
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
45 changes: 45 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# https://shaneutt.com/blog/rust-fast-small-docker-image-builds/

FROM rustlang/rust:nightly-slim as cargo-build

RUN apt-get update

RUN apt-get install musl-tools -y

RUN rustup target add x86_64-unknown-linux-musl

WORKDIR /usr/src/myapp

COPY Cargo.toml Cargo.toml

RUN mkdir src/

RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs

RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86_64-unknown-linux-musl

RUN rm -f target/x86_64-unknown-linux-musl/release/deps/myapp*

COPY . .

RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86_64-unknown-linux-musl

# ------------------------------------------------------------------------------
# Final Stage
# ------------------------------------------------------------------------------

FROM mhart/alpine-node:latest

RUN addgroup -g 1000 myapp

RUN adduser -D -s /bin/sh -u 1000 -G myapp myapp

WORKDIR /home/myapp/bin/

COPY --from=cargo-build /usr/src/myapp/target/x86_64-unknown-linux-musl/release/code-runner-rust .

RUN chown myapp:myapp -R code-runner-rust && chmod 777 /tmp

USER myapp

CMD ["./code-runner-rust"]
13 changes: 13 additions & 0 deletions build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Build code-runner-rust

docker build -t code-runner-rust .
docker exec -it --rm code-runner-rust:latest sh

// https://www.shellhacks.com/docker-cp-command-copy-file-to-from-container/

docker cp <container-id>:/home/myapp/bin/code-runner-rust runner-alpine
docker cp f5c291c14c6b:/home/myapp/bin/code-runner-rust runner-alpine

echo '{ "language":"javascript", "stdin": "1|2", "command":"ls", "files": [{"name": "main.js", "content": "const fn"},{"name": "fn.js","content": "const fn"}]}' | ./code-runner-rust

echo '{ "language":"javascript", "stdin": "1|2", "command":"", "files": [{"name": "main.js", "content": "const fn"},{"name": "fn.js","content": "const fn"}]}' | ./code-runner-rust
9 changes: 9 additions & 0 deletions build_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# cargo build --target x86_64-unknown-linux-musl --release

docker build -t code-runner-rust .

# need to create container and extract runner from it
# docker exec -it --rm code-runner-rust:latest sh
# docker cp <container-id>:/home/myapp/bin/code-runner-rust runner-alpine
24 changes: 17 additions & 7 deletions src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{Write, Error};
use std::io::{Write, Error, ErrorKind};
use std::process::{Command, Stdio};
use std::result::Result;
use types::ExecutorResult;
Expand All @@ -13,15 +13,15 @@ pub fn run_stdin(work_dir: &str, stdin: &str, args: &[&str]) -> Result<ExecutorR
.stderr(Stdio::piped());

let mut child = command
.spawn()
.expect("Failed to spawn child process");
.spawn()?;
//.expect("Failed to spawn child process");

{
let stdin_stream = child.stdin.as_mut().expect("Failed to open stdin");
stdin_stream.write_all(stdin.as_bytes()).expect("Failed to write to stdin");
let stdin_stream = child.stdin.as_mut().ok_or(Error::new(ErrorKind::Other, "Failed to open stdin"))?;//.expect("Failed to open stdin");
stdin_stream.write_all(stdin.as_bytes())?;//.expect("Failed to write to stdin");
}

let output = child.wait_with_output().expect("Failed to read stdout");
let output = child.wait_with_output()?;//.expect("Failed to read stdout");

let code = match output.status.code() {
Some(code) => code,
Expand All @@ -40,7 +40,17 @@ pub fn run(work_dir: &str, args: &[&str]) -> Result<ExecutorResult, Error>{
}

pub fn run_bash_stdin(work_dir: &str, command: &str, stdin: &str) -> Result<ExecutorResult, Error> {
run_stdin(work_dir, stdin, &["bash", "-c", command])
match run_stdin(work_dir, stdin, &["bash", "-c", command]) {
Ok(executor_result) => Ok(executor_result),
Err(e) => {
if let ErrorKind::NotFound = e.kind() {
println!("aqui");
run_stdin(work_dir, stdin, &["sh", "-c", command])
} else {
Err(e)
}
}
}
}

/* pub fn run_bash(work_dir: &str, command: &str) -> Result<ExecutorResult, Error> {
Expand Down

0 comments on commit ff3fbef

Please sign in to comment.