Skip to content

Commit 3aed184

Browse files
author
Philip Guo
committed
first successful deployment
1 parent e2dd60d commit 3aed184

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎v4-cokapi/cokapi.js‎

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131

3232
// VERY IMPORTANT - turn on the sandbox when deploying online, or else
3333
// you'll be executing untrusted code on your server!
34-
var USE_DOCKER_SANDBOX = false;
34+
var USE_DOCKER_SANDBOX = true;
3535

3636
var assert = require('assert');
3737
var child_process = require('child_process');
@@ -78,47 +78,67 @@ var app = express();
7878
app.use(serveStatic('frontends/')); // put all static files in here
7979

8080
app.get('/exec_py2', function(req, res) {
81-
if (USE_DOCKER_SANDBOX) {
82-
assert(false);
83-
} else {
84-
executePython(PYTHON2_BIN, req, res);
85-
}
81+
executePython('py2', req, res);
8682
});
8783

8884
app.get('/exec_py3', function(req, res) {
89-
if (USE_DOCKER_SANDBOX) {
90-
assert(false);
91-
} else {
92-
executePython(PYTHON3_BIN, req, res);
93-
}
85+
executePython('py3', req, res);
9486
});
9587

9688
app.get('/exec_js', function(req, res) {
89+
var usrCod = req.query.user_script;
90+
91+
var exeFile;
92+
var args = [];
93+
9794
if (USE_DOCKER_SANDBOX) {
98-
assert(false);
95+
// this needs to match the docker setup in Dockerfile
96+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
97+
args.push('run', '--rm', 'pgbovine/cokapi:v1',
98+
'node',
99+
'--expose-debug-as=Debug',
100+
'/tmp/javascript/jslogger.js');
99101
} else {
100-
var usrCod = req.query.user_script;
101-
var args = ['--expose-debug-as=Debug',
102-
'backends/javascript/jslogger.js',
103-
'--jsondump=true',
104-
'--code=' + usrCod];
105-
106-
child_process.execFile(NODE_BIN, args,
107-
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
108-
maxBuffer: 2000 * 1024, /* 2MB data max */},
109-
postExecHandler.bind(null, res));
102+
exeFile = NODE_BIN;
103+
args.push('--expose-debug-as=Debug',
104+
'backends/javascript/jslogger.js');
110105
}
106+
args.push('--jsondump=true');
107+
args.push('--code=' + usrCod);
108+
109+
child_process.execFile(exeFile, args,
110+
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
111+
maxBuffer: 2000 * 1024 /* 2MB data max */,
112+
// make SURE docker gets the kill signal;
113+
// this signal seems to allow docker to clean
114+
// up after itself to --rm the container, but
115+
// double-check with 'docker ps -a'
116+
killSignal: 'SIGINT'},
117+
postExecHandler.bind(null, res));
111118
});
112119

113-
function executePython(pythonExe, req, res) {
120+
function executePython(pyVer, req, res) {
114121
var parsedOptions = JSON.parse(req.query.options_json);
115122

116123
var usrCod = req.query.user_script;
117124
var usrInputJson = req.query.raw_input_json;
118125
var cumulativeMode = parsedOptions.cumulative_mode;
119126
var heapPrimitives = parsedOptions.heap_primitives;
120127

121-
var args = ['backends/python/generate_json_trace.py'];
128+
var exeFile;
129+
var args = [];
130+
131+
if (USE_DOCKER_SANDBOX) {
132+
// this needs to match the docker setup in Dockerfile
133+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
134+
args.push('run', '--rm', 'pgbovine/cokapi:v1',
135+
(pyVer == 'py2' ? 'python' : 'python3'),
136+
'/tmp/python/generate_json_trace.py');
137+
} else {
138+
exeFile = (pyVer == 'py2' ? PYTHON2_BIN : PYTHON3_BIN);
139+
args.push('backends/python/generate_json_trace.py');
140+
}
141+
122142
if (cumulativeMode) {
123143
args.push('-c');
124144
}
@@ -130,9 +150,14 @@ function executePython(pythonExe, req, res) {
130150
}
131151
args.push('--code=' + usrCod);
132152

133-
child_process.execFile(pythonExe, args,
153+
child_process.execFile(exeFile, args,
134154
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
135-
maxBuffer: 2000 * 1024, /* 2MB data max */},
155+
maxBuffer: 2000 * 1024 /* 2MB data max */,
156+
// make SURE docker gets the kill signal;
157+
// this signal seems to allow docker to clean
158+
// up after itself to --rm the container, but
159+
// double-check with 'docker ps -a'
160+
killSignal: 'SIGINT'},
136161
postExecHandler.bind(null, res));
137162
}
138163

‎v4-cokapi/frontends/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<br/>
130130
<a class="exampleLink" id="jsFactExLink" href="#">factorial</a> |
131131
<a class="exampleLink" id="jsDatatypesExLink" href="#">data types</a> |
132-
<a class="exampleLink" id="jsExceptionExLink" href="#">exception</a>
132+
<a class="exampleLink" id="jsExceptionExLink" href="#">exception</a> |
133133
<a class="exampleLink" id="jsClosureExLink" href="#">closure</a> |
134134
<a class="exampleLink" id="jsShadowingExLink" href="#">shadowing</a> |
135135
<a class="exampleLink" id="jsConstructorExLink" href="#">constructor</a> |

0 commit comments

Comments
 (0)