2021-02-20 23:39:03 +01:00
|
|
|
const logger = require('logplease').create('runtime');
|
2022-01-30 06:41:24 +01:00
|
|
|
const cp = require('child_process');
|
2021-02-20 23:39:03 +01:00
|
|
|
const config = require('./config');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
class Runtime {
|
2021-10-02 14:08:36 +02:00
|
|
|
constructor({
|
2021-10-08 15:16:57 +02:00
|
|
|
language,
|
|
|
|
version,
|
|
|
|
aliases,
|
|
|
|
runtime,
|
2022-01-30 06:41:24 +01:00
|
|
|
run,
|
|
|
|
compile,
|
|
|
|
packageSupport,
|
2022-01-30 10:29:21 +01:00
|
|
|
flake_path,
|
2021-10-08 15:16:57 +02:00
|
|
|
timeouts,
|
|
|
|
memory_limits,
|
|
|
|
max_process_count,
|
|
|
|
max_open_files,
|
|
|
|
max_file_size,
|
2022-01-31 05:47:27 +01:00
|
|
|
output_max_size,
|
2021-10-02 14:08:36 +02:00
|
|
|
}) {
|
2021-05-08 02:30:40 +02:00
|
|
|
this.language = language;
|
|
|
|
this.runtime = runtime;
|
2022-01-31 05:47:27 +01:00
|
|
|
|
2021-10-02 14:08:36 +02:00
|
|
|
this.timeouts = timeouts;
|
|
|
|
this.memory_limits = memory_limits;
|
|
|
|
this.max_process_count = max_process_count;
|
|
|
|
this.max_open_files = max_open_files;
|
|
|
|
this.max_file_size = max_file_size;
|
|
|
|
this.output_max_size = output_max_size;
|
2022-01-30 06:41:24 +01:00
|
|
|
|
|
|
|
this.aliases = aliases;
|
2022-01-31 05:47:27 +01:00
|
|
|
this.version = version;
|
|
|
|
|
2022-01-30 06:41:24 +01:00
|
|
|
this.run = run;
|
|
|
|
this.compile = compile;
|
|
|
|
|
2022-01-30 10:29:21 +01:00
|
|
|
this.flake_path = flake_path;
|
2022-01-30 06:41:24 +01:00
|
|
|
this.package_support = packageSupport;
|
2021-10-02 14:08:36 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 05:47:27 +01:00
|
|
|
ensure_built() {
|
2022-01-30 06:41:24 +01:00
|
|
|
logger.info(`Ensuring ${this} is built`);
|
2021-05-08 02:30:40 +02:00
|
|
|
|
2022-01-30 10:29:21 +01:00
|
|
|
const flake_path = this.flake_path;
|
2021-05-08 02:30:40 +02:00
|
|
|
|
2022-01-31 05:47:27 +01:00
|
|
|
function _ensure_built(key) {
|
2022-01-30 10:29:21 +01:00
|
|
|
const command = `nix build ${flake_path}.metadata.${key} --no-link`;
|
2022-01-31 05:47:27 +01:00
|
|
|
cp.execSync(command, { stdio: 'pipe' });
|
2021-05-08 02:30:40 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 05:47:27 +01:00
|
|
|
_ensure_built('run');
|
|
|
|
if (this.compiled) _ensure_built('compile');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2022-01-31 05:47:27 +01:00
|
|
|
logger.debug(`Finished ensuring ${this} is installed`);
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 06:41:24 +01:00
|
|
|
get compiled() {
|
|
|
|
return this.compile !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `${this.language}-${this.version}`;
|
2021-05-08 02:30:40 +02:00
|
|
|
}
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 22:03:46 +01:00
|
|
|
function compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
limit_name,
|
|
|
|
language_limit_overrides
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
(config.limit_overrides[language_name] &&
|
|
|
|
config.limit_overrides[language_name][limit_name]) ||
|
|
|
|
(language_limit_overrides &&
|
|
|
|
language_limit_overrides[limit_name]) ||
|
|
|
|
config[limit_name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function compute_all_limits(language_name, language_limit_overrides) {
|
|
|
|
return {
|
|
|
|
timeouts: {
|
|
|
|
compile: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'compile_timeout',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
run: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'run_timeout',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
},
|
|
|
|
memory_limits: {
|
|
|
|
compile: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'compile_memory_limit',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
run: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'run_memory_limit',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
},
|
|
|
|
max_process_count: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'max_process_count',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
max_open_files: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'max_open_files',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
max_file_size: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'max_file_size',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
output_max_size: compute_single_limit(
|
|
|
|
language_name,
|
|
|
|
'output_max_size',
|
|
|
|
language_limit_overrides
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_runtime_from_flakes(runtime_name) {
|
|
|
|
const flake_path = `${config.flake_path}#pistonRuntimeSets.${config.runtime_set}.${runtime_name}`;
|
|
|
|
const metadata_command = `nix eval --json ${flake_path}.metadata`;
|
|
|
|
const metadata = JSON.parse(cp.execSync(metadata_command));
|
|
|
|
|
|
|
|
const this_runtime = new Runtime({
|
|
|
|
...metadata,
|
|
|
|
...compute_all_limits(
|
|
|
|
metadata.language,
|
|
|
|
metadata.limitOverrides
|
|
|
|
),
|
|
|
|
flake_path,
|
|
|
|
});
|
|
|
|
|
|
|
|
return this_runtime
|
|
|
|
}
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
module.exports.Runtime = Runtime;
|
2022-02-11 22:03:46 +01:00
|
|
|
module.exports.get_runtime_from_flakes = get_runtime_from_flakes;
|