From ce9a81e6361504ea68286cd9fda72391d0aaba12 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 30 Jul 2018 03:06:35 +0000 Subject: [server] Changed old modules to the new rounter restructure. --- server/api/backends.js | 48 ++++++++++++++++++++++++++++++---------------- server/api/ipxe-loader.js | 42 ---------------------------------------- server/api/ipxe.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ server/lib/shell.js | 2 +- server/router.js | 24 +---------------------- 5 files changed, 82 insertions(+), 83 deletions(-) delete mode 100644 server/api/ipxe-loader.js create mode 100644 server/api/ipxe.js diff --git a/server/api/backends.js b/server/api/backends.js index 3b9551e..e4d3ff6 100644 --- a/server/api/backends.js +++ b/server/api/backends.js @@ -3,7 +3,8 @@ const path = require('path') const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends', 'external-backends.js')) var db = require(path.join(__appdir, 'lib', 'sequelize')) -module.exports = { +// GET requests +module.exports.get = { getCredentialsByType: function (req, res) { const backendType = req.query.type const b = new ExternalBackends() @@ -14,7 +15,7 @@ module.exports = { res.status(200).send(instance.getCredentials()) }, - getBackendInfoById: function (req, res) { + getInfoById: function (req, res) { const backendId = req.query.id db.backend.findOne({ where: { id: backendId } }).then(backend => { const b = { @@ -27,7 +28,7 @@ module.exports = { }) }, - getBackendTypes: function (req, res) { + getTypes: function (req, res) { const backends = new ExternalBackends() var files = backends.getBackends() @@ -39,7 +40,7 @@ module.exports = { res.status(200).send(files) }, - getBackendList: function (req, res) { + getList: function (req, res) { db.backend.findAll({ attributes: ['id', 'name', 'type'] }).then(function (backends) { @@ -62,10 +63,12 @@ module.exports = { res.status(200).send({ success: connection.success, msg: connection.msg }) }) }) - }, + } +} - // POST REQUESTS - saveBackend: function (req, res) { +// POST requests +module.exports.post = { + save: function (req, res) { // Save credentials in the db. const formData = req.body const credentialString = JSON.stringify(formData.backendCredentials) @@ -82,7 +85,7 @@ module.exports = { res.status(200).send('success') }, - deleteBackends: function (req, res) { + delete: function (req, res) { const backendIds = req.body.id db.backend.destroy({ where: { id: backendIds } }).then(function () { @@ -90,15 +93,26 @@ module.exports = { }) }, + // If id is set the backend connection in the db is testest. + // Else backendinfo has to be set manually, to test unsaved connections. checkConnection: function (req, res) { - const type = req.body.backendType - // const credentials = req.body.backendCredentials + const id = req.body.id - const b = new ExternalBackends() - const instance = b.getInstance(type) - instance.checkConnection().then(connection => { - res.status(200).send({ success: connection.success, msg: connection.msg }) - }) - // TODO: Set credentials + var getBackend = null + if (id) getBackend = db.backend.findOne({ where: { id: id } }) + else getBackend = new Promise(resolve => resolve(req.body)) + + getBackend.then(backend => { + var bCredentials = { + type: backend.type, + credentials: backend.credentials + } + // Creating the backend instance and calling the specific checkConnection. + const b = new ExternalBackends() + const instance = b.getInstance(bCredentials.type) + instance.checkConnection(bCredentials).then(connection => { + res.status(200).send({ success: connection.success, msg: connection.msg }) + }) + }) } -} +} \ No newline at end of file diff --git a/server/api/ipxe-loader.js b/server/api/ipxe-loader.js deleted file mode 100644 index c484578..0000000 --- a/server/api/ipxe-loader.js +++ /dev/null @@ -1,42 +0,0 @@ -module.exports = { - loadScript: function (req, res) { - res.setHeader('content-type', 'text/plain') - res.status(200).send(`#!ipxe -dhcp - -:start -menu Please choose a webserver to load the ipxe menu: -item pxelnx PxeLinux -item exit Exit -item exit0 Exit0 -item exit1 Exit1 -item sh [Shell] -choose target && goto \${target} - -:exit -exit -:exit0 -exit 0 -:exit1 -exit 1 -:pxelnx -# set 210:string https://bas.stfu-kthx.net:8888/ -# chain \${210:string}pxelinux.0 || goto start -# chain https://bas.stfu-kthx.net:8888/pxelinux.0 -# set next-server bas-stfu-kthx.net:8888 -# set 209:string https://bas.stfu-kthx.net:8888/pxelinux.cfg -# imgload pxelinux.0 -set net0/next-server 192.52.3.91 || -set netX/next-server 192.52.3.91 || -set next-server 192.52.3.91 || - -# set 209:string pxelinux.cfg/default -# set 210:string bas.stfu-kthx.net -shell || -boot tftp://bas.stfu-kthx.net/pxelinux.0 || goto start - -:sh -shell -goto start`) - } -} diff --git a/server/api/ipxe.js b/server/api/ipxe.js new file mode 100644 index 0000000..79c0ad8 --- /dev/null +++ b/server/api/ipxe.js @@ -0,0 +1,49 @@ +/* global __appdir */ +var path = require('path') +var shell = require(path.join(__appdir, 'lib', 'shell')) + +module.exports.get = { + build: function(req, res) { + shell.buildIpxe(req, res) + }, + loadScript: function (req, res) { + res.setHeader('content-type', 'text/plain') + res.status(200).send(`#!ipxe +dhcp + +:start +menu Please choose a webserver to load the ipxe menu: +item pxelnx PxeLinux +item exit Exit +item exit0 Exit0 +item exit1 Exit1 +item sh [Shell] +choose target && goto \${target} + +:exit +exit +:exit0 +exit 0 +:exit1 +exit 1 +:pxelnx +# set 210:string https://bas.stfu-kthx.net:8888/ +# chain \${210:string}pxelinux.0 || goto start +# chain https://bas.stfu-kthx.net:8888/pxelinux.0 +# set next-server bas-stfu-kthx.net:8888 +# set 209:string https://bas.stfu-kthx.net:8888/pxelinux.cfg +# imgload pxelinux.0 +set net0/next-server 192.52.3.91 || +set netX/next-server 192.52.3.91 || +set next-server 192.52.3.91 || + +# set 209:string pxelinux.cfg/default +# set 210:string bas.stfu-kthx.net +shell || +boot tftp://bas.stfu-kthx.net/pxelinux.0 || goto start + +:sh +shell +goto start`) + } +} diff --git a/server/lib/shell.js b/server/lib/shell.js index 311af1d..e0782f1 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -4,7 +4,7 @@ var shell = require('shelljs') var ipxeGIT = 'git://git.ipxe.org/ipxe.git' module.exports = { - buildIPXE: function (req, res) { + buildIpxe: function (req, res) { if (!shell.which('git')) { return res.status(500).send({ status: 'GIT_MISSING', error_message: 'Please install git on the server.' }) } diff --git a/server/router.js b/server/router.js index 828f8b7..7d5613a 100644 --- a/server/router.js +++ b/server/router.js @@ -10,7 +10,7 @@ router.post('/signup', auth.signup) router.post('/logout', auth.logout) router.post('/changepassword', auth.changePassword) -// ############ Legacy Code: TODO(Jannik): Rework to api and get/post or delete! ############ +// ############ Legacy Code: TODO(Chris): Rework to api and get/post or delete! ############ // User API var user = require(path.join(__dirname, 'api', 'user')) router.get('/user/info', auth.verifyToken, user.info) @@ -20,28 +20,6 @@ var permissions = require(path.join(__dirname, 'api', 'permissions')) router.get('/getRolesByUserid', permissions.getRolesByUserid) router.post('/getRoleById', auth.verifyToken, permissions.getRoleById) -// Shell API -var shell = require(path.join(__dirname, 'lib', 'shell')) -router.get('/shell/buildipxe', shell.buildIPXE) - -// Nodemailer API -var nodemailer = require(path.join(__dirname, 'lib', 'nodemailer')) -router.get('/mail/send', nodemailer.sendMail) - -// External backends API -var backends = require(path.join(__dirname, 'api', 'backends')) -router.get('/backends/getCredentialsByType', auth.verifyToken, backends.getCredentialsByType) -router.get('/backends/getBackendInfoById', auth.verifyToken, backends.getBackendInfoById) -router.get('/backends/getBackendList', auth.verifyToken, backends.getBackendList) -router.get('/backends/getBackendTypes', backends.getBackendTypes) -router.get('/backends/checkConnection', auth.verifyToken, backends.checkConnectionById) -router.post('/backends/saveBackend', auth.verifyToken, backends.saveBackend) -router.post('/backends/deleteBackends', auth.verifyToken, backends.deleteBackends) -router.post('/backends/checkConnection', auth.verifyToken, backends.checkConnection) - -// Load ipxe scipts API -var ipxeloader = require(path.join(__dirname, 'api', 'ipxe-loader')) -router.get('/ipxe-loader/load-script', ipxeloader.loadScript) // ############################################################################ // Dynamic API routes -- cgit v1.2.3-55-g7522