piston/api/src/config.js

192 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-02-20 23:39:03 +01:00
const fss = require('fs');
const yargs = require('yargs');
2021-03-13 06:01:04 +01:00
const hide_bin = require('yargs/helpers').hideBin;
2021-02-20 23:39:03 +01:00
const Logger = require('logplease');
const logger = Logger.create('config');
const yaml = require('js-yaml');
2021-02-20 15:13:56 +01:00
const header = `#
2021-03-13 06:01:04 +01:00
# ____ _ _
# | _ \\(_)___| |_ ___ _ __
# | |_) | / __| __/ _ \\| '_ \\
2021-02-20 15:13:56 +01:00
# | __/| \\__ \\ || (_) | | | |
# |_| |_|___/\\__\\___/|_| |_|
#
2021-03-13 06:01:04 +01:00
# A High performance code execution engine
2021-02-20 15:13:56 +01:00
# github.com/engineer-man/piston
#
2021-02-20 23:39:03 +01:00
`;
2021-02-20 15:13:56 +01:00
const argv = yargs(hide_bin(process.argv))
2021-02-20 23:39:03 +01:00
.usage('Usage: $0 -c [config]')
2021-03-13 06:01:04 +01:00
.demandOption('c')
2021-02-20 23:39:03 +01:00
.option('config', {
alias: 'c',
describe: 'config file to load from',
default: '/piston/config.yaml'
2021-02-20 15:13:56 +01:00
})
2021-02-20 23:39:03 +01:00
.option('make-config', {
alias: 'm',
type: 'boolean',
describe: 'create config file and populate defaults if it does not already exist'
2021-03-13 06:01:04 +01:00
})
.argv;
2021-02-20 15:13:56 +01:00
const options = [
{
2021-02-20 23:39:03 +01:00
key: 'log_level',
desc: 'Level of data to log',
default: 'INFO',
2021-02-20 15:13:56 +01:00
options: Object.values(Logger.LogLevels),
2021-03-13 06:01:04 +01:00
validators: [
x => Object.values(Logger.LogLevels).includes(x) || `Log level ${x} does not exist`
]
2021-02-20 15:13:56 +01:00
},
{
2021-02-20 23:39:03 +01:00
key: 'bind_address',
desc: 'Address to bind REST API on\nThank @Bones for the number',
default: '0.0.0.0:6969',
2021-02-20 15:13:56 +01:00
validators: []
},
{
2021-02-20 23:39:03 +01:00
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`]
2021-02-20 15:13:56 +01:00
},
{
2021-02-20 23:39:03 +01:00
key: 'runner_uid_min',
desc: 'Minimum uid to use for runner',
2021-02-20 15:13:56 +01:00
default: 1000,
validators: []
},
{
2021-02-20 23:39:03 +01:00
key: 'runner_uid_max',
desc: 'Maximum uid to use for runner',
2021-02-20 15:13:56 +01:00
default: 1500,
validators: []
},
{
2021-02-20 23:39:03 +01:00
key: 'runner_gid_min',
desc: 'Minimum gid to use for runner',
2021-02-20 15:13:56 +01:00
default: 1000,
validators: []
},
{
2021-02-20 23:39:03 +01:00
key: 'runner_gid_max',
desc: 'Maximum gid to use for runner',
2021-02-20 15:13:56 +01:00
default: 1500,
validators: []
2021-02-21 09:37:13 +01:00
},
{
key: 'enable_unshare',
desc: 'Enable using unshare to disable networking',
default: true,
validators: []
},
{
key: 'output_max_size',
desc: 'Max size of each stdio buffer',
default: 1024,
validators: []
2021-02-22 10:56:54 +01:00
},
{
key: 'max_process_count',
desc: 'Max number of processes per job',
default: 64,
validators: []
},
{
key: 'max_open_files',
desc: 'Max number of open files per job',
default: 2048,
validators: []
2021-03-06 07:17:56 +01:00
},
{
key: 'repo_url',
desc: 'URL of repo index',
default: 'https://github.com',
validators: []
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
];
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
const make_default_config = () => {
2021-03-05 07:29:09 +01:00
let content = header.split('\n');
options.forEach(option => {
2021-03-06 07:17:56 +01:00
content = content.concat(option.desc.split('\n').map(x=>`# ${x}`));
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
if (option.options) {
content.push('# Options: ' + option.options.join(', '));
2021-03-13 06:01:04 +01:00
}
2021-03-05 07:29:09 +01:00
content.push(`${option.key}: ${option.default}`);
2021-03-13 06:01:04 +01:00
content.push(''); // New line between
2021-03-05 07:29:09 +01:00
});
return content.join('\n');
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(`Loading Configuration from ${argv.config}`);
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
if (argv['make-config']) {
2021-03-05 07:29:09 +01:00
logger.debug('Make configuration flag is set');
2021-03-13 06:01:04 +01:00
}
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
if (!!argv['make-config'] && !fss.exists_sync(argv.config)) {
2021-02-20 23:39:03 +01:00
logger.info('Writing default configuration...');
2021-02-20 15:13:56 +01:00
try {
2021-03-13 06:01:04 +01:00
fss.write_file_sync(argv.config, make_default_config());
} catch (e) {
logger.error('Error writing default configuration:', e.message);
2021-02-20 23:39:03 +01:00
process.exit(1);
2021-02-20 15:13:56 +01:00
}
}
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-02-20 23:39:03 +01:00
logger.debug('Reading config file');
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
try {
2021-02-20 23:39:03 +01:00
const cfg_content = fss.read_file_sync(argv.config);
2021-03-05 07:29:09 +01:00
config = yaml.load(cfg_content);
2021-03-13 06:01:04 +01:00
} catch(err) {
2021-03-05 07:29:09 +01:00
logger.error('Error reading configuration file:', err.message);
2021-02-20 23:39:03 +01:00
process.exit(1);
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
logger.debug('Validating config entries');
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
let errored = false;
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
options.for_each(option => {
2021-03-05 07:29:09 +01:00
logger.debug('Checking option', option.key);
2021-03-13 06:01:04 +01:00
let cfg_val = config[option.key];
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
if (cfg_val === undefined) {
2021-02-20 23:39:03 +01:00
errored = true;
2021-03-05 07:29:09 +01:00
logger.error(`Config key ${option.key} does not exist on currently loaded configuration`);
2021-02-20 23:39:03 +01:00
return;
2021-02-20 15:13:56 +01:00
}
2021-03-05 07:29:09 +01:00
2021-03-13 06:01:04 +01:00
option.validators.for_each(validator => {
let response = validator(cfg_val);
if (!response) {
2021-02-20 23:39:03 +01:00
errored = true;
2021-03-05 07:29:09 +01:00
logger.error(`Config option ${option.key} failed validation:`, response);
2021-02-20 23:39:03 +01:00
return;
2021-02-20 15:13:56 +01:00
}
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) {
process.exit(1);
}
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;