feat: idle machine polling
This commit is contained in:
parent
ea36d66167
commit
20727a60cd
7 changed files with 198 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -32,6 +32,8 @@ scratch/
|
|||
seeds/
|
||||
mnemonics/
|
||||
certs/
|
||||
test/stress/machines
|
||||
test/stress/config.json
|
||||
lamassu.json
|
||||
|
||||
terraform.*
|
||||
|
|
|
|||
68
test/stress/index.js
Normal file
68
test/stress/index.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
const https = require('https')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const uuid = require('uuid')
|
||||
const cmd = require('./scripts')
|
||||
const variables = require('./utils/variables')
|
||||
|
||||
async function createMachines () {
|
||||
await cmd.execCommand(
|
||||
`bash ./scripts/create-machines.sh ${variables.NUMBER_OF_MACHINES} ${variables.SERVER_CERT_PATH} ${variables.MACHINE_PATH}`
|
||||
)
|
||||
}
|
||||
|
||||
function getCert (machineIndex) {
|
||||
try {
|
||||
return {
|
||||
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
|
||||
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
|
||||
}
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function connectionInfo (machineIndex) {
|
||||
console.log(machineIndex)
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
let index = 0
|
||||
|
||||
function request (machineIndex, pid) {
|
||||
https.get({
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + index,
|
||||
method: 'GET',
|
||||
key: getCert(machineIndex).key,
|
||||
cert: getCert(machineIndex).cert,
|
||||
ca: connectionInfo(machineIndex).ca,
|
||||
headers: {
|
||||
date: new Date().toISOString(),
|
||||
'request-id': uuid.v4()
|
||||
}
|
||||
}, res => {
|
||||
res.on('data', (d) => {
|
||||
console.log(Buffer.from(d).toString())
|
||||
})
|
||||
})
|
||||
|
||||
index++
|
||||
}
|
||||
|
||||
function run () {
|
||||
createMachines().then(() => {
|
||||
for (let i = 1; i <= variables.NUMBER_OF_MACHINES; i++) {
|
||||
const pid = uuid.v4()
|
||||
request(i, pid)
|
||||
setInterval(() => request(i, pid), 5000)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
61
test/stress/scripts/create-machines.sh
Normal file
61
test/stress/scripts/create-machines.sh
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
echo "usage: ./build-machines [number_of_machines] /path/to/server/cert/lamassu_op_root_ca.pem /path/to/machine/" && exit 1
|
||||
fi
|
||||
|
||||
case $1 in
|
||||
''|*[!0-9]*) echo "usage: ./build-machines [number_of_machines] /path/to/server/cert/lamassu_op_root_ca.pem /path/to/machine/" && exit 1;;
|
||||
esac
|
||||
|
||||
SERVER_CERT=$(perl -pe 's/\n/\\n/' < $2)
|
||||
if [ -z "$SERVER_CERT" ]
|
||||
then
|
||||
echo "Lamassu-op-root-ca.pem is empty" && exit 1
|
||||
fi
|
||||
|
||||
# Remove old folders
|
||||
rm -rf ./machines/*
|
||||
|
||||
# Create stress database
|
||||
# sudo -u postgres psql postgres -c "drop database if exists lamassu_stress"
|
||||
# sudo -u postgres psql postgres -c "create database lamassu_stress with template lamassu"
|
||||
|
||||
START=1
|
||||
END=$1
|
||||
for (( c=$START; c<=$END; c++ ))
|
||||
do
|
||||
NUMBER=$c
|
||||
mkdir -p ./machines/$NUMBER/
|
||||
cp "$3"/data/client.sample.pem ./machines/$NUMBER/
|
||||
cp "$3"/data/client.sample.key ./machines/$NUMBER/
|
||||
|
||||
|
||||
cat > ./machines/$NUMBER/connection_info.json << EOL
|
||||
{"host":"localhost","ca":"$SERVER_CERT"}
|
||||
EOL
|
||||
|
||||
echo 'Generating certs...'
|
||||
node ./utils/init-cert.js $NUMBER
|
||||
|
||||
# Get device_id
|
||||
DEVICE_ID=`openssl x509 -outform der -in ./machines/$NUMBER/client.pem | sha256sum | cut -d " " -f 1`
|
||||
|
||||
# Update db config
|
||||
sudo -u postgres psql -t -d lamassu -c "select data from user_config where type='config' order by id desc limit 1" > config.json
|
||||
NEW_CONFIG=$(node ./utils/save-config.js $NUMBER $DEVICE_ID)
|
||||
sudo -u postgres psql "lamassu" << EOF
|
||||
insert into user_config(type, data, created, valid)
|
||||
values('config', '$NEW_CONFIG', now(), 't')
|
||||
EOF
|
||||
|
||||
# Add device on db
|
||||
sudo -u postgres psql "lamassu" << EOF
|
||||
insert into devices(device_id, cashbox, cassette1, cassette2, paired, display, created, name, last_online, location)
|
||||
values ('$DEVICE_ID', 0, 0, 0, 't', 't', now(), $NUMBER, now(), '{}'::json)
|
||||
EOF
|
||||
done
|
||||
|
||||
echo "Done!"
|
||||
22
test/stress/scripts/index.js
Normal file
22
test/stress/scripts/index.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
const exec = require('child_process')
|
||||
|
||||
/**
|
||||
* Execute simple shell command (async wrapper).
|
||||
* @param {String} cmd
|
||||
* @return {Object} { stdout: String, stderr: String }
|
||||
*/
|
||||
async function execCommand (cmd) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
exec.exec(cmd, (err, stdout, stderr) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
console.log(stdout)
|
||||
console.error(stderr)
|
||||
// resolve({ stdout, stderr })
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { execCommand }
|
||||
12
test/stress/utils/init-cert.js
Normal file
12
test/stress/utils/init-cert.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
const path = require('path')
|
||||
const variables = require('./variables')
|
||||
const { init } = require(`../${variables.MACHINE_PATH}/lib/pairing`)
|
||||
|
||||
const number = process.argv[2]
|
||||
|
||||
const certPath = {
|
||||
cert: path.resolve(process.cwd(), 'machines', number, 'client.pem'),
|
||||
key: path.resolve(process.cwd(), 'machines', number, 'client.key')
|
||||
}
|
||||
|
||||
init(certPath)
|
||||
28
test/stress/utils/save-config.js
Normal file
28
test/stress/utils/save-config.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
const number = process.argv[2]
|
||||
const machine = process.argv[3]
|
||||
const old = require('../config.json')
|
||||
|
||||
function configAddField (scope, fieldCode, fieldType, fieldClass, value) {
|
||||
return {
|
||||
fieldLocator: {
|
||||
fieldScope: {
|
||||
crypto: scope.crypto,
|
||||
machine: scope.machine
|
||||
},
|
||||
code: fieldCode,
|
||||
fieldType,
|
||||
fieldClass
|
||||
},
|
||||
fieldValue: { fieldType, value }
|
||||
}
|
||||
}
|
||||
|
||||
const scope = { crypto: 'global', machine }
|
||||
|
||||
const newFields = [
|
||||
configAddField(scope, 'cashOutEnabled', 'onOff', null, false),
|
||||
configAddField(scope, 'machineName', 'string', null, number),
|
||||
configAddField(scope, 'machineModel', 'string', null, 'Linux')
|
||||
]
|
||||
const data = { config: newFields.concat(old.config) }
|
||||
console.log(JSON.stringify(data))
|
||||
5
test/stress/utils/variables.js
Normal file
5
test/stress/utils/variables.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
const NUMBER_OF_MACHINES = 4
|
||||
const SERVER_CERT_PATH = `../../certs/Lamassu_OP_Root_CA.pem`
|
||||
const MACHINE_PATH = `../../../lamassu-machine`
|
||||
|
||||
module.exports = { NUMBER_OF_MACHINES, SERVER_CERT_PATH, MACHINE_PATH }
|
||||
Loading…
Add table
Add a link
Reference in a new issue