Skip to content

Commit 08189f3

Browse files
committed
updated cokapi documentation and Makefiles/Dockerfiles to be more modular
1 parent 13efd89 commit 08189f3

9 files changed

Lines changed: 142 additions & 134 deletions

File tree

‎v4-cokapi/Dockerfile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# TODO: gradually peel off backends from here into their own separate
2-
# Docker images. right now this is for the Java and Ruby backends
1+
# 2016-06-22: THIS FILE IS NOW DEPRECATED. See Dockerfiles in backends/
2+
# for each individual language backend
33

44
# to build:
55
# docker build -t="pgbovine/cokapi:v1" .

‎v4-cokapi/Makefile‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,3 @@ prod:
99
deps:
1010
npm install express
1111
#npm install express jshint serve-static sqlite3
12-
13-
docker:
14-
docker build -t="pgbovine/cokapi:v1" .
15-
16-
dockerbash:
17-
docker run -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi:v1 bash
18-
19-
test:
20-
@echo 'Python'
21-
@time --portability docker run --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi:v1 python /tmp/python/generate_json_trace.py --code="x=1; y=2; z=x+y"
22-
@echo
23-
@echo 'JS'
24-
@time --portability docker run --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi:v1 /tmp/javascript/node-v6.0.0-linux-x64/bin/node --expose-debug-as=Debug /tmp/javascript/jslogger.js --jsondump=true --code="var x=1; var y=2; var z=x+y;"
25-
@echo
26-
@echo 'Java'
27-
@time --portability docker run -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi:v1 java -cp /tmp/java_jail/cp:/tmp/java_jail/cp/javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar traceprinter.InMemory < backends/java/java_jail/cp/traceprinter/test-input.txt
28-
@echo
29-
@echo 'Ruby'
30-
@time --portability docker run -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi:v1 /tmp/ruby/ruby /tmp/ruby/pg_logger.rb -c "x = 1; y = 2"

‎v4-cokapi/backends/java/Dockerfile‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# to build:
2+
# docker build -t="pgbovine/cokapi-java:v1" .
3+
#
4+
# to test:
5+
# docker run -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-java:v1 bash
6+
#
7+
# remember --rm or else stale old containers will be left around!
8+
# use "docker ps -a" to see all containers
9+
10+
# don't use 'latest' tag since that might change
11+
FROM ubuntu:14.04.1
12+
MAINTAINER Philip Guo <philip@pgbovine.net>
13+
14+
# if apt-get doesn't work, then follow these instructions:
15+
# http://stackoverflow.com/questions/24991136/docker-build-could-not-resolve-archive-ubuntu-com-apt-get-fails-to-install-a
16+
# Uncomment the following line in /etc/default/docker DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
17+
# Restart the Docker service sudo service docker restart
18+
RUN apt-get update && apt-get install -y \
19+
openjdk-7-jdk
20+
21+
RUN mkdir /tmp/java_jail
22+
RUN mkdir /tmp/java_jail/cp
23+
ADD java_jail/cp/ /tmp/java_jail/cp
24+
25+
# compile all Java files within cp/
26+
RUN cd /tmp/java_jail/cp && find . -name "*.java" -print | xargs javac -Xlint:unchecked -cp .:javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar
27+
28+
COPY run-java-backend.sh /tmp/
29+
RUN chmod a+x /tmp/run-java-backend.sh
30+
31+
RUN useradd netuser

‎v4-cokapi/backends/java/Makefile‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# build a docker container
12
all:
2-
cd java_jail/cp && find . -name "*.java" -print | xargs javac -cp .:javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar
3+
docker build -t="pgbovine/cokapi-java:v1" .
34

5+
# test running the Java backend in the docker container on a simple test input JSON passed as a command-line arg:
46
test:
5-
java -Xmx128M -cp java_jail/cp:java_jail/cp/javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar:java_jail/cp/visualizer-stdlib traceprinter.InMemory < java_jail/cp/traceprinter/test-input.txt
7+
docker run -m 512M --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-java:v1 /tmp/run-java-backend.sh '{"usercode": "public class Test { public static void main(String[] args) { int x=42; x+=1; x+=1; x+=1;} }", "options": {}, "args": [], "stdin": ""}'
68

7-
bigtest:
8-
java -Xmx128M -cp java_jail/cp:java_jail/cp/javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar:java_jail/cp/visualizer-stdlib traceprinter.InMemory < big-input.txt
9+
bash:
10+
docker run -m 512M -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-java:v1 bash

