summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-18 22:34:42 +0100
committerJannik Schönartz2019-03-18 22:34:42 +0100
commit5c560ee32d356eebf44b976e876081b161b86ecb (patch)
tree17b842c52b6c04145b2f0f54a6385a18cbb7ee4a
parent[server/registration] Add server registration (diff)
downloadbas-5c560ee32d356eebf44b976e876081b161b86ecb.tar.gz
bas-5c560ee32d356eebf44b976e876081b161b86ecb.tar.xz
bas-5c560ee32d356eebf44b976e876081b161b86ecb.zip
[server/registration] Add client with json instead of parameters (ipxe)
-rw-r--r--server/api/registration.js9
-rw-r--r--server/ipxe/registration.ipxe8
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js30
-rw-r--r--server/lib/iphelper.js6
4 files changed, 36 insertions, 17 deletions
diff --git a/server/api/registration.js b/server/api/registration.js
index 4026615..11de700 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -110,9 +110,12 @@ noAuthRouter.post('/group', (req, res) => {
* Reworked add method for adding a client or server.
*/
noAuthRouter.postAsync('/', async (req, res) => {
- console.log(req.body)
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
+
if (!client.type) client.type = 'CLIENT'
if (!client.title) client.title = client.type + '_' + client.uuid
@@ -176,7 +179,7 @@ noAuthRouter.postAsync('/add', async (req, res) => {
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 } }
+ 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
@@ -319,7 +322,7 @@ noAuthRouter.post('/:uuid/success', (req, res) => {
db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => {
getNextHookScript(groupids, hook.sortvalue).then(resID => {
// Update the client's registration state
- client.updateAttributes({
+ client.update({
registrationState: resID
})
res.send({ status: 'SUCCESS' })
diff --git a/server/ipxe/registration.ipxe b/server/ipxe/registration.ipxe
index 537cb58..40d6a2a 100644
--- a/server/ipxe/registration.ipxe
+++ b/server/ipxe/registration.ipxe
@@ -26,11 +26,9 @@ goto start
:automatic
params
-param mac ${net0/mac}
-param uuid ${uuid}
-param ip ${net0/ip}
-param purpose Pool PC
-chain https://bas.intra.uni-freiburg.de/api/registration/add##params
+param ipxe true
+param client { "type": "CLIENT", "uuid": "${uuid}", "purpose": "Pool PC", "network": { "ip": "${net0/ip}", "mac": "${net0/mac}" } }
+chain https://bas.intra.uni-freiburg.de/api/registration##params
:key
chain -ar http://132.230.4.6/tbk/ReplaceKeysWithOwnKeys.efi
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index 74a59fe..8c1a4c3 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -168,7 +168,6 @@ class IdoitBackend extends ExternalBackends {
const c = this.mapCredentials(credentials)
const headers = await this.getHeaders(c)
if (headers.error) return headers
-
let params = {
'apikey': c.apikey,
'title': client.title,
@@ -176,14 +175,15 @@ class IdoitBackend extends ExternalBackends {
'categories': {}
}
- if (client.type === 'CLIENT') params['type'] = 10
- else if (client.type === 'SERVER') {
+ if (client.type === 'CLIENT') {
+ params['type'] = 10
+ if (client.parentId) params.categories.C__CATG__LOCATION = { 'data': { 'parent': client.parentId } }
+ } else if (client.type === 'SERVER') {
params['type'] = 5
if (client.location) params.categories.C__CATG__LOCATION = { 'data': { 'parent': client.parentId, 'option': client.location.assembly, 'insertion': client.location.insertion, 'pos': client.location.pos } }
if (client.formfactor) params.categories.C__CATG__FORMFACTOR = { 'data': { 'formfactor': client.formfactor, 'rackunits': client.formfactor.rackunits } }
- // TODO: Assign to rack with the given rackid
- } else if (client.parentId) params.categories.C__CATG__LOCATION = { 'data': { 'parent': client.parentId } }
+ }
// Add categories to the object
if (client.uuid) params.categories.C__CATG__MODEL = { 'data': { 'productid': client.uuid } }
@@ -192,7 +192,7 @@ class IdoitBackend extends ExternalBackends {
// First read if there are current entries.
// Delete the previous entries.
// Finally create the new entry.
- if (client.network.mac) params.categories.C__CATG__NETWORK_PORT = { 'data': { 'category_id': 1, 'mac': client.network.mac } }
+
if (client.network.ip) params.categories.C__CATG__IP = { 'data': { 'category_id': 1, 'ipv4_address': client.network.ip } }
}
@@ -203,6 +203,24 @@ class IdoitBackend extends ExternalBackends {
// Error handling
if (requestCreate.error) return { error: requestCreate.errno, message: 'Connection was refused.' }
else if (requestCreate[0].error) return { error: 'IDOIT_ERROR', message: requestCreate[0].error.message }
+
+ // Add mac address: Network port is a subcategory of network so it need an extra request.
+ if (client.network && client.network.mac) {
+ params.categories.C__CATG__NETWORK_PORT = { 'data': { 'category_id': 1, 'mac': client.network.mac } }
+ const paramsMac = {
+ 'object': requestCreate[0].result.id,
+ 'objID': requestCreate[0].result.id,
+ 'category': 'C__CATG__NETWORK_PORT',
+ 'data': {
+ 'category_id': 1,
+ 'mac': client.network.mac
+ },
+ 'apikey': c.apikey
+ }
+ const bodyMac = this.getBody('cmdb.category.save', paramsMac, 'add_mac_address')
+ const requestMac = await this.axiosRequest(c.url, [bodyMac], headers)
+ }
+
// Purpose for Clients:
// 1 = Production | 5 = PVS
// 2 = Test | 7 = Pool PC
diff --git a/server/lib/iphelper.js b/server/lib/iphelper.js
index 5c40313..769dcc3 100644
--- a/server/lib/iphelper.js
+++ b/server/lib/iphelper.js
@@ -1,11 +1,11 @@
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-module.exports = { toDecimal, toString, toIPv4, getGroups }
+module.exports = { toDecimal, toIPv4, getGroups, isIPv4 }
// Finds the groups where the ip fits best in the subnets.
async function getGroups (ipString) {
- const ipInt = toDecimal(ipString)
+ const ipInt = toDecimal(ipString)
let fittingIpRanges = await db.iprange.findAll({ where: { startIp: { [db.Op.lte]: ipInt }, endIp: { [db.Op.gte]: ipInt } } })
fittingIpRanges = fittingIpRanges.map(x => x.groupId)
@@ -42,7 +42,7 @@ function eliminateParents (groupId, groupMap, eliminateArray, alreadyChecked = [
// Takes an ip address and converts it to an integer.
function toDecimal (ipString) {
- if (isIPv4(ipString)) return false
+ if (!isIPv4(ipString)) return false
const ipArray = ipString.split('.')
if (ipArray.length !== 4) return false