From c6141d8aba461e2ff7851087c5801fa2cd156265 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Sun, 5 May 2019 20:58:34 +0000 Subject: [ipxe builder] Merge efi and bios ipxe builder to one tab & add parameters ui [server] Remove all req, res from the shell.js library Add ipxe config for the build parameters & parameter list Add api getter/setter methods for the ipxe config [webapp] Add ui for repository, branch and build targets Output: All of the scroll to bottom is now clickable --- server/lib/shell.js | 140 +++++++++++++++++++++------------------------------- 1 file changed, 55 insertions(+), 85 deletions(-) (limited to 'server/lib/shell.js') diff --git a/server/lib/shell.js b/server/lib/shell.js index 863e06b..ed2f4cd 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -1,87 +1,68 @@ /* global __appdir */ var path = require('path') var shell = require('shelljs') -// var child = require('child_process') -// var ipxeGIT = 'git://git.ipxe.org/ipxe.git' -var ipxeGIT = 'http://git.ipxe.org/ipxe.git' var io = require(path.join(__appdir, 'lib', 'socketio')) const fs = require('fs') - // Only one building process per version at a time. -var building = { efi: false, bios: false } -var make = {} module.exports = { - buildIpxe: async function (req, res) { - const ipxeVersion = req.params.version - - // Different make commands for efi / bios are needed. + buildIpxe: async function (buildParameter = '', gitURL = 'http://git.ipxe.org/ipxe.git', gitBranch = '') { var makeCmd = '' - // Only one building process can be running. - if (!building[ipxeVersion]) { - makeCmd = 'make ' - if (ipxeVersion === 'efi') makeCmd += 'bin-x86_64-efi/snponly.efi ' - makeCmd += 'EMBED=' + path.join(__appdir, 'ipxe', 'embedded_' + ipxeVersion + '.ipxe') + ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem')// + ' bin/undionly.kpxe' - - updateInProgress(ipxeVersion, true) - } else { - res.send({ status: 'ALREADY_BUILDING', error: 'Building ' + ipxeVersion + '-iPXE is already in progress.' }) - return - } - // Sending feedback, that the ipxe building progress started. - res.status(200).send({ status: 'SUCCESS', msg: 'Starting iPXE building process' }) + // Only one building process can be running. (lock the ipxe directory) + // TODO: + // If file is already locked + // if (false) return { status: 'ALREADY_BUILDING', error: 'iPXE-building process is already in progress.' } + + makeCmd = 'make ' + buildParameter + makeCmd += ' EMBED=' + path.join(__appdir, 'ipxe', 'embedded.ipxe') + makeCmd += ' TRUST=' + path.join(__appdir, 'bin', 'fullchain.pem') // Cloning git. - sendToLog(ipxeVersion, 'Cloning git ...\n', 'primary') - await cloneIpxe(ipxeVersion) - if (!building[ipxeVersion]) return + sendToLog('Cloning git ...\n', 'primary') + await cloneIpxe(gitURL, gitBranch) // Copying configs. - sendToLog(ipxeVersion, 'Copying configs ...\n', 'primary') - copyConfigs(ipxeVersion) - if (!building[ipxeVersion]) return + sendToLog('Copying configs ...\n', 'primary') + copyConfigs() // Make ipxe. - sendToLog(ipxeVersion, 'Make iPXE ...\n', 'primary') - shell.cd(path.join(__appdir, 'ipxe', 'ipxe_' + ipxeVersion, 'src')) + sendToLog('Make iPXE ...\n', 'primary') + shell.cd(path.join(__appdir, 'ipxe', 'ipxeGIT', 'src')) await new Promise((resolve, reject) => { - if (!building[ipxeVersion]) return - make[ipxeVersion] = shell.exec(makeCmd, { async: true }, () => { - // make[ipxeVersion] = child.exec(makeCmd, { async: true }, () => { + const process = shell.exec(makeCmd, { async: true }, () => { resolve() }) // Send the output to the frontend log. - make[ipxeVersion].stdout.on('data', data => { + process.stdout.on('data', data => { const multiline = data.split('\n') if (multiline.length > 2) { multiline.forEach(line => { - if (line) sendToLog(ipxeVersion, line, 'normal') + if (line) sendToLog(line, 'normal') }) - } else sendToLog(ipxeVersion, data, 'normal') + } else sendToLog(data, 'normal') }) - make[ipxeVersion].stderr.on('data', data => { + process.stderr.on('data', data => { const multiline = data.split('\n') if (multiline.length > 2) { multiline.forEach(line => { - if (line) sendToLog(ipxeVersion, line, 'error') + if (line) sendToLog(line, 'error') }) - } else sendToLog(ipxeVersion, data, 'error') + } else sendToLog(data, 'error') }) }) - - if (!building[ipxeVersion]) return // Copy and rename the ipxe file. - sendToLog(ipxeVersion, 'Copying ipxe file ...\n', 'primary') - shell.cp('bin/undionly.kpxe', path.join(__appdir, 'ipxe')) - shell.mv(path.join(__appdir, 'ipxe', 'undionly.kpxe'), path.join(__appdir, 'ipxe', 'ipxe.' + ipxeVersion)) - sendToLog(ipxeVersion, 'done\n', 'success') - updateInProgress(ipxeVersion, false) + // sendToLog('Copying ipxe file ...\n', 'primary') + // shell.cp('bin/undionly.kpxe', path.join(__appdir, 'ipxe')) + // shell.mv(path.join(__appdir, 'ipxe', 'undionly.kpxe'), path.join(__appdir, 'ipxe', 'ipxe.' + ipxeVersion)) + // sendToLog(ipxeVersion, 'done\n', 'success') + // updateInProgress(ipxeVersion, false) }, - cancelBuilding: async function (req, res) { + cancelBuilding: async function () { + /* const ipxeVersion = req.params.version const process = make[ipxeVersion] if (process) { @@ -90,66 +71,55 @@ module.exports = { updateInProgress(ipxeVersion, false) } res.send({ status: 'SUCCESS', data: process }) + */ }, - clean: async function (req, res) { - const ipxeVersion = req.params.version - if (building[ipxeVersion]) { - res.send({ status: 'ALREADY_BUILDING', error: 'Can\'t clean, while building' + ipxeVersion + '-iPXE is in progress.' }) - return - } else { - updateInProgress(ipxeVersion, true) - res.send({ status: 'SUCCESS', msg: 'Cleaning iPXE started' }) - } + clean: async function () { + // If building return + // return { status: 'ALREADY_BUILDING', error: 'Can\'t clean, while iPXE-building is in progress.' } + // TODO more logging of what get's deleted 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')) - updateInProgress(ipxeVersion, false) - sendToLog(ipxeVersion, 'iPXE files successfully cleaned', 'success', false) + shell.rm('-rf', 'ipxeGIT') + shell.rm(path.join(__appdir, 'ipxe', 'ipxelog.txt')) + sendToLog('iPXE files successfully cleaned', 'success', false) + return { status: 'SUCCESS', msg: 'Cleaning iPXE started' } }, status: function (ipxeVersion) { - return building[ipxeVersion] + return false } } -async function cloneIpxe (ipxeVersion) { +async function cloneIpxe (url, branch) { // Check if git is installed on the server. - if (!shell.which('git')) { - return { status: 'GIT_MISSING', error: 'Please install git on the server.' } - } + if (!shell.which('git')) return { status: 'GIT_MISSING', error: '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 }, () => { + let command = 'git clone ' + url + if (branch) command += ' --branch ' + branch + ' --single-branch' + var clone = shell.exec(command + ' ipxeGIT', { async: true }, () => { resolve() }) clone.stdout.on('data', data => { - sendToLog(ipxeVersion, data, 'normal') + sendToLog(data, 'normal') }) clone.stderr.on('data', data => { - sendToLog(ipxeVersion, data, 'error') + sendToLog(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')) -} - -function updateInProgress (ipxeVersion, inProgress) { - building[ipxeVersion] = inProgress - const event = ipxeVersion + ' inProgress' - io.in('broadcast ipxeBuild').emit(event, inProgress) + shell.rm(path.join(__appdir, 'ipxe', 'ipxeGIT', 'src', 'config', 'general.h')) + shell.rm(path.join(__appdir, 'ipxe', 'ipxeGIT', 'src', 'config', 'console.h')) + shell.cp(path.join(__appdir, 'ipxe', 'general.h'), path.join(__appdir, 'ipxe', 'ipxeGIT', 'src', 'config', 'general.h')) + shell.cp(path.join(__appdir, 'ipxe', 'console.h'), path.join(__appdir, 'ipxe', 'ipxeGIT', 'src', 'config', 'console.h')) } -function sendToLog (ipxeVersion, msg, status, log = true) { +function sendToLog (msg, status, log = true) { const date = new Date() const pad = x => x < 10 ? '0' + x : x var dateString = '[' + date.getFullYear() + '-' @@ -161,13 +131,13 @@ function sendToLog (ipxeVersion, msg, status, log = true) { const logEntry = { date: dateString, status: status, msg: msg } - const event = ipxeVersion + ' log' + const event = 'log' io.in('broadcast ipxeBuild').emit(event, logEntry) - if (log) writeLog(ipxeVersion, logEntry) + if (log) writeLog(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) => { +function writeLog (logEntry) { + fs.writeFile(path.join(__appdir, 'ipxe', 'ipxelog.txt'), logEntry.date + '\t' + logEntry.status + '\t' + logEntry.msg, { flag: 'a+' }, (err) => { if (err) throw err }) } -- cgit v1.2.3-55-g7522