summaryrefslogtreecommitdiffstats
path: root/server/api/registration.js
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-20 05:22:50 +0100
committerJannik Schönartz2019-03-20 05:22:50 +0100
commit67d1402c75e562af34058d0021cf6a14b5588d22 (patch)
treed977ee4fce3adbf0de27b4372c5589ece81e54d9 /server/api/registration.js
parent[server/registration] Add client with json instead of parameters (ipxe) (diff)
downloadbas-67d1402c75e562af34058d0021cf6a14b5588d22.tar.gz
bas-67d1402c75e562af34058d0021cf6a14b5588d22.tar.xz
bas-67d1402c75e562af34058d0021cf6a14b5588d22.zip
[server/registration/backends] Rework addClient and updateClient to receive json
Add idoit workaround for saving floats again Rework the grepSystemInfo bash script to match the new api
Diffstat (limited to 'server/api/registration.js')
-rw-r--r--server/api/registration.js157
1 files changed, 20 insertions, 137 deletions
diff --git a/server/api/registration.js b/server/api/registration.js
index 11de700..4891e31 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -107,17 +107,17 @@ noAuthRouter.post('/group', (req, res) => {
})
/*
- * Reworked add method for adding a client or server.
+ * Add method for adding a client or server.
*/
noAuthRouter.postAsync('/', async (req, res) => {
let client = req.body.client
if (typeof client === 'string') client = JSON.parse(client)
- const ipxe = req.body.ipxe
- if (typeof ipxe === 'string') ipxe === 'true' ? true : false
+ let ipxe = req.body.ipxe
+ if (typeof ipxe === 'string') ipxe = true
if (!client.type) client.type = 'CLIENT'
- if (!client.title) client.title = client.type + '_' + client.uuid
+ if (!client.name) client.name = client.type + '_' + client.uuid
// If the client already exists return the configloader ipxe script.
const clientDb = await db.client.findOne({ where: { uuid: client.uuid } })
@@ -128,7 +128,7 @@ noAuthRouter.postAsync('/', async (req, res) => {
// Client does not exist.
if (!client.parents) client.parents = []
- const createClient = { name: client.title, description: client.type, ip: client.network.ip, mac: client.network.mac, uuid: client.uuid }
+ const createClient = { name: client.name, description: client.type, ip: client.network.ip, mac: client.network.mac, uuid: client.uuid }
if (client.type === 'CLIENT') createClient.registrationState = await getNextHookScript(client.parents)
const newClient = await db.client.create(createClient)
client.id = newClient.id
@@ -136,7 +136,7 @@ noAuthRouter.postAsync('/', async (req, res) => {
// Add groups to the client.
if (client.parents.length === 0) client.parents = await ipHelper.getGroups(client.network.ip)
client.parents.forEach(pid => { newClient.addGroup(pid) })
- log({ category: 'CLIENT_REGISTRATION', description: client.type + ' added successfully.', clientId: newClient.id })
+ log({ category: 'CLIENT_REGISTRATION', description: 'Client added successfully.', clientId: newClient.id })
// Add the client to the backends.
const result = await backendHelper.addClient(client)
@@ -144,142 +144,25 @@ noAuthRouter.postAsync('/', async (req, res) => {
else return res.send(result)
})
-/*
- * Adds the client to the database and set parents if a parent was selected. Calls addClient for all external-backends.
- */
-noAuthRouter.postAsync('/add', async (req, res) => {
- const feedback = req.body.feedback
- const mac = req.body.mac
- const uuid = req.body.uuid
- const ip = req.body.ip
- let name = req.body.name
- const parentId = parseInt(req.body.id)
- const purpose = req.body.purpose
- let parentIds = []
-
- if (!name) name = 'Client_' + uuid
-
- // If the client already exists return the configloader ipxe script.
- const client = await db.client.findOne({ where: { uuid: uuid } })
- if (client) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
-
- // Else (Client does not exist)
- var groupids = []
- if (parentId) groupids = [parentId]
- const resId = await getNextHookScript(groupids)
- const newClient = await db.client.create({ name: name, description: 'Client', ip: ip, mac: mac, uuid: uuid, registrationState: resId })
- if (parentId) {
- newClient.addGroup(parentId)
- parentIds.push(parentId)
- } else {
- // Filtered list with all group we will add the client
- parentIds = await ipHelper.getGroups(ip)
- parentIds.forEach(pid => { newClient.addGroup(pid) })
- }
- log({ category: 'CLIENT_REGISTRATION', description: 'Client added successfully.', clientId: newClient.id })
-
- // Add the client to the backends.
- var c = { id: newClient.id, title: name, uuid: uuid, network: { mac: mac, ip: ip }, type: req.body.type }
- if (parentIds.length > 0) c.parents = parentIds
- if (purpose) c.purpose = purpose
-
- var result = await backendHelper.addClient(c)
-
- if (feedback) res.send(result)
- else res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
-})
-
-noAuthRouter.postAsync('/:uuid/update', async (req, res) => {
- const uuid = req.params.uuid
- const name = req.body.name
- const parentId = req.body.id
-
- // System
- const sysManufacturer = req.body.sys_manufacturer
- const sysModel = req.body.sys_model
- const sysSerial = req.body.sys_serial
-
- // CPU
- const cpuModel = req.body.cpu_model
- const cpuManufacturer = req.body.cpu_manufacturer
- const cpuType = req.body.cpu_type
- var cpuFrequency = req.body.cpu_frequency / 1000
- const cpuCores = req.body.cpu_cores
-
- // RAM
- if (req.body.ram_size) {
- const ramSize = req.body.ram_size.split('\n')
- const ramManufacturer = req.body.ram_manufacturer.split('\n')
- const ramFormfactor = req.body.ram_formfactor.split('\n')
- const ramType = req.body.ram_type.split('\n')
- const ramIsEcc = req.body.ram_isecc.replace('Error Correction Type: ', '')
- var ramModules = []
-
- // Build ram array
- for (let ram in ramSize) {
- if (ramSize[ram].replace('Size: ', '') !== 'No Module Installed') {
- const size = ramSize[ram].replace('Size: ', '').split(' ')
- let title = ramFormfactor[ram].replace('Form Factor: ', '')
- if (ramIsEcc === 'Single-bit ECC') title += '-ECC'
-
- const ramModule = {
- capacity: size[0],
- unit: size[1],
- manufacturer: ramManufacturer[ram].replace('Manufacturer: ', ''),
- title: title,
- type: ramType[ram].replace('Type: ', '')
- }
- ramModules.push(ramModule)
- }
- }
- }
+noAuthRouter.postAsync('/:uuid', async (req, res) => {
+ let client = req.body.client
- // SSD / HDD
- if (req.body.drives) {
- const drivesRaw = req.body.drives.split('%OBJECT_SPLITTER%')
- var drives = []
- for (let driveRaw in drivesRaw) {
- if (drivesRaw[driveRaw].length > 0) {
- const dRaw = drivesRaw[driveRaw].split('%ATTRIBUTE_SPLITTER%')
- const drive = {
- model: dRaw[0].trim().replace('Device Model: ', ''),
- serial: dRaw[1].trim().replace('Serial Number: ', ''),
- capacity: dRaw[2].trim().split(' ')[0],
- unit: dRaw[2].trim().split(' ')[1],
- type: dRaw[3].trim().replace('Rotation Rate: ', ''),
- formfactor: dRaw[4].trim().replace('Form Factor: ', ''),
- connection: dRaw[5].trim().replace('SATA Version is: ', '')
- }
- drives.push(drive)
- }
- }
+ // Add the name to the ram modules.
+ for (let ram of client.ram.modules) {
+ ram.name = ram.formfactor
+ if (client.ram.isEcc === 'Single-bit ECC') ram.name += '-ECC'
}
- const client = await db.client.findOne({ where: { uuid: uuid } })
- if (!client) return res.status(404).send({ error: 'CLIENT_NOT_FOUND', message: 'There is no client matching the provided uuid.' })
-
- client.update({ name: name })
- var c = { uuid: uuid, id: client.id }
- if (name) c.title = name
- if (parentId) c.parentId = parentId
+ const clientDb = await db.client.findOne({ where: { uuid: client.uuid } })
+ if (!clientDb) return res.status(404).send({ error: 'CLIENT_NOT_FOUND', message: 'There is no client matching the provided uuid.' })
+ if (client.name) clientDb.update({ name: client.name })
+ client.id = clientDb.id
// System data. Sometime just string with whitespaces only.
- c.system = {}
- if (/\S/.test(sysManufacturer)) c.system.manufacturer = sysManufacturer
- else c.system.manufacturer = 'Not set'
-
- if (/\S/.test(sysModel)) c.system.model = sysModel
- else c.system.model = 'Not set'
-
- if (/\S/.test(sysSerial)) c.system.serialnumber = sysSerial
- else c.system.serialnumber = 'Not set'
-
- // TODO: MULTI GPU's ?!
- c.cpu = { model: cpuModel, manufacturer: cpuManufacturer, type: cpuType, frequency: cpuFrequency, cores: cpuCores }
- if (ramModules) c.ram = ramModules
- if (drives) c.drives = drives
-
- const result = await backendHelper.updateClient(c)
+ if (!/\S/.test(client.system.manufacturer)) client.system.manufacturer = 'unavailable'
+ if (!/\S/.test(client.system.model)) client.system.model = 'unavailable'
+ if (!/\S/.test(client.system.serialnumber)) client.system.serialnumber = 'unavailable'
+ const result = await backendHelper.updateClient(client)
res.send(result)
})