Merge pull request #2096 from erikarvstedt/backend-packaging

Simplify packaging for backend
This commit is contained in:
wiz 2022-09-07 15:01:18 +02:00 committed by GitHub
commit 11d6b372ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 64 deletions

View File

@ -110,6 +110,11 @@ Run the Mempool backend:
``` ```
npm run start npm run start
```
You can also set env var `MEMPOOL_CONFIG_FILE` to specify a custom config file location:
```
MEMPOOL_CONFIG_FILE=/path/to/mempool-config.json npm run start
``` ```
When it's running, you should see output like this: When it's running, you should see output like this:

View File

@ -22,7 +22,10 @@
"main": "index.ts", "main": "index.ts",
"scripts": { "scripts": {
"tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json", "tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json",
"build": "npm run tsc", "build": "npm run tsc && npm run create-resources",
"create-resources": "cp ./src/tasks/price-feeds/mtgox-weekly.json ./dist/tasks && node dist/api/fetch-version.js",
"package": "npm run build && rm -rf package && mv dist package && mv node_modules package && npm run package-rm-build-deps",
"package-rm-build-deps": "(cd package/node_modules; rm -r typescript @typescript-eslint)",
"start": "node --max-old-space-size=2048 dist/index.js", "start": "node --max-old-space-size=2048 dist/index.js",
"start-production": "node --max-old-space-size=4096 dist/index.js", "start-production": "node --max-old-space-size=4096 dist/index.js",
"test": "./node_modules/.bin/jest --coverage", "test": "./node_modules/.bin/jest --coverage",

View File

@ -1,60 +1,37 @@
import * as fs from 'fs'; import fs from 'fs';
import * as os from 'os'; import path from 'path';
import logger from '../logger'; import os from 'os';
import { IBackendInfo } from '../mempool.interfaces'; import { IBackendInfo } from '../mempool.interfaces';
const { spawnSync } = require('child_process');
class BackendInfo { class BackendInfo {
private gitCommitHash = ''; private backendInfo: IBackendInfo;
private hostname = '';
private version = '';
constructor() { constructor() {
this.setLatestCommitHash(); // This file is created by ./fetch-version.ts during building
this.setVersion(); const versionFile = path.join(__dirname, 'version.json')
this.hostname = os.hostname(); var versionInfo;
} if (fs.existsSync(versionFile)) {
versionInfo = JSON.parse(fs.readFileSync(versionFile).toString());
public getBackendInfo(): IBackendInfo { } else {
return { // Use dummy values if `versionFile` doesn't exist (e.g., during testing)
hostname: this.hostname, versionInfo = {
gitCommit: this.gitCommitHash, version: '?',
version: this.version, gitCommit: '?'
};
}
this.backendInfo = {
hostname: os.hostname(),
version: versionInfo.version,
gitCommit: versionInfo.gitCommit
}; };
} }
public getBackendInfo(): IBackendInfo {
return this.backendInfo;
}
public getShortCommitHash() { public getShortCommitHash() {
return this.gitCommitHash.slice(0, 7); return this.backendInfo.gitCommit.slice(0, 7);
}
private setLatestCommitHash(): void {
//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) {
const output = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
this.gitCommitHash = output ? output : '?';
} 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 = '?';
}
}
}
private setVersion(): void {
try {
const packageJson = fs.readFileSync('package.json').toString();
this.version = JSON.parse(packageJson).version;
} catch (e) {
throw new Error(e instanceof Error ? e.message : 'Error');
}
} }
} }

View File

@ -0,0 +1,37 @@
import fs from 'fs';
import path from "path";
const { spawnSync } = require('child_process');
function getVersion(): string {
const packageJson = fs.readFileSync('package.json').toString();
return JSON.parse(packageJson).version;
}
function getGitCommit(): string {
if (process.env.MEMPOOL_COMMIT_HASH) {
return process.env.MEMPOOL_COMMIT_HASH;
} else {
const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']);
if (!gitRevParse.error) {
const output = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
if (output) {
return output;
} else {
console.log('Could not fetch git commit: No repo available');
}
} else if (gitRevParse.error.code === 'ENOENT') {
console.log('Could not fetch git commit: Command `git` is unavailable');
}
}
return '?';
}
const versionInfo = {
version: getVersion(),
gitCommit: getGitCommit()
}
fs.writeFileSync(
path.join(__dirname, 'version.json'),
JSON.stringify(versionInfo, null, 2) + "\n"
);

View File

