From ba5482abf1aa5c308f9f2d16748014335bc3444f Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 8 Apr 2019 05:17:38 +0200 Subject: [server/external-backends] Fix dhcp registration stuff --- server/api/registration.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'server/api/registration.js') diff --git a/server/api/registration.js b/server/api/registration.js index dc18bf1..5638747 100644 --- a/server/api/registration.js +++ b/server/api/registration.js @@ -139,18 +139,19 @@ noAuthRouter.postAsync('/clients', async (req, res) => { const network = client.networks[0] // Get the dhcp backend. Only one dhcp backend can exist else -> conflict. const dhcp = await backendHelper.getDhcp() + let ipSelection = false + let setIpError = undefined if (dhcp) { if (automatic) { // Set the name of the client if it's not set if (!client.name) client.name = client.type + '_' + client.uuid - const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.ip, network.mac, client.name, true) - + const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.ip, network.mac, undefined, true) // Check for errors. if (!setIp.error) { // Client ip set successfully client.networks[0].ip = setIp.ip } else { - log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}. Error: ${setIp.msg}` }) + log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}` }) } } else if (network.dhcp) { // If networks.dhcp is set the user already choose the ip and we have to set it now. @@ -159,16 +160,23 @@ noAuthRouter.postAsync('/clients', async (req, res) => { // Check for errors. if (setIp.error) { // Setting the client ip failed - log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}. Error: ${setIp.msg}` }) + ipSelection = true + delete client.networks[0].dhcp + delete client.name + setIpError = setIp.msg } else { // Client ip set successfully client.networks[0].ip = network.dhcp } } else { + ipSelection = true + } + + if (ipSelection) { // If not check if the client has a leased ipv4 address. const ipCheck = await dhcp.instance.checkIp(dhcp.backend.credentials, network.ip) // Build ipxe and return - if (ipxe && ipCheck && !ipCheck.error) return res.send(buildSelectIpIpxeMenu(client, ipCheck)) + if (ipxe && ipCheck && !ipCheck.error) return res.send(buildSelectIpIpxeMenu(client, ipCheck, setIpError)) else if (!ipxe && ipCheck && !ipCheck.error) return res.send({ client: client, ipList: ipCheck }) } } @@ -404,17 +412,20 @@ function buildIpxeMenu (id, name, groups, parents) { return script } -function buildSelectIpIpxeMenu (client, ipList) { +function buildSelectIpIpxeMenu (client, ipList, error = undefined) { const basUrl = 'https://' + url let script = '#!ipxe\r\n' + if (error) script += 'echo\r\necho ' + error + '\r\necho\r\nprompt Press any key to select a new ip address\r\n' + let menuscript = '' script += ':start\r\n' - script += 'menu Select the ip for this client:\r\n' + script += 'menu Select the ip for this client: \r\n' for (let index in ipList) { const ip = ipList[index] client.networks[0].dhcp = ip script += 'item ' + ip + ' ' + ip + '\r\n' menuscript += ':' + ip + '\r\n' + 'params\r\nparam client ' + JSON.stringify(client) + '\r\n' + menuscript += 'param ipxe true\r\n' menuscript += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n' } script += `choose target && goto \${target}\r\n\r\n` @@ -428,6 +439,7 @@ function buildNameClientIpxeMenu (client) { script += '\r\necho Enter client name\r\nread clientname\r\nparams\r\n' client.name = `\${clientname}` script += 'param 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