piston/api/src/config.js

232 lines
7.4 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');
const options = {
log_level: {
2021-05-08 02:30:40 +02:00
desc: 'Level of data to log',
default: 'INFO',
validators: [
x =>
Object.values(Logger.LogLevels).includes(x) ||
`Log level ${x} does not exist`,
],
},
bind_address: {
desc: 'Address to bind REST API on',
2022-01-28 16:58:00 +01:00
default: `0.0.0.0:${process.env['PORT'] || 2000}`,
2021-05-08 02:30:40 +02:00
validators: [],
},
data_directory: {
2021-05-08 02:30:40 +02:00
desc: 'Absolute path to store all piston related data at',
default: '/piston',
validators: [
x => fss.exists_sync(x) || `Directory ${x} does not exist`,
],
},
runner_uid_min: {
2021-05-08 02:30:40 +02:00
desc: 'Minimum uid to use for runner',
default: 1001,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
runner_uid_max: {
2021-05-08 02:30:40 +02:00
desc: 'Maximum uid to use for runner',
default: 1500,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
runner_gid_min: {
2021-05-08 02:30:40 +02:00
desc: 'Minimum gid to use for runner',
default: 1001,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
runner_gid_max: {
2021-05-08 02:30:40 +02:00
desc: 'Maximum gid to use for runner',
default: 1500,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
disable_networking: {
2021-05-08 02:30:40 +02:00
desc: 'Set to true to disable networking',
default: true,
parser: x => x === 'true',
validators: [x => typeof x === 'boolean' || `${x} is not a boolean`],
},
output_max_size: {
2021-05-08 02:30:40 +02:00
desc: 'Max size of each stdio buffer',
default: 1024,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
max_process_count: {
2021-05-08 02:30:40 +02:00
desc: 'Max number of processes per job',
default: 64,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
max_open_files: {
2021-05-08 02:30:40 +02:00
desc: 'Max number of open files per job',
default: 2048,
parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
},
max_file_size: {
2021-05-08 02:30:40 +02:00
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`],
},
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`],
},
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`],
},
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`],
},
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`],
},
repo_url: {
2021-05-08 02:30:40 +02:00
desc: 'URL of repo index',
default:
'https://github.com/engineer-man/piston/releases/download/pkgs/index',
validators: [],
},
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
},
limit_overrides: {
2021-10-01 21:41:09 +02:00
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: [
2022-01-28 16:58:00 +01:00
x => !!x || `Failed to parse the overrides\n${x}`,
validate_overrides,
2021-10-08 15:16:57 +02:00
],
},
};
2021-02-20 15:13:56 +01:00
2022-01-28 16:58:00 +01:00
Object.freeze(options);
function apply_validators(validators, validator_parameters) {
for (const validator of validators) {
const validation_response = validator(...validator_parameters);
if (validation_response !== true) {
return validation_response;
}
}
return true;
}
function parse_overrides(overrides_string) {
function get_parsed_json_or_null(overrides) {
try {
return JSON.parse(overrides);
} catch (e) {
return null;
}
}
const overrides = get_parsed_json_or_null(overrides_string);
if (overrides === null) {
2022-01-28 16:58:00 +01:00
return null;
}
const parsed_overrides = {};
for (const language in overrides) {
parsed_overrides[language] = {};
for (const key in overrides[language]) {
if (
![
'max_process_count',
'max_open_files',
'max_file_size',
'compile_memory_limit',
'run_memory_limit',
'compile_timeout',
'run_timeout',
'output_max_size',
].includes(key)
) {
return null;
}
// Find the option for the override
const option = options[key];
2022-01-28 16:58:00 +01:00
const parser = option.parser;
const raw_value = overrides[language][key];
const parsed_value = parser(raw_value);
parsed_overrides[language][key] = parsed_value;
2022-01-28 16:58:00 +01:00
}
}
return parsed_overrides;
}
2021-02-20 15:13:56 +01:00
2022-01-28 16:58:00 +01:00
function validate_overrides(overrides) {
for (const language in overrides) {
for (const key in overrides[language]) {
const value = overrides[language][key];
const option = options[key];
2022-01-28 16:58:00 +01:00
const validators = option.validators;
const validation_response = apply_validators(validators, [
value,
value,
]);
if (validation_response !== true) {
return `In overridden option ${key} for ${language}, ${validation_response}`;
}
}
}
return true;
}
logger.info(`Loading Configuration from environment`);
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
for (const option_name in options) {
const env_key = 'PISTON_' + option_name.to_upper_case();
const option = options[option_name];
2021-05-08 02:30:40 +02:00
const parser = option.parser || (x => x);
const env_val = process.env[env_key];
const parsed_val = parser(env_val);
2021-10-10 22:01:26 +02:00
const value = env_val === undefined ? option.default : parsed_val;
2022-01-28 16:58:00 +01:00
const validator_parameters =
env_val === undefined ? [value, value] : [parsed_val, env_val];
const validation_response = apply_validators(
option.validators,
validator_parameters
);
if (validation_response !== true) {
logger.error(
`Config option ${option_name} failed validation:`,
2022-01-28 16:58:00 +01:00
validation_response
);
process.exit(1);
}
config[option_name] = value;
}
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;