summaryrefslogtreecommitdiffstats
path: root/server/api/ipxe.js
blob: 7b5ad852accdf1be8adf3314eb3083103b81478e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* global __appdir */
var path = require('path')
const fs = require('fs')
var shell = require(path.join(__appdir, 'lib', 'shell'))
var express = require('express')
var router = express.Router()
var noAuthRouter = express.Router()

// GET requests.
router.get('/script', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'embedded.ipxe'))
})

router.get('/certificate', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'bin', 'fullchain.pem'))
})

router.get('/general', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'general.h'))
})

router.get('/console', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'console.h'))
})

router.get('/log', async (req, res) => {
  const max = req.query.max ? req.query.max : -1
  res.setHeader('content-type', 'text/plain')
  const filepath = path.join(__appdir, 'ipxe', 'ipxelog.txt')

  fs.readFile(filepath, 'utf-8', function (err, content) {
    if (err) res.end()
    if (max !== -1 && content) {
      let c = content.split('\n')
      c = c.splice(-max)
      res.send(c.join('\n'))
    } else res.send(content)
  })
})

router.get('/config', (req, res) => {
  delete require.cache[require.resolve(path.join(__appdir, 'config', 'ipxe'))]
  var config = require(path.join(__appdir, 'config', 'ipxe'))
  res.send(config)
})

router.post('/config', (req, res) => {
  let buildTargets = req.body.buildTargets
  const targetList = req.body.targetList
  const branch = req.body.branch
  const repository = req.body.repository
  var config = require(path.join(__appdir, 'config', 'ipxe'))

  if (repository) config.repository = repository
  if (branch) config.branch = branch
  if (targetList && config.targets.custom) config.targets.list = targetList

  if (buildTargets) {
    if (!config.targets.custom) buildTargets = buildTargets.filter(target => config.targets.list.indexOf(target) >= 0)
    config.targets.build = buildTargets
  }

  fs.writeFile(path.join(__appdir, 'config', 'ipxe.json'), JSON.stringify(config, null, 4), {}, (err) => {
    if (err) res.status(500).end()
    else res.send()
  })
})

router.put('/:filename', (req, res) => {
  var filepath = null
  // Set the file path
  if (req.params.filename === 'script') {
    filepath = path.join(__appdir, 'ipxe', 'embedded.ipxe')
  } else if (req.params.filename === 'console' || req.params.filename === 'general') {
    filepath = path.join(__appdir, 'ipxe', req.params.filename + '.h')
  } else if (req.params.filename === 'certificate') {
    filepath = path.join(__appdir, 'bin', 'fullchain.pem')
  } else {
    res.status(500).send({ status: 'UNKNOWN_FILENAME', error: 'The provided filename is unknown' })
    return
  }

  // Write File
  fs.writeFile(filepath, req.body.data, err => {
    if (err) res.status(500).send({ status: 'WRITE_FILE_ERROR', error: err })
    else res.status(200).send({ status: 'SUCCESS' })
  })
})

/*
 * @return: Rebuild the ipxe.
 */
router.get('/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)
  res.send(build)
})

router.get('/cancel', async (req, res) => {
  const result = await shell.cancelBuilding(req, res)
  res.send(result)
})

router.get('/clean', async (req, res) => {
  const result = await shell.clean()
  res.send(result)
})

router.get('/status', (req, res) => {
  res.send({ status: 'SUCCESS', data: shell.status(req.params.version) })
})

module.exports.router = router

noAuthRouter.get('/load/script', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  fs.readFile(path.join(__appdir, 'ipxe', 'default.ipxe'), 'utf-8', function (err, content) {
    if (err) res.end()
    res.send(content)
  })
})

noAuthRouter.get('/load/registration', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe'))
})

module.exports.noAuthRouter = noAuthRouter