2020-05-27 15:18:04 +07:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
2020-10-13 15:27:52 +07:00
|
|
|
import logger from '../logger';
|
2021-04-12 22:17:13 +04:00
|
|
|
import { IBackendInfo } from '../mempool.interfaces';
|
2022-03-23 12:17:23 -07:00
|
|
|
const { spawnSync } = require('child_process');
|
2020-05-27 15:18:04 +07:00
|
|
|
|
|
|
|
class BackendInfo {
|
2021-04-12 22:17:13 +04:00
|
|
|
private gitCommitHash = '';
|
|
|
|
private hostname = '';
|
|
|
|
private version = '';
|
2020-05-27 15:18:04 +07:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.setLatestCommitHash();
|
2021-04-12 22:17:13 +04:00
|
|
|
this.setVersion();
|
2020-05-27 15:18:04 +07:00
|
|
|
this.hostname = os.hostname();
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:17:13 +04:00
|
|
|
public getBackendInfo(): IBackendInfo {
|
2020-05-27 15:18:04 +07:00
|
|
|
return {
|
2021-04-12 22:17:13 +04:00
|
|
|
hostname: this.hostname,
|
|
|
|
gitCommit: this.gitCommitHash,
|
|
|
|
version: this.version,
|
2020-05-27 15:18:04 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:48:43 +07:00
|
|
|
public getShortCommitHash() {
|
|
|
|
return this.gitCommitHash.slice(0, 7);
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:18:04 +07:00
|
|
|
private setLatestCommitHash(): void {
|
2022-03-23 12:17:23 -07:00
|
|
|
//TODO: share this logic with `generate-config.js`
|
|
|
|
if (process.env.DOCKER_COMMIT_HASH) {
|
|
|
|
this.gitCommitHash = process.env.DOCKER_COMMIT_HASH;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']);
|
|
|
|
|
|
|
|
if (!gitRevParse.error) {
|
|
|
|
this.gitCommitHash = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
|
|
|
|
console.log(`mempool revision ${this.gitCommitHash}`);
|
|
|
|
} else if (gitRevParse.error.code === 'ENOENT') {
|
|
|
|
console.log('git not found, cannot parse git hash');
|
|
|
|
this.gitCommitHash = '?';
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
console.log('Could not load git commit info: ' + e.message);
|
|
|
|
this.gitCommitHash = '?';
|
|
|
|
}
|
2020-05-27 15:18:04 +07:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 22:17:13 +04:00
|
|
|
|
|
|
|
private setVersion(): void {
|
|
|
|
try {
|
|
|
|
const packageJson = fs.readFileSync('package.json').toString();
|
|
|
|
this.version = JSON.parse(packageJson).version;
|
|
|
|
} catch (e) {
|
2021-08-31 15:09:33 +03:00
|
|
|
throw new Error(e instanceof Error ? e.message : 'Error');
|
2021-04-12 22:17:13 +04:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 15:18:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new BackendInfo();
|