From ab83a69b71edb684e13ac4eb8db5dbaf8f20aac9 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Thu, 31 Jan 2019 04:39:40 +0000 Subject: [ipxe builder] Disable building/cleaning if a build process is already running --- server/lib/shell.js | 107 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 34 deletions(-) (limited to 'server/lib/shell.js') diff --git a/server/lib/shell.js b/server/lib/shell.js index 080e5d6..8d609e4 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -1,18 +1,33 @@ -// 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 child = require('child_process') var ipxeGIT = 'git://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. + var makeCmd = '' + // Only one building process can be running. + if (!building[ipxeVersion]) { + 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' + 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' }) + // Cloning git. sendToLog(ipxeVersion, 'Cloning git ...\n', 'primary') await cloneIpxe(ipxeVersion) @@ -25,20 +40,18 @@ module.exports = { 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 }, () => { + make[ipxeVersion] = shell.exec(makeCmd, { async: true }, () => { + // make[ipxeVersion] = child.exec(makeCmd, { async: true }, () => { + updateInProgress(ipxeVersion, false) resolve() }) // Send the output to the frontend log. - make.stdout.on('data', data => { + make[ipxeVersion].stdout.on('data', data => { sendToLog(ipxeVersion, data, 'normal') }) - make.stderr.on('data', data => { + make[ipxeVersion].stderr.on('data', data => { sendToLog(ipxeVersion, data, 'error') }) }) @@ -48,19 +61,50 @@ module.exports = { shell.mv(path.join(__appdir, 'ipxe', 'undionly.kpxe'), path.join(__appdir, 'ipxe', 'ipxe.' + ipxeVersion)) }, - clean: async function (ipxeVersion) { + stopBuilding: async function (req, res) { + // TODO: KILLING IS NOT WORKING.. fml + const process = make[req.params.version] + console.log(process) + if (process) { + console.log('KILLING IT °_°') + // process.stdin.pause() + // shell.kill(process.pid, 'SIGINT') + // const kill = 'kill -2 ' + process.pid + // console.log(kill) + // shell.exec(kill) + // process.kill("SIGINT") + // child.exec("kill -2 " + process.pid) + } + 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' }) + } + 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) + }, + + status: function (ipxeVersion) { + return building[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.' } + return { status: 'GIT_MISSING', error: 'Please install git on the server.' } } shell.cd(path.join(__appdir, 'ipxe')) @@ -85,31 +129,26 @@ function copyConfigs (ipxeVersion) { 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) +} + function sendToLog (ipxeVersion, msg, status, log = true) { - var date = new Date() + const date = new Date() + const pad = x => x < 10 ? '0' + x : x var dateString = '[' + date.getFullYear() + '-' + dateString += pad(date.getMonth()) + '-' + dateString += pad(date.getDate()) + ' ' + dateString += pad(date.getHours()) + ':' + dateString += pad(date.getMinutes()) + ':' + dateString += pad(date.getSeconds()) + ']' - 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 } + const logEntry = { date: dateString, status: status, msg: msg } - var socket = ipxeVersion + ' log' - io.in('broadcast ipxeBuild').emit(socket, logEntry) + const event = ipxeVersion + ' log' + io.in('broadcast ipxeBuild').emit(event, logEntry) if (log) writeLog(ipxeVersion, logEntry) } -- cgit v1.2.3-55-g7522