summaryrefslogtreecommitdiffstats
path: root/server/api/configloader.js
diff options
context:
space:
mode:
authorChristian Hofmaier2019-03-24 17:02:18 +0100
committerChristian Hofmaier2019-03-24 17:02:18 +0100
commit1ee0e6c1d7484930387438b7ebb15340595b5383 (patch)
treecff87a96c777b9fee0c3e066d1bd6d88763f1915 /server/api/configloader.js
parent[webapp] small design fixes (diff)
downloadbas-1ee0e6c1d7484930387438b7ebb15340595b5383.tar.gz
bas-1ee0e6c1d7484930387438b7ebb15340595b5383.tar.xz
bas-1ee0e6c1d7484930387438b7ebb15340595b5383.zip
[eventmanager] Add module and functionality
- list to show all events, buttons to create/delete events - can add groups/clients to event - can add blacklist to event
Diffstat (limited to 'server/api/configloader.js')
-rw-r--r--server/api/configloader.js230
1 files changed, 154 insertions, 76 deletions
diff --git a/server/api/configloader.js b/server/api/configloader.js
index 2422693..0ede64f 100644
--- a/server/api/configloader.js
+++ b/server/api/configloader.js
@@ -1,70 +1,125 @@
/* global __appdir */
-const path = require('path')
+var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
-var noAuthRouter = express.Router()
+const { decorateApp } = require('@awaitjs/express')
+var noAuthRouter = decorateApp(express.Router())
// if client in db -> load script (default if none is found), else load registration script
-noAuthRouter.get('/:uuid', (req, res) => {
+noAuthRouter.getAsync('/:uuid', async (req, res) => {
const uuid = req.params.uuid
res.setHeader('content-type', 'text/plain')
- db.client.findOne({ where: { uuid: uuid }, include: ['groups'] }).then(client => {
- if (client !== null) {
- // Check for registration hooks.
- if (client.registrationState !== null) {
- // client is in registration state, load scripts
- db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => {
- if (hook.type === 'IPXE') res.send(hook.script)
- else if (hook.type === 'BASH') res.sendFile(path.join(__appdir, 'ipxe', 'minilinux.ipxe'))
- })
+ var client = await db.client.findOne({ where: { uuid: uuid }, include: ['groups', 'events'] })
+ if (client !== null) {
+ // Client is in db, check for registration hooks.
+ if (client.registrationState !== null) {
+ // client is in registration state, load scripts
+ var hook = await db.registrationhook.findOne({ where: { id: client.registrationState } })
+ if (hook.type === 'IPXE') res.send(hook.script)
+ else if (hook.type === 'BASH') res.sendFile(path.join(__appdir, 'ipxe', 'minilinux.ipxe'))
+ return
+ }
+
+ var events = []
+ var blacklist = []
+ var importantEvents = []
+ var groupIds = []
+ var result
+
+ for (let i = 0; i < client.events.length; i++) {
+ if (client.events[i].important) importantEvents.push(client.events[i])
+ if (client.events[i].client_x_event.blacklist) blacklist.push(client.events[i].id)
+ else events.push(client.events[i])
+ }
+
+ importantEvents = importantEvents.filter(e => !blacklist.includes(e.id))
+ importantEvents = importantEvents.map(e => e.config)
+ importantEvents = importantEvents.filter(c => c !== null)
+ importantEvents = importantEvents.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos })
+ if (importantEvents.length === 1) {
+ result = await createIpxeScript(importantEvents[0])
+ res.send(result)
+ return
+ }
+ if (importantEvents.length > 1) {
+ result = await createDynamicMenu(importantEvents)
+ res.send(result)
+ return
+ }
+
+ events = events.filter(e => !blacklist.includes(e.id))
+ events = events.map(e => e.config)
+ events = events.filter(c => c !== null)
+ events = events.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos })
+
+ groupIds = client.groups.map(x => x.id)
+
+ var response = await fetchParentConfigs(groupIds, blacklist)
+ if (response.ids.length > 0) {
+ // Check if there is an important event
+ if (response.type === 'important') {
+ if (response.ids.length === 1) {
+ result = await createIpxeScript(response.ids[0])
+ res.send(result)
+ return
+ }
+ if (response.ids.length > 1) {
+ result = await createDynamicMenu(response.ids)
+ res.send(result)
+ return
+ }
+ }
+ // No important event, use client event if existent
+ if (events.length === 1) {
+ result = await createIpxeScript(events[0])
+ res.send(result)
return
}
-
- // client is in db
- if (client.configId !== null) {
- // client has a config
- createIpxeScript(client.configId).then(response => {
- res.send(response)
- })
+ if (events.length > 1) {
+ result = await createDynamicMenu(events)
+ res.send(result)
return
}
- // client has no config, check parent groups
- var parentIds = []
- var configIds = []
- for (var i = 0; i < client.groups.length; i++) {
- // gather parent ids
- parentIds.push(client.groups[i].id)
+ // No client event, use events of lowest parents
+ if (response.type === 'event') {
+ if (response.ids.length === 1) {
+ result = await createIpxeScript(response.ids[0])
+ res.send(result)
+ return
+ }
+ if (response.ids.length > 1) {
+ result = await createDynamicMenu(response.ids)
+ res.send(result)
+ return
+ }
+ }
+ // No events, use client config
+ if (client.configId !== null) {
+ result = await createIpxeScript(client.configId)
+ res.send(result)
+ return
}
- 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 client config, use configs of lowest parents
+ if (response.type === 'config') {
+ if (response.ids.length === 1) {
+ result = await createIpxeScript(response.ids[0])
+ res.send(result)
+ return
+ }
+ if (response.ids.length > 1) {
+ result = await createDynamicMenu(response.ids)
+ res.send(result)
+ return
+ }
}
- } else {
- // client not known, start registration
- res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe'))
}
- })
+ // No config found, use default config
+ res.sendFile(path.join(__appdir, 'ipxe', 'default.ipxe'))
+ } else {
+ // client not known, start registration
+ res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe'))
+ }
})
// load config by given id
@@ -77,32 +132,54 @@ noAuthRouter.get('/getconfig/:configId', (req, res) => {
})
})
-// recursive iteration through the layers of parents until a config is found or no parents left
-function checkGroupsForConfigs (groupIds) {
+async function fetchParentConfigs(groupIds, blacklist) {
+ if (groupIds.length === 0) return {'ids': [], 'type': ''}
+
+ var importantEvents = []
+ var events = []
+ var configs = []
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 || parentIds.length === 0) {
- return configIds
- } else {
- return checkGroupsForConfigs(parentIds).then(response => {
- return response
- })
+ var newBlacklist = blacklist.slice(0)
+
+ var groups = await db.group.findAll({ where: { id: groupIds }, include: ['parents', 'events'] })
+ for (let i = 0; i < groups.length; i++) {
+ configs.push(groups[i].configId)
+ // groups[i].map(g => g.parents.map(p => p.id))
+ for (let j = 0; j < groups[i].parents.length; j++) {
+ parentIds.push(groups[i].parents[j].id)
}
- })
+ for (let j = 0; j < groups[i].events.length; j++) {
+ if (groups[i].events[j].important) importantEvents.push(groups[i].events[j])
+ if (groups[i].events[j].group_x_event.blacklist) newBlacklist.push(groups[i].events[j].id)
+ else events.push(groups[i].events[j])
+ }
+ }
+
+ importantEvents = importantEvents.filter(e => !newBlacklist.includes(e.id))
+ importantEvents = importantEvents.map(e => e.config)
+ importantEvents = importantEvents.filter(c => c !== null)
+ importantEvents = importantEvents.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos })
+ if (importantEvents.length > 0) return {'ids': importantEvents, 'type': 'important'}
+
+ var response = await fetchParentConfigs(parentIds, newBlacklist)
+
+ if (response.type === 'important') return response
+
+ events = events.filter(e => !newBlacklist.includes(e.id))
+ events = events.map(e => e.config)
+ events = events.filter(c => c !== null)
+ events = events.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos })
+ if (events.length > 0) return {'ids': events, 'type': 'event'}
+
+ if (response.type === 'event') return response
+
+ configs = configs.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos })
+ configs = configs.filter(c => c !== null)
+ if (configs.length > 0) return {'ids': configs, 'type': 'config'}
+
+ if (response.type === 'config') return response
+
+ return {'ids': [], 'type': ''}
}
// create the config script from database
@@ -143,6 +220,7 @@ function createIpxeScript (id) {
}
// create dynamic menu to load the different given configs for a client
+// TODO: Hardcoded Chain Port?!
function createDynamicMenu (ids) {
return db.config.findAll({ where: { id: ids } }).then(configs => {
var script = ''