From cfa4dabd5e2aba66c6a8c0dfc3f43404fdbad3ec Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 17 Sep 2018 15:39:01 +0000 Subject: [external-backends/idoit] Rework getDataTree from promises to async / await --- server/api/ipxe.js | 2 +- server/ipxe/ipxe.0 | Bin 90057 -> 0 bytes server/ipxe/main.ipxe | 1 - .../external-backends/backends/idoit-backend.js | 64 +++++++++++---------- server/lib/external-backends/index.js | 2 +- server/lib/shell.js | 1 + 6 files changed, 38 insertions(+), 32 deletions(-) delete mode 100644 server/ipxe/ipxe.0 (limited to 'server') diff --git a/server/api/ipxe.js b/server/api/ipxe.js index 285a426..f50d9d3 100644 --- a/server/api/ipxe.js +++ b/server/api/ipxe.js @@ -19,7 +19,7 @@ module.exports.router = router noAuthRouter.get('/load/script', (req, res) => { res.setHeader('content-type', 'text/plain') - fs.readFile(path.join(__appdir, 'ipxe', 'exitTestScript.ipxe'), 'utf-8', function (err, content) { + fs.readFile(path.join(__appdir, 'ipxe', 'default.ipxe'), 'utf-8', function (err, content) { if (err) res.end() res.send(content) }) diff --git a/server/ipxe/ipxe.0 b/server/ipxe/ipxe.0 deleted file mode 100644 index aa09a89..0000000 Binary files a/server/ipxe/ipxe.0 and /dev/null differ diff --git a/server/ipxe/main.ipxe b/server/ipxe/main.ipxe index 58320d2..64b9f2e 100644 --- a/server/ipxe/main.ipxe +++ b/server/ipxe/main.ipxe @@ -1,5 +1,4 @@ #!ipxe ifopen -#set crosscert http://ca.ipxe.org/auto/ chain https://bas.stfu-kthx.net:8888/api/configloader/${uuid} \ No newline at end of file diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js index 7db2bf2..a48c740 100644 --- a/server/lib/external-backends/backends/idoit-backend.js +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -185,7 +185,7 @@ class IdoitBackend extends ExternalBackends { } // Function to use the same session for multiple requests - getDataTree (credentials, array) { + async getDataTree (credentials, array) { var c = this.mapCredentials(credentials) // LOGIN @@ -209,36 +209,42 @@ class IdoitBackend extends ExternalBackends { } var config = { - timeout: 90000, + timeout: 180000, headers: headers } + // Make a login request and see if we are authenticated. - return axios.post(c.url, body, config).then(log => { - var sid = log.data.result['session-id'] - // Headers - config.headers = { 'X-RPC-Auth-Session': sid, 'Content-Type': 'application/json' } - body.method = 'cmdb.location_tree' - var promises = [] - - array.forEach(element => { - const bod = { - 'version': '2.0', - 'method': 'cmdb.location_tree', - 'params': { - 'id': element.eid, - 'apikey': c.apikey, - 'language': 'en' - }, - 'id': 1 - } - promises.push(axios.post(c.url, bod, config).then(result => { - return { gid: element.gid, childs: result.data.result } - })) - }) - return Promise.all(promises).then(ret => { - return ret - }) - }) + var log = await axios.post(c.url, body, config) + log = log.data.result + var sid = log['session-id'] + + // Headers + config.headers = { 'X-RPC-Auth-Session': sid, 'Content-Type': 'application/json' } + body.method = 'cmdb.location_tree' + + // Go through the objects and get all the childs. + var promises = [] + var e + for (e in array) { + var element = array[e] + const bod = { + 'version': '2.0', + 'method': 'cmdb.location_tree', + 'params': { + 'id': element.eid, + 'apikey': c.apikey, + 'language': 'en' + }, + 'id': 1 + } + + var tmp = await axios.post(c.url, bod, config) + promises.push({ gid: element.gid, childs: tmp.data.result }) + + // Add a delay, so that idoit can handle it. + // await new Promise(resolve => { setTimeout(() => { resolve() }, 500) }) + } + return promises } // ############################################################################ @@ -291,7 +297,7 @@ class IdoitBackend extends ExternalBackends { } var config = { - timeout: 90000, + timeout: 180000, headers: headers } config.headers['Content-Type'] = 'application/json' diff --git a/server/lib/external-backends/index.js b/server/lib/external-backends/index.js index 40e462d..9f99153 100644 --- a/server/lib/external-backends/index.js +++ b/server/lib/external-backends/index.js @@ -90,7 +90,7 @@ class ExternalBackends { * * return: [{ gid: , childs: [{ id: , }, ...]}, ...] */ - getDataTree (credendtials, objects) { + async getDataTree (credendtials, objects) { return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getDataTree method' } } diff --git a/server/lib/shell.js b/server/lib/shell.js index 37de779..0d2767e 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -21,6 +21,7 @@ module.exports = { shell.cp(path.join(__appdir, 'ipxe', 'console.h'), path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config')) // var make = 'make EMBED=' + path.join(__appdir, 'ipxe', 'main.ipxe'); var make = 'make EMBED=' + path.join(__appdir, 'ipxe', 'main.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem') + ' bin/undionly.kpxe' + // var make = 'make EMBED=' + path.join(__appdir, 'ipxe', 'reboot.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem') + ' bin/ipxe.usb' // shell.env.PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin'; shell.env.DEBUG = '' shell.exec(make, function (code, stdout, stderr) { -- cgit v1.2.3-55-g7522