@ -1,4 +1,6 @@
const configFile = require('../mempool-config.json'); const configFromFile = require(
process.env.MEMPOOL_CONFIG_FILE ? process.env.MEMPOOL_CONFIG_FILE : '../mempool-config.json'
);
interface IConfig { interface IConfig {
MEMPOOL: { MEMPOOL: {
@ -249,7 +251,7 @@ class Config implements IConfig {
MAXMIND: IConfig['MAXMIND']; MAXMIND: IConfig['MAXMIND'];
constructor() { constructor() {
const configs = this.merge(configFile, defaults); const configs = this.merge(configFromFile, defaults);
this.MEMPOOL = configs.MEMPOOL; this.MEMPOOL = configs.MEMPOOL;
this.ESPLORA = configs.ESPLORA; this.ESPLORA = configs.ESPLORA;
this.ELECTRUM = configs.ELECTRUM; this.ELECTRUM = configs.ELECTRUM;

View File

@ -1,4 +1,5 @@
import * as fs from 'fs'; import * as fs from 'fs';
import path from "path";
import { Common } from '../api/common'; import { Common } from '../api/common';
import config from '../config'; import config from '../config';
import logger from '../logger'; import logger from '../logger';
@ -159,7 +160,7 @@ class PriceUpdater {
const existingPriceTimes = await PricesRepository.$getPricesTimes(); const existingPriceTimes = await PricesRepository.$getPricesTimes();
// Insert MtGox weekly prices // Insert MtGox weekly prices
const pricesJson: any[] = JSON.parse(fs.readFileSync('./src/tasks/price-feeds/mtgox-weekly.json').toString()); const pricesJson: any[] = JSON.parse(fs.readFileSync(path.join(__dirname, 'mtgox-weekly.json')).toString());
const prices = this.getEmptyPricesObj(); const prices = this.getEmptyPricesObj();
let insertedCount: number = 0; let insertedCount: number = 0;
for (const price of pricesJson) { for (const price of pricesJson) {

View File

@ -1,7 +1,7 @@
FROM node:16.16.0-buster-slim AS builder FROM node:16.16.0-buster-slim AS builder
ARG commitHash ARG commitHash
ENV DOCKER_COMMIT_HASH=${commitHash} ENV MEMPOOL_COMMIT_HASH=${commitHash}
WORKDIR /build WORKDIR /build
COPY . . COPY . .
@ -9,18 +9,15 @@ COPY . .
RUN apt-get update RUN apt-get update
RUN apt-get install -y build-essential python3 pkg-config RUN apt-get install -y build-essential python3 pkg-config
RUN npm install --omit=dev --omit=optional RUN npm install --omit=dev --omit=optional
RUN npm run build RUN npm run package
FROM node:16.16.0-buster-slim FROM node:16.16.0-buster-slim
WORKDIR /backend WORKDIR /backend
COPY --from=builder /build/ . RUN chown 1000:1000 ./
COPY --from=builder --chown=1000:1000 /build/package ./package/
RUN chmod +x /backend/start.sh COPY --from=builder --chown=1000:1000 /build/mempool-config.json /build/start.sh /build/wait-for-it.sh ./
RUN chmod +x /backend/wait-for-it.sh
RUN chown -R 1000:1000 /backend && chmod -R 755 /backend
USER 1000 USER 1000

2
docker/backend/start.sh Normal file → Executable file
View File

@ -205,4 +205,4 @@ sed -i "s!__LND_REST_API_URL__!${__LND_REST_API_URL__}!g" mempool-config.json
# CLN # CLN
sed -i "s!__CLN_SOCKET__!${__CLN_SOCKET__}!g" mempool-config.json sed -i "s!__CLN_SOCKET__!${__CLN_SOCKET__}!g" mempool-config.json
node /backend/dist/index.js node /backend/package/index.js

0
docker/backend/wait-for-it.sh Normal file → Executable file
View File

View File

@ -1,10 +1,7 @@
#!/bin/sh #!/bin/sh
#backend #backend
gitMaster="\.\.\/\.git\/refs\/heads\/master"
git ls-remote https://github.com/mempool/mempool.git "$1^{}" | awk '{ print $1}' > ./backend/master
cp ./docker/backend/* ./backend/ cp ./docker/backend/* ./backend/
sed -i "s/${gitMaster}/master/g" ./backend/src/api/backend-info.ts
#frontend #frontend
localhostIP="127.0.0.1" localhostIP="127.0.0.1"