summaryrefslogtreecommitdiffstats
path: root/server/api/registrations.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-10-11 23:02:32 +0200
committerJannik Schönartz2018-10-11 23:02:32 +0200
commitb2a6274c13a49a5d1e9362850df2cc32b1b7a183 (patch)
treecf25c5ff097c8acb9fc2ae5456db08fac95f0189 /server/api/registrations.js
parent[configloader] add missing line to make the dynamic menu selectable (diff)
downloadbas-b2a6274c13a49a5d1e9362850df2cc32b1b7a183.tar.gz
bas-b2a6274c13a49a5d1e9362850df2cc32b1b7a183.tar.xz
bas-b2a6274c13a49a5d1e9362850df2cc32b1b7a183.zip
[registration] Set registration state when client is added
[configloader] Add functionality to boot the ipxe script or minilinux. Add ipxe script, which boots the minilinux. Add script to grep the hw data with dmidecode
Diffstat (limited to 'server/api/registrations.js')
-rw-r--r--server/api/registrations.js56
1 files changed, 37 insertions, 19 deletions
diff --git a/server/api/registrations.js b/server/api/registrations.js
index 3c5c8d8..492118e 100644
--- a/server/api/registrations.js
+++ b/server/api/registrations.js
@@ -67,12 +67,17 @@ noAuthRouter.post('/add', (req, res) => {
db.client.findOne({ where: { uuid: uuid } }).then(client => {
if (client) res.status(200).send('#!ipxe\r\necho Client already exists\r\necho Press any key to continue ...\r\nread x\r\nreboot')
else {
- db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid }).then(client => {
- if (parentId) {
- client.addGroup(parentId)
- }
- res.send('#!ipxe\r\nreboot')
+ var groupids = []
+ if (parentId) groupids = [parentId]
+ getNextHookScript(groupids, 0).then(resId => {
+ db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid, registrationState: resId }).then(client => {
+ if (parentId) {
+ client.addGroup(parentId)
+ }
+ res.send('#!ipxe\r\nreboot')
+ })
})
+
}
})
})
@@ -80,10 +85,9 @@ noAuthRouter.post('/add', (req, res) => {
/*
* Open api method for setting the registration state of a given uuid.
*/
-noAuthRouter.post('/:uuid/state', (req, res) => {
+noAuthRouter.post('/:uuid/success', (req, res) => {
const uuid = req.params.uuid
- const state = parseInt(req.body.state)
-
+ const id = parseInt(req.body.id)
db.client.findOne({ where: { uuid: uuid }, include: ['groups'] }).then(client => {
// Client not found handling
if (client === null) {
@@ -91,8 +95,8 @@ noAuthRouter.post('/:uuid/state', (req, res) => {
return
}
- // Check if the finished script id (state) matches the current state of the client.
- if (client.registrationState !== state) {
+ // Check if the finished script id (id) matches the current state of the client.
+ if (client.registrationState !== id) {
res.status(400).send({ status: 'INVALID_SCRIPT', error: 'This script should not have been executed.' })
return
}
@@ -106,16 +110,12 @@ noAuthRouter.post('/:uuid/state', (req, res) => {
// Get the sort value of the current hook.
db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => {
- // Gets the list of all groupids inclusive the recursive parents.
- getRecursiveParents(groupids).then(gids => {
- // Get the list of all hooks where the parent is null or those who fullfill the group dependency.
- db.registrationhook.findAll({ where: { '$groups.id$': { $or: [null, gids] }, sortvalue: { $gt: hook.sortvalue } }, include: ['groups'], order: [['sortvalue', 'ASC']] }).then(result => {
- // Update the client's registration state
- client.updateAttributes({
- registrationState: result[0].id
- })
- res.send({ status: 'SUCCESS' })
+ getNextHookScript(groupids, hook.sortvalue).then(resID => {
+ // Update the client's registration state
+ client.updateAttributes({
+ registrationState: resID
})
+ res.send({ status: 'SUCCESS' })
})
})
})
@@ -136,6 +136,7 @@ noAuthRouter.get('/:uuid/nexthook', (req, res) => {
if (hook.type !== 'BASH') {
res.send()
} else {
+ res.set('id', client.registrationState)
res.send(hook.script)
}
})
@@ -145,6 +146,23 @@ noAuthRouter.get('/:uuid/nexthook', (req, res) => {
module.exports.noAuthRouter = noAuthRouter
/*
+ * parentIds:
+ * sortvalue:
+ *
+ */
+function getNextHookScript (groupids, sortvalue) {
+ // Gets the list of all groupids inclusive the recursive parents.
+ return getRecursiveParents(groupids).then(gids => {
+ // Get the list of all hooks where the parent is null or those who fullfill the group dependency.
+ return db.registrationhook.findAll({ where: { '$groups.id$': { $or: [null, gids] }, sortvalue: { $gt: sortvalue } }, include: ['groups'], order: [['sortvalue', 'ASC']] }).then(result => {
+ var resID = null
+ if (result.length >= 1) resID = result[0].id
+ return resID
+ })
+ })
+}
+
+/*
* groupids: Array of group ids to get the parents from.
*
* Returns a list of the grop ids and all recursive ids of their parents.