From 71df171f1ef2195d066f9c42784dbc68dae721cc Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 16 Apr 2019 10:59:47 +0000 Subject: [server/registration] Add ipxe overview menu --- server/api/registration.js | 65 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 9 deletions(-) (limited to 'server') diff --git a/server/api/registration.js b/server/api/registration.js index 207413c..f588ea1 100644 --- a/server/api/registration.js +++ b/server/api/registration.js @@ -114,9 +114,11 @@ noAuthRouter.postAsync('/clients', async (req, res) => { if (typeof client === 'string') client = JSON.parse(client) let ipxe = req.body.ipxe - if (typeof ipxe === 'string') ipxe = true + if (typeof ipxe === 'string' && ipxe === 'true') ipxe = true let automatic = req.body.automatic if (typeof automatic === 'string' && automatic === 'true') automatic = true + let confirmation = req.body.confirmation + if (typeof confirmation === 'string' && confirmation === 'true') confirmation = true // If the client already exists return the configloader ipxe script. const clientDb = await db.client.findOne({ where: { uuid: client.uuid } }) @@ -158,6 +160,7 @@ noAuthRouter.postAsync('/clients', async (req, res) => { } else if (network.dhcp) { // If networks.dhcp is set the user already choose the ip and we have to set it now. if (!client.name) return res.send(buildNameClientIpxeMenu(client)) + if (confirmation) return res.send(buildOverviewIpxeMenu(client)) const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.dhcp, network.mac, client.name) dhcp.ref = setIp.ref // Check for errors. @@ -217,10 +220,12 @@ noAuthRouter.postAsync('/clients', async (req, res) => { noAuthRouter.postAsync('/clients/:uuid', async (req, res) => { let client = req.body.client - // Add the name to the ram modules. - for (let ram of client.ram.modules) { - ram.name = ram.formfactor - if (client.ram.isEcc === 'Single-bit ECC') ram.name += '-ECC' + if (client && client.ram && client.ram.modules) { + // Add the name to the ram modules. + for (let ram of client.ram.modules) { + ram.name = ram.formfactor + if (client.ram.isEcc === 'Single-bit ECC') ram.name += '-ECC' + } } const clientDb = await db.client.findOne({ where: { uuid: client.uuid } }) @@ -228,10 +233,13 @@ noAuthRouter.postAsync('/clients/:uuid', async (req, res) => { if (client.name) clientDb.update({ name: client.name }) client.id = clientDb.id - // System data. Sometime just string with whitespaces only. - if (!/\S/.test(client.system.manufacturer)) client.system.manufacturer = 'unavailable' - if (!/\S/.test(client.system.model)) client.system.model = 'unavailable' - if (!/\S/.test(client.system.serialnumber)) client.system.serialnumber = 'unavailable' + if (client && client.system) { + // System data. Sometime just string with whitespaces only. + if (!/\S/.test(client.system.manufacturer)) client.system.manufacturer = 'unavailable' + if (!/\S/.test(client.system.model)) client.system.model = 'unavailable' + if (!/\S/.test(client.system.serialnumber)) client.system.serialnumber = 'unavailable' + } + const result = await backendHelper.updateClient(client) res.send(result) }) @@ -453,6 +461,45 @@ function buildNameClientIpxeMenu (client) { client.name = `\${clientname}` script += 'param client ' + JSON.stringify(client) + '\r\n' script += 'param ipxe true\r\n' + // Trigger the overview ipxe menu + script += 'param confirmation true\r\n' + script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n' + return script +} + +function buildOverviewIpxeMenu (client) { + const basUrl = 'https://' + url + const c = JSON.stringify(client) + let script = '#!ipxe\r\n' + script += ':start' + script += 'menu Overview Register Client\r\n' + script += 'item --gap Name: ' + client.name + '\r\n' + script += 'item --gap UUID: ' + client.uuid + '\r\n' + + for (let index in client.networks) { + const network = client.networks[index] + script += 'item --gap\r\n' + script += 'item --gap IP: ' + network.ip + '\r\n' + script += 'item --gap MAC: ' + network.mac + '\r\n' + if (network.dhcp) delete network.dhcp + } + script += 'item --gap\r\n' + script += 'item default\r\n' + script += 'item --key y confirm Confirm [y]es\r\n' + script += 'item --key n cancel Cancel [n]o\r\n' + script += `choose --default default target && goto \${target}\r\n\r\n` + + script += ':default\r\n' + script += 'goto start\r\n\r\n' + + script += ':confirm\r\n' + script += 'params\r\nparam client ' + c + '\r\n' + script += 'param ipxe true\r\n' + script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n' + + script += ':cancel\r\n' + script += 'params\r\nparam client ' + JSON.stringify(client) + '\r\n' + script += 'param ipxe true\r\n' script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n' return script } -- cgit v1.2.3-55-g7522