‎v4-cokapi/backends/java/README‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ Checked out my own copy of https://github.com/daveagp/java_jail in early
22
Feb 2014 into here as java_jail/, then made some local tweaks. (I didn't
33
use git submodules since I couldn't figure out how to check in my local
44
tweaks in a sensible way, ugh.)
5+
6+
---
7+
Create a Docker container using "make" and then test it using "make test"
8+
9+
Use ../../cokapi.js as the webserver to invoke the Docker container

‎v4-cokapi/backends/java/run-java-backend.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# java executable WITHOUT interpreting escape chars such as '\n' ...
1010
# echo doesn't work here since it interprets '\n' and other chars
1111
#
12-
# TODO: use -Xmx512m if we need more memory
13-
cat <<ENDEND | java -Xmx384m -cp /tmp/java_jail/cp:/tmp/java_jail/cp/javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar:/tmp/java_jail/cp/visualizer-stdlib traceprinter.InMemory
12+
# TODO: try different -Xmx512m memory settings depending on user needs
13+
cat <<ENDEND | java -Xmx512m -cp /tmp/java_jail/cp:/tmp/java_jail/cp/javax.json-1.0.jar:/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar:/tmp/java_jail/cp/visualizer-stdlib traceprinter.InMemory
1414
$1
1515
ENDEND

‎v4-cokapi/backends/ruby/Dockerfile‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# to build:
2+
# docker build -t="pgbovine/cokapi-ruby:v1" .
3+
#
4+
# to test:
5+
# docker run -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-ruby:v1 bash
6+
#
7+
# remember --rm or else stale old containers will be left around!
8+
# use "docker ps -a" to see all containers
9+
10+
# don't use 'latest' tag since that might change
11+
FROM ubuntu:14.04.1
12+
MAINTAINER Philip Guo <philip@pgbovine.net>
13+
14+
# if apt-get doesn't work, then follow these instructions:
15+
# http://stackoverflow.com/questions/24991136/docker-build-could-not-resolve-archive-ubuntu-com-apt-get-fails-to-install-a
16+
# Uncomment the following line in /etc/default/docker DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
17+
# Restart the Docker service sudo service docker restart
18+
#
19+
# lots of packages required especially to build ruby and to use the compiled version of 'gem'
20+
RUN apt-get update && apt-get install -y \
21+
python \
22+
build-essential \
23+
zlibc \
24+
zlib1g \
25+
zlib1g-dev \
26+
libssl-dev
27+
28+
29+
# Ruby backend: pretty complex setup since we need to compile and
30+
# install a custom Ruby interpreter
31+
#
32+
# run all of this stuff early so that we don't invalidate the cache
33+
RUN mkdir /tmp/ruby
34+
# automatically untars
35+
ADD custom-ruby-interpreter/ruby-2.2.2.tar.gz /tmp/ruby
36+
ADD custom-ruby-interpreter/altered-files /tmp/ruby/ruby-2.2.2
37+
RUN mkdir /tmp/ruby/ruby-2.2.2-custom-OPT/
38+
RUN cd /tmp/ruby/ruby-2.2.2 && ./configure --prefix=/tmp/ruby/ruby-2.2.2-custom-OPT/
39+
# beware takes a long time! so make sure we cache the build
40+
RUN cd /tmp/ruby/ruby-2.2.2 && make && make install
41+
# install custom gem
42+
RUN /tmp/ruby/ruby-2.2.2-custom-OPT/bin/gem install debug_inspector
43+
44+
COPY pg_logger.rb /tmp/ruby/
45+
RUN cd /tmp/ruby && ln -s ruby-2.2.2-custom-OPT/bin/ruby
46+
47+
# To properly run Ruby tests, become root since it needs to write files (maybe ?):
48+
# $ docker run -t -i --rm --net=none --cap-drop all pgbovine/cokapi:v1 bash
49+
#COPY golden_test.py /tmp/ruby/
50+
#RUN mkdir /tmp/ruby/tests
51+
#ADD tests /tmp/ruby/tests
52+
53+
RUN useradd netuser

‎v4-cokapi/backends/ruby/Makefile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# build a docker container
2+
all:
3+
docker build -t="pgbovine/cokapi-ruby:v1" .
4+
5+
test:
6+
docker run -m 512M --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-ruby:v1 /tmp/ruby/ruby /tmp/ruby/pg_logger.rb -c "x = 1; y = 2; z = 3;"
7+
8+
bash:
9+
docker run -m 512M -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-ruby:v1 bash

‎v4-cokapi/cokapi.js‎

Lines changed: 34 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727
*/
2828

29-
// This is a nodejs server based on express that serves the v4-cokapi/ app
29+
// This is a nodejs server based on express that serves the v4-cokapi/ app,
30+
// which implements OPT language backends for languages such as Java,
31+
// Ruby, JavaScript, TypeScript, C, C++, ...
32+
3033
// To test locally, run 'make' and load http://localhost:3000/
3134

3235
// Run with an 'https' command-line flag to use https (must have
@@ -38,10 +41,6 @@ var PRODUCTION_PORT = 3000;
3841
var PRODUCTION_HTTPS_PORT = 8001;
3942
var DEBUG_PORT = 5001;
4043

41-
// VERY IMPORTANT - turn on the sandbox when deploying online, or else
42-
// you'll be executing untrusted code on your server!
43-
var USE_DOCKER_SANDBOX = true;
44-
4544
var assert = require('assert');
4645
var child_process = require('child_process');
4746
var express = require('express');
@@ -63,7 +62,7 @@ var CPP_TIMEOUT_SECS = 15; // the C/C++ backend is also SUPER SLOW :/
6362

6463
var MAX_BUFFER_SIZE = 10 * 1024 * 1024;
6564

66-
var MEM_LIMIT = "256M";
65+
var MEM_LIMIT = "512M";
6766

6867

6968
// bind() res and useJSONP before using
@@ -119,14 +118,6 @@ var app = express();
119118
// http://ilee.co.uk/jsonp-in-express-nodejs/
120119
app.set("jsonp callback", true);
121120

122-
app.get('/exec_py2', function(req, res) {
123-
executePython('py2', req, res);
124-
});
125-
126-
app.get('/exec_py3', function(req, res) {
127-
executePython('py3', req, res);
128-
});
129-
130121
app.get('/exec_js', exec_js_handler.bind(null, false, false));
131122
app.get('/exec_js_jsonp', exec_js_handler.bind(null, true, false));
132123

@@ -139,20 +130,13 @@ function exec_js_handler(useJSONP /* use bind first */, isTypescript /* use bind
139130
var exeFile;
140131
var args = [];
141132

142-
if (USE_DOCKER_SANDBOX) {
143-
// this needs to match the docker setup in Dockerfile
144-
exeFile = '/usr/bin/docker'; // absolute path to docker executable
145-
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi-js:v1',
146-
//'node', // built-in Node.js version
147-
'/tmp/javascript/node-v6.0.0-linux-x64/bin/node', // custom Node.js version
148-
'--expose-debug-as=Debug',
149-
'/tmp/javascript/jslogger.js');
150-
} else {
151-
assert(false);
152-
//exeFile = NODE_BIN;
153-
//args.push('--expose-debug-as=Debug',
154-
// 'backends/javascript/jslogger.js');
155-
}
133+
// must match the docker setup in backends/javascript/Dockerfile
134+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
135+
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi-js:v1',
136+
'/tmp/javascript/node-v6.0.0-linux-x64/bin/node', // custom Node.js version
137+
'--expose-debug-as=Debug',
138+
'/tmp/javascript/jslogger.js');
139+
156140
if (isTypescript) {
157141
args.push('--typescript=true');
158142
}
@@ -170,6 +154,7 @@ function exec_js_handler(useJSONP /* use bind first */, isTypescript /* use bind
170154
postExecHandler.bind(null, res, useJSONP));
171155
}
172156

157+
173158
app.get('/exec_java', exec_java_handler.bind(null, false));
174159
app.get('/exec_java_jsonp', exec_java_handler.bind(null, true));
175160

@@ -199,15 +184,11 @@ function exec_java_handler(useJSONP /* use bind first */, req, res) {
199184

200185
var inputObjJSON = JSON.stringify(inputObj);
201186

202-
if (USE_DOCKER_SANDBOX) {
203-
// this needs to match the docker setup in Dockerfile
204-
exeFile = '/usr/bin/docker'; // absolute path to docker executable
205-
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi:v1',
206-
'/tmp/run-java-backend.sh',
207-
inputObjJSON);
208-
} else {
209-
assert(false);
210-
}
187+
// must match the docker setup in backends/java/Dockerfile
188+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
189+
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi-java:v1',
190+
'/tmp/run-java-backend.sh',
191+
inputObjJSON);
211192

