From ffc5050fcdd0efed4f01d7f1f20465cadd9c9f5b Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 15 Jan 2019 03:39:31 +0000 Subject: [ipxe builder] Add ipxe builder module EFI and BIOS version can be build and configured Fancy log to see the stdout and stderr --- server/lib/shell.js | 144 +++++++++++++++++++++++++++++++++++++------------ server/lib/socketio.js | 1 + 2 files changed, 110 insertions(+), 35 deletions(-) (limited to 'server/lib') diff --git a/server/lib/shell.js b/server/lib/shell.js index 2b1ea0b..09a3ae2 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -1,47 +1,121 @@ +// Those packages needs to be installed to build ipxe: +// sudo apt install liblzma-dev +// sudo apt install mkisofs + /* global __appdir */ var path = require('path') var shell = require('shelljs') var ipxeGIT = 'git://git.ipxe.org/ipxe.git' +var io = require(path.join(__appdir, 'lib', 'socketio')) +const fs = require('fs') module.exports = { - buildIpxe: function (req, res) { - if (!shell.which('git')) { - return res.status(500).send({ status: 'GIT_MISSING', error_message: 'Please install git on the server.' }) - } + buildIpxe: async function (req, res) { + const ipxeVersion = req.params.version + + // Cloning git. + sendToLog(ipxeVersion, 'Cloning git ...\n', 'primary') + await cloneIpxe(ipxeVersion) - var gitclone = 'git clone ' + ipxeGIT - shell.cd(path.join(__appdir, 'ipxe')) - shell.exec(gitclone, function (code, stdout, stderr) { - shell.cd(path.join(__appdir, 'ipxe', 'ipxe', 'src')) - - // Remove the general config and paste in the own one - shell.rm(path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config', 'general.h')) - // shell.cp(path.join(__appdir, 'ipxe', 'general.h'), path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config')) - shell.cp(path.join(__appdir, 'ipxe', 'general_efi.h'), path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config', 'general.h')) - shell.rm(path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config', 'console.h')) - // shell.cp(path.join(__appdir, 'ipxe', 'console.h'), path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config')) - shell.cp(path.join(__appdir, 'ipxe', 'console_efi.h'), path.join(__appdir, 'ipxe', 'ipxe', 'src', 'config', 'console.h')) - // PCBIOS Variant - // var make = 'make EMBED=' + path.join(__appdir, 'ipxe', 'main.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' - - // EFI Variant - var make = 'make bin-x86_64-efi/snponly.efi EMBED=' + path.join(__appdir, 'ipxe', 'main.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' - - // USB - // var make = 'make EMBED=' + path.join(__appdir, 'ipxe', 'reboot.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem') + ' bin/ipxe.usb' - shell.env.DEBUG = '' - shell.exec(make, function (code, stdout, stderr) { - // shell.rm(path.join(__appdir, 'tftp', 'ipxe.0')) - shell.rm(path.join(__appdir, 'ipxe', 'ipxe.0')) - // shell.cp('bin/undionly.kpxe', path.join(__appdir, 'tftp')) - shell.cp('bin/undionly.kpxe', path.join(__appdir, 'ipxe')) - shell.mv(path.join(__appdir, 'ipxe', 'undionly.kpxe'), path.join(__appdir, 'ipxe', 'ipxe.0')) - // shell.rm('-rf', 'ipxe'); - return res.status(200).send({ status: 'success' }) + // Copying configs. + sendToLog(ipxeVersion, 'Copying configs ...\n', 'primary') + copyConfigs(ipxeVersion) + + // Make ipxe. + sendToLog(ipxeVersion, 'Make iPXE ...\n', 'primary') + shell.cd(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src')) + + // Different make command for efi / bios are needed. + var makeCmd = '' + if (ipxeVersion === 'efi') makeCmd = 'make bin-x86_64-efi/snponly.efi EMBED=' + path.join(__appdir, 'ipxe', 'embedded_' + ipxeVersion + '.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' + else if (ipxeVersion === 'bios') makeCmd = 'make EMBED=' + path.join(__appdir, 'ipxe', 'embedded_' + ipxeVersion + '.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' + await new Promise((resolve, reject) => { + var make = shell.exec(makeCmd, { async: true }, () => { + resolve() + }) + + // Send the output to the frontend log. + make.stdout.on('data', data => { + sendToLog(ipxeVersion, data, 'normal') + }) + make.stderr.on('data', data => { + sendToLog(ipxeVersion, data, 'error') }) }) + + // Copy and rename the ipxe file. + shell.cp('bin/undionly.kpxe', path.join(__appdir, 'ipxe')) + shell.mv(path.join(__appdir, 'ipxe', 'undionly.kpxe'), path.join(__appdir, 'ipxe', 'ipxe.' + ipxeVersion)) + }, + + clean: async function (ipxeVersion) { + shell.cd(path.join(__appdir, 'ipxe')) + shell.rm('-rf', 'ipxe_' + ipxeVersion) + shell.rm(path.join(__appdir, 'ipxe', 'ipxe.' + ipxeVersion)) + shell.rm(path.join(__appdir, 'ipxe', 'log_' + ipxeVersion + '.txt')) + sendToLog(ipxeVersion, 'iPXE files successfully cleaned', 'success', false) } } -// sudo apt-get install liblzma-dev -// sudo apt-get install mkisofs +async function cloneIpxe(ipxeVersion) { + // Check if git is installed on the server. + if (!shell.which('git')) { + return { status: 'GIT_MISSING', error_message: 'Please install git on the server.' } + } + + shell.cd(path.join(__appdir, 'ipxe')) + return new Promise((resolve, reject) => { + var clone = shell.exec('git clone ' + ipxeGIT + ' ipxe_' + ipxeVersion, { async: true }, () => { + resolve() + }) + clone.stdout.on('data', data => { + sendToLog(ipxeVersion, data, 'normal') + }) + clone.stderr.on('data', data => { + sendToLog(ipxeVersion, data, 'error') + }) + }) +} + +function copyConfigs(ipxeVersion) { + // Remove the default configs and paste in the customized ones. + shell.rm(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'general.h')) + shell.rm(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'console.h')) + shell.cp(path.join(__appdir, 'ipxe', 'general_' + ipxeVersion + '.h'), path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'general.h')) + shell.cp(path.join(__appdir, 'ipxe', 'console_' + ipxeVersion + '.h'), path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'console.h')) + return +} + +function sendToLog(ipxeVersion, msg, status, writeLog = true) { + var date = new Date(); + var dateString = '[' + date.getFullYear() + '-' + + var month = parseInt(date.getMonth()) + 1 + if (month < 10) dateString += '0' + month + '-' + else dateString += month + '-' + + if (parseInt(date.getDate()) < 10) dateString += '0' + date.getDate() + ' ' + else dateString += date.getDate() + ' ' + + if (parseInt(date.getHours()) < 10) dateString += '0' + date.getHours() + ':' + else dateString += date.getHours() + ':' + + if (parseInt(date.getMinutes()) < 10) dateString += '0' + date.getMinutes() + ':' + else dateString += date.getMinutes() + ':' + + if (parseInt(date.getSeconds()) < 10) dateString += '0' + date.getSeconds() + else dateString += date.getSeconds() + dateString += ']' + + var logEntry = { date: dateString, status: status, msg: msg } + + var socket = ipxeVersion + ' log' + io.in('broadcast ipxeBuild').emit(socket, logEntry) + if (writeLog) writeLog(ipxeVersion, logEntry) +} + +function writeLog(ipxeVersion, logEntry) { + fs.writeFile(path.join(__appdir, 'ipxe', 'log_' + ipxeVersion + '.txt'), logEntry.date + '\t' + logEntry.status + '\t' + logEntry.msg, { flag: 'a+' }, (err) => { + if (err) throw err + }) +} diff --git a/server/lib/socketio.js b/server/lib/socketio.js index 07c2f22..5cbedb7 100644 --- a/server/lib/socketio.js +++ b/server/lib/socketio.js @@ -27,6 +27,7 @@ io.on('connection', socket => { // Join broadcast rooms (TODO: check if user has permission to join those rooms) socket.join('broadcast newClient') + socket.join('broadcast ipxeBuild') // Notification broadcasts socket.on('notifications clearAll', () => socket.to(userId).emit('notifications clearAll')) -- cgit v1.2.3-55-g7522 From 64e9a8ec113805b97d24b3301e88eb2b68edee8a Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 15 Jan 2019 03:42:45 +0000 Subject: eslint fixes :| --- server/lib/shell.js | 35 ++++++++++++++++----------------- webapp/src/components/DashboardPage.vue | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) (limited to 'server/lib') diff --git a/server/lib/shell.js b/server/lib/shell.js index 09a3ae2..080e5d6 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -12,7 +12,7 @@ const fs = require('fs') module.exports = { buildIpxe: async function (req, res) { const ipxeVersion = req.params.version - + // Cloning git. sendToLog(ipxeVersion, 'Cloning git ...\n', 'primary') await cloneIpxe(ipxeVersion) @@ -24,7 +24,7 @@ module.exports = { // Make ipxe. sendToLog(ipxeVersion, 'Make iPXE ...\n', 'primary') shell.cd(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src')) - + // Different make command for efi / bios are needed. var makeCmd = '' if (ipxeVersion === 'efi') makeCmd = 'make bin-x86_64-efi/snponly.efi EMBED=' + path.join(__appdir, 'ipxe', 'embedded_' + ipxeVersion + '.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' @@ -33,7 +33,7 @@ module.exports = { var make = shell.exec(makeCmd, { async: true }, () => { resolve() }) - + // Send the output to the frontend log. make.stdout.on('data', data => { sendToLog(ipxeVersion, data, 'normal') @@ -57,12 +57,12 @@ module.exports = { } } -async function cloneIpxe(ipxeVersion) { +async function cloneIpxe (ipxeVersion) { // Check if git is installed on the server. if (!shell.which('git')) { return { status: 'GIT_MISSING', error_message: 'Please install git on the server.' } } - + shell.cd(path.join(__appdir, 'ipxe')) return new Promise((resolve, reject) => { var clone = shell.exec('git clone ' + ipxeGIT + ' ipxe_' + ipxeVersion, { async: true }, () => { @@ -77,44 +77,43 @@ async function cloneIpxe(ipxeVersion) { }) } -function copyConfigs(ipxeVersion) { +function copyConfigs (ipxeVersion) { // Remove the default configs and paste in the customized ones. shell.rm(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'general.h')) shell.rm(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'console.h')) shell.cp(path.join(__appdir, 'ipxe', 'general_' + ipxeVersion + '.h'), path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'general.h')) shell.cp(path.join(__appdir, 'ipxe', 'console_' + ipxeVersion + '.h'), path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src', 'config', 'console.h')) - return } -function sendToLog(ipxeVersion, msg, status, writeLog = true) { - var date = new Date(); +function sendToLog (ipxeVersion, msg, status, log = true) { + var date = new Date() var dateString = '[' + date.getFullYear() + '-' - + var month = parseInt(date.getMonth()) + 1 if (month < 10) dateString += '0' + month + '-' else dateString += month + '-' - + if (parseInt(date.getDate()) < 10) dateString += '0' + date.getDate() + ' ' else dateString += date.getDate() + ' ' - + if (parseInt(date.getHours()) < 10) dateString += '0' + date.getHours() + ':' else dateString += date.getHours() + ':' - + if (parseInt(date.getMinutes()) < 10) dateString += '0' + date.getMinutes() + ':' else dateString += date.getMinutes() + ':' - + if (parseInt(date.getSeconds()) < 10) dateString += '0' + date.getSeconds() else dateString += date.getSeconds() dateString += ']' - + var logEntry = { date: dateString, status: status, msg: msg } - + var socket = ipxeVersion + ' log' io.in('broadcast ipxeBuild').emit(socket, logEntry) - if (writeLog) writeLog(ipxeVersion, logEntry) + if (log) writeLog(ipxeVersion, logEntry) } -function writeLog(ipxeVersion, logEntry) { +function writeLog (ipxeVersion, logEntry) { fs.writeFile(path.join(__appdir, 'ipxe', 'log_' + ipxeVersion + '.txt'), logEntry.date + '\t' + logEntry.status + '\t' + logEntry.msg, { flag: 'a+' }, (err) => { if (err) throw err }) diff --git a/webapp/src/components/DashboardPage.vue b/webapp/src/components/DashboardPage.vue index f408ebb..9696466 100644 --- a/webapp/src/components/DashboardPage.vue +++ b/webapp/src/components/DashboardPage.vue @@ -59,7 +59,7 @@ - + -- cgit v1.2.3-55-g7522