Skip to content

Commit

Permalink
feat(dashmate): verify system requirements (#1914)
Browse files Browse the repository at this point in the history
Co-authored-by: thephez <[email protected]>
  • Loading branch information
shumkov and thephez authored Jul 9, 2024
1 parent f6c195d commit de883f9
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 5 deletions.
31 changes: 31 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions packages/dashmate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"bs58": "^4.0.1",
"chalk": "^4.1.0",
"cron": "^2.1.0",
"diskusage": "^1.2.0",
"dockerode": "^3.3.5",
"dot": "^1.1.3",
"dotenv": "^8.6.0",
Expand All @@ -94,6 +95,7 @@
"qs": "^6.11.0",
"rxjs": "^6.6.7",
"semver": "^7.5.3",
"systeminformation": "^5.22.11",
"table": "^6.8.1",
"wrap-ansi": "^7.0.0"
},
Expand Down
4 changes: 0 additions & 4 deletions packages/dashmate/src/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export default class SetupCommand extends BaseCommand {

const tasks = new Listr(
[
{
title: 'System requirements',
task: async () => dockerCompose.throwErrorIfNotInstalled(),
},
{
title: 'Configuration preset',
task: async (ctx, task) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/dashmate/src/createDIContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ import registerMasternodeWithCoreWalletFactory from './listr/tasks/setup/regular
import registerMasternodeWithDMTFactory from './listr/tasks/setup/regular/registerMasternode/registerMasternodeWithDMT.js';
import writeConfigTemplatesFactory from './templates/writeConfigTemplatesFactory.js';
import importCoreDataTaskFactory from './listr/tasks/setup/regular/importCoreDataTaskFactory.js';
import verifySystemRequirementsTaskFactory
from './listr/tasks/setup/regular/verifySystemRequirementsTaskFactory.js';

/**
* @param {Object} [options]
Expand Down Expand Up @@ -300,6 +302,8 @@ export default async function createDIContainer(options = {}) {
registerMasternodeWithDMT: asFunction(registerMasternodeWithDMTFactory)
.singleton(),
importCoreDataTask: asFunction(importCoreDataTaskFactory).singleton(),
verifySystemRequirementsTask: asFunction(verifySystemRequirementsTaskFactory)
.singleton(),
});

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import chalk from 'chalk';
import { Listr } from 'listr2';
import os from 'os';
import * as diskusage from 'diskusage';
import si from 'systeminformation';

/**
*
* @param {Docker} docker
* @param {DockerCompose} dockerCompose
* @return {verifySystemRequirementsTask}
*/
export default function verifySystemRequirementsTaskFactory(docker, dockerCompose) {
/**
* @typedef {function} verifySystemRequirementsTask
* @returns {Listr}
*/
async function verifySystemRequirementsTask() {
return new Listr([
{
title: 'System requirements',
task: async (ctx, task) => {
await dockerCompose.throwErrorIfNotInstalled();

const MINIMUM_CPU_CORES = ctx.isHP ? 4 : 2;
const MINIMUM_CPU_FREQUENCY = 2.4; // GHz
const MINIMUM_RAM = ctx.isHP ? 8 : 4; // GB
const MINIMUM_DISK_SPACE = ctx.isHP ? 200 : 100; // GB

const warnings = [];

// Get system info
let systemInfo;
try {
systemInfo = await docker.info();
} catch (e) {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn(`Can't get docker info: ${e}`);
}
}

if (systemInfo) {
if (Number.isInteger(systemInfo.NCPU)) {
// Check CPU cores
const cpuCores = systemInfo.NCPU;

if (cpuCores < MINIMUM_CPU_CORES) {
warnings.push(`${cpuCores} CPU cores detected. At least ${MINIMUM_CPU_CORES} are required`);
}
} else {
// eslint-disable-next-line no-console
console.warn('Can\'t get NCPU from docker info');
}

// Check RAM
if (Number.isInteger(systemInfo.MemTotal)) {
const memoryGb = systemInfo.MemTotal / (1024 ** 3); // Convert to GB

if (memoryGb < MINIMUM_RAM) {
warnings.push(`${memoryGb.toFixed(2)}GB RAM detected. At least ${MINIMUM_RAM}GB is required`);
}
} else {
// eslint-disable-next-line no-console
console.warn('Can\'t get MemTotal from docker info');
}
}

// Check CPU frequency
let hostCpu;
try {
hostCpu = await si.cpu();
} catch {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn('Can\'t get CPU info');
}
}

if (hostCpu) {
if (hostCpu.speed === 0) {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn('Can\'t get CPU frequency');
}
} else if (hostCpu.speed < MINIMUM_CPU_FREQUENCY) {
warnings.push(`${hostCpu.speed.toFixed(1)}GHz CPU frequency detected. At least ${MINIMUM_CPU_FREQUENCY}GHz is required`);
}
}

// Check swap information
let swap;
try {
swap = await si.mem();
} catch (e) {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn(`Can't get swap info: ${e}`);
}
}

if (swap) {
const swapTotalGb = (swap.swaptotal / (1024 ** 3)); // Convert bytes to GB

if (swapTotalGb < 2) {
warnings.push(`Swap space is ${swapTotalGb.toFixed(2)}GB. 2GB is recommended`);
}
}

// Get disk usage info
let diskInfo;

if (systemInfo) {
try {
diskInfo = await diskusage.check(systemInfo.DockerRootDir);
} catch (e) {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn(`Can't get disk usage for '${systemInfo.DockerRootDir}': ${e}`);
}
}
}

if (!diskInfo) {
try {
diskInfo = await diskusage.check(os.platform() === 'win32' ? 'c:' : '/');
} catch (e) {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn(`Can't get disk usage for root directory: ${e}`);
}
}
}

if (diskInfo) {
const availableDiskSpace = diskInfo.available / (1024 ** 3); // Convert to GB

if (availableDiskSpace < MINIMUM_DISK_SPACE) {
warnings.push(`${availableDiskSpace.toFixed(2)}GB available disk space detected. At least ${MINIMUM_DISK_SPACE}GB is required`);
}
}

if (warnings.length > 0) {
const warningsText = warnings.map((warning) => ` - ${warning}`).join('\n');

const header = chalk` Minimum requirements have not been met:
{red ${warningsText}}
Dash Platform requires more resources than the current system provides.
Evonode rewards are paid based on block production, and resource-limited
nodes may not be able to produce blocks quickly enough to receive reward
payments. Upgrading system resources is recommended before proceeding.
{bold This server may not receive Dash Platform reward payments due to its resource limitations.}\n`;

// This option is used for tests
if (ctx.acceptUnmetSystemRequirements) {
// eslint-disable-next-line no-console
console.warn(header);
} else {
const proceed = await task.prompt({
type: 'toggle',
header,
message: ' Are you sure you want to proceed?',
enabled: 'Yes',
disabled: 'No',
initial: false,
});

if (!proceed) {
throw new Error('System requirements have not been met');
}
}
}
},
options: {
persistentOutput: true,
},
},
]);
}

return verifySystemRequirementsTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import generateRandomString from '../../../util/generateRandomString.js';
* @param {resolveDockerHostIp} resolveDockerHostIp
* @param {generateHDPrivateKeys} generateHDPrivateKeys
* @param {HomeDir} homeDir
* @param {DockerCompose} dockerCompose
*/
export default function setupLocalPresetTaskFactory(
configFile,
Expand All @@ -23,13 +24,18 @@ export default function setupLocalPresetTaskFactory(
resolveDockerHostIp,
generateHDPrivateKeys,
homeDir,
dockerCompose,
) {
/**
* @typedef {setupLocalPresetTask}
* @return {Listr}
*/
function setupLocalPresetTask() {
return new Listr([
{
title: 'System requirements',
task: async () => dockerCompose.throwErrorIfNotInstalled(),
},
{
title: 'Set the number of nodes',
enabled: (ctx) => ctx.nodeCount === undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import generateRandomString from '../../../util/generateRandomString.js';
* @param {configureNodeTask} configureNodeTask
* @param {configureSSLCertificateTask} configureSSLCertificateTask
* @param {DefaultConfigs} defaultConfigs
* @param {verifySystemRequirementsTask} verifySystemRequirementsTask
* @param {importCoreDataTask} importCoreDataTask
*/
export default function setupRegularPresetTaskFactory(
Expand All @@ -37,6 +38,7 @@ export default function setupRegularPresetTaskFactory(
configureSSLCertificateTask,
defaultConfigs,
importCoreDataTask,
verifySystemRequirementsTask,
) {
/**
* @typedef {setupRegularPresetTask}
Expand Down Expand Up @@ -81,6 +83,7 @@ export default function setupRegularPresetTaskFactory(

ctx.config = defaultConfigs.get(ctx.preset);

// TODO: We need to change this and enable platform on mainnet
ctx.config.set('platform.enable', ctx.isHP && ctx.config.get('network') !== PRESET_MAINNET);
ctx.config.set('core.masternode.enable', ctx.nodeType === NODE_TYPE_MASTERNODE);

Expand All @@ -100,6 +103,9 @@ export default function setupRegularPresetTaskFactory(
persistentOutput: true,
},
},
{
task: () => verifySystemRequirementsTask(),
},
{
enabled: (ctx) => ctx.nodeType === NODE_TYPE_MASTERNODE,
task: async (ctx, task) => {
Expand Down
1 change: 1 addition & 0 deletions packages/dashmate/test/e2e/testnetEvonode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Testnet Evonode', function main() {
chainFilePath: certificatePath,
privateFilePath: privKeyPath,
},
acceptUnmetSystemRequirements: true,
isVerbose: true,
});

Expand Down
1 change: 1 addition & 0 deletions packages/dashmate/test/e2e/testnetFullnode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('Testnet Fullnode', function main() {
chainFilePath: certificatePath,
privateFilePath: privKeyPath,
},
acceptUnmetSystemRequirements: true,
isVerbose: true,
});

Expand Down
Loading

0 comments on commit de883f9

Please sign in to comment.