2021-02-20 23:39:03 +01:00
|
|
|
const logger = require('logplease').create('executor/job');
|
2021-03-13 08:57:21 +01:00
|
|
|
const {v4: uuidv4} = require('uuid');
|
2021-02-20 23:39:03 +01:00
|
|
|
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
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
let uid = 0;
|
|
|
|
let gid = 0;
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
class Job {
|
2021-03-13 06:01:04 +01:00
|
|
|
|
2021-04-23 01:52:50 +02:00
|
|
|
constructor({ runtime, files, args, stdin, timeouts, alias }) {
|
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;
|
2021-03-13 08:57:21 +01:00
|
|
|
this.alias = alias;
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
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
|
|
|
logger.debug(`Transfering ownership uid=${this.uid} gid=${this.gid}`);
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
await fs.mkdir(this.dir, { mode:0o700 });
|
2021-02-20 23:39:03 +01:00
|
|
|
await fs.chown(this.dir, this.uid, this.gid);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
for (const file of this.files) {
|
|
|
|
let file_path = path.join(this.dir, file.name);
|
|
|
|
|
|
|
|
await fs.write_file(file_path, file.content);
|
|
|
|
await fs.chown(file_path, this.uid, this.gid);
|
|
|
|
}
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
this.state = job_states.PRIMED;
|
2021-03-13 06:01:04 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Primed job');
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
async safe_call(file, args, timeout) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-04-07 02:31:30 +02:00
|
|
|
const nonetwork = config.disable_networking ? ['nosocket'] : [];
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-02-22 10:56:54 +01:00
|
|
|
const prlimit = [
|
|
|
|
'prlimit',
|
|
|
|
'--nproc=' + config.max_process_count,
|
|
|
|
'--nofile=' + config.max_open_files
|
|
|
|
];
|
2021-02-22 10:51:19 +01:00
|
|
|
|
|
|
|
const proc_call = [
|
|
|
|
...prlimit,
|
2021-04-07 02:31:30 +02:00
|
|
|
...nonetwork,
|
2021-03-14 03:33:29 +01:00
|
|
|
'bash',file,
|
|
|
|
...args
|
2021-02-22 10:51:19 +01:00
|
|
|
];
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-02-21 09:36:49 +01:00
|
|
|
var stdout = '';
|
|
|
|
var stderr = '';
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-02-21 09:36:49 +01:00
|
|
|
const proc = cp.spawn(proc_call[0], proc_call.splice(1) ,{
|
2021-03-13 08:57:21 +01:00
|
|
|
env: {
|
|
|
|
...this.runtime.env_vars,
|
|
|
|
PISTON_ALIAS: this.alias
|
|
|
|
},
|
2021-02-22 09:55:51 +01:00
|
|
|
stdio: 'pipe',
|
2021-02-20 15:13:56 +01:00
|
|
|
cwd: this.dir,
|
|
|
|
uid: this.uid,
|
2021-02-22 09:55:51 +01:00
|
|
|
gid: this.gid,
|
2021-03-05 07:29:09 +01:00
|
|
|
detached: true //give this process its own process group
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-03-13 06:01:04 +01:00
|
|
|
|
2021-02-23 07:52:49 +01:00
|
|
|
proc.stdin.write(this.stdin);
|
|
|
|
proc.stdin.end();
|
2021-02-22 09:55:51 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
const kill_timeout = set_timeout(_ => proc.kill('SIGKILL'), timeout);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
proc.stderr.on('data', data => {
|
|
|
|
if (stderr.length > config.output_max_size) {
|
|
|
|
proc.kill('SIGKILL');
|
|
|
|
} else {
|
|
|
|
stderr += data;
|
|
|
|
}
|
2021-03-13 08:57:21 +01:00
|
|
|
});
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
proc.stdout.on('data', data => {
|
|
|
|
if (stdout.length > config.output_max_size) {
|
|
|
|
proc.kill('SIGKILL');
|
|
|
|
} else {
|
|
|
|
stdout += data;
|
|
|
|
}
|
2021-03-13 08:57:21 +01:00
|
|
|
});
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
const exit_cleanup = () => {
|
|
|
|
clear_timeout(kill_timeout);
|
2021-02-23 07:52:49 +01:00
|
|
|
|
2021-02-22 10:51:19 +01:00
|
|
|
proc.stderr.destroy();
|
|
|
|
proc.stdout.destroy();
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
try {
|
2021-02-22 10:51:19 +01:00
|
|
|
process.kill(-proc.pid, 'SIGKILL');
|
2021-03-13 06:01:04 +01:00
|
|
|
} catch {
|
2021-03-05 07:29:09 +01:00
|
|
|
// Process will be dead already, so nothing to kill.
|
2021-02-22 10:51:19 +01:00
|
|
|
}
|
2021-03-13 06:01:04 +01:00
|
|
|
};
|
2021-02-22 09:55:51 +01:00
|
|
|
|
|
|
|
proc.on('exit', (code, signal)=>{
|
2021-02-22 10:51:19 +01:00
|
|
|
exit_cleanup();
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
resolve({ stdout, stderr, code, signal });
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-21 01:32:35 +01:00
|
|
|
proc.on('error', (err) => {
|
2021-02-22 10:51:19 +01:00
|
|
|
exit_cleanup();
|
2021-02-21 09:36:49 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
reject({ error: err, stdout, stderr });
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
|
|
|
});
|
2021-02-22 09:55:51 +01:00
|
|
|
}
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
async execute() {
|
|
|
|
if (this.state !== job_states.PRIMED) {
|
2021-03-05 07:29:09 +01:00
|
|
|
throw new Error('Job must be in primed state, current state: ' + this.state.toString());
|
2021-03-13 06:01:04 +01:00
|
|
|
}
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-02-22 09:55:51 +01:00
|
|
|
logger.info(`Executing job uuid=${this.uuid} uid=${this.uid} gid=${this.gid} runtime=${this.runtime.toString()}`);
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-02-22 09:55:51 +01:00
|
|
|
logger.debug('Compiling');
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
let compile;
|
|
|
|
|
|
|
|
if (this.runtime.compiled) {
|
2021-02-27 13:18:04 +01:00
|
|
|
compile = await this.safe_call(
|
|
|
|
path.join(this.runtime.pkgdir, 'compile'),
|
2021-03-13 06:01:04 +01:00
|
|
|
this.files.map(x => x.name),
|
|
|
|
this.timeouts.compile
|
|
|
|
);
|
|
|
|
}
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-22 09:55:51 +01:00
|
|
|
logger.debug('Running');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-22 10:00:37 +01:00
|
|
|
const run = await this.safe_call(
|
|
|
|
path.join(this.runtime.pkgdir, 'run'),
|
2021-04-23 01:52:50 +02:00
|
|
|
[this.files[0].name, ...this.args],
|
2021-03-13 06:01:04 +01:00
|
|
|
this.timeouts.run
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
async cleanup() {
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.info(`Cleaning up job uuid=${this.uuid}`);
|
2021-03-13 06:01:04 +01:00
|
|
|
await fs.rm(this.dir, { recursive: true, force: true });
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
2021-03-13 06:01:04 +01:00
|
|
|
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
module.exports = {
|
|
|
|
Job
|
|
|
|
};
|