mirror of
https://github.com/engineer-man/piston.git
synced 2025-04-24 05:56:26 +02:00
move around files
This commit is contained in:
parent
11ccbd9e15
commit
5cf2d5853d
9 changed files with 7 additions and 8 deletions
56
lxc/execute.js
Normal file
56
lxc/execute.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
const { writeFileSync, unlinkSync, mkdirSync } = require('fs');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const OUTPUT_LIMIT = 65535;
|
||||
const LXC_ROOT = '/var/lib/lxc/piston/rootfs';
|
||||
|
||||
function execute(language, source, stdin = '', args = []) {
|
||||
return new Promise(resolve => {
|
||||
const id = new Date().getTime() + '_' + Math.floor(Math.random() * 10000000);
|
||||
|
||||
mkdirSync(`${LXC_ROOT}/tmp/${id}`);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/code.code`, source);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/stdin.stdin`, stdin);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/args.args`, args.join('\n'));
|
||||
|
||||
const process = spawn(__dirname + '/execute', [
|
||||
language.name,
|
||||
id,
|
||||
]);
|
||||
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
let output = '';
|
||||
|
||||
process.stderr.on('data', chunk => {
|
||||
if (stderr.length >= OUTPUT_LIMIT) return;
|
||||
|
||||
stderr += chunk;
|
||||
output += chunk;
|
||||
});
|
||||
|
||||
process.stdout.on('data', chunk => {
|
||||
if (stdout.length >= OUTPUT_LIMIT) return;
|
||||
|
||||
stdout += chunk;
|
||||
output += chunk;
|
||||
});
|
||||
|
||||
process.on('exit', code => {
|
||||
stderr = stderr.trim().substring(0, OUTPUT_LIMIT);
|
||||
stdout = stdout.trim().substring(0, OUTPUT_LIMIT);
|
||||
output = output.trim().substring(0, OUTPUT_LIMIT);
|
||||
|
||||
resolve({
|
||||
stdout,
|
||||
stderr,
|
||||
output,
|
||||
ran: code === 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
execute,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue