2021-02-20 23:39:03 +01:00
|
|
|
const logger = require('logplease').create('ppman/package');
|
|
|
|
const semver = require('semver');
|
|
|
|
const config = require('../config');
|
|
|
|
const globals = require('../globals');
|
2021-03-06 07:17:56 +01:00
|
|
|
const fetch = require('node-fetch');
|
2021-02-20 23:39:03 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs/promises');
|
|
|
|
const fss = require('fs');
|
|
|
|
const cp = require('child_process');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const runtime = require('../runtime');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
class Package {
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
constructor({ language, version, download, checksum }){
|
2021-02-20 23:39:03 +01:00
|
|
|
this.language = language;
|
|
|
|
this.version = semver.parse(version);
|
2021-03-06 07:17:56 +01:00
|
|
|
this.checksum = checksum;
|
2021-02-20 23:39:03 +01:00
|
|
|
this.download = download;
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
get installed() {
|
2021-02-20 23:39:03 +01:00
|
|
|
return fss.exists_sync(path.join(this.install_path, globals.pkg_installed_file));
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
get install_path() {
|
|
|
|
return path.join(
|
|
|
|
config.data_directory,
|
2021-02-20 15:13:56 +01:00
|
|
|
globals.data_directories.packages,
|
|
|
|
this.language,
|
2021-03-13 06:01:04 +01:00
|
|
|
this.version.raw
|
|
|
|
);
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
async install() {
|
|
|
|
if (this.installed) {
|
|
|
|
throw new Error('Already installed');
|
|
|
|
}
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.info(`Installing ${this.language}-${this.version.raw}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
if (fss.exists_sync(this.install_path)) {
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.warn(`${this.language}-${this.version.raw} has residual files. Removing them.`);
|
2021-03-13 06:01:04 +01:00
|
|
|
await fs.rm(this.install_path, { recursive: true, force: true });
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug(`Making directory ${this.install_path}`);
|
|
|
|
await fs.mkdir(this.install_path, {recursive: true});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
logger.debug(`Downloading package from ${this.download} in to ${this.install_path}`);
|
|
|
|
const pkgpath = path.join(this.install_path, 'pkg.tar.gz');
|
|
|
|
const download = await fetch(this.download);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-06 07:17:56 +01:00
|
|
|
const file_stream = fss.create_write_stream(pkgpath);
|
|
|
|
await new Promise((resolve, reject) => {
|
2021-03-13 06:01:04 +01:00
|
|
|
download.body.pipe(file_stream);
|
|
|
|
download.body.on('error', reject);
|
|
|
|
|
|
|
|
file_stream.on('finish', resolve);
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-06 07:17:56 +01:00
|
|
|
logger.debug('Validating checksums');
|
2021-03-13 06:01:04 +01:00
|
|
|
logger.debug(`Assert sha256(pkg.tar.gz) == ${this.checksum}`);
|
2021-03-06 07:17:56 +01:00
|
|
|
const cs = crypto.create_hash("sha256")
|
|
|
|
.update(fss.readFileSync(pkgpath))
|
|
|
|
.digest('hex');
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
if (cs !== this.checksum) {
|
|
|
|
throw new Error(`Checksum miss-match want: ${val} got: ${cs}`);
|
|
|
|
}
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-06 07:17:56 +01:00
|
|
|
logger.debug(`Extracting package files from archive ${pkgpath} in to ${this.install_path}`);
|
2021-03-05 07:29:09 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
await new Promise((resolve, reject) => {
|
2021-03-06 07:17:56 +01:00
|
|
|
const proc = cp.exec(`bash -c 'cd "${this.install_path}" && tar xzf ${pkgpath}'`);
|
2021-03-13 06:01:04 +01:00
|
|
|
|
|
|
|
proc.once('exit', (code, _) => {
|
|
|
|
code === 0 ? resolve() : reject();
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-03-13 06:01:04 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
proc.stdout.pipe(process.stdout);
|
|
|
|
proc.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
proc.once('error', reject);
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.debug('Registering runtime');
|
2021-03-06 07:17:56 +01:00
|
|
|
new runtime.Runtime(this.install_path);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Caching environment');
|
2021-03-06 07:17:56 +01:00
|
|
|
const get_env_command = `cd ${this.install_path}; source environment; env`;
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
const envout = await new Promise((resolve, reject) => {
|
|
|
|
let stdout = '';
|
|
|
|
|
|
|
|
const proc = cp
|
|
|
|
.spawn(
|
|
|
|
'env',
|
|
|
|
['-i','bash','-c',`${get_env_command}`],
|
|
|
|
{
|
|
|
|
stdio: ['ignore', 'pipe', 'pipe']
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
proc.once('exit', (code, _) => {
|
2021-03-13 11:09:13 +01:00
|
|
|
code === 0 ? resolve(stdout) : reject();
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
proc.stdout.on('data', data => {
|
2021-02-20 23:39:03 +01:00
|
|
|
stdout += data;
|
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
proc.once('error', reject);
|
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-03-13 06:01:04 +01:00
|
|
|
const filtered_env = envout
|
|
|
|
.split('\n')
|
|
|
|
.filter(l => !['PWD','OLDPWD','_', 'SHLVL'].includes(l.split('=',2)[0]))
|
2021-02-20 23:39:03 +01:00
|
|
|
.join('\n');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
await fs.write_file(path.join(this.install_path, '.env'), filtered_env);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Writing installed state to disk');
|
|
|
|
await fs.write_file(path.join(this.install_path, globals.pkg_installed_file), Date.now().toString());
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.info(`Installed ${this.language}-${this.version.raw}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
language: this.language,
|
|
|
|
version: this.version.raw
|
2021-02-20 23:39:03 +01:00
|
|
|
};
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-04-10 06:10:18 +02:00
|
|
|
|
|
|
|
async uninstall(){
|
|
|
|
logger.info(`Uninstalling ${this.language}-${this.version.raw}`);
|
|
|
|
|
|
|
|
logger.debug("Finding runtime")
|
2021-04-11 03:15:14 +02:00
|
|
|
const found_runtime = runtime.get_latest_runtime_matching_language_version(this.language, this.version.raw);
|
2021-04-10 06:10:18 +02:00
|
|
|
|
|
|
|
logger.debug("Unregistering runtime")
|
2021-04-11 03:15:14 +02:00
|
|
|
found_runtime.unregister();
|
2021-04-10 06:10:18 +02:00
|
|
|
|
|
|
|
logger.debug("Cleaning files from disk")
|
|
|
|
await fs.rmdir(this.install_path, {recursive: true})
|
|
|
|
|
|
|
|
logger.info(`Uninstalled ${this.language}-${this.version.raw}`)
|
|
|
|
|
|
|
|
return {
|
|
|
|
language: this.language,
|
|
|
|
version: this.version.raw
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 = {
|
|
|
|
Package
|
|
|
|
};
|