piston/api/src/config.js

250 lines
7.5 KiB
JavaScript
Raw Normal View History

2021-02-20 23:39:03 +01:00
const fss = require('fs');
const Logger = require('logplease');
const logger = Logger.create('config');
2021-10-01 21:41:09 +02:00
function parse_overrides(overrides) {
try {
return JSON.parse(overrides);
2021-10-08 15:16:57 +02:00
} catch (e) {
2021-10-01 21:41:09 +02:00
return null;
}
}
function validate_overrides(overrides, options) {
for (const language in overrides) {
for (const key in overrides[language]) {
2021-10-01 21:41:09 +02:00
if (
![
2021-10-08 15:16:57 +02:00
'max_process_count',
'max_open_files',
'max_file_size',
'compile_memory_limit',
'run_memory_limit',
'compile_timeout',
'run_timeout',
'output_max_size',
2021-10-01 21:41:09 +02:00
].includes(key)
) {
logger.error(`Invalid overridden option: ${key}`);
return false;
}
const option = options.find(o => o.key === key);
const parser = option.parser;
const raw = overrides[language][key];
const value = parser(raw);
const validators = option.validators;
for (const validator of validators) {
const response = validator(value, raw);
2021-10-01 21:41:09 +02:00
if (response !== true) {
2021-10-08 15:16:57 +02:00
logger.error(
`Failed to validate overridden option: ${key}`,
response
);
2021-10-01 21:41:09 +02:00
return false;
}
}
overrides[language][key] = value;
}
2021-10-02 14:08:36 +02:00
// Modifies the reference
2021-10-08 15:16:57 +02:00
options[
options.index_of(options.find(o => o.key === 'limit_overrides'))
] = overrides;
2021-10-01 21:41:09 +02:00
}
2021-10-02 14:08:36 +02:00
return true;
2021-10-01 21:41:09 +02:00
}
2021-02-20 15:13:56 +01:00
const options = [
2021-05-08 02:30:40 +02:00
{
key: 'log_level',
desc: 'Level of data to log',
default: 'INFO',
options: Object.values(Logger.LogLevels),
validators: [
x =>
Object.values(Logger.LogLevels).includes(x) ||
`Log level ${x} does not exist`,
],
},
{
key: 'bind_address',
desc: 'Address to bind REST API on',
default: `0.0.0.0:${process.env["PORT"] || 2000}`,
2021-05-08 02:30:40 +02:00
validators: [],
},
{
key: 'data_directory',
desc: 'Absolute path to store all piston related data at',
default: '/piston',
validators: [
x => fss.exists_sync(x) || `Directory ${x} does not exist`,
],
},
{
key: 'runner_uid_min',
desc: 'Minimum uid to use for runner',
default: 1001,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'runner_uid_max',
desc: 'Maximum uid to use for runner',
default: 1500,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'runner_gid_min',
desc: 'Minimum gid to use for runner',
default: 1001,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'runner_gid_max',
desc: 'Maximum gid to use for runner',
default: 1500,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'disable_networking',
desc: 'Set to true to disable networking',
default: true,
parser: x => x === 'true',
validators: [x => typeof x === 'boolean' || `${x} is not a boolean`],
},
{
key: 'output_max_size',
desc: 'Max size of each stdio buffer',
default: 1024,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'max_process_count',
desc: 'Max number of processes per job',
default: 64,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'max_open_files',
desc: 'Max number of open files per job',
default: 2048,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'max_file_size',
desc: 'Max file size in bytes for a file',
default: 10000000, //10MB
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
2021-10-01 21:41:09 +02:00
{
key: 'compile_timeout',
2021-10-08 15:16:57 +02:00
desc: 'Max time allowed for compile stage in milliseconds',
2021-10-01 21:41:09 +02:00
default: 10000, // 10 seconds
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'run_timeout',
2021-10-08 15:16:57 +02:00
desc: 'Max time allowed for run stage in milliseconds',
2021-10-01 21:41:09 +02:00
default: 3000, // 3 seconds
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
2021-05-08 02:30:40 +02:00
{
key: 'compile_memory_limit',
2021-10-08 15:16:57 +02:00
desc: 'Max memory usage for compile stage in bytes (set to -1 for no limit)',
2021-05-08 02:30:40 +02:00
default: -1, // no limit
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'run_memory_limit',
2021-10-08 15:16:57 +02:00
desc: 'Max memory usage for run stage in bytes (set to -1 for no limit)',
2021-05-08 02:30:40 +02:00
default: -1, // no limit
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
{
key: 'flake_path',
desc: 'Path to nix flake defining runtimes to install',
default: 'github:engineer-man/piston?directory=packages',
2021-05-08 02:30:40 +02:00
validators: [],
},
{
key: 'runtime_set',
desc: 'Key on the flake specified by flake_path to access runtimes from',
default: 'all',
validators: []
},
{
key: 'max_concurrent_jobs',
desc: 'Maximum number of concurrent jobs to run at one time',
default: 64,
parser: parse_int,
2021-10-08 15:16:57 +02:00
validators: [x => x > 0 || `${x} cannot be negative`],
2021-10-01 21:41:09 +02:00
},
{
key: 'limit_overrides',
desc: 'Per-language exceptions in JSON format for each of:\
max_process_count, max_open_files, max_file_size, compile_memory_limit,\
run_memory_limit, compile_timeout, run_timeout, output_max_size',
default: {},
parser: parse_overrides,
2021-10-02 14:08:36 +02:00
validators: [
2021-10-08 15:16:57 +02:00
x => !!x || `Invalid JSON format for the overrides\n${x}`,
(overrides, _, options) =>
validate_overrides(overrides, options) ||
`Failed to validate the overrides`,
],
},
2021-02-20 23:39:03 +01:00
];
2021-02-20 15:13:56 +01:00
logger.info(`Loading Configuration from environment`);
2021-02-20 15:13:56 +01:00
let errored = false;
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
let config = {};
2021-03-05 07:29:09 +01:00
2021-05-08 02:30:40 +02:00
options.forEach(option => {
const env_key = 'PISTON_' + option.key.to_upper_case();
2021-03-05 07:29:09 +01:00
2021-05-08 02:30:40 +02:00
const parser = option.parser || (x => x);
2021-03-05 07:29:09 +01:00
2021-05-08 02:30:40 +02:00
const env_val = process.env[env_key];
2021-03-05 07:29:09 +01:00
2021-05-08 02:30:40 +02:00
const parsed_val = parser(env_val);
2021-03-05 07:29:09 +01:00
2021-10-10 22:01:26 +02:00
const value = env_val === undefined ? option.default : parsed_val;
2021-03-05 07:29:09 +01:00
2021-05-08 02:30:40 +02:00
option.validators.for_each(validator => {
let response = null;
2021-10-02 14:08:36 +02:00
if (env_val) response = validator(parsed_val, env_val, options);
else response = validator(value, value, options);
2021-03-13 06:01:04 +01:00
2021-05-08 02:30:40 +02:00
if (response !== true) {
errored = true;
logger.error(
`Config option ${option.key} failed validation:`,
response
);
return;
}
});
2021-05-08 02:30:40 +02:00
config[option.key] = value;
2021-02-20 23:39:03 +01:00
});
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
if (errored) {
2021-05-08 02:30:40 +02:00
process.exit(1);
2021-03-13 06:01:04 +01:00
}
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.info('Configuration successfully loaded');
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
module.exports = config;