From ef9c3ae091df569cfb0d1072c2f49dfebb24ff20 Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Thu, 20 Sep 2018 12:04:34 -0500 Subject: [PATCH] add additional executors --- api/main.go | 27 ++++++++++++++++----------- docker/Dockerfile | 2 -- docker/execute | 18 +++++++++++++++--- docker/executors/c | 4 ++++ docker/executors/cpp | 4 ++++ docker/executors/python2 | 3 +++ docker/executors/python3 | 3 +++ 7 files changed, 45 insertions(+), 16 deletions(-) create mode 100755 docker/executors/c create mode 100755 docker/executors/cpp create mode 100755 docker/executors/python2 create mode 100755 docker/executors/python3 diff --git a/api/main.go b/api/main.go index 1de1890..78a6c41 100644 --- a/api/main.go +++ b/api/main.go @@ -2,13 +2,13 @@ package main import ( "bytes" - "fmt" - "time" "encoding/json" - "net/http" + "fmt" "io/ioutil" + "net/http" "os/exec" "strings" + "time" ) type inbound struct { @@ -33,6 +33,7 @@ func main() { } func Execute(res http.ResponseWriter, req *http.Request) { + var found = false res.Header().Set("Content-Type", "application/json") // get json @@ -41,26 +42,28 @@ func Execute(res http.ResponseWriter, req *http.Request) { message.Decode(&inbound) whitelist := []string{ - "python2", "python", + "python2", "python3", "ruby", "javascript", "js", "node", + "c", + "cpp", + "c++", } - found := false - // check if the supplied language is supported + // now calls function and returns for _, lang := range whitelist { if lang == inbound.Language { - found = true - break + launch(inbound, res) + return } } - // send an error if it isn't + // now only called when the language is not supported if !found { problem := problem{ Code: "unsupported_language", @@ -72,14 +75,16 @@ func Execute(res http.ResponseWriter, req *http.Request) { res.Write(pres) return } +} +func launch(inbound2 inbound, res http.ResponseWriter){ // write the code to temp dir filename := fmt.Sprintf("/tmp/%d.code", time.Now().UnixNano()) - ioutil.WriteFile(filename, []byte(inbound.Source), 0644) + ioutil.WriteFile(filename, []byte(inbound2.Source), 0644) // set up the execution - cmd := exec.Command("../docker/execute", inbound.Language, filename) + cmd := exec.Command("../docker/execute", inbound2.Language, filename) // capture out/err var stdout, stderr bytes.Buffer diff --git a/docker/Dockerfile b/docker/Dockerfile index cf8f30a..478c60a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -7,5 +7,3 @@ RUN apt-get -y install python RUN apt-get -y install python3 RUN apt-get -y install ruby RUN apt-get -y install nodejs - -CMD sleep infinity diff --git a/docker/execute b/docker/execute index 05b155c..5432409 100755 --- a/docker/execute +++ b/docker/execute @@ -5,6 +5,8 @@ #docker run --rm -it piston /usr/bin/ruby --version #docker run --rm -it piston /usr/bin/nodejs --version +dir="$( cd "$( dirname "$0" )" && pwd )" + if [ -z "$1" ]; then echo "invalid args" exit @@ -21,10 +23,10 @@ file=$(basename $abs) bin= case "$lang" in "python2") - bin=python2 + bin=executor_python2 ;; "python" | "python3") - bin=python3 + bin=executor_python3 ;; "ruby") bin=ruby @@ -32,6 +34,12 @@ case "$lang" in "javascript" | "js" | "node") bin=nodejs ;; +"c") + bin=executor_c + ;; +"cpp" | "c++") + bin=executor_cpp + ;; *) echo "invalid language" exit @@ -42,5 +50,9 @@ docker run \ --cpus=".5" \ --rm \ -v $abs:/$file \ + -v $dir/executors/python2:/executor_python2 \ + -v $dir/executors/python3:/executor_python3 \ + -v $dir/executors/c:/executor_c \ + -v $dir/executors/cpp:/executor_cpp \ piston \ - timeout -s HUP 3 $bin /$file 2>&1 + timeout -s HUP 3 /$bin /$file $file 2>&1 diff --git a/docker/executors/c b/docker/executors/c new file mode 100755 index 0000000..5a4c471 --- /dev/null +++ b/docker/executors/c @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc -o binary -x c $1 +./binary diff --git a/docker/executors/cpp b/docker/executors/cpp new file mode 100755 index 0000000..8ddd248 --- /dev/null +++ b/docker/executors/cpp @@ -0,0 +1,4 @@ +#!/bin/sh + +g++ -o binary -x c $1 +./binary diff --git a/docker/executors/python2 b/docker/executors/python2 new file mode 100755 index 0000000..de90a94 --- /dev/null +++ b/docker/executors/python2 @@ -0,0 +1,3 @@ +#!/bin/sh + +python2 $* diff --git a/docker/executors/python3 b/docker/executors/python3 new file mode 100755 index 0000000..5dafcf7 --- /dev/null +++ b/docker/executors/python3 @@ -0,0 +1,3 @@ +#!/bin/sh + +python3.6 $*