feat: insert stress testing machines into DB
This commit is contained in:
parent
0a120203be
commit
99dd90d51a
5 changed files with 74 additions and 41 deletions
|
|
@ -1,11 +1,16 @@
|
|||
const cp = require('node:child_process')
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const { fork } = require('node:child_process')
|
||||
const { createHash } = require('node:crypto')
|
||||
const { mkdirSync, rmSync } = require('node:fs')
|
||||
const { readFile } = require('node:fs/promises')
|
||||
const { join } = require('node:path')
|
||||
|
||||
require('../../lib/environment-helper')
|
||||
const db = require('../../lib/db')
|
||||
|
||||
const { EXIT } = require('./consts')
|
||||
const CLI = require('./cli')
|
||||
|
||||
const help_message = "Setup fake machines to be used as stress test clients."
|
||||
const help_message = "Create and insert fake machines into the DB."
|
||||
|
||||
const cli = CLI({
|
||||
grammar: [
|
||||
|
|
@ -13,6 +18,7 @@ const cli = CLI({
|
|||
[["--machine", "PATH"], "Path to the machine's source code root"],
|
||||
[["--fake_data_dir", "PATH"], "Where to save the fake machines' data"],
|
||||
[["-n", "NUMBER"], "Number of fake machines to create"],
|
||||
[["--replace_existing"], "Remove machines of previous runs"],
|
||||
],
|
||||
})
|
||||
|
||||
|
|
@ -23,13 +29,19 @@ const help = (exit_code) => {
|
|||
return exit_code
|
||||
}
|
||||
|
||||
const create_fake_machine = async (gencerts_path, fake_data_dir, i) =>
|
||||
const compute_machine_id = pem_path => (
|
||||
readFile(pem_path, { encoding: 'utf8' })
|
||||
.then(cert => cert.split('\r\n'))
|
||||
.then(cert => Buffer.from(cert.slice(1, cert.length-2).join(''), 'base64'))
|
||||
.then(raw => createHash('sha256').update(raw).digest('hex'))
|
||||
)
|
||||
|
||||
const create_fake_machine = async (gencerts_path, machine_data_dir, i) => (
|
||||
new Promise((resolve, reject) => {
|
||||
const machine_data_dir = path.join(fake_data_dir, i.toString())
|
||||
fs.mkdirSync(machine_data_dir, { recursive: true, mode: 0o750 })
|
||||
mkdirSync(machine_data_dir, { recursive: true, mode: 0o750 })
|
||||
|
||||
console.log("Creating fake machine number", i)
|
||||
const gc = cp.fork(gencerts_path, [machine_data_dir], {
|
||||
const gc = fork(gencerts_path, [machine_data_dir], {
|
||||
cwd: process.cwd(),
|
||||
encoding: 'utf8',
|
||||
})
|
||||
|
|
@ -45,26 +57,40 @@ const create_fake_machine = async (gencerts_path, fake_data_dir, i) =>
|
|||
resolve(typeof(code) === 'number' ? code : EXIT.EXCEPTION)
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
const create_fake_machines = async ({ machine, fake_data_dir, n }) => {
|
||||
const create_fake_machines = async ({ machine, fake_data_dir, n, replace_existing }) => {
|
||||
n = parseInt(n)
|
||||
if (Number.isNaN(n) || n <= 0) {
|
||||
console.error("Expected n to be a positive number, got", n)
|
||||
return help(EXIT.BADARGS)
|
||||
}
|
||||
|
||||
/* TODO: Remove all data of previous machines? */
|
||||
//fs.rmSync(fake_data_dir, { recursive: true, force: true })
|
||||
if (replace_existing) {
|
||||
rmSync(fake_data_dir, { recursive: true, force: true })
|
||||
await db.none("DELETE FROM devices")
|
||||
}
|
||||
|
||||
/* Create the root data directory */
|
||||
fs.mkdirSync(fake_data_dir, { recursive: true, mode: 0o750 })
|
||||
mkdirSync(fake_data_dir, { recursive: true, mode: 0o750 })
|
||||
|
||||
const gencerts_path = path.join(machine, "tools", "generate-certificates")
|
||||
let exit_code = EXIT.OK
|
||||
for (let i = 0; i < n && exit_code === EXIT.OK; i++)
|
||||
exit_code = await create_fake_machine(gencerts_path, fake_data_dir, i)
|
||||
const gencerts_path = join(machine, "tools", "generate-certificates")
|
||||
for (let i = 0; i < n; i++) {
|
||||
const machine_data_dir = join(fake_data_dir, i.toString())
|
||||
const exit_code = await create_fake_machine(gencerts_path, machine_data_dir, i)
|
||||
if (exit_code !== EXIT.OK)
|
||||
return exit_code
|
||||
|
||||
return exit_code
|
||||
const device_id = await compute_machine_id(join(machine_data_dir, "client.pem"))
|
||||
|
||||
await db.none(
|
||||
`INSERT INTO devices (device_id, cassette1, cassette2, paired, display, created, name, last_online, location)
|
||||
VALUES ($1, 0, 0, 't', 't', now(), $2, now(), '{}'::json)`,
|
||||
[device_id, `machine_${i}`]
|
||||
)
|
||||
}
|
||||
|
||||
return EXIT.OK
|
||||
}
|
||||
|
||||
const run = async (args) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue