From 5fbd058f8ac7e13d1593dde996715cf534edec8b Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 31 Aug 2020 17:33:34 +0000 Subject: npm install needed [ipxe builder] Rework to link directorys instead of single files --- server/api/ipxe.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'server/api/ipxe.js') diff --git a/server/api/ipxe.js b/server/api/ipxe.js index b807c00..e232cda 100644 --- a/server/api/ipxe.js +++ b/server/api/ipxe.js @@ -7,13 +7,16 @@ const { decorateApp } = require('@awaitjs/express') var router = decorateApp(express.Router()) var noAuthRouter = express.Router() const log = require(path.join(__appdir, 'lib', 'log')) +const buildLinkPath = path.join(__appdir, 'ipxe', 'current_builds') +const buildsPath = path.join(__appdir, 'ipxe', 'builds') +const sanitize = require('sanitize-filename') // Permission check middleware router.all('/:x', async (req, res, next) => { switch (req.method) { case 'GET': switch (req.params.x) { - case 'script': case 'certificate': case 'general': case 'console': case 'log': case 'config': case 'status': + case 'script': case 'certificate': case 'general': case 'console': case 'log': case 'config': case 'status': case 'builds': if (!await req.user.hasPermission('ipxe.view')) return res.status(403).send({ error: 'Missing permission', permission: 'ipxe.view' }) break @@ -157,7 +160,7 @@ router.getAsync('/build', async (req, res) => { delete require.cache[require.resolve(path.join(__appdir, 'config', 'ipxe'))] const config = require(path.join(__appdir, 'config', 'ipxe')) - const build = await shell.buildIpxe(config.targets.build.join(' '), config.repository, config.branch) + const build = await shell.buildIpxe(config.targets.build, config.repository, config.branch) res.send(build) }) @@ -185,6 +188,66 @@ router.get('/status', (req, res) => { res.send({ status: 'SUCCESS', data: shell.status(req.params.version) }) }) +/* + * Return a json of all available builds in /ipxe/builds + * { 'YYYY-M-DD_h-m-s': { type: '', children: { ... }, selected: }} + */ +router.get('/builds', (req, res) => { + // Reads directory of the builded ipxe targets /ipxe/builds + let recursiveDirectory = shell.readdirRecursive(buildsPath) + let linkname = fs.readlinkSync(buildLinkPath).split('/').slice(-1)[0] + for (let buildDir of recursiveDirectory) { + buildDir.selected = (buildDir.name === linkname) + } + res.send(recursiveDirectory) +}) + +/* + * Recreates the symlinks to the selected target. + */ +router.post('/builds', (req, res) => { + let buildname = sanitize(req.body.build) + const buildDir = fs.readdirSync(buildsPath) + + if (buildDir.includes(buildname)) { + // Recreate the symlink + return res.send(shell.forceSymlink(path.join(buildsPath, buildname), buildLinkPath)) + } else return res.send({ status: 'ERROR', error: 'BUILD_NOT_FOUND' }) +}) + +/* + * Renames the directory of the builds + */ +router.post('/builds/:buildname', (req, res) => { + const buildname = sanitize(req.params.buildname) + const newname = sanitize(req.body.name) + + const buildDir = fs.readdirSync(buildsPath) + + // Check if the build exists and the destination don't + if (!buildDir.includes(buildname)) return res.send({ status: 'ERROR', error: 'BUILD_NOT_FOUND' }) + if (buildDir.includes(newname)) return res.send({ status: 'ERROR', error: 'BUILD_ALREADY_EXISTS' }) + + // Rename the build + fs.renameSync(path.join(buildsPath, buildname), path.join(buildsPath, newname)) + + // Check if the link has to be changed (if the current default build was renamed) + let linkname = fs.readlinkSync(buildLinkPath).split('/').slice(-1)[0] + if (linkname === buildname) shell.forceSymlink(path.join(buildsPath, newname), buildLinkPath) + + return res.send({ status: 'SUCCESS' }) +}) + +router.delete('/builds/:buildname', (req, res) => { + let buildname = sanitize(req.params.buildname) + + const buildDir = fs.readdirSync(buildsPath) + if (buildDir.includes(buildname)) { + shell.forceDeleteBuild(path.join(__appdir, 'ipxe', 'builds', buildname)) + return res.send() + } else return res.send({ status: 'ERROR', error: 'BUILDS_NOT_FOUND' }) +}) + module.exports.router = router noAuthRouter.get('/load/script', (req, res) => { -- cgit v1.2.3-55-g7522