summaryrefslogtreecommitdiffstats
path: root/server/api/registrations.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-28 19:35:51 +0200
committerJannik Schönartz2018-08-28 19:35:51 +0200
commit17759278184463f84e2dfdbaa93882d0d3dd5423 (patch)
treeb79822af67ac41edeea1e9e7cea0c8dc8353cfbd /server/api/registrations.js
parent[configloader] made the code actually work (diff)
downloadbas-17759278184463f84e2dfdbaa93882d0d3dd5423.tar.gz
bas-17759278184463f84e2dfdbaa93882d0d3dd5423.tar.xz
bas-17759278184463f84e2dfdbaa93882d0d3dd5423.zip
[registration/dhcp] Implement dhcp as external-backend
Registration -> Registrations to match API guidelines. Fixed shell script to only build undionly.kpxe. Added infoblock external-backend. Removed dhcp from config.json Fixed ipxe scripts to match the registrations.
Diffstat (limited to 'server/api/registrations.js')
-rw-r--r--server/api/registrations.js125
1 files changed, 125 insertions, 0 deletions
diff --git a/server/api/registrations.js b/server/api/registrations.js
new file mode 100644
index 0000000..7537124
--- /dev/null
+++ b/server/api/registrations.js
@@ -0,0 +1,125 @@
+/* global __appdir */
+var path = require('path')
+var express = require('express')
+var router = express.Router()
+var noAuthRouter = express.Router()
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
+
+// GET requests.
+
+/*
+ * @return:
+ */
+router.get('/', (req, res) => {
+ db.backend.findOne({ where: { type: 'infoblox' } }).then(result => {
+ const b = new ExternalBackends()
+ const instance = b.getInstance(result.type)
+ instance.test(result.credentials).then(result => {
+ res.status(200).send(result)
+ })
+ })
+})
+
+module.exports.router = router
+
+// GET requests.
+
+// POST requests.
+
+/*
+ *
+ * @return:
+ */
+noAuthRouter.post('/client', (req, res) => {
+ // const mac = req.body.mac
+ // const uuid = req.body.uuid
+ // const ip = req.body.ip
+ // const name = req.body.name
+ // console.log(name.concat(' ', ip, ' ', mac, ' ', uuid))
+
+ var script = '#!ipxe\r\n'
+ script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
+ db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
+ groups.forEach(g => {
+ script = script.concat('echo', ' [', g.id, '] ', g.name, '\r\n')
+ })
+
+ script = script.concat('read group\r\n')
+ script = script.concat('params\r\n')
+ script = script.concat(`param id \${group}\r\n`)
+ script = script.concat('chain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n')
+ res.status(200).send(script)
+ })
+})
+
+noAuthRouter.post('/group', (req, res) => {
+ const id = req.body.id
+ console.log(id)
+ if (id === '0') {
+ db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
+ if (groups) {
+ res.send(buildIpxeMenu(id, 'Root', groups))
+ } else {
+ res.status(404).end()
+ }
+ })
+ } else {
+ db.group.findOne({ where: { id: id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
+ if (group) {
+ res.send(buildIpxeMenu(id, group.name, group.subgroups))
+ } else {
+ res.status(404).end()
+ }
+ })
+ }
+})
+
+noAuthRouter.post('/add', (req, res) => {
+ const mac = req.body.mac
+ const uuid = req.body.uuid
+ const ip = req.body.ip
+ const name = req.body.name
+ const parentId = req.body.id
+ 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')
+ })
+ }
+ })
+})
+
+module.exports.noAuthRouter = noAuthRouter
+
+function buildIpxeMenu (id, name, groups) {
+ var script = '#!ipxe\r\n'
+ script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
+ var menuscript = ''
+ script = script.concat(':start\r\n')
+ script = script.concat('menu Choose the group you want the client to be saved in\r\n')
+ var counter = '1'
+ groups.forEach(group => {
+ // script = script.concat('echo', ' [', subgroup.id, '] ', subgroup.name, '\r\n')
+ script = script.concat('item --key ', counter, ' ', counter, ' [', group.id, '] ', group.name, '\r\n')
+ menuscript = menuscript.concat(':', counter, '\r\n', 'params\r\nparam id ', group.id, '\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
+ counter++
+ })
+ script = script.concat('item select Add client to ', name, '\r\n')
+ menuscript = menuscript.concat(`:select\r\necho Enter client name\r\nread clientname\r\nparams\r\nparam name \${clientname}\r\n`)
+ menuscript = menuscript.concat('param id ', id, `\r\nparam mac \${net0/mac}\r\nparam uuid \${uuid}\r\nparam ip \${net0/ip}\r\n`)
+ menuscript = menuscript.concat('chain https://bas.stfu-kthx.net:8888/api/registrations/add##params\r\n\r\n')
+ if (id !== '0') {
+ script = script.concat('item reset Go to start\r\n')
+ menuscript = menuscript.concat(':reset\r\nparams\r\nparam id ', 0, '\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
+ }
+ script = script.concat('item exit Exit manual registration\r\n')
+ menuscript = menuscript.concat(':exit\r\nexit 1\r\n\r\n')
+ script = script.concat(`choose target && goto \${target}\r\n\r\n`)
+ script = script.concat(menuscript)
+ return script
+}