Skip to content

Commit

Permalink
Fix number of cpus calculation, registration do speedtest after
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Rodriguez Baquero committed Nov 1, 2022
1 parent 8474c9a commit 4694b96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
32 changes: 19 additions & 13 deletions container/shim/src/modules/registration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { X509Certificate } from 'node:crypto'
import http from 'http'
import http from 'node:http'
import https from 'node:https'
import fsPromises from 'node:fs/promises'
import fetch from 'node-fetch'
Expand Down Expand Up @@ -32,29 +32,27 @@ const TWO_DAYS_MS = 2 * 24 * 60 * 60 * 1000
export async function register (initial) {
const requirements = await fetch(`${ORCHESTRATOR_URL}/requirements`, { agent }).then(res => res.json())

if (parseVersionNumber(NODE_VERSION) < requirements.minVersion) {
throw new Error(`Node version ${NODE_VERSION} is too old. Minimum version: ${requirements.minVersion}. Please update your node and set up auto-update.`)
}

const stats = {
version: parseVersionNumber(NODE_VERSION),
memoryStats: await getMemoryStats(),
diskStats: await getDiskStats(),
cpuStats: await getCPUStats(),
nicStats: await getNICStats()
}

verifyHWRequirements(requirements, stats)

if (NODE_VERSION !== DEV_VERSION && initial) {
let speedtest
try {
speedtest = await getSpeedtest()
} catch (err) {
debug(`Error while performing speedtest: ${err.name} ${err.message}`)
}
verifyUplinkRequirements(requirements.minUploadSpeedMbps, speedtest)
Object.assign(stats, { speedtest })
}

verifyRequirements(requirements, stats)

const body = {
nodeId,
level: 1,
Expand Down Expand Up @@ -171,8 +169,12 @@ export const addRegisterCheckRoute = (app) => app.get('/register-check', (req, r
res.sendStatus(200)
})

function verifyRequirements (requirements, stats) {
const { minCPUCores, minMemoryGB, minUploadSpeedMbps, minDiskGB } = requirements
function verifyHWRequirements (requirements, stats) {
const { minVersion, minCPUCores, minMemoryGB, minDiskGB } = requirements

if (stats.version < minVersion) {
throw new Error(`Node version ${stats.version} is too old. Minimum version: ${requirements.minVersion}. Please update your node and set up auto-update.`)
}

if (stats.cpuStats.numCPUs < minCPUCores) {
throw new Error(`Not enough CPU cores. Required: ${minCPUCores}, current: ${stats.cpuStats.numCPUs}`)
Expand All @@ -182,17 +184,21 @@ function verifyRequirements (requirements, stats) {
throw new Error(`Not enough memory. Required: ${minMemoryGB} GB, available: ${stats.memoryStats.totalMemory}`)
}

if (stats.speedtest?.upload.bandwidth < (minUploadSpeedMbps * 1_000_000 / 8)) {
throw new Error(`Not enough upload speed. Required: ${minUploadSpeedMbps} Mbps, current: ${stats.speedtest.upload.bandwidth / 1_000_000 * 8} Mbps`)
}

if (stats.diskStats.totalDisk < minDiskGB) {
throw new Error(`Not enough disk space. Required: ${minDiskGB} GB, available: ${stats.diskStats.totalDisk}`)
}

debug('All requirements met')
}

function verifyUplinkRequirements (minUploadSpeedMbps, speedtest) {
if (!speedtest || speedtest?.upload.bandwidth < (minUploadSpeedMbps * 1_000_000 / 8)) {
throw new Error(`Not enough upload speed. Required: ${minUploadSpeedMbps} Mbps, current: ${speedtest.upload.bandwidth / 1_000_000 * 8} Mbps`)
}

debug('Speed requirement met')
}

function postOptions (body) {
return {
agent,
Expand Down
5 changes: 2 additions & 3 deletions container/shim/src/utils/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadavg } from 'node:os'
import { loadavg, cpus } from 'node:os'
import fsPromises from 'node:fs/promises'
import { debug as Debug } from './logging.js'
import { promisify } from 'node:util'
Expand Down Expand Up @@ -31,8 +31,7 @@ export async function getDiskStats () {
}

export async function getCPUStats () {
const result = await fsPromises.readFile('/proc/cpuinfo', 'utf-8')
const numCPUs = result.trim().split('\n\n').length
const numCPUs = cpus().length
const loadAvgs = loadavg()
debug(`CPUs: ${numCPUs} (${loadAvgs.join(', ')})`)
return { numCPUs, loadAvgs }
Expand Down

0 comments on commit 4694b96

Please sign in to comment.