summaryrefslogtreecommitdiffstats
path: root/server/api
diff options
context:
space:
mode:
authorJannik Schönartz2020-08-31 19:33:34 +0200
committerJannik Schönartz2020-08-31 19:33:34 +0200
commit5fbd058f8ac7e13d1593dde996715cf534edec8b (patch)
tree2c80b3e025d8855adfc0fe233c447e211066caca /server/api
parent[server/embedded-script] Move the bas wallpaper to a later point in the process (diff)
downloadbas-5fbd058f8ac7e13d1593dde996715cf534edec8b.tar.gz
bas-5fbd058f8ac7e13d1593dde996715cf534edec8b.tar.xz
bas-5fbd058f8ac7e13d1593dde996715cf534edec8b.zip
npm install needed [ipxe builder] Rework to link directorys instead of single files
Diffstat (limited to 'server/api')
-rw-r--r--server/api/ipxe.js67
1 files changed, 65 insertions, 2 deletions
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: '<directory/file>', children: { ... }, selected: <true/false> }}
+ */
+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) => {