From 08d8329d3d963bccc176ad6136d6bb3452bbb08c Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Sun, 7 Oct 2018 17:54:29 -0500 Subject: [PATCH] multi piston in progress --- api/main.go | 27 +++++++++++-- api/start | 2 +- docker/execute | 5 ++- docker/test.js | 1 + var/dock@.service | 45 ++++++++++++++++++++++ var/multidock | 3 ++ var/{systemd_prod_unit => piston@.service} | 4 +- 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 docker/test.js create mode 100644 var/dock@.service create mode 100644 var/multidock rename var/{systemd_prod_unit => piston@.service} (77%) diff --git a/api/main.go b/api/main.go index 876c8ab..b73979e 100644 --- a/api/main.go +++ b/api/main.go @@ -6,8 +6,10 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "os/exec" "strings" + "strconv" "time" ) @@ -27,10 +29,24 @@ type outbound struct { Output string `json:"output"` } -func main() { - http.HandleFunc("/execute", Execute) +var instance int - http.ListenAndServe("0.0.0.0:1337", nil) +func main() { + port := 2000 + + if len(os.Args) > 1 { + if i, err := strconv.Atoi(os.Args[1]); err == nil { + instance = i + port += i + } + } else { + instance = 0 + } + + fmt.Println("starting api on port", port) + + http.HandleFunc("/execute", Execute) + http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil) } func Execute(res http.ResponseWriter, req *http.Request) { @@ -93,6 +109,11 @@ func launch(request inbound, res http.ResponseWriter) { // set up the execution cmd := exec.Command("../docker/execute", args...) + cmd.Env = os.Environ() + + if instance > 0 { + cmd.Env = append(cmd.Env, fmt.Sprintf("SOCKET=unix:///var/run/docker-%d.sock", instance)) + } // capture out/err var stdout, stderr bytes.Buffer diff --git a/api/start b/api/start index 10f5da6..9cf6e69 100755 --- a/api/start +++ b/api/start @@ -1,3 +1,3 @@ #!/usr/bin/env bash -go run main.go +go run main.go $* diff --git a/docker/execute b/docker/execute index e989271..a0db8e6 100755 --- a/docker/execute +++ b/docker/execute @@ -16,6 +16,7 @@ filepath=$(realpath $2) file=$(basename $2) argpath="/tmp/$(date +%s%N).args" arg=$(basename $argpath) +socket=${SOCKET:-unix:///var/run/docker.sock} # write arg file echo "${@:3}" > $argpath @@ -63,7 +64,9 @@ case "$lang" in exit esac -docker run \ +docker \ + -H $socket \ + run \ -m 64m \ --network none \ --rm \ diff --git a/docker/test.js b/docker/test.js new file mode 100644 index 0000000..6687898 --- /dev/null +++ b/docker/test.js @@ -0,0 +1 @@ +console.log('hi') diff --git a/var/dock@.service b/var/dock@.service new file mode 100644 index 0000000..1acd190 --- /dev/null +++ b/var/dock@.service @@ -0,0 +1,45 @@ +[Unit] +Description=Docker Application Container Engine +Documentation=http://docs.docker.com +After=network.target +Wants=docker-storage-setup.service +Requires=docker-cleanup.timer + +[Service] +Type=notify +NotifyAccess=main +EnvironmentFile=-/run/containers/registries.conf +EnvironmentFile=-/etc/sysconfig/docker +EnvironmentFile=-/etc/sysconfig/docker-storage +EnvironmentFile=-/etc/sysconfig/docker-network +Environment=GOTRACEBACK=crash +Environment=DOCKER_HTTP_HOST_COMPAT=1 +Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin +ExecStart=/usr/bin/dockerd-current \ + --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \ + --default-runtime=docker-runc \ + --exec-opt native.cgroupdriver=systemd \ + --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \ + --init-path=/usr/libexec/docker/docker-init-current \ + --seccomp-profile=/etc/docker/seccomp.json \ + --exec-root=/var/run/docker-%i \ + --graph=/var/lib/docker-%i \ + --host unix:///var/run/docker-%i.sock \ + --pidfile /var/run/docker-%i.pid \ + $OPTIONS \ + $DOCKER_STORAGE_OPTIONS \ + $DOCKER_NETWORK_OPTIONS \ + $ADD_REGISTRY \ + $BLOCK_REGISTRY \ + $INSECURE_REGISTRY \ + $REGISTRIES +ExecReload=/bin/kill -s HUP $MAINPID +LimitNOFILE=1048576 +LimitNPROC=1048576 +LimitCORE=infinity +TimeoutStartSec=0 +Restart=on-abnormal +KillMode=process + +[Install] +WantedBy=multi-user.target diff --git a/var/multidock b/var/multidock new file mode 100644 index 0000000..4aa7ebc --- /dev/null +++ b/var/multidock @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +docker -H unix:///var/run/docker-$1.sock ${@:2} diff --git a/var/systemd_prod_unit b/var/piston@.service similarity index 77% rename from var/systemd_prod_unit rename to var/piston@.service index f6e6ae5..c105794 100644 --- a/var/systemd_prod_unit +++ b/var/piston@.service @@ -1,12 +1,12 @@ [Unit] -Description=Piston +Description=Piston API [Service] Type=simple User=root Environment=PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin WorkingDirectory=/root/piston/api -ExecStart=/root/piston/api/start +ExecStart=/root/piston/api/start %i Restart=on-failure [Install]