add additional executors

This commit is contained in:
Brian Seymour 2018-09-20 12:04:34 -05:00
parent 64eda86817
commit ef9c3ae091
7 changed files with 45 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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

4
docker/executors/c Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
gcc -o binary -x c $1
./binary

4
docker/executors/cpp Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
g++ -o binary -x c $1
./binary

3
docker/executors/python2 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
python2 $*

3
docker/executors/python3 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
python3.6 $*