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 ( import (
"bytes" "bytes"
"fmt"
"time"
"encoding/json" "encoding/json"
"net/http" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"os/exec" "os/exec"
"strings" "strings"
"time"
) )
type inbound struct { type inbound struct {
@ -33,6 +33,7 @@ func main() {
} }
func Execute(res http.ResponseWriter, req *http.Request) { func Execute(res http.ResponseWriter, req *http.Request) {
var found = false
res.Header().Set("Content-Type", "application/json") res.Header().Set("Content-Type", "application/json")
// get json // get json
@ -41,26 +42,28 @@ func Execute(res http.ResponseWriter, req *http.Request) {
message.Decode(&inbound) message.Decode(&inbound)
whitelist := []string{ whitelist := []string{
"python2",
"python", "python",
"python2",
"python3", "python3",
"ruby", "ruby",
"javascript", "javascript",
"js", "js",
"node", "node",
"c",
"cpp",
"c++",
} }
found := false
// check if the supplied language is supported // check if the supplied language is supported
// now calls function and returns
for _, lang := range whitelist { for _, lang := range whitelist {
if lang == inbound.Language { if lang == inbound.Language {
found = true launch(inbound, res)
break return
} }
} }
// send an error if it isn't // now only called when the language is not supported
if !found { if !found {
problem := problem{ problem := problem{
Code: "unsupported_language", Code: "unsupported_language",
@ -72,14 +75,16 @@ func Execute(res http.ResponseWriter, req *http.Request) {
res.Write(pres) res.Write(pres)
return return
} }
}
func launch(inbound2 inbound, res http.ResponseWriter){
// write the code to temp dir // write the code to temp dir
filename := fmt.Sprintf("/tmp/%d.code", time.Now().UnixNano()) 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 // set up the execution
cmd := exec.Command("../docker/execute", inbound.Language, filename) cmd := exec.Command("../docker/execute", inbound2.Language, filename)
// capture out/err // capture out/err
var stdout, stderr bytes.Buffer 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 python3
RUN apt-get -y install ruby RUN apt-get -y install ruby
RUN apt-get -y install nodejs 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/ruby --version
#docker run --rm -it piston /usr/bin/nodejs --version #docker run --rm -it piston /usr/bin/nodejs --version
dir="$( cd "$( dirname "$0" )" && pwd )"
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "invalid args" echo "invalid args"
exit exit
@ -21,10 +23,10 @@ file=$(basename $abs)
bin= bin=
case "$lang" in case "$lang" in
"python2") "python2")
bin=python2 bin=executor_python2
;; ;;
"python" | "python3") "python" | "python3")
bin=python3 bin=executor_python3
;; ;;
"ruby") "ruby")
bin=ruby bin=ruby
@ -32,6 +34,12 @@ case "$lang" in
"javascript" | "js" | "node") "javascript" | "js" | "node")
bin=nodejs bin=nodejs
;; ;;
"c")
bin=executor_c
;;
"cpp" | "c++")
bin=executor_cpp
;;
*) *)
echo "invalid language" echo "invalid language"
exit exit
@ -42,5 +50,9 @@ docker run \
--cpus=".5" \ --cpus=".5" \
--rm \ --rm \
-v $abs:/$file \ -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 \ 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 $*