piston/api/src/executor/job.js

161 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-02-20 23:39:03 +01:00
const logger = require('logplease').create('executor/job');
const { v4: uuidv4 } = require('uuid');
const cp = require('child_process');
const path = require('path');
const config = require('../config');
const globals = require('../globals');
const fs = require('fs/promises');
2021-02-20 15:13:56 +01:00
const job_states = {
2021-02-20 23:39:03 +01:00
READY: Symbol('Ready to be primed'),
PRIMED: Symbol('Primed and ready for execution'),
EXECUTED: Symbol('Executed and ready for cleanup')
};
2021-02-20 15:13:56 +01:00
var uid=0;
var gid=0;
class Job {
constructor(runtime, files, args, stdin, timeouts, main){
2021-02-20 23:39:03 +01:00
this.uuid = uuidv4();
this.runtime = runtime;
this.files = files;
this.args = args;
this.stdin = stdin;
this.timeouts = timeouts;
this.main = main;
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
if(!this.files.map(f=>f.name).includes(this.main))
throw new Error(`Main file "${this.main}" will not be written to disk`);
2021-02-20 15:13:56 +01:00
this.uid = config.runner_uid_min + uid;
this.gid = config.runner_gid_min + gid;
2021-02-20 23:39:03 +01:00
uid++;
gid++;
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
uid %= (config.runner_uid_max - config.runner_uid_min) + 1;
gid %= (config.runner_gid_max - config.runner_gid_min) + 1;
2021-02-20 15:13:56 +01:00
this.state = job_states.READY;
this.dir = path.join(config.data_directory, globals.data_directories.jobs, this.uuid);
}
async prime(){
2021-02-20 23:39:03 +01:00
logger.info(`Priming job uuid=${this.uuid}`);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.debug('Writing files to job cache');
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await fs.mkdir(this.dir, {mode:0o700});
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const files = this.files.map(({name: file_name, content}) => {
return fs.write_file(path.join(this.dir, file_name), content);
});
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await Promise.all(files);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.debug(`Transfering ownership uid=${this.uid} gid=${this.gid}`);
await fs.chown(this.dir, this.uid, this.gid);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const chowns = this.files.map(({name:file_name}) => {
return fs.chown(path.join(this.dir, file_name), this.uid, this.gid);
});
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await Promise.all(chowns);
2021-02-20 15:13:56 +01:00
this.state = job_states.PRIMED;
2021-02-20 23:39:03 +01:00
logger.debug('Primed job');
2021-02-20 15:13:56 +01:00
}
async execute(){
2021-02-20 23:39:03 +01:00
if(this.state != job_states.PRIMED) throw new Error('Job must be in primed state, current state: ' + this.state.toString());
logger.info(`Executing job uuid=${this.uuid} uid=${this.uid} gid=${this.gid} runtime=${this.runtime.toString()}`);
logger.debug('Compiling');
2021-02-20 15:13:56 +01:00
const compile = this.runtime.compiled && await new Promise((resolve, reject) => {
2021-02-21 09:36:49 +01:00
const proc_call = ['unshare', '-n', '-r', 'bash', path.join(this.runtime.pkgdir, 'compile'),this.main, ...this.files].slice(!config.enable_unshare * 3)
var stdout = '';
var stderr = '';
const proc = cp.spawn(proc_call[0], proc_call.splice(1) ,{
2021-02-20 15:13:56 +01:00
env: this.runtime.env_vars,
stdio: ['pipe', 'pipe', 'pipe'],
cwd: this.dir,
uid: this.uid,
gid: this.gid
2021-02-20 23:39:03 +01:00
});
2021-02-20 15:13:56 +01:00
2021-02-21 09:36:49 +01:00
const kill_timeout = setTimeout(_ => proc.kill('SIGKILL'), this.timeouts.compile);
2021-02-20 15:13:56 +01:00
2021-02-21 09:36:49 +01:00
proc.stderr.on('data', d=>{if(stderr.length>config.output_max_size) proc.kill('SIGKILL'); else stderr += d;});
proc.stdout.on('data', d=>{if(stdout.length>config.output_max_size) proc.kill('SIGKILL'); else stdout += d;});
2021-02-20 15:13:56 +01:00
proc.on('exit', (code, signal)=>{
2021-02-20 23:39:03 +01:00
clearTimeout(kill_timeout);
2021-02-21 09:36:49 +01:00
proc.stderr.destroy()
proc.stdout.destroy()
2021-02-20 23:39:03 +01:00
resolve({stdout, stderr, code, signal});
});
2021-02-20 15:13:56 +01:00
2021-02-21 01:32:35 +01:00
proc.on('error', (err) => {
2021-02-20 23:39:03 +01:00
clearTimeout(kill_timeout);
2021-02-21 09:36:49 +01:00
proc.stderr.destroy()
proc.stdout.destroy()
2021-02-21 01:32:35 +01:00
reject({error: err, stdout, stderr});
2021-02-20 23:39:03 +01:00
});
});
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.debug('Running');
2021-02-20 15:13:56 +01:00
const run = await new Promise((resolve, reject) => {
2021-02-21 09:36:49 +01:00
const proc_call = ['unshare', '-n', '-r', 'bash', path.join(this.runtime.pkgdir, 'run'), this.main, ...this.args].slice(!config.enable_unshare * 3);
2021-02-21 01:32:35 +01:00
var stdout = '';
var stderr = '';
2021-02-21 09:36:49 +01:00
const proc = cp.spawn(proc_call[0], proc_call.slice(1) ,{
2021-02-20 15:13:56 +01:00
env: this.runtime.env_vars,
stdio: ['pipe', 'pipe', 'pipe'],
cwd: this.dir,
uid: this.uid,
gid: this.gid
2021-02-20 23:39:03 +01:00
});
2021-02-21 09:36:49 +01:00
const kill_timeout = setTimeout(_ => proc.kill('SIGKILL'), this.timeouts.run);
2021-02-20 15:13:56 +01:00
2021-02-21 09:36:49 +01:00
proc.stderr.on('data', d=>{if(stderr.length>config.output_max_size) proc.kill('SIGKILL'); else stderr += d;});
proc.stdout.on('data', d=>{if(stdout.length>config.output_max_size) proc.kill('SIGKILL'); else stdout += d;});
2021-02-20 15:13:56 +01:00
2021-02-21 09:36:49 +01:00
proc.stdin.write(this.stdin)
proc.stdin.end()
2021-02-20 15:13:56 +01:00
proc.on('exit', (code, signal)=>{
2021-02-20 23:39:03 +01:00
clearTimeout(kill_timeout);
2021-02-21 09:36:49 +01:00
proc.stderr.destroy()
proc.stdout.destroy()
2021-02-20 23:39:03 +01:00
resolve({stdout, stderr, code, signal});
});
2021-02-20 15:13:56 +01:00
2021-02-21 01:32:35 +01:00
proc.on('error', (err) => {
2021-02-20 23:39:03 +01:00
clearTimeout(kill_timeout);
2021-02-21 09:36:49 +01:00
proc.stderr.destroy()
proc.stdout.destroy()
2021-02-21 01:32:35 +01:00
reject({error: err, stdout, stderr});
2021-02-20 23:39:03 +01:00
});
});
2021-02-20 15:13:56 +01:00
this.state = job_states.EXECUTED;
return {
compile,
run
2021-02-20 23:39:03 +01:00
};
2021-02-20 15:13:56 +01:00
}
async cleanup(){
2021-02-20 23:39:03 +01:00
logger.info(`Cleaning up job uuid=${this.uuid}`);
await fs.rm(this.dir, {recursive: true, force: true});
2021-02-20 15:13:56 +01:00
}
}
2021-02-20 23:39:03 +01:00
module.exports = {Job};