summaryrefslogtreecommitdiffstats
path: root/server/api/configloader.js
diff options
context:
space:
mode:
authorChristian Hofmaier2018-10-11 17:50:13 +0200
committerChristian Hofmaier2018-10-11 17:50:13 +0200
commited11b442619a67456c656760347f7c06e45af755 (patch)
tree8eeb7f123d7409a7678775704c083a6ea022685c /server/api/configloader.js
parent[registration] Add functionality to the set registration state API call (diff)
downloadbas-ed11b442619a67456c656760347f7c06e45af755.tar.gz
bas-ed11b442619a67456c656760347f7c06e45af755.tar.xz
bas-ed11b442619a67456c656760347f7c06e45af755.zip
[configloader] load parent config
if client has no config, search the parents if multiple parents of a layer have a config, build a dynamic menu if client has no parents or no parent has a config, load default
Diffstat (limited to 'server/api/configloader.js')
-rw-r--r--server/api/configloader.js114
1 files changed, 91 insertions, 23 deletions
diff --git a/server/api/configloader.js b/server/api/configloader.js
index 3831c28..7639226 100644
--- a/server/api/configloader.js
+++ b/server/api/configloader.js
@@ -9,7 +9,7 @@ noAuthRouter.get('/:uuid', (req, res) => {
const uuid = req.params.uuid
res.setHeader('content-type', 'text/plain')
- db.client.findOne({ where: {uuid: uuid}, include: ['groups'] }).then(client => {
+ db.client.findOne({ where: { uuid: uuid }, include: ['groups'] }).then(client => {
if (client !== null) {
// client is in db
if (client.configId !== null) {
@@ -19,17 +19,37 @@ noAuthRouter.get('/:uuid', (req, res) => {
})
return
}
- // client has no config, check parent groups (first layer)
+ // client has no config, check parent groups
+ var parentIds = []
+ var configIds = []
for (var i = 0; i < client.groups.length; i++) {
- if (client.groups[i].configId !== null) {
- createIpxeScript(client.groups[i].configId).then(response => {
- res.send(response)
- })
- return
- }
+ // gather parent ids
+ parentIds.push(client.groups[i].id)
+ }
+ if (parentIds.length !== 0) {
+ // client has a parent to look for
+ checkGroupsForConfigs(parentIds).then(response => {
+ configIds = response
+ if (configIds.length === 1) {
+ // only one parent has a config, load it
+ createIpxeScript(configIds[0]).then(response => {
+ res.send(response)
+ })
+ return
+ } else if (configIds.length > 1) {
+ // multiple parents have a config, make dynamic menu
+ createDynamicMenu(configIds).then(response => {
+ res.send(response)
+ })
+ return
+ }
+ // no parent has a config, load default
+ res.sendFile(path.join(__appdir, 'ipxe', 'default.ipxe'))
+ })
+ } else {
+ // no config given, load default
+ res.sendFile(path.join(__appdir, 'ipxe', 'default.ipxe'))
}
- // no config given, load default
- res.sendFile(path.join(__appdir, 'ipxe', 'default.ipxe'))
} else {
// client not known, start registration
res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe'))
@@ -37,8 +57,47 @@ noAuthRouter.get('/:uuid', (req, res) => {
})
})
+// load config by given id
+noAuthRouter.get('/getconfig/:configId', (req, res) => {
+ const configId = req.params.configId
+ res.setHeader('content-type', 'text/plain')
+
+ createIpxeScript(configId).then(response => {
+ res.send(response)
+ })
+})
+
+// recursive iteration through the layers of parents until a config is found or no parents left
+function checkGroupsForConfigs (groupIds) {
+ var parentIds = []
+ var configIds = []
+ if (groupIds.length === 0) {
+ return configIds
+ }
+ return db.group.findAll({ where: { id: groupIds }, include: ['parents'] }).then(groups => {
+ groups.forEach(group => {
+ group.parents.forEach(parent => {
+ if (!parentIds.includes(parent.id)) {
+ parentIds.push(parent.id)
+ }
+ })
+ if (group.configId !== null && !configIds.includes(group.configId)) {
+ configIds.push(group.configId)
+ }
+ })
+ if (configIds.length !== 0) {
+ return configIds
+ } else {
+ return checkGroupsForConfigs(parentIds).then(response => {
+ return response
+ })
+ }
+ })
+}
+
+// create the config script from database
function createIpxeScript (id) {
- return db.config.findOne({ where: {id: id}, include: ['entries'], order: [[['entries'], 'sortValue', 'ASC']] }).then(config => {
+ return db.config.findOne({ where: { id: id }, include: ['entries'], order: [[['entries'], 'sortValue', 'ASC']] }).then(config => {
if (config.script !== null) {
return config.script
}
@@ -73,19 +132,28 @@ function createIpxeScript (id) {
})
}
-/*
-Entry: ID, Name, Script
-Config: ID, Name, Description, DefaultEntry, timeout, script
-config_x_entries: configid, entryid, sortvalue, customName, keyBind
-
-function getConfigId
- -> Check First Layer of Parents
- -> Check Recursive until a layer with configids is found
- -> get all the config ids and build a menu with all the configs
+// create dynamic menu to load the different given configs for a client
+function createDynamicMenu (ids) {
+ return db.config.findAll({ where: { id: ids } }).then(configs => {
+ var script = ''
+ var menuscript = ''
+ script += '#!ipxe\r\n\r\n'
+ script += ':start\r\n'
+ script += 'menu ' + 'Placeholder Title' + '\r\n'
+ configs.forEach(config => {
+ script += 'item '
+ script += ' menuentry' + config.id + ' '
+ script += config.name
+ script += '\r\n'
-https://bas.stfu-kthx.net:8890/api/ipxe/load/registration
+ menuscript += ':' + 'menuentry' + config.id + '\r\n'
+ menuscript += 'chain ' + 'https://bas.stfu-kthx.net:8888/api/configloader/getconfig/' + config.id + '\r\n\r\n'
+ })
+ script += '\r\n'
+ script += menuscript
-https://awwapp.com/b/ur9b4jivj/
-*/
+ return script
+ })
+}
module.exports.noAuthRouter = noAuthRouter