212193
child_process.execFile(exeFile, args,
213194
{timeout: JAVA_TIMEOUT_SECS * 1000 /* milliseconds */,
@@ -220,6 +201,7 @@ function exec_java_handler(useJSONP /* use bind first */, req, res) {
220201
postExecHandler.bind(null, res, useJSONP));
221202
}
222203

204+
223205
app.get('/exec_ruby', exec_ruby_handler.bind(null, false));
224206
app.get('/exec_ruby_jsonp', exec_ruby_handler.bind(null, true));
225207

@@ -229,18 +211,13 @@ function exec_ruby_handler(useJSONP /* use bind first */, req, res) {
229211
var exeFile;
230212
var args = [];
231213

232-
if (USE_DOCKER_SANDBOX) {
233-
// this needs to match the docker setup in Dockerfile
234-
exeFile = '/usr/bin/docker'; // absolute path to docker executable
235-
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi:v1',
236-
'/tmp/ruby/ruby',
237-
'/tmp/ruby/pg_logger.rb',
238-
'-c',
239-
usrCod);
240-
} else {
241-
assert(false);
242-
// must use Docker
243-
}
214+
// must match the docker setup in backends/ruby/Dockerfile
215+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
216+
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi-ruby:v1',
217+
'/tmp/ruby/ruby',
218+
'/tmp/ruby/pg_logger.rb',
219+
'-c',
220+
usrCod);
244221

245222
child_process.execFile(exeFile, args,
246223
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
@@ -253,6 +230,7 @@ function exec_ruby_handler(useJSONP /* use bind first */, req, res) {
253230
postExecHandler.bind(null, res, useJSONP));
254231
}
255232

233+
256234
app.get('/exec_c', exec_cpp_handler.bind(null, false, false));
257235
app.get('/exec_c_jsonp', exec_cpp_handler.bind(null, false, true));
258236
app.get('/exec_cpp', exec_cpp_handler.bind(null, true, false));
@@ -264,18 +242,13 @@ function exec_cpp_handler(useCPP /* use bind first */, useJSONP /* use bind firs
264242
var exeFile;
265243
var args = [];
266244

267-
if (USE_DOCKER_SANDBOX) {
268-
// this needs to match the docker setup in Dockerfile
269-
exeFile = '/usr/bin/docker'; // absolute path to docker executable
270-
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/opt-cpp-backend:v1',
271-
'python',
272-
'/tmp/opt-cpp-backend/run_cpp_backend.py',
273-
usrCod,
274-
useCPP ? 'cpp' : 'c');
275-
} else {
276-
assert(false);
277-
// must use Docker
278-
}
245+
// this needs to match the docker setup in opt-cpp-backend/Dockerfile (in the https://github.com/pgbovine/opt-cpp-backend repo)
246+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
247+
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/opt-cpp-backend:v1',
248+
'python',
249+
'/tmp/opt-cpp-backend/run_cpp_backend.py',
250+
usrCod,
251+
useCPP ? 'cpp' : 'c');
279252

280253
child_process.execFile(exeFile, args,
281254
{timeout: CPP_TIMEOUT_SECS * 1000 /* milliseconds */,
@@ -289,52 +262,6 @@ function exec_cpp_handler(useCPP /* use bind first */, useJSONP /* use bind firs
289262
}
290263

291264

292-
293-
function executePython(pyVer, req, res) {
294-
var parsedOptions = JSON.parse(req.query.options_json);
295-
296-
var usrCod = req.query.user_script;
297-
var usrInputJson = req.query.raw_input_json;
298-
var cumulativeMode = parsedOptions.cumulative_mode;
299-
var heapPrimitives = parsedOptions.heap_primitives;
300-
301-
var exeFile;
302-
var args = [];
303-
304-
if (USE_DOCKER_SANDBOX) {
305-
// this needs to match the docker setup in Dockerfile
306-
exeFile = '/usr/bin/docker'; // absolute path to docker executable
307-
args.push('run', '-m', MEM_LIMIT, '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/cokapi:v1',
308-
(pyVer == 'py2' ? 'python' : 'python3'),
309-
'/tmp/python/generate_json_trace.py');
310-
} else {
311-
assert(false);
312-
//exeFile = (pyVer == 'py2' ? PYTHON2_BIN : PYTHON3_BIN);
313-
//args.push('backends/python/generate_json_trace.py');
314-
}
315-
316-
if (cumulativeMode) {
317-
args.push('-c');
318-
}
319-
if (heapPrimitives) {
320-
args.push('-p');
321-
}
322-
if (usrInputJson) {
323-
args.push('--input=' + usrInputJson);
324-
}
325-
args.push('--code=' + usrCod);
326-
327-
child_process.execFile(exeFile, args,
328-
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
329-
maxBuffer: MAX_BUFFER_SIZE,
330-
// make SURE docker gets the kill signal;
331-
// this signal seems to allow docker to clean
332-
// up after itself to --rm the container, but
333-
// double-check with 'docker ps -a'
334-
killSignal: 'SIGINT'},
335-
postExecHandler.bind(null, res, false));
336-
}
337-
338265
// https support
339266
var https = require('https');
340267
var fs = require('fs');

0 commit comments

Comments
 (0)