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');
|
|
|
|
const helpers = require('../helpers');
|
|
|
|
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 {
|
|
|
|
constructor(repo, {author, language, version, checksums, dependencies, size, buildfile, download, signature}){
|
2021-02-20 23:39:03 +01:00
|
|
|
this.author = author;
|
|
|
|
this.language = language;
|
|
|
|
this.version = semver.parse(version);
|
|
|
|
this.checksums = checksums;
|
|
|
|
this.dependencies = dependencies;
|
|
|
|
this.size = size;
|
|
|
|
this.buildfile = buildfile;
|
|
|
|
this.download = download;
|
|
|
|
this.signature = signature;
|
|
|
|
|
|
|
|
this.repo = repo;
|
2021-02-20 15:13:56 +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
|
|
|
}
|
|
|
|
|
|
|
|
get download_url(){
|
2021-02-20 23:39:03 +01:00
|
|
|
return helpers.add_url_base_if_required(this.download, this.repo.base_u_r_l);
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get install_path(){
|
|
|
|
return path.join(config.data_directory,
|
|
|
|
globals.data_directories.packages,
|
|
|
|
this.language,
|
2021-02-20 23:39:03 +01:00
|
|
|
this.version.raw);
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async install(){
|
2021-02-20 23:39:03 +01:00
|
|
|
if(this.installed) throw new Error('Already installed');
|
|
|
|
logger.info(`Installing ${this.language}-${this.version.raw}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
if(fss.exists_sync(this.install_path)){
|
|
|
|
logger.warn(`${this.language}-${this.version.raw} has residual files. Removing them.`);
|
|
|
|
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-02-20 23:39:03 +01:00
|
|
|
logger.debug(`Downloading package from ${this.download_url} in to ${this.install_path}`);
|
|
|
|
const pkgfile = helpers.url_basename(this.download_url);
|
|
|
|
const pkgpath = path.join(this.install_path, pkgfile);
|
2021-02-21 00:56:35 +01:00
|
|
|
await helpers.buffer_from_url(this.download_url)
|
2021-02-20 23:39:03 +01:00
|
|
|
.then(buf=> fs.write_file(pkgpath, buf));
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Validating checksums');
|
2021-02-20 15:13:56 +01:00
|
|
|
Object.keys(this.checksums).forEach(algo => {
|
2021-02-20 23:39:03 +01:00
|
|
|
var val = this.checksums[algo];
|
|
|
|
logger.debug(`Assert ${algo}(${pkgpath}) == ${val}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
var cs = crypto.create_hash(algo)
|
2021-02-20 23:39:03 +01:00
|
|
|
.update(fss.read_file_sync(pkgpath))
|
|
|
|
.digest('hex');
|
|
|
|
if(cs != val) throw new Error(`Checksum miss-match want: ${val} got: ${cs}`);
|
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
await this.repo.import_keys();
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Validating signatutes');
|
2021-02-21 00:56:35 +01:00
|
|
|
if(this.signature != "")
|
|
|
|
await new Promise((resolve,reject)=>{
|
|
|
|
const gpgspawn = cp.spawn('gpg', ['--verify', '-', pkgpath], {
|
|
|
|
stdio: ['pipe', 'ignore', 'ignore']
|
|
|
|
});
|
|
|
|
|
|
|
|
gpgspawn.once('exit', (code, _) => {
|
|
|
|
if(code == 0) resolve();
|
|
|
|
else reject(new Error('Invalid signature'));
|
|
|
|
});
|
|
|
|
|
|
|
|
gpgspawn.once('error', reject);
|
|
|
|
|
|
|
|
gpgspawn.stdin.write(this.signature);
|
|
|
|
gpgspawn.stdin.end();
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
});
|
2021-02-21 00:56:35 +01:00
|
|
|
else
|
|
|
|
logger.warn("Package does not contain a signature - allowing install, but proceed with caution")
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug(`Extracting package files from archive ${pkgfile} in to ${this.install_path}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
await new Promise((resolve, reject)=>{
|
2021-02-20 23:39:03 +01:00
|
|
|
const proc = cp.exec(`bash -c 'cd "${this.install_path}" && tar xzf ${pkgfile}'`);
|
|
|
|
proc.once('exit', (code,_)=>{
|
|
|
|
if(code == 0) resolve();
|
|
|
|
else reject(new Error('Failed to extract package'));
|
|
|
|
});
|
|
|
|
proc.stdout.pipe(process.stdout);
|
|
|
|
proc.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
proc.once('error', reject);
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.debug('Ensuring binary files exist for package');
|
|
|
|
const pkgbin = path.join(this.install_path, `${this.language}-${this.version.raw}`);
|
2021-02-20 15:13:56 +01:00
|
|
|
try{
|
2021-02-20 23:39:03 +01:00
|
|
|
const pkgbinstat = await fs.stat(pkgbin);
|
2021-02-20 15:13:56 +01:00
|
|
|
//eslint-disable-next-line snakecasejs/snakecasejs
|
2021-02-20 23:39:03 +01:00
|
|
|
if(!pkgbinstat.isDirectory()) throw new Error();
|
2021-02-20 15:13:56 +01:00
|
|
|
}catch(err){
|
2021-02-20 23:39:03 +01:00
|
|
|
throw new Error(`Invalid package: could not find ${this.language}-${this.version.raw}/ contained within package files`);
|
2021-02-20 15:13:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Symlinking into runtimes');
|
|
|
|
await fs.symlink(
|
2021-02-20 15:13:56 +01:00
|
|
|
pkgbin,
|
|
|
|
path.join(config.data_directory,
|
|
|
|
globals.data_directories.runtimes,
|
|
|
|
`${this.language}-${this.version.raw}`)
|
2021-02-20 23:39:03 +01:00
|
|
|
).catch((err)=>err); //catch
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
logger.debug('Registering runtime');
|
|
|
|
const pkgruntime = 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');
|
|
|
|
const required_pkgs = [pkgruntime, ...pkgruntime.get_all_dependencies()];
|
2021-02-20 15:13:56 +01:00
|
|
|
const get_env_command = [...required_pkgs.map(p=>`cd "${p.runtime_dir}"; source environment; `),
|
2021-02-20 23:39:03 +01:00
|
|
|
'env' ].join(' ');
|
2021-02-20 15:13:56 +01:00
|
|
|
|
|
|
|
const envout = await new Promise((resolve, reject)=>{
|
2021-02-20 23:39:03 +01:00
|
|
|
var stdout = '';
|
|
|
|
const proc = cp.spawn('env',['-i','bash','-c',`${get_env_command}`], {
|
|
|
|
stdio: ['ignore', 'pipe', 'pipe']});
|
|
|
|
proc.once('exit', (code,_)=>{
|
|
|
|
if(code == 0) resolve(stdout);
|
|
|
|
else reject(new Error('Failed to cache environment'));
|
|
|
|
});
|
2021-02-20 15:13:56 +01:00
|
|
|
|
2021-02-20 23:39:03 +01:00
|
|
|
proc.stdout.on('data', (data)=>{
|
|
|
|
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-02-20 23:39:03 +01:00
|
|
|
const filtered_env = envout.split('\n')
|
|
|
|
.filter(l=>!['PWD','OLDPWD','_', 'SHLVL'].includes(l.split('=',2)[0]))
|
|
|
|
.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-02-20 23:39:03 +01:00
|
|
|
module.exports = {Package